ccm commited on
Commit
412e24c
·
1 Parent(s): cd28d02

Lets give this app a go

Browse files
Files changed (1) hide show
  1. app.py +313 -0
app.py ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+ from matplotlib.offsetbox import OffsetImage, AnnotationBbox
5
+ import tensorflow
6
+ import warnings
7
+ from tensorflow.python.framework.ops import disable_eager_execution
8
+ import pandas as pd
9
+
10
+ warnings.filterwarnings('ignore')
11
+ disable_eager_execution()
12
+
13
+ load_data = np.load('train_test_split_data.npz') # Data saved by the VAE
14
+
15
+ # Convert Data to Tuples and Assign to respective variables
16
+ box_matrix_train, box_density_train, additional_pixels_train, box_shape_train = tuple(load_data['box_matrix_train']), tuple(load_data['box_density_train']), tuple(load_data['additional_pixels_train']), tuple(load_data['box_shape_train'])
17
+ box_matrix_test, box_density_test, additional_pixels_test, box_shape_test = tuple(load_data['box_matrix_test']), tuple(load_data['box_density_test']), tuple(load_data['additional_pixels_test']), tuple(load_data['box_shape_test'])
18
+ testX = box_matrix_test # Shows the relationship to the MNIST Dataset vs the Shape Dataset
19
+ image_size = np.shape(testX)[-1] # Determines the size of the images
20
+ test_data = np.reshape(testX, (len(testX), image_size, image_size, 1))
21
+
22
+ # Creates tuples that contain all of the data generated
23
+ allX = np.append(box_matrix_train,box_matrix_test, axis=0)
24
+ all_box_density = np.append(box_density_train, box_density_test, axis=0)
25
+ all_additional_pixels = np.append(additional_pixels_train, additional_pixels_test,axis=0)
26
+ all_box_shape = np.append(box_shape_train, box_shape_test,axis=0)
27
+ all_data = np.reshape(allX, (len(allX), image_size, image_size, 1))
28
+
29
+ # train_latent_points = []
30
+ # train_data = np.reshape(box_matrix_train, (len(box_matrix_train), image_size, image_size, 1))
31
+ # for i in range(len(box_shape_train)):
32
+ # predicted_train = encoder_model_boxes.predict(np.array([train_data[i]]))
33
+ # train_latent_points.append(predicted_train[0])
34
+ # train_latent_points = np.array(train_latent_points)
35
+
36
+ shapes = ("basic_box", "diagonal_box_split", "horizontal_vertical_box_split", "back_slash_box", "forward_slash_box",
37
+ "back_slash_plus_box", "forward_slash_plus_box", "hot_dog_box", "hamburger_box", "x_hamburger_box",
38
+ "x_hot_dog_box", "x_plus_box")
39
+
40
+ import numpy as np
41
+ import matplotlib.pyplot as plt
42
+ import math
43
+
44
+ def basic_box_array(image_size):
45
+ A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
46
+ # Creates the outside edges of the box
47
+ for i in range(image_size):
48
+ for j in range(image_size):
49
+ if i == 0 or j == 0 or i == image_size - 1 or j == image_size - 1:
50
+ A[i][j] = 1
51
+ return A
52
+
53
+ def back_slash_array(image_size):
54
+ A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
55
+ for i in range(image_size):
56
+ for j in range(image_size):
57
+ if i == j:
58
+ A[i][j] = 1
59
+ return A
60
+
61
+ def forward_slash_array(image_size):
62
+ A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
63
+ for i in range(image_size):
64
+ for j in range(image_size):
65
+ if i == (image_size-1)-j:
66
+ A[i][j] = 1
67
+ return A
68
+
69
+ def hot_dog_array(image_size):
70
+ # Places pixels down the vertical axis to split the box
71
+ A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
72
+ for i in range(image_size):
73
+ for j in range(image_size):
74
+ if j == math.floor((image_size - 1) / 2) or j == math.ceil((image_size - 1) / 2):
75
+ A[i][j] = 1
76
+ return A
77
+
78
+ def hamburger_array(image_size):
79
+ # Places pixels across the horizontal axis to split the box
80
+ A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
81
+ for i in range(image_size):
82
+ for j in range(image_size):
83
+ if i == math.floor((image_size - 1) / 2) or i == math.ceil((image_size - 1) / 2):
84
+ A[i][j] = 1
85
+ return A
86
+
87
+ def center_array(image_size):
88
+ A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
89
+ for i in range(image_size):
90
+ for j in range(image_size):
91
+ if i == math.floor((image_size-1)/2) and j == math.ceil((image_size-1)/2):
92
+ A[i][j] = 1
93
+ if i == math.floor((image_size-1)/2) and j == math.floor((image_size-1)/2):
94
+ A[i][j] = 1
95
+ if j == math.ceil((image_size-1)/2) and i == math.ceil((image_size-1)/2):
96
+ A[i][j] = 1
97
+ if j == math.floor((image_size-1)/2) and i == math.ceil((image_size-1)/2):
98
+ A[i][j] = 1
99
+ return A
100
+
101
+ def update_array(array_original, array_new, image_size):
102
+ A = array_original
103
+ for i in range(image_size):
104
+ for j in range(image_size):
105
+ if array_new[i][j] == 1:
106
+ A[i][j] = 1
107
+ return A
108
+
109
+ def add_pixels(array_original, additional_pixels, image_size):
110
+ # Adds pixels to the thickness of each component of the box
111
+ A = array_original
112
+ A_updated = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
113
+ for dens in range(additional_pixels):
114
+ for i in range(1, image_size - 1):
115
+ for j in range(1, image_size - 1):
116
+ if A[i - 1][j] + A[i + 1][j] + A[i][j - 1] + A[i][j + 1] > 0:
117
+ A_updated[i][j] = 1
118
+ A = update_array(A, A_updated,image_size)
119
+ return A
120
+
121
+
122
+
123
+ import numpy as np
124
+ import matplotlib.pyplot as plt
125
+ import math
126
+
127
+ def basic_box(additional_pixels, density, image_size):
128
+ A = basic_box_array(image_size) # Creates the outside edges of the box
129
+ # Increase the thickness of each part of the box
130
+ A = add_pixels(A, additional_pixels, image_size)
131
+ return A*density
132
+
133
+ def horizontal_vertical_box_split(additional_pixels, density, image_size):
134
+ A = basic_box_array(image_size) # Creates the outside edges of the box
135
+ # Place pixels across the horizontal and vertical axes to split the box
136
+ A = update_array(A, hot_dog_array(image_size), image_size)
137
+ A = update_array(A, hamburger_array(image_size), image_size)
138
+ # Increase the thickness of each part of the box
139
+ A = add_pixels(A, additional_pixels, image_size)
140
+ return A*density
141
+
142
+ def diagonal_box_split(additional_pixels, density, image_size):
143
+ A = basic_box_array(image_size) # Creates the outside edges of the box
144
+
145
+ # Add pixels along the diagonals of the box
146
+ A = update_array(A, back_slash_array(image_size), image_size)
147
+ A = update_array(A, forward_slash_array(image_size), image_size)
148
+
149
+ # Adds pixels to the thickness of each component of the box
150
+ # Increase the thickness of each part of the box
151
+ A = add_pixels(A, additional_pixels, image_size)
152
+ return A*density
153
+
154
+ def back_slash_box(additional_pixels, density, image_size):
155
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
156
+ A = update_array(A, back_slash_array(image_size), image_size)
157
+ A = add_pixels(A, additional_pixels, image_size)
158
+ return A * density
159
+
160
+ def forward_slash_box(additional_pixels, density, image_size):
161
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
162
+ A = update_array(A, forward_slash_array(image_size), image_size)
163
+ A = add_pixels(A, additional_pixels, image_size)
164
+ return A * density
165
+
166
+ def hot_dog_box(additional_pixels, density, image_size):
167
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
168
+ A = update_array(A, hot_dog_array(image_size), image_size)
169
+ A = add_pixels(A, additional_pixels, image_size)
170
+ return A * density
171
+
172
+ def hamburger_box(additional_pixels, density, image_size):
173
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
174
+ A = update_array(A, hamburger_array(image_size), image_size)
175
+ A = add_pixels(A, additional_pixels, image_size)
176
+ return A * density
177
+
178
+ def x_plus_box(additional_pixels, density, image_size):
179
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
180
+ A = update_array(A, hot_dog_array(image_size), image_size)
181
+ A = update_array(A, hamburger_array(image_size), image_size)
182
+ A = update_array(A, forward_slash_array(image_size), image_size)
183
+ A = update_array(A, back_slash_array(image_size), image_size)
184
+ A = add_pixels(A, additional_pixels, image_size)
185
+ return A * density
186
+
187
+ def forward_slash_plus_box(additional_pixels, density, image_size):
188
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
189
+ A = update_array(A, hot_dog_array(image_size), image_size)
190
+ A = update_array(A, hamburger_array(image_size), image_size)
191
+ A = update_array(A, forward_slash_array(image_size), image_size)
192
+ # A = update_array(A, back_slash_array(image_size), image_size)
193
+ A = add_pixels(A, additional_pixels, image_size)
194
+ return A * density
195
+
196
+ def back_slash_plus_box(additional_pixels, density, image_size):
197
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
198
+ A = update_array(A, hot_dog_array(image_size), image_size)
199
+ A = update_array(A, hamburger_array(image_size), image_size)
200
+ # A = update_array(A, forward_slash_array(image_size), image_size)
201
+ A = update_array(A, back_slash_array(image_size), image_size)
202
+ A = add_pixels(A, additional_pixels, image_size)
203
+ return A * density
204
+
205
+ def x_hot_dog_box(additional_pixels, density, image_size):
206
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
207
+ A = update_array(A, hot_dog_array(image_size), image_size)
208
+ # A = update_array(A, hamburger_array(image_size), image_size)
209
+ A = update_array(A, forward_slash_array(image_size), image_size)
210
+ A = update_array(A, back_slash_array(image_size), image_size)
211
+ A = add_pixels(A, additional_pixels, image_size)
212
+ return A * density
213
+
214
+ def x_hamburger_box(additional_pixels, density, image_size):
215
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
216
+ # A = update_array(A, hot_dog_array(image_size), image_size)
217
+ A = update_array(A, hamburger_array(image_size), image_size)
218
+ A = update_array(A, forward_slash_array(image_size), image_size)
219
+ A = update_array(A, back_slash_array(image_size), image_size)
220
+ A = add_pixels(A, additional_pixels, image_size)
221
+ return A * density
222
+
223
+ def center_box(additional_pixels, density, image_size):
224
+ A = basic_box_array(image_size) # Initializes A matrix with 0 values
225
+ A = update_array(A, center_array(image_size), image_size)
226
+ A = add_pixels(A, additional_pixels, image_size)
227
+ return A * density
228
+
229
+
230
+
231
+ import tensorflow as tf
232
+ sess = tf.compat.v1.Session()
233
+
234
+ from keras import backend as K
235
+ K.set_session(sess)
236
+
237
+ # Gradio Interface
238
+
239
+ import gradio
240
+ import numpy
241
+
242
+ endpoint_types = shapes
243
+ density_options = ["{:.2f}".format(x) for x in numpy.linspace(0, 1, 11)]
244
+ thickness_options = [str(int(x)) for x in numpy.linspace(0, 10, 11)]
245
+ interpolation_options = [str(int(x)) for x in [3, 5, 10]]
246
+
247
+ def interpolate(t1, t2, d1, d2, th1, th2, steps):
248
+ decoder_model_boxes = tensorflow.keras.models.load_model('decoder_model_boxes', compile=False)
249
+ # # compile=False ignores a warning from tensorflow, can be removed to see warning
250
+
251
+ # # import the encoder model architecture
252
+ json_file_loaded = open('model.json', 'r')
253
+ loaded_model_json = json_file_loaded.read()
254
+
255
+ # load model using the saved json file
256
+ encoder_model_boxes = tensorflow.keras.models.model_from_json(loaded_model_json)
257
+
258
+ # load weights into newly loaded_model
259
+ encoder_model_boxes.load_weights('model_tf')
260
+
261
+ num_internal = int(steps)
262
+ number_1 = globals()[t1](int(th1), float(d1), 28)
263
+ number_2 = globals()[t2](int(th2), float(d2), 28)
264
+
265
+ # resize the array to match the prediction size requirement
266
+ number_1_expand = np.expand_dims(np.expand_dims(number_1, axis=2), axis=0)
267
+ number_2_expand = np.expand_dims(np.expand_dims(number_2, axis=2), axis=0)
268
+
269
+ # Determine the latent point that will represent our desired number
270
+ latent_point_1 = encoder_model_boxes.predict(number_1_expand)[0]
271
+ latent_point_2 = encoder_model_boxes.predict(number_2_expand)[0]
272
+
273
+ latent_dimensionality = len(latent_point_1) # define the dimensionality of the latent space
274
+ num_interp = num_internal + 2 # the number of images to be pictured
275
+ latent_matrix = [] # This will contain the latent points of the interpolation
276
+ for column in range(latent_dimensionality):
277
+ new_column = np.linspace(latent_point_1[column], latent_point_2[column], num_interp)
278
+ latent_matrix.append(new_column)
279
+ latent_matrix = np.array(latent_matrix).T # Transposes the matrix so that each row can be easily indexed
280
+
281
+ plot_rows = 2
282
+ plot_columns = num_interp + 2
283
+
284
+ predicted_interps = [number_1_expand[0, :, :, 0]]
285
+
286
+ for latent_point in range(2, num_interp + 2): # cycles the latent points through the decoder model to create images
287
+ generated_image = decoder_model_boxes.predict(np.array([latent_matrix[latent_point - 2]]))[0] # generates an interpolated image based on the latent point
288
+ predicted_interps.append(generated_image[:, :, -1])
289
+
290
+ predicted_interps.append(number_2_expand[0, :, :, 0])
291
+
292
+ transition_region = predicted_interps[0]
293
+ for i in range(len(predicted_interps)-1):
294
+ transition_region = numpy.hstack((transition_region, predicted_interps[i+1]))
295
+
296
+ return transition_region
297
+
298
+ with gradio.Blocks() as demo:
299
+ with gradio.Row():
300
+ with gradio.Column():
301
+ t1 = gradio.Dropdown(endpoint_types, label="Type 1", value="basic_box")
302
+ d1 = gradio.Dropdown(density_options, label="Density 1", value="0.10")
303
+ th1 = gradio.Dropdown(thickness_options, label="Thickness 1", value="5")
304
+ with gradio.Column():
305
+ t2 = gradio.Dropdown(endpoint_types, label="Type 2", value="basic_box")
306
+ d2 = gradio.Dropdown(density_options, label="Density 2", value="0.10")
307
+ th2 = gradio.Dropdown(thickness_options, label="Thickness 2", value="5")
308
+ steps = gradio.Dropdown(interpolation_options, label="Interpolation Length", value="5")
309
+ btn = gradio.Button("Run")
310
+ img = gradio.Image(label="Transition")
311
+ btn.click(fn=interpolate, inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img])
312
+
313
+ demo.launch(debug=True)