Refactor examples

This commit is contained in:
allegroai
2020-06-15 22:48:51 +03:00
parent bec31c7ac4
commit 99368abb1c
78 changed files with 3505 additions and 1294 deletions

View File

@@ -0,0 +1,63 @@
# TRAINS - Example of manual graphs and statistics reporting
#
import numpy as np
from trains import Task, Logger
def report_plots(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting plots to plots section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
# report 3d surface
surface = np.random.randint(10, size=(10, 10))
logger.report_surface(
"example_surface",
"series1",
iteration=iteration,
matrix=surface,
xaxis="title X",
yaxis="title Y",
zaxis="title Z",
)
# report 3d scatter plot
scatter3d = np.random.randint(10, size=(10, 3))
logger.report_scatter3d(
"example_scatter_3d",
"series_xyz",
iteration=iteration,
scatter=scatter3d,
xaxis="title x",
yaxis="title y",
zaxis="title z",
)
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="3D plot reporting")
print('reporting 3D plot graphs')
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report graphs
report_plots(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,49 @@
import os
from time import sleep
import pandas as pd
import numpy as np
from PIL import Image
from trains import Task
task = Task.init('examples', 'artifacts example')
df = pd.DataFrame(
{
'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [10, 2, 1, 8]
},
index=['falcon', 'dog', 'spider', 'fish']
)
# Register Pandas object as artifact to watch
# (it will be monitored in the background and automatically synced and uploaded)
task.register_artifact('train', df, metadata={'counting': 'legs', 'max legs': 69})
# change the artifact object
df.sample(frac=0.5, replace=True, random_state=1)
# or access it from anywhere using the Task's get_registered_artifacts()
Task.current_task().get_registered_artifacts()['train'].sample(frac=0.5, replace=True, random_state=1)
# add and upload pandas.DataFrame (onetime snapshot of the object)
task.upload_artifact('Pandas', artifact_object=df)
# add and upload local file artifact
task.upload_artifact('local file', artifact_object=os.path.join('data_samples', 'dancing.jpg'))
# add and upload dictionary stored as JSON)
task.upload_artifact('dictionary', df.to_dict())
# add and upload Numpy Object (stored as .npz file)
task.upload_artifact('Numpy Eye', np.eye(100, 100))
# add and upload Image (stored as .png file)
im = Image.open(os.path.join('data_samples', 'dancing.jpg'))
task.upload_artifact('pillow_image', im)
# add and upload a folder, artifact_object should be the folder path
task.upload_artifact('local folder', artifact_object=os.path.join('data_samples'))
# add and upload a wildcard
task.upload_artifact('wildcard jpegs', artifact_object=os.path.join('data_samples', '*.jpg'))
# do something here
sleep(1.)
print(df)
# we are done
print('Done')

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View File

@@ -0,0 +1,8 @@
{
"list_of_ints": [1,2,3,4],
"dict": {
"sub_value": "string",
"sub_integer": 11
},
"value": 13.37
}

Binary file not shown.

View File

@@ -0,0 +1,245 @@
# TRAINS - Example of manual graphs and statistics reporting
#
import math
import numpy as np
from bokeh.models import ColumnDataSource, GraphRenderer, Oval, StaticLayoutProvider
from bokeh.palettes import Spectral5, Spectral8
from bokeh.plotting import figure, output_file, save
from bokeh.sampledata.autompg import autompg_clean as bokeh_df
from bokeh.sampledata.periodic_table import elements
from bokeh.transform import dodge, factor_cmap
from trains import Task, Logger
def report_html_url(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting html from url to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
logger.report_media("html", "url_html", iteration=iteration, url="https://allegro.ai/docs/index.html")
def report_html_periodic_table(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting interactive (html) of periodic table to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
output_file("periodic.html")
periods = ["I", "II", "III", "IV", "V", "VI", "VII"]
groups = [str(x) for x in range(1, 19)]
autompg_clean = elements.copy()
autompg_clean["atomic mass"] = autompg_clean["atomic mass"].astype(str)
autompg_clean["group"] = autompg_clean["group"].astype(str)
autompg_clean["period"] = [periods[x - 1] for x in autompg_clean.period]
autompg_clean = autompg_clean[autompg_clean.group != "-"]
autompg_clean = autompg_clean[autompg_clean.symbol != "Lr"]
autompg_clean = autompg_clean[autompg_clean.symbol != "Lu"]
cmap = {
"alkali metal": "#a6cee3",
"alkaline earth metal": "#1f78b4",
"metal": "#d93b43",
"halogen": "#999d9a",
"metalloid": "#e08d49",
"noble gas": "#eaeaea",
"nonmetal": "#f1d4Af",
"transition metal": "#599d7A",
}
source = ColumnDataSource(autompg_clean)
p = figure(
plot_width=900,
plot_height=500,
title="Periodic Table (omitting LA and AC Series)",
x_range=groups,
y_range=list(reversed(periods)),
toolbar_location=None,
tools="hover",
)
p.rect(
"group",
"period",
0.95,
0.95,
source=source,
fill_alpha=0.6,
legend_label="metal",
color=factor_cmap(
"metal", palette=list(cmap.values()), factors=list(cmap.keys())
),
)
text_props = {"source": source, "text_align": "left", "text_baseline": "middle"}
x = dodge("group", -0.4, range=p.x_range)
r = p.text(x=x, y="period", text="symbol", **text_props)
r.glyph.text_font_style = "bold"
r = p.text(
x=x, y=dodge("period", 0.3, range=p.y_range), text="atomic number", **text_props
)
r.glyph.text_font_size = "8pt"
r = p.text(
x=x, y=dodge("period", -0.35, range=p.y_range), text="name", **text_props
)
r.glyph.text_font_size = "5pt"
r = p.text(
x=x, y=dodge("period", -0.2, range=p.y_range), text="atomic mass", **text_props
)
r.glyph.text_font_size = "5pt"
p.text(
x=["3", "3"],
y=["VI", "VII"],
text=["LA", "AC"],
text_align="center",
text_baseline="middle",
)
p.hover.tooltips = [
("Name", "@name"),
("Atomic number", "@{atomic number}"),
("Atomic mass", "@{atomic mass}"),
("Type", "@metal"),
("CPK color", "$color[hex, swatch]:CPK"),
("Electronic configuration", "@{electronic configuration}"),
]
p.outline_line_color = None
p.grid.grid_line_color = None
p.axis.axis_line_color = None
p.axis.major_tick_line_color = None
p.axis.major_label_standoff = 0
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
save(p)
logger.report_media("html", "periodic_html", iteration=iteration, local_path="periodic.html")
def report_html_groupby(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting bokeh groupby (html) to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
output_file("bar_pandas_groupby_nested.html")
bokeh_df.cyl = bokeh_df.cyl.astype(str)
bokeh_df.yr = bokeh_df.yr.astype(str)
group = bokeh_df.groupby(by=["cyl", "mfr"])
index_cmap = factor_cmap(
"cyl_mfr", palette=Spectral5, factors=sorted(bokeh_df.cyl.unique()), end=1
)
p = figure(
plot_width=800,
plot_height=300,
title="Mean MPG by # Cylinders and Manufacturer",
x_range=group,
toolbar_location=None,
tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", "@cyl_mfr")],
)
p.vbar(
x="cyl_mfr",
top="mpg_mean",
width=1,
source=group,
line_color="white",
fill_color=index_cmap,
)
p.y_range.start = 0
p.x_range.range_padding = 0.05
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "Manufacturer grouped by # Cylinders"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None
save(p)
logger.report_media(
"html",
"pandas_groupby_nested_html",
iteration=iteration,
local_path="bar_pandas_groupby_nested.html",
)
def report_html_graph(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting bokeh graph (html) to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
nodes = 8
node_indices = list(range(nodes))
plot = figure(
title="Graph Layout Demonstration",
x_range=(-1.1, 1.1),
y_range=(-1.1, 1.1),
tools="",
toolbar_location=None,
)
graph = GraphRenderer()
graph.node_renderer.data_source.add(node_indices, "index")
graph.node_renderer.data_source.add(Spectral8, "color")
graph.node_renderer.glyph = Oval(height=0.1, width=0.2, fill_color="color")
graph.edge_renderer.data_source.data = dict(start=[0] * nodes, end=node_indices)
# start of layout code
circ = [i * 2 * math.pi / 8 for i in node_indices]
x = [math.cos(i) for i in circ]
y = [math.sin(i) for i in circ]
graph_layout = dict(zip(node_indices, zip(x, y)))
graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout)
plot.renderers.append(graph)
output_file("graph.html")
save(plot)
logger.report_media("html", "Graph_html", iteration=iteration, local_path="graph.html")
def report_html_image(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting bokeh image (html) to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
# First html
samples = 500
x = np.linspace(0, 10, samples)
y = np.linspace(0, 10, samples)
xx, yy = np.meshgrid(x, y)
d = np.sin(xx) * np.cos(yy)
p = figure(tooltips=[("x", "$x"), ("y", "$y"), ("value", "@image")])
p.x_range.range_padding = p.y_range.range_padding = 0
# must give a vector of image data for image parameter
p.image(image=[d], x=0, y=0, dw=10, dh=10, palette="Spectral11", level="image")
p.grid.grid_line_width = 0.5
output_file("image.html", title="image.py example")
save(p)
logger.report_media("html", "Spectral_html", iteration=iteration, local_path="image.html")
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="html samples reporting")
print('reporting html files into debug samples section')
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report html as debug samples
report_html_image(logger)
report_html_graph(logger)
report_html_groupby(logger)
report_html_periodic_table(logger)
report_html_url(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,59 @@
# TRAINS - example code, ArgumentParser parameter logging, absl parameter logging, and dictionary parameter logging
#
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
from argparse import ArgumentParser
from absl import app
from absl import flags
from absl import logging
from trains import Task
FLAGS = flags.FLAGS
flags.DEFINE_string('echo', None, 'Text to echo.')
flags.DEFINE_string('another_str', 'My string', 'A string', module_name='test')
task = Task.init(project_name='examples', task_name='hyper-parameters example')
flags.DEFINE_integer('echo3', 3, 'Text to echo.')
flags.DEFINE_string('echo5', '5', 'Text to echo.', module_name='test')
parameters = {
'list': [1, 2, 3],
'dict': {'a': 1, 'b': 2},
'tuple': (1, 2, 3),
'int': 3,
'float': 2.2,
'string': 'my string',
}
parameters = task.connect(parameters)
# adding new parameter after connect (will be logged as well)
parameters['new_param'] = 'this is new'
# changing the value of a parameter (new value will be stored instead of previous one)
parameters['float'] = '9.9'
print(parameters)
def main(_):
print('Running under Python {0[0]}.{0[1]}.{0[2]}'.format(sys.version_info), file=sys.stderr)
logging.info('echo is %s.', FLAGS.echo)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--argparser_int_value', help='integer value', type=int, default=1)
parser.add_argument('--argparser_disabled', action='store_true', default=False, help='disables something')
parser.add_argument('--argparser_str_value', help='string value', default='a string')
args = parser.parse_args()
app.run(main)

View File

@@ -0,0 +1,67 @@
# TRAINS - Example of manual graphs and statistics reporting
#
import os
import numpy as np
from PIL import Image
from trains import Task, Logger
def report_debug_images(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting images to debug samples section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
# report image as float image
m = np.eye(256, 256, dtype=np.float)
logger.report_image("image", "image float", iteration=iteration, image=m)
# report image as uint8
m = np.eye(256, 256, dtype=np.uint8) * 255
logger.report_image("image", "image uint8", iteration=iteration, image=m)
# report image as uint8 RGB
m = np.concatenate((np.atleast_3d(m), np.zeros((256, 256, 2), dtype=np.uint8)), axis=2)
logger.report_image("image", "image color red", iteration=iteration, image=m)
# report PIL Image object
image_open = Image.open(os.path.join("data_samples", "picasso.jpg"))
logger.report_image("image", "image PIL", iteration=iteration, image=image_open)
# Image can be uploaded via 'report_media' too.
logger.report_media(
"image",
"image with report media",
iteration=iteration,
local_path=os.path.join("data_samples", "picasso.jpg"),
file_extension="jpg",
)
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="image reporting")
print('reporting a few debug images')
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report debug images
report_debug_images(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,24 @@
# TRAINS - Example reporting video or audio links/file
#
import os
from trains import Task, Logger
task = Task.init(project_name="examples", task_name="audio and video reporting")
print('reporting audio and video samples to the debug samples section')
# report video, an already uploaded video media (url)
Logger.current_logger().report_media(
'video', 'big bunny', iteration=1,
url='https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/720/Big_Buck_Bunny_720_10s_1MB.mp4')
# report audio, report an already uploaded audio media (url)
Logger.current_logger().report_media(
'audio', 'pink panther', iteration=1,
url='https://www2.cs.uic.edu/~i101/SoundFiles/PinkPanther30.wav')
# report audio, report local media audio file
Logger.current_logger().report_media(
'audio', 'tada', iteration=1,
local_path=os.path.join('data_samples', 'sample.mp3'))

View File

@@ -0,0 +1,36 @@
# TRAINS - Example of manual model configuration
#
import os
from trains import Task
task = Task.init(project_name='examples', task_name='Model configuration example')
# Connect a local configuration file
config_file = os.path.join('data_samples', 'sample.json')
config_file = task.connect_configuration(config_file)
# then read configuration as usual, the backend will contain a copy of it.
# later when executing remotely, the returned `config_file` will be a temporary file
# containing a new copy of the configuration retrieved form the backend
# # model_config_dict = json.load(open(config_file, 'rt'))
# Or Store dictionary of definition for a specific network design
model_config_dict = {
'value': 13.37,
'dict': {'sub_value': 'string', 'sub_integer': 11},
'list_of_ints': [1, 2, 3, 4],
}
model_config_dict = task.connect_configuration(model_config_dict)
# We now update the dictionary after connecting it, and the changes will be tracked as well.
model_config_dict['new value'] = 10
model_config_dict['value'] *= model_config_dict['new value']
# store the label enumeration of the training model
labels = {'background': 0, 'cat': 1, 'dog': 2}
task.connect_label_enumeration(labels)
# storing a model: Any saved model (keras / pytorch / tensorflow / etc.)
# will have the task network configuration and label enumeration
print('Any model stored from this point onwards, will contain both model_config and label_enumeration')

View File

@@ -0,0 +1,57 @@
# TRAINS - Example of manual graphs and statistics reporting
#
import pandas as pd
from trains import Task, Logger
def report_table(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting tables to the plots section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
# report tables
# Report table - DataFrame with index
df = pd.DataFrame(
{
"num_legs": [2, 4, 8, 0],
"num_wings": [2, 0, 0, 0],
"num_specimen_seen": [10, 2, 1, 8],
},
index=["falcon", "dog", "spider", "fish"],
)
df.index.name = "id"
logger.report_table("table pd", "PD with index", iteration=iteration, table_plot=df)
# Report table - CSV from path
csv_url = "https://raw.githubusercontent.com/plotly/datasets/master/Mining-BTC-180.csv"
logger.report_table("table csv", "remote csv", iteration=iteration, url=csv_url)
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="pandas table reporting")
print('reporting pandas tablea into the plots section')
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report graphs
report_table(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,20 @@
# TRAINS - Example of Plotly integration and reporting
#
from trains import Task
import plotly.express as px
task = Task.init('examples', 'plotly reporting')
print('reporting plotly figures')
# Iris dataset
df = px.data.iris()
# create complex plotly figure
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", marginal_y="rug", marginal_x="histogram")
# report the plotly figure
task.get_logger().report_plotly(title="iris", series="sepal", iteration=0, figure=fig)
print('done')

View File

@@ -0,0 +1,6 @@
absl-py>=0.7.1
bokeh>=2.1.0
numpy
pandas
pillow>=4.0
trains

View File

@@ -0,0 +1,45 @@
# TRAINS - Example of manual graphs and statistics reporting
#
from trains import Task, Logger
def report_scalars(logger):
# type: (Logger) -> ()
"""
reporting scalars to scalars section
:param logger: The task.logger to use for sending the scalars
"""
# report two scalar series on the same graph
for i in range(100):
logger.report_scalar("unified graph", "series A", iteration=i, value=1./(i+1))
logger.report_scalar("unified graph", "series B", iteration=i, value=10./(i+1))
# report two scalar series on two different graphs
for i in range(100):
logger.report_scalar("graph A", "series A", iteration=i, value=1./(i+1))
logger.report_scalar("graph B", "series B", iteration=i, value=10./(i+1))
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="scalar reporting")
print('reporting scalar graphs')
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report scalars
report_scalars(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,116 @@
# TRAINS - Example of manual graphs and statistics reporting
#
import numpy as np
from trains import Task, Logger
def report_plots(logger, iteration=0):
# type: (Logger, int) -> ()
"""
reporting plots to plots section
:param logger: The task.logger to use for sending the plots
:param iteration: The iteration number of the current reports
"""
# report a single histogram
histogram = np.random.randint(10, size=10)
logger.report_histogram(
"single_histogram",
"random histogram",
iteration=iteration,
values=histogram,
xaxis="title x",
yaxis="title y",
)
# report a two histograms on the same graph (plot)
histogram1 = np.random.randint(13, size=10)
histogram2 = histogram * 0.75
logger.report_histogram(
"two_histogram",
"series 1",
iteration=iteration,
values=histogram1,
xaxis="title x",
yaxis="title y",
)
logger.report_histogram(
"two_histogram",
"series 2",
iteration=iteration,
values=histogram2,
xaxis="title x",
yaxis="title y",
)
# report confusion matrix
confusion = np.random.randint(10, size=(10, 10))
logger.report_matrix(
"example_confusion",
"ignored",
iteration=iteration,
matrix=confusion,
xaxis="title X",
yaxis="title Y",
)
scatter2d = np.hstack(
(np.atleast_2d(np.arange(0, 10)).T, np.random.randint(10, size=(10, 1)))
)
# report 2d scatter plot with lines
logger.report_scatter2d(
"example_scatter",
"series_xy",
iteration=iteration,
scatter=scatter2d,
xaxis="title x",
yaxis="title y",
)
# report 2d scatter plot with markers
logger.report_scatter2d(
"example_scatter",
"series_markers",
iteration=iteration,
scatter=scatter2d,
xaxis="title x",
yaxis="title y",
mode='markers'
)
# report 2d scatter plot with markers
logger.report_scatter2d(
"example_scatter",
"series_lines+markers",
iteration=iteration,
scatter=scatter2d,
xaxis="title x",
yaxis="title y",
mode='lines+markers'
)
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="2D plots reporting")
print('reporting some graphs')
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report graphs
report_plots(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,58 @@
# TRAINS - Example of manual graphs and statistics reporting
#
import logging
import sys
from trains import Task, Logger
def report_logs(logger):
# type: (Logger) -> ()
"""
reporting text to logs section
:param logger: The task.logger to use for sending the text
"""
# standard python logging
logging.info("This is an info message")
# this is a loguru test example
try:
from loguru import logger as loguru_logger # noqa
loguru_logger.info("That's it, beautiful and simple logging! (using ANSI colors)")
except ImportError:
print('loguru not installed, skipping loguru test')
# report text
logger.report_text("hello, this is plain text")
def main():
# Create the experiment Task
task = Task.init(project_name="examples", task_name="text reporting")
print('reporting text logs')
# report regular console print
print('This is standard output test')
# report stderr
print('This is standard error test', file=sys.stderr)
# Get the task logger,
# You can also call Task.current_task().get_logger() from anywhere in your code.
logger = task.get_logger()
# report text based logs
report_logs(logger)
# force flush reports
# If flush is not called, reports are flushed in the background every couple of seconds,
# and at the end of the process execution
logger.flush()
print('We are done reporting, have a great day :)')
if __name__ == "__main__":
main()