-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashImApp.py
464 lines (312 loc) · 12.1 KB
/
DashImApp.py
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
from dash.dependencies import Input, Output, State
import plotly.graph_objs as go
import plotly.offline as py
import plotly.graph_objs as go
import numpy as np
from skimage import io, transform
py.init_notebook_mode(connected=True)
import csv
from skimage import color
from sklearn import manifold, preprocessing
import glob, os, flask
import tqdm
from numba import cuda
import argparse
import os.path
import re
import sys
import tarfile
import numpy as np
from six.moves import urllib
import tensorflow as tf
import random
import os
from joblib import Parallel, delayed
#Let's setup the random seed.
random.seed(1912)
# SET POSSIBLE DIRECTORIES TO BE LISTS OF FOLDER PATHS!!!!
# possible_directories = glob.glob('/media/leonardo/DATA/CRASSH/Animals_with_Attributes2/JPEGImages' + '/*/')
#possible_directories = glob.glob('/home/leonardo/Documents/GPUcomputing/drapery/style/humans_out' + '/*/')
#./possible_directories = glob.glob('/media/leonardo/DATA/Annunciation_Baptism/Annunciation/Others/*/')
#possible_directories = glob.glob('/home/leonardo/Documents/GPUcomputing/BEGAN-tensorflow/data/ven_bodies/')
possible_directories = glob.glob('/home/leonardo/Documents/GPUcomputing/Fototeca/Train_icon_text/*/')
#possible_directories = glob.glob('/home/leonardo/Documents/GPUcomputing/Messicano/*/')
# possible_directories = glob.glob('./imout' + '/*/')
static_image_route = '/static/'
# image_directory = '/home/leonardo/Documents/GPUcomputing/Fototeca/Train_materials/Öl/'
# image_directory = './imout/flemish/'
def makeThumbnail(imfn, baseheight):
testim = io.imread(imfn)
hpercent = (baseheight / float(testim.shape[1]))
wsize = int((float(testim.shape[0]) * float(hpercent)))
smallim = transform.resize(testim,(wsize, baseheight))
if len(testim.shape) == 2:
smallim = color.grey2rgb(smallim)
io.imsave('./tmpim/' + imfn.split('/')[-1],smallim)
def prepareImages(image_directory, Nmax=50):
for file in os.listdir('./tmpim/'):
if file.endswith('.jpg'):
os.remove('./tmpim/' + file)
list_of_images = [os.path.basename(x) for x in glob.glob('{}*.jpg'.format(image_directory))]
NIms = len(list_of_images)
#randomly rearrange so we don't have bias by files...
random.shuffle(list_of_images)
if(NIms>Nmax):
list_of_images = list_of_images[:Nmax]
NIms = len(list_of_images)
ImPaths = [image_directory + fn for fn in list_of_images]
ImPaths = ImPaths[:Nmax]
baseheight = 256.0
##Make the thumbnails change size in extreme cases
if Nmax > 300:
baseheight = 128.0
if Nmax < 200:
baseheight = 512.0
print('writing thumbnails')
Parallel(n_jobs=36)(delayed(makeThumbnail)(i, baseheight) for i in ImPaths)
print('wrote thumbnails')
image_directory = './tmpim/'
ImPaths = [image_directory + imf for imf in list_of_images]
ImURLs = [static_image_route + imf for imf in list_of_images]
return [ImPaths, ImURLs]
model_dir = './model/'
# pylint: disable=line-too-long
DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
# pylint: enable=line-too-long
def create_graph():
"""Creates a graph from saved GraphDef file and returns a saver."""
# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(os.path.join(
model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
def run_inference_on_image(image):
"""Runs inference on an image.
Args:
image: Image file name.
Returns:
Nothing
"""
if not tf.gfile.Exists(image):
tf.logging.fatal('File does not exist %s', image)
image_data = tf.gfile.FastGFile(image, 'rb').read()
# Creates graph from saved GraphDef.
create_graph()
with tf.Session() as sess:
# Some useful tensors:
# 'softmax:0': A tensor containing the normalized prediction across
# 1000 labels.
# 'pool_3:0': A tensor containing the next-to-last layer containing 2048
# float description of the image.
# 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
# encoding of the image.
# Runs the softmax tensor by feeding the image_data as input to the graph.
softmax_tensor = sess.graph.get_tensor_by_name('pool_3:0')
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
return predictions
def maybe_download_and_extract():
"""Download and extract model tar file."""
dest_directory = model_dir
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
filename = DATA_URL.split('/')[-1]
filepath = os.path.join(dest_directory, filename)
if not os.path.exists(filepath):
def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (
filename, float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress)
print()
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(dest_directory)
maybe_download_and_extract()
app = dash.Dash()
# Add a static image route that serves images from desktop
# Be *very* careful here - you don't want to serve arbitrary files
# from your computer or server
@app.server.route('{}<image_path>.jpg'.format(static_image_route))
def serve_image(image_path):
image_name = '{}.jpg'.format(image_path)
# if image_name not in list_of_images:
# raise Exception('"{}" is excluded from the allowed static files'.format(image_path))
return flask.send_from_directory('./tmpim/', image_name)
def processImages(ImPaths, ImURLs):
hoverText = []
for i in range(len(ImURLs)):
hoverText.append( ImURLs[i].split('/')[-1].split('.jpg')[0] )
red = []
green = []
blue = []
hue = []
bright = []
neuralV = []
cuda.select_device(0)
SF = 10000.0 #Scale Factor
AR = 2.0 ## Aspect Ratio: x / y
# This should allow some dynamic memory allocation...
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
with tf.Session(config=config) as sess:
create_graph()
# ops = sess.graph.get_operations()
# for o in ops:
# print(o.values())
softmax_tensor = sess.graph.get_tensor_by_name('pool_3:0')
for i in tqdm.tqdm(range( len(ImPaths) )):
myim_filename = ImPaths[i]
im = io.imread(myim_filename)
greyim = color.rgb2gray(im)
maskim = greyim>0.05
avim = np.mean(im.flatten())
red.append( int(np.mean(im[maskim,0].flatten()) ) )
green.append( int(np.mean(im[maskim,1].flatten()) ) )
blue.append( int(np.mean(im[maskim,2].flatten()) ) )
hue.append(color.rgb2hsv([[[red[-1],green[-1],blue[-1] ]]])[0][0][0] )
bright.append(avim)
image_data = tf.gfile.FastGFile(myim_filename, 'rb').read()
predictions = sess.run(softmax_tensor,
{'DecodeJpeg/contents:0': image_data})
predictions = np.squeeze(predictions)
neuralV.append(predictions)
## I tried to make this parallel, but to no avail...
# image_data = []
# for i in tqdm.tqdm(range( len(ImPaths) )):
# myim_filename = ImPaths[i]
# im = io.imread(myim_filename)
# image_data.append(tf.gfile.FastGFile(myim_filename, 'rb').read() )
# all_image_data = tf.stack(image_data)
# predictions = sess.run(softmax_tensor,
# {'DecodeJpeg/contents:0': all_image_data})
dataMatrix = np.asarray(neuralV)
tsne_out = manifold.TSNE(n_components=2).fit_transform(dataMatrix).transpose()
tsne_out = preprocessing.normalize(tsne_out)
FirstVec = tsne_out[0]
SecondVec = tsne_out[1]
FirstVec *= SF * AR
SecondVec *= SF
mytrace = go.Scatter(
x=FirstVec,
y=SecondVec,
text=hoverText,
hoverinfo='text',
showlegend=False,
mode = 'markers',
)
return [mytrace, FirstVec, SecondVec]
# def update_point(trace, points, selector):
# print('clicked')
# # c = list(scatter.marker.color)
# # s = list(scatter.marker.size)
# # for i in points.point_inds:
# # c[i] = '#bae2be'
# # s[i] = 20
# # scatter.marker.color = c
# # scatter.marker.size = s
# trace0.on_click(update_point)
def layoutFromImages(ImURLs, FirstVec, SecondVec):
layout = {}
layout['images'] = []
for i in range( len(ImURLs) ):
imsize = 100
if ( len(ImURLs) ) > 300:
imsize = 50
if ( len(ImURLs) ) < 200:
imsize = 200
layout['images'].append(dict(
source= ImURLs[i],
xref= "x",
yref= "y",
x= FirstVec[i],
y= SecondVec[i],
sizex= imsize,
sizey= imsize,
xanchor= "center",
yanchor= "middle" ##This really is how the two have to be defined..... seems pretty silly
))
layout['hovermode'] = 'closest'
layout['xaxis'] = dict(
)
layout['yaxis'] = dict(
scaleanchor = "x"
)
return layout
# [ImPaths, ImURLs] = prepareImages(image_directory)
# [trace0, FirstVec, SecondVec] = processImages(ImPaths, ImURLs)
# data = [trace0]
# layout = layoutFromImages(ImURLs, FirstVec, SecondVec)
data = []
layout = {}
app.layout = html.Div([
html.Div([
dcc.Dropdown(
id='directory_choice',
options=[{'label': i.split('/')[-2], 'value': i} for i in possible_directories],
value=possible_directories[0],
style={'width': 400}
),
dcc.Dropdown(
id='num_images',
options=[{'label': str(i), 'value': i} for i in [50, 100, 200, 300, 400, 500, 750, 1000]],
value=200,
style={'width': 400}
)
]),
dcc.Graph(
figure=go.Figure(data,layout=layout),
style={'height': 900},
id='my-graph'
)
])
@app.callback(
dash.dependencies.Output('my-graph', 'figure'),
[dash.dependencies.Input('directory_choice', 'value'),
dash.dependencies.Input('num_images','value')])
def update_graph(directory_choice, num_images):
print('updated')
[ImPaths, ImURLs] = prepareImages(directory_choice, Nmax=num_images)
[trace0, FirstVec, SecondVec] = processImages(ImPaths, ImURLs)
data = [trace0]
layout = layoutFromImages(ImURLs, FirstVec, SecondVec)
return {
'data': data,
'layout': layout
# 'data': [go.Scatter(
# x=dff[dff['Indicator Name'] == xaxis_column_name]['Value'],
# y=dff[dff['Indicator Name'] == yaxis_column_name]['Value'],
# text=dff[dff['Indicator Name'] == yaxis_column_name]['Country Name'],
# mode='markers',
# marker={
# 'size': 15,
# 'opacity': 0.5,
# 'line': {'width': 0.5, 'color': 'white'}
# }
# )],
# 'layout': go.Layout(
# xaxis={
# 'title': xaxis_column_name,
# 'type': 'linear' if xaxis_type == 'Linear' else 'log'
# },
# yaxis={
# 'title': yaxis_column_name,
# 'type': 'linear' if yaxis_type == 'Linear' else 'log'
# },
# margin={'l': 40, 'b': 40, 't': 10, 'r': 0},
# hovermode='closest'
# )
}
# cuda.close()
if __name__ == '__main__':
app.run_server(debug=False, host='0.0.0.0', port=8002)