-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
291 lines (258 loc) · 14 KB
/
app.py
File metadata and controls
291 lines (258 loc) · 14 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
import base64
import cv2
import io
import numpy as np
import os
from PIL import Image
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from werkzeug.utils import secure_filename
import serve
import visualize
# ===== MODEL =================================================================
DEVICE = serve.get_device()
MODEL = serve.get_model().to(DEVICE)
MODEL_LAYERS = serve.get_conv_layers(MODEL)
MODEL_CLASSES = serve.get_model_classes()
INV_MODEL_CLASSES = serve.inv_model_classes(MODEL_CLASSES)
TRANSFORMS = serve.get_transforms()
# ===== CONSTANTS =================================================================
MAP_TABS = {
'single-image-tab' : {
'name' : 'Compare Layers for a Single Image',
'image' : ['single-image-choose-image'],
'image-display' : ['single-image-display-image'],
'class' : ['single-image-choose-class'],
'layer' : 'layer-choice',
'storage' : 'single-image-storage',
'submit' : 'single-image-submit',
'plot' : 'single-image-plot'
},
'multiple-images-tab' : {
'name' : 'Compare Layers for Multiple Images',
'image' : ['multiple-images-choose-image1',
'multiple-images-choose-image2'],
'image-display' : ['multiple-images-display-image1',
'multiple-images-display-image2'],
'class' : ['multiple-images-choose-class'],
'layer' : 'layer-choice',
'storage' : 'multiple-images-storage',
'submit' : 'multiple-images-submit',
'plot' : 'multiple-images-plot'
},
'multiple-classes-tab' : {
'name' : 'Compare Layers & Classes for a Single Image',
'image' : ['multiple-classes-choose-image'],
'image-display' : ['multiple-classes-display-image'],
'class' : ['multiple-classes-choose-class1',
'multiple-classes-choose-class2'],
'layer' : 'layer-choice',
'storage' : 'multiple-classes-storage',
'submit' : 'multiple-classes-submit',
'plot' : 'multiple-classs-plot'
}
}
EXTERNAL_STYLESHEETS = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
UPLOAD_DIRECTORY = ""
# ===== INIT APP ==============================================================
app = dash.Dash(__name__, external_stylesheets=EXTERNAL_STYLESHEETS)
app.config['suppress_callback_exceptions']=True
# ===== USER INPUT =================================================================
def class_dropdown(div_id, model_classes):
return dcc.Dropdown(id=div_id, options=[{'label' : name, 'value' : value} for name, value in model_classes])
def layer_checklist(div_id, model_layers):
return dcc.Checklist(id=div_id, options=[{'label' : layer, 'value' : layer} for layer in model_layers])
def image_upload(div_id):
return dcc.Upload(id=div_id,
children=html.Div(['Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
multiple=False)
def submit_button(div_id):
return html.Button(id=div_id, n_clicks=0, children='Submit')
# ===== IMAGE UPLOAD =================================================================
def parse_image(contents, filename, save_dir):
content_type, content_string = contents.split(',')
if 'image' in content_type:
decoded = base64.b64decode(content_string)
image = Image.open(io.BytesIO(decoded))
fname = secure_filename(filename)
if save_dir is not None:
img_pth = os.path.abspath(os.path.join(save_dir, fname))
image.save(img_pth)
return img_pth, image, fname
return None, image, fname
return None
# contents_fname = [(c1, f1), (c2, f2), ...]
# image_paths = list of existing image paths
def upload_images(contents_fname, image_paths):
for i, (contents, fname) in enumerate(contents_fname):
if contents is not None:
image_paths[i] = parse_image(contents, fname, UPLOAD_DIRECTORY)[0]
return image_paths
@app.callback([Output(MAP_TABS['single-image-tab']['storage'], 'data'),
Output(MAP_TABS['single-image-tab']['image-display'][0], 'children')],
[Input(MAP_TABS['single-image-tab']['image'][0], 'contents')],
[State(MAP_TABS['single-image-tab']['image'][0], 'filename'),
State(MAP_TABS['single-image-tab']['storage'], 'data')])
def single_image_upload(contents, filename, image_paths):
img_div = dash.no_update if contents is None else html.Img(src=contents, style={'maxWidth':'50%'})
return upload_images([(contents, filename)], image_paths), img_div
@app.callback([Output(MAP_TABS['multiple-images-tab']['storage'], 'data'),
Output(MAP_TABS['multiple-images-tab']['image-display'][0], 'children'),
Output(MAP_TABS['multiple-images-tab']['image-display'][1], 'children')],
[Input(MAP_TABS['multiple-images-tab']['image'][0], 'contents'),
Input(MAP_TABS['multiple-images-tab']['image'][1], 'contents')],
[State(MAP_TABS['multiple-images-tab']['image'][0], 'filename'),
State(MAP_TABS['multiple-images-tab']['image'][1], 'filename'),
State(MAP_TABS['multiple-images-tab']['storage'], 'data')])
def multiple_image_upload(contents1, contents2, filename1, filename2, image_paths):
img_div1 = dash.no_update if contents1 is None else html.Img(src=contents1, style={'maxWidth':'50%'})
img_div2 = dash.no_update if contents2 is None else html.Img(src=contents2, style={'maxWidth':'50%'})
return upload_images([(contents1, filename1), (contents2, filename2)], image_paths), img_div1, img_div2
@app.callback([Output(MAP_TABS['multiple-classes-tab']['storage'], 'data'),
Output(MAP_TABS['multiple-classes-tab']['image-display'][0], 'children')],
[Input(MAP_TABS['multiple-classes-tab']['image'][0], 'contents')],
[State(MAP_TABS['multiple-classes-tab']['image'][0], 'filename'),
State(MAP_TABS['multiple-classes-tab']['storage'], 'data')])
def multiple_class_upload(contents, filename, image_paths):
img_div = dash.no_update if contents is None else html.Img(src=contents, style={'maxWidth':'50%'})
return upload_images([(contents, filename)], image_paths), img_div
# ===== COMPONENTS =================================================================
@app.callback(Output(MAP_TABS['single-image-tab']['plot'], 'children'),
[Input(MAP_TABS['single-image-tab']['submit'], 'n_clicks')],
[State(MAP_TABS['single-image-tab']['layer'], 'value'),
State(MAP_TABS['single-image-tab']['class'][0], 'value'),
State(MAP_TABS['single-image-tab']['storage'], 'data')])
def compare_layers(n_clicks, chosen_layers, chosen_class, img_pths):
img_pth = img_pths[0]
if (chosen_layers is None) or (chosen_class is None) or (img_pth is None):
return dash.no_update
image = cv2.imread(img_pth)
run_gradcam = serve.serve_gradcam(MODEL, TRANSFORMS, chosen_layers)
cams = run_gradcam(image, int(chosen_class), DEVICE)
fig = visualize.layer_comp_subplots(cams, serve.get_resize_transform()(image))
return dcc.Graph(figure=fig)
@app.callback(Output(MAP_TABS['multiple-classes-tab']['plot'], 'children'),
[Input(MAP_TABS['multiple-classes-tab']['submit'], 'n_clicks')],
[State(MAP_TABS['multiple-classes-tab']['layer'], 'value'),
State(MAP_TABS['multiple-classes-tab']['class'][0], 'value'),
State(MAP_TABS['multiple-classes-tab']['class'][1], 'value'),
State(MAP_TABS['multiple-classes-tab']['storage'], 'data')])
def compare_classes(n_clicks, chosen_layers, chosen_class1, chosen_class2, img_pths):
img_pth = img_pths[0]
# Compare Layers for Different Classes
if (chosen_layers is None) or (chosen_class1 is None) or (chosen_class2 is None) or (img_pth is None):
return dash.no_update
image = cv2.imread(img_pth)
run_gradcam = serve.serve_gradcam(MODEL, TRANSFORMS, chosen_layers)
cams1 = run_gradcam(image, int(chosen_class1), DEVICE)
cams2 = run_gradcam(image, int(chosen_class2), DEVICE)
fig = visualize.class_comp_subplots(cams1, cams2,
[INV_MODEL_CLASSES[chosen_class1], INV_MODEL_CLASSES[chosen_class2]],
serve.get_resize_transform()(image))
return dcc.Graph(figure=fig)
@app.callback(Output(MAP_TABS['multiple-images-tab']['plot'], 'children'),
[Input(MAP_TABS['multiple-images-tab']['submit'], 'n_clicks')],
[State(MAP_TABS['multiple-images-tab']['layer'], 'value'),
State(MAP_TABS['multiple-images-tab']['class'][0], 'value'),
State(MAP_TABS['multiple-images-tab']['storage'], 'data')])
def compare_images(n_clicks, chosen_layers, chosen_class, img_pths):
img_pth1, img_pth2 = img_pths
# Compare Layers for Two different Images for a Chosen Class
if (chosen_layers is None) or (chosen_class is None) or (img_pth1 is None) or (img_pth2 is None):
return dash.no_update
image1 = cv2.imread(img_pth1)
image2 = cv2.imread(img_pth2)
run_gradcam = serve.serve_gradcam(MODEL, TRANSFORMS, chosen_layers)
cams1 = run_gradcam(image1, int(chosen_class), DEVICE)
cams2 = run_gradcam(image2, int(chosen_class), DEVICE)
fig = visualize.image_comp_subplots(cams1, cams2,
serve.get_resize_transform()(image1), serve.get_resize_transform()(image2))
return dcc.Graph(figure=fig)
# ===== TABS =================================================================
def single_image_tab():
tab_contents = html.Div([
html.Label('Choose Image'),
html.Div(id=MAP_TABS['single-image-tab']['image-display'][0],
style={"textAlign" : "center", "display" : "block"}),
html.Div(id="original-image",
style={"textAlign" : "center", "display" : "block"}),
image_upload(MAP_TABS['single-image-tab']['image'][0]),
html.Label('Choose Class'),
class_dropdown(MAP_TABS['single-image-tab']['class'][0], MODEL_CLASSES),
html.Label('Choose Layers'),
layer_checklist(MAP_TABS['single-image-tab']['layer'], MODEL_LAYERS),
submit_button(MAP_TABS['single-image-tab']['submit']),
dcc.Store(id=MAP_TABS['single-image-tab']['storage'], data=[None])
], style={'height':'100vh'})
return tab_contents
def multiple_image_tab():
tab_contents = html.Div([
html.Label('Choose Image 1'),
html.Div(id=MAP_TABS['multiple-images-tab']['image-display'][0],
style={"textAlign" : "center", "display" : "block"}),
image_upload(MAP_TABS['multiple-images-tab']['image'][0]),
html.Label('Choose Image 2'),
html.Div(id=MAP_TABS['multiple-images-tab']['image-display'][1],
style={"textAlign" : "center", "display" : "block"}),
image_upload(MAP_TABS['multiple-images-tab']['image'][1]),
html.Label('Choose Class'),
class_dropdown(MAP_TABS['multiple-images-tab']['class'][0], MODEL_CLASSES),
html.Label('Choose Layers'),
layer_checklist(MAP_TABS['multiple-images-tab']['layer'], MODEL_LAYERS),
submit_button(MAP_TABS['multiple-images-tab']['submit']),
dcc.Store(id=MAP_TABS['multiple-images-tab']['storage'], data=[None, None])
], style={'height':'100vh'})
return tab_contents
def multiple_class_tab():
tab_contents = html.Div([
html.Label('Choose An Image'),
html.Div(id=MAP_TABS['multiple-classes-tab']['image-display'][0],
style={"textAlign" : "center", "display" : "block"}),
image_upload(MAP_TABS['multiple-classes-tab']['image'][0]),
html.Label('Choose Class 1'),
class_dropdown(MAP_TABS['multiple-classes-tab']['class'][0], MODEL_CLASSES),
html.Label('Choose Class 2'),
class_dropdown(MAP_TABS['multiple-classes-tab']['class'][1], MODEL_CLASSES),
html.Label('Choose Layers'),
layer_checklist(MAP_TABS['multiple-classes-tab']['layer'], MODEL_LAYERS),
submit_button(MAP_TABS['multiple-classes-tab']['submit']),
dcc.Store(id=MAP_TABS['multiple-classes-tab']['storage'], data=[None])
], style={'height':'100vh'})
return tab_contents
@app.callback([Output('config', 'children'),
Output('plot', 'children')],
[Input('tabs', 'value')])
def choose_tabs(selected_tab):
if selected_tab == 'single-image-tab':
return single_image_tab(), html.Div(id=MAP_TABS['single-image-tab']['plot'])
elif selected_tab == 'multiple-images-tab':
return multiple_image_tab(), html.Div(id=MAP_TABS['multiple-images-tab']['plot'])
elif selected_tab == 'multiple-classes-tab':
return multiple_class_tab(), html.Div(id=MAP_TABS['multiple-classes-tab']['plot'])
# ===== MAIN =================================================================
app.layout = html.Div([
html.H4("ConvNets: Peek Under the Hood"),
html.Div(dcc.Tabs(id='tabs', value='single-image-tab',
children=[dcc.Tab(label=v['name'], value=k) for k, v in MAP_TABS.items()])),
html.Div(children=[html.Div(id='config',
style={'width': '25%', 'display': 'inline-block', 'float' : 'left'}),
html.Div(id='plot',
style={'width': '74%', 'display': 'inline-block', 'float' : 'right',
'textAlign' : 'center', 'overflowY' : 'scroll', 'height' : '95vh'})])
])
if __name__ == "__main__":
app.run_server(debug=True)