-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathapp.py
More file actions
559 lines (499 loc) · 20.3 KB
/
app.py
File metadata and controls
559 lines (499 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import dash
import pathlib
from dash import Dash, html, dcc, Input, Output, State, callback, callback_context
import pandas as pd
import plotly.graph_objs as go
from plotly import tools
from demo_utils import demo_callbacks, demo_explanation
# get relative data folder
DATA_PATH = pathlib.Path(__file__).parent.joinpath("data").resolve()
LOGFILE = "examples/run_log.csv"
# app = dash.Dash(__name__)
app = dash.Dash(
__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}]
)
app.title = "Live Model Training"
server = app.server
demo_mode = True
def div_graph(name):
# Generates an html Div containing graph and control options for smoothing and display, given the name
return html.Div(
className="row",
children=[
html.Div(
className="two columns",
style={"padding-bottom": "5%"},
children=[
html.Div(
[
html.Div(
className="graph-checkbox-smoothing",
children=["Smoothing:"],
),
dcc.Checklist(
options=[
{"label": " Training", "value": "train"},
{"label": " Validation", "value": "val"},
],
value=[],
id=f"checklist-smoothing-options-{name}",
className="checklist-smoothing",
),
],
style={"margin-top": "10px"},
),
html.Div(
[
dcc.Slider(
min=0,
max=1,
step=0.05,
marks={i / 5: str(i / 5) for i in range(0, 6)},
value=0.6,
updatemode="drag",
id=f"slider-smoothing-{name}",
)
],
style={"margin-bottom": "40px"},
className="slider-smoothing",
),
html.Div(
[
html.P(
"Plot Display Mode:",
style={"font-weight": "bold", "margin-bottom": "0px"},
className="plot-display-text",
),
html.Div(
[
dcc.RadioItems(
options=[
{
"label": " Overlapping",
"value": "overlap",
},
{
"label": " Separate (Vertical)",
"value": "separate_vertical",
},
{
"label": " Separate (Horizontal)",
"value": "separate_horizontal",
},
],
value="overlap",
id=f"radio-display-mode-{name}",
labelStyle={"verticalAlign": "middle"},
className="plot-display-radio-items",
)
],
className="radio-item-div",
),
html.Div(id=f"div-current-{name}-value"),
],
className="entropy-div",
),
],
),
html.Div(id=f"div-{name}-graph", className="ten columns"),
],
)
app.layout = html.Div(
style={"height": "100%"},
children=[
# Banner display
html.Div(
[
html.H2(
"Live Model Training Viewer",
id="title",
className="eight columns",
style={"margin-left": "3%"},
),
html.Button(
id="learn-more-button",
className="two columns",
children=["Learn More"],
),
html.Img(
src=app.get_asset_url("dash-logo.png"),
className="two columns",
id="plotly-logo",
),
],
className="banner row",
),
html.Div(html.Div(id="demo-explanation", children=[])),
html.Div(
className="container",
style={"padding": "35px 25px"},
children=[
dcc.Store(id="storage-simulated-run", storage_type="memory"),
# Increment the simulation step count at a fixed time interval
dcc.Interval(
id="interval-simulated-step",
interval=125, # Updates every 100 milliseconds, i.e. every step takes 25 ms
n_intervals=0,
),
html.Div(
className="row",
style={"margin": "8px 0px"},
children=[
html.Div(
className="twelve columns",
children=[
html.Div(
className="eight columns",
children=[
html.Div(
dcc.Dropdown(
id="dropdown-demo-dataset",
options=[
{
"label": "CIFAR 10",
"value": "cifar",
},
{
"label": "MNIST",
"value": "mnist",
},
{
"label": "Fashion MNIST",
"value": "fashion",
},
],
value="mnist",
placeholder="Select a demo dataset",
searchable=False,
),
className="six columns dropdown-box-first",
),
html.Div(
dcc.Dropdown(
id="dropdown-simulation-model",
options=[
{
"label": "1-Layer Neural Net",
"value": "softmax",
},
{
"label": "Simple Conv Net",
"value": "cnn",
},
],
value="cnn",
placeholder="Select Model to Simulate",
searchable=False,
),
className="six columns dropdown-box-second",
),
html.Div(
dcc.Dropdown(
id="dropdown-interval-control",
options=[
{
"label": "No Updates",
"value": "no",
},
{
"label": "Slow Updates",
"value": "slow",
},
{
"label": "Regular Updates",
"value": "regular",
},
{
"label": "Fast Updates",
"value": "fast",
},
],
value="regular",
className="twelve columns dropdown-box-third",
clearable=False,
searchable=False,
)
),
],
),
html.Div(
className="four columns",
id="div-interval-control",
children=[
html.Div(
id="div-total-step-count",
className="twelve columns",
),
html.Div(
id="div-step-display",
className="twelve columns",
),
],
),
],
)
],
),
dcc.Interval(id="interval-log-update", n_intervals=0),
dcc.Store(id="run-log-storage", storage_type="memory"),
],
),
html.Div(className="container", children=[div_graph("accuracy")]),
html.Div(
className="container",
style={"margin-bottom": "30px"},
children=[div_graph("cross-entropy")],
),
],
)
def update_graph(
graph_id,
graph_title,
y_train_index,
y_val_index,
run_log_json,
display_mode,
checklist_smoothing_options,
slider_smoothing,
yaxis_title,
):
"""
:param graph_id: ID for Dash callbacks
:param graph_title: Displayed on layout
:param y_train_index: name of column index for y train we want to retrieve
:param y_val_index: name of column index for y val we want to retrieve
:param run_log_json: the json file containing the data
:param display_mode: 'separate' or 'overlap'
:param checklist_smoothing_options: 'train' or 'val'
:param slider_smoothing: value between 0 and 1, at interval of 0.05
:return: dcc Graph object containing the updated figures
"""
def smooth(scalars, weight=0.6):
last = scalars[0]
smoothed = list()
for point in scalars:
smoothed_val = last * weight + (1 - weight) * point
smoothed.append(smoothed_val)
last = smoothed_val
return smoothed
if run_log_json:
layout = go.Layout(
title=graph_title,
margin=go.layout.Margin(l=50, r=50, b=50, t=50),
yaxis={"title": yaxis_title},
)
run_log_df = pd.read_json(run_log_json, orient="split")
step = run_log_df["step"]
y_train = run_log_df[y_train_index]
y_val = run_log_df[y_val_index]
# Apply Smoothing if needed
if "train" in checklist_smoothing_options:
y_train = smooth(y_train, weight=slider_smoothing)
if "val" in checklist_smoothing_options:
y_val = smooth(y_val, weight=slider_smoothing)
# line charts
trace_train = go.Scatter(
x=step,
y=y_train,
mode="lines",
name="Training",
line=dict(color="rgb(54, 218, 170)"),
showlegend=False,
)
trace_val = go.Scatter(
x=step,
y=y_val,
mode="lines",
name="Validation",
line=dict(color="rgb(246, 236, 145)"),
showlegend=False,
)
if display_mode == "separate_vertical":
figure = tools.make_subplots(rows=2, cols=1, print_grid=False)
figure.append_trace(trace_train, 1, 1)
figure.append_trace(trace_val, 2, 1)
figure["layout"].update(
title=layout.title,
margin=layout.margin,
scene={"domain": {"x": (0.0, 0.5), "y": (0.5, 1)}},
)
figure["layout"]["yaxis1"].update(title=yaxis_title)
figure["layout"]["yaxis2"].update(title=yaxis_title)
elif display_mode == "separate_horizontal":
figure = tools.make_subplots(
rows=1, cols=2, print_grid=False, shared_xaxes=True
)
figure.append_trace(trace_train, 1, 1)
figure.append_trace(trace_val, 1, 2)
figure["layout"].update(title=layout.title, margin=layout.margin)
figure["layout"]["yaxis1"].update(title=yaxis_title)
figure["layout"]["yaxis2"].update(title=yaxis_title)
elif display_mode == "overlap":
figure = go.Figure(data=[trace_train, trace_val], layout=layout)
else:
figure = None
return dcc.Graph(figure=figure, id=graph_id)
return dcc.Graph(id=graph_id)
demo_callbacks(app, demo_mode)
@app.callback(
[Output("demo-explanation", "children"), Output("learn-more-button", "children")],
[Input("learn-more-button", "n_clicks")],
)
def learn_more(n_clicks):
if n_clicks is None:
n_clicks = 0
if (n_clicks % 2) == 1:
n_clicks += 1
return (
html.Div(
className="container",
style={"margin-bottom": "30px"},
children=[demo_explanation(demo_mode)],
),
"Close",
)
n_clicks += 1
return (html.Div(), "Learn More")
@app.callback(
Output("interval-log-update", "interval"),
[Input("dropdown-interval-control", "value")],
)
def update_interval_log_update(interval_rate):
if interval_rate == "fast":
return 500
elif interval_rate == "regular":
return 1000
elif interval_rate == "slow":
return 5 * 1000
# Refreshes every 24 hours
elif interval_rate == "no":
return 24 * 60 * 60 * 1000
if not demo_mode:
@app.callback(
Output("run-log-storage", "data"), [Input("interval-log-update", "n_intervals")]
)
def get_run_log(_):
names = [
"step",
"train accuracy",
"val accuracy",
"train cross entropy",
"val cross entropy",
]
try:
run_log_df = pd.read_csv(DATA_PATH.joinpath(LOGFILE), names=names)
json = run_log_df.to_json(orient="split")
except FileNotFoundError as error:
print(error)
print(
"Please verify if the csv file generated by your model is placed in the correct directory."
)
return None
return json
@app.callback(
Output("div-step-display", "children"), [Input("run-log-storage", "data")]
)
def update_div_step_display(run_log_json):
if run_log_json:
run_log_df = pd.read_json(run_log_json, orient="split")
return html.H6(
f"Step: {run_log_df['step'].iloc[-1]}",
style={"margin-top": "3px", "float": "right"},
)
@app.callback(
Output("div-accuracy-graph", "children"),
[
Input("run-log-storage", "data"),
Input("radio-display-mode-accuracy", "value"),
Input("checklist-smoothing-options-accuracy", "value"),
Input("slider-smoothing-accuracy", "value"),
],
)
def update_accuracy_graph(
run_log_json, display_mode, checklist_smoothing_options, slider_smoothing
):
graph = update_graph(
"accuracy-graph",
"Prediction Accuracy",
"train accuracy",
"val accuracy",
run_log_json,
display_mode,
checklist_smoothing_options,
slider_smoothing,
"Accuracy",
)
try:
if display_mode in ["separate_horizontal", "overlap"]:
graph.figure.layout.yaxis["range"] = [0, 1.3]
graph.figure.layout.yaxis2["range"] = [0, 1.3]
else:
graph.figure.layout.yaxis1["range"] = [0, 1.3]
graph.figure.layout.yaxis2["range"] = [0, 1.3]
except AttributeError:
pass
return [graph]
@app.callback(
Output("div-cross-entropy-graph", "children"),
[
Input("run-log-storage", "data"),
Input("radio-display-mode-cross-entropy", "value"),
Input("checklist-smoothing-options-cross-entropy", "value"),
Input("slider-smoothing-cross-entropy", "value"),
],
)
def update_cross_entropy_graph(
run_log_json, display_mode, checklist_smoothing_options, slider_smoothing
):
graph = update_graph(
"cross-entropy-graph",
"Cross Entropy Loss",
"train cross entropy",
"val cross entropy",
run_log_json,
display_mode,
checklist_smoothing_options,
slider_smoothing,
"Loss",
)
return [graph]
@app.callback(
Output("div-current-accuracy-value", "children"), [Input("run-log-storage", "data")]
)
def update_div_current_accuracy_value(run_log_json):
if run_log_json:
run_log_df = pd.read_json(run_log_json, orient="split")
return [
html.P(
"Current Accuracy:",
style={
"font-weight": "bold",
"margin-top": "15px",
"margin-bottom": "0px",
},
),
html.Div(f"Training: {run_log_df['train accuracy'].iloc[-1]:.4f}"),
html.Div(f"Validation: {run_log_df['val accuracy'].iloc[-1]:.4f}"),
]
@app.callback(
Output("div-current-cross-entropy-value", "children"),
[Input("run-log-storage", "data")],
)
def update_div_current_cross_entropy_value(run_log_json):
if run_log_json:
run_log_df = pd.read_json(run_log_json, orient="split")
return [
html.P(
"Current Loss:",
style={
"font-weight": "bold",
"margin-top": "15px",
"margin-bottom": "0px",
},
),
html.Div(f"Training: {run_log_df['train cross entropy'].iloc[-1]:.4f}"),
html.Div(f"Validation: {run_log_df['val cross entropy'].iloc[-1]:.4f}"),
]
# Running the server
if __name__ == "__main__":
app.run_server(debug=True)