Spaces:
Runtime error
Runtime error
Several more cleanups
Browse files
app.py
CHANGED
@@ -1,30 +1,34 @@
|
|
1 |
import random
|
|
|
2 |
import numpy
|
3 |
import tensorflow
|
4 |
-
from tensorflow.python.framework.ops import disable_eager_execution
|
5 |
import math
|
|
|
6 |
|
7 |
# Becuase important
|
8 |
-
disable_eager_execution()
|
9 |
|
10 |
# Load the training and testing data
|
11 |
load_data = numpy.load('data/train_test_split_data.npz') # Data saved by the VAE
|
12 |
|
13 |
# Convert Data to Tuples and Assign to respective variables
|
14 |
-
box_matrix_train, box_density_train, additional_pixels_train, box_shape_train = tuple(
|
15 |
-
|
|
|
|
|
|
|
16 |
testX = box_matrix_test # Shows the relationship to the MNIST Dataset vs the Shape Dataset
|
17 |
image_size = numpy.shape(testX)[-1] # Determines the size of the images
|
18 |
test_data = numpy.reshape(testX, (len(testX), image_size, image_size, 1))
|
19 |
|
20 |
# Creates tuples that contain all of the data generated
|
21 |
-
allX = numpy.append(box_matrix_train,box_matrix_test, axis=0)
|
22 |
all_box_density = numpy.append(box_density_train, box_density_test, axis=0)
|
23 |
-
all_additional_pixels = numpy.append(additional_pixels_train, additional_pixels_test,axis=0)
|
24 |
-
all_box_shape = numpy.append(box_shape_train, box_shape_test,axis=0)
|
25 |
all_data = numpy.reshape(allX, (len(allX), image_size, image_size, 1))
|
26 |
|
27 |
-
|
28 |
|
29 |
def basic_box_array(image_size):
|
30 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
@@ -44,14 +48,16 @@ def back_slash_array(image_size):
|
|
44 |
A[i][j] = 1
|
45 |
return A
|
46 |
|
|
|
47 |
def forward_slash_array(image_size):
|
48 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
49 |
for i in range(image_size):
|
50 |
for j in range(image_size):
|
51 |
-
if i == (image_size-1)-j:
|
52 |
A[i][j] = 1
|
53 |
return A
|
54 |
|
|
|
55 |
def hot_dog_array(image_size):
|
56 |
# Places pixels down the vertical axis to split the box
|
57 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
@@ -61,6 +67,7 @@ def hot_dog_array(image_size):
|
|
61 |
A[i][j] = 1
|
62 |
return A
|
63 |
|
|
|
64 |
def hamburger_array(image_size):
|
65 |
# Places pixels across the horizontal axis to split the box
|
66 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
@@ -70,17 +77,18 @@ def hamburger_array(image_size):
|
|
70 |
A[i][j] = 1
|
71 |
return A
|
72 |
|
|
|
73 |
def center_array(image_size):
|
74 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
75 |
for i in range(image_size):
|
76 |
for j in range(image_size):
|
77 |
-
if i == math.floor((image_size-1)/2) and j == math.ceil((image_size-1)/2):
|
78 |
A[i][j] = 1
|
79 |
-
if i == math.floor((image_size-1)/2) and j == math.floor((image_size-1)/2):
|
80 |
A[i][j] = 1
|
81 |
-
if j == math.ceil((image_size-1)/2) and i == math.ceil((image_size-1)/2):
|
82 |
A[i][j] = 1
|
83 |
-
if j == math.floor((image_size-1)/2) and i == math.ceil((image_size-1)/2):
|
84 |
A[i][j] = 1
|
85 |
return A
|
86 |
|
@@ -103,7 +111,7 @@ def add_pixels(array_original, additional_pixels, image_size):
|
|
103 |
for j in range(1, image_size - 1):
|
104 |
if A[i - 1][j] + A[i + 1][j] + A[i][j - 1] + A[i][j + 1] > 0:
|
105 |
A_updated[i][j] = 1
|
106 |
-
A = update_array(A, A_updated,image_size)
|
107 |
return A
|
108 |
|
109 |
|
@@ -111,7 +119,7 @@ def basic_box(additional_pixels, density, image_size):
|
|
111 |
A = basic_box_array(image_size) # Creates the outside edges of the box
|
112 |
# Increase the thickness of each part of the box
|
113 |
A = add_pixels(A, additional_pixels, image_size)
|
114 |
-
return A*density
|
115 |
|
116 |
|
117 |
def horizontal_vertical_box_split(additional_pixels, density, image_size):
|
@@ -121,7 +129,7 @@ def horizontal_vertical_box_split(additional_pixels, density, image_size):
|
|
121 |
A = update_array(A, hamburger_array(image_size), image_size)
|
122 |
# Increase the thickness of each part of the box
|
123 |
A = add_pixels(A, additional_pixels, image_size)
|
124 |
-
return A*density
|
125 |
|
126 |
|
127 |
def diagonal_box_split(additional_pixels, density, image_size):
|
@@ -134,7 +142,7 @@ def diagonal_box_split(additional_pixels, density, image_size):
|
|
134 |
# Adds pixels to the thickness of each component of the box
|
135 |
# Increase the thickness of each part of the box
|
136 |
A = add_pixels(A, additional_pixels, image_size)
|
137 |
-
return A*density
|
138 |
|
139 |
|
140 |
def back_slash_box(additional_pixels, density, image_size):
|
@@ -223,18 +231,20 @@ def center_box(additional_pixels, density, image_size):
|
|
223 |
|
224 |
|
225 |
import tensorflow as tf
|
|
|
226 |
sess = tf.compat.v1.Session()
|
227 |
|
228 |
from keras import backend as K
|
|
|
229 |
K.set_session(sess)
|
230 |
|
231 |
# Gradio Interface
|
232 |
|
233 |
-
import gradio
|
234 |
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
238 |
density_options = ["{:.2f}".format(x) for x in numpy.linspace(0.1, 1, 10)]
|
239 |
thickness_options = [str(int(x)) for x in numpy.linspace(0, 10, 11)]
|
240 |
interpolation_options = [str(int(x)) for x in [3, 5, 10, 20]]
|
@@ -280,14 +290,15 @@ def interpolate(t1, t2, d1, d2, th1, th2, steps):
|
|
280 |
predicted_interps = [number_1_expand[0, :, :, 0]]
|
281 |
|
282 |
for latent_point in range(2, num_interp + 2): # cycles the latent points through the decoder model to create images
|
283 |
-
generated_image = decoder_model_boxes.predict(numpy.array([latent_matrix[latent_point - 2]]))[
|
|
|
284 |
predicted_interps.append(generated_image[:, :, -1])
|
285 |
|
286 |
predicted_interps.append(number_2_expand[0, :, :, 0])
|
287 |
|
288 |
transition_region = predicted_interps[0]
|
289 |
-
for i in range(len(predicted_interps)-1):
|
290 |
-
transition_region = numpy.hstack((transition_region, predicted_interps[i+1]))
|
291 |
|
292 |
return transition_region
|
293 |
|
@@ -299,13 +310,13 @@ def generate_unit_cell(t, d, th):
|
|
299 |
with gradio.Blocks() as demo:
|
300 |
with gradio.Row():
|
301 |
with gradio.Column():
|
302 |
-
t1 = gradio.Dropdown(
|
303 |
d1 = gradio.Dropdown(density_options, label="Density 1", value=random.choice(density_options))
|
304 |
th1 = gradio.Dropdown(thickness_options, label="Thickness 1", value=random.choice(thickness_options))
|
305 |
with gradio.Column():
|
306 |
img1 = gradio.Image(label="Endpoint 1")
|
307 |
with gradio.Column():
|
308 |
-
t2 = gradio.Dropdown(
|
309 |
d2 = gradio.Dropdown(density_options, label="Density 2", value=random.choice(density_options))
|
310 |
th2 = gradio.Dropdown(thickness_options, label="Thickness 2", value=random.choice(thickness_options))
|
311 |
with gradio.Column():
|
@@ -316,10 +327,14 @@ with gradio.Blocks() as demo:
|
|
316 |
lattice_inputs_2 = [t2, d2, th2]
|
317 |
[x.change(fn=generate_unit_cell, inputs=lattice_inputs_2, outputs=[img2]) for x in lattice_inputs_2]
|
318 |
|
319 |
-
steps = gradio.Dropdown(interpolation_options, label="Interpolation Length",
|
|
|
320 |
btn = gradio.Button("Run")
|
321 |
img = gradio.Image(label="Transition")
|
322 |
btn.click(fn=interpolate, inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img])
|
323 |
-
examples = gradio.Examples(examples=[["hamburger_box", "hot_dog_box", "1.00", "1.00", "2", "2", "20"],
|
|
|
|
|
|
|
324 |
|
325 |
-
demo.launch()
|
|
|
1 |
import random
|
2 |
+
import gradio
|
3 |
import numpy
|
4 |
import tensorflow
|
|
|
5 |
import math
|
6 |
+
# from tensorflow.python.framework.ops import disable_eager_execution
|
7 |
|
8 |
# Becuase important
|
9 |
+
tensorflow.python.framework.ops.disable_eager_execution()
|
10 |
|
11 |
# Load the training and testing data
|
12 |
load_data = numpy.load('data/train_test_split_data.npz') # Data saved by the VAE
|
13 |
|
14 |
# Convert Data to Tuples and Assign to respective variables
|
15 |
+
box_matrix_train, box_density_train, additional_pixels_train, box_shape_train = tuple(
|
16 |
+
load_data['box_matrix_train']), tuple(load_data['box_density_train']), tuple(
|
17 |
+
load_data['additional_pixels_train']), tuple(load_data['box_shape_train'])
|
18 |
+
box_matrix_test, box_density_test, additional_pixels_test, box_shape_test = tuple(load_data['box_matrix_test']), tuple(
|
19 |
+
load_data['box_density_test']), tuple(load_data['additional_pixels_test']), tuple(load_data['box_shape_test'])
|
20 |
testX = box_matrix_test # Shows the relationship to the MNIST Dataset vs the Shape Dataset
|
21 |
image_size = numpy.shape(testX)[-1] # Determines the size of the images
|
22 |
test_data = numpy.reshape(testX, (len(testX), image_size, image_size, 1))
|
23 |
|
24 |
# Creates tuples that contain all of the data generated
|
25 |
+
allX = numpy.append(box_matrix_train, box_matrix_test, axis=0)
|
26 |
all_box_density = numpy.append(box_density_train, box_density_test, axis=0)
|
27 |
+
all_additional_pixels = numpy.append(additional_pixels_train, additional_pixels_test, axis=0)
|
28 |
+
all_box_shape = numpy.append(box_shape_train, box_shape_test, axis=0)
|
29 |
all_data = numpy.reshape(allX, (len(allX), image_size, image_size, 1))
|
30 |
|
31 |
+
|
32 |
|
33 |
def basic_box_array(image_size):
|
34 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
|
|
48 |
A[i][j] = 1
|
49 |
return A
|
50 |
|
51 |
+
|
52 |
def forward_slash_array(image_size):
|
53 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
54 |
for i in range(image_size):
|
55 |
for j in range(image_size):
|
56 |
+
if i == (image_size - 1) - j:
|
57 |
A[i][j] = 1
|
58 |
return A
|
59 |
|
60 |
+
|
61 |
def hot_dog_array(image_size):
|
62 |
# Places pixels down the vertical axis to split the box
|
63 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
|
|
67 |
A[i][j] = 1
|
68 |
return A
|
69 |
|
70 |
+
|
71 |
def hamburger_array(image_size):
|
72 |
# Places pixels across the horizontal axis to split the box
|
73 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
|
|
77 |
A[i][j] = 1
|
78 |
return A
|
79 |
|
80 |
+
|
81 |
def center_array(image_size):
|
82 |
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
83 |
for i in range(image_size):
|
84 |
for j in range(image_size):
|
85 |
+
if i == math.floor((image_size - 1) / 2) and j == math.ceil((image_size - 1) / 2):
|
86 |
A[i][j] = 1
|
87 |
+
if i == math.floor((image_size - 1) / 2) and j == math.floor((image_size - 1) / 2):
|
88 |
A[i][j] = 1
|
89 |
+
if j == math.ceil((image_size - 1) / 2) and i == math.ceil((image_size - 1) / 2):
|
90 |
A[i][j] = 1
|
91 |
+
if j == math.floor((image_size - 1) / 2) and i == math.ceil((image_size - 1) / 2):
|
92 |
A[i][j] = 1
|
93 |
return A
|
94 |
|
|
|
111 |
for j in range(1, image_size - 1):
|
112 |
if A[i - 1][j] + A[i + 1][j] + A[i][j - 1] + A[i][j + 1] > 0:
|
113 |
A_updated[i][j] = 1
|
114 |
+
A = update_array(A, A_updated, image_size)
|
115 |
return A
|
116 |
|
117 |
|
|
|
119 |
A = basic_box_array(image_size) # Creates the outside edges of the box
|
120 |
# Increase the thickness of each part of the box
|
121 |
A = add_pixels(A, additional_pixels, image_size)
|
122 |
+
return A * density
|
123 |
|
124 |
|
125 |
def horizontal_vertical_box_split(additional_pixels, density, image_size):
|
|
|
129 |
A = update_array(A, hamburger_array(image_size), image_size)
|
130 |
# Increase the thickness of each part of the box
|
131 |
A = add_pixels(A, additional_pixels, image_size)
|
132 |
+
return A * density
|
133 |
|
134 |
|
135 |
def diagonal_box_split(additional_pixels, density, image_size):
|
|
|
142 |
# Adds pixels to the thickness of each component of the box
|
143 |
# Increase the thickness of each part of the box
|
144 |
A = add_pixels(A, additional_pixels, image_size)
|
145 |
+
return A * density
|
146 |
|
147 |
|
148 |
def back_slash_box(additional_pixels, density, image_size):
|
|
|
231 |
|
232 |
|
233 |
import tensorflow as tf
|
234 |
+
|
235 |
sess = tf.compat.v1.Session()
|
236 |
|
237 |
from keras import backend as K
|
238 |
+
|
239 |
K.set_session(sess)
|
240 |
|
241 |
# Gradio Interface
|
242 |
|
|
|
243 |
|
244 |
+
endpoint_options = (
|
245 |
+
"basic_box", "diagonal_box_split", "horizontal_vertical_box_split", "back_slash_box", "forward_slash_box",
|
246 |
+
"back_slash_plus_box", "forward_slash_plus_box", "hot_dog_box", "hamburger_box", "x_hamburger_box",
|
247 |
+
"x_hot_dog_box", "x_plus_box")
|
248 |
density_options = ["{:.2f}".format(x) for x in numpy.linspace(0.1, 1, 10)]
|
249 |
thickness_options = [str(int(x)) for x in numpy.linspace(0, 10, 11)]
|
250 |
interpolation_options = [str(int(x)) for x in [3, 5, 10, 20]]
|
|
|
290 |
predicted_interps = [number_1_expand[0, :, :, 0]]
|
291 |
|
292 |
for latent_point in range(2, num_interp + 2): # cycles the latent points through the decoder model to create images
|
293 |
+
generated_image = decoder_model_boxes.predict(numpy.array([latent_matrix[latent_point - 2]]))[
|
294 |
+
0] # generates an interpolated image based on the latent point
|
295 |
predicted_interps.append(generated_image[:, :, -1])
|
296 |
|
297 |
predicted_interps.append(number_2_expand[0, :, :, 0])
|
298 |
|
299 |
transition_region = predicted_interps[0]
|
300 |
+
for i in range(len(predicted_interps) - 1):
|
301 |
+
transition_region = numpy.hstack((transition_region, predicted_interps[i + 1]))
|
302 |
|
303 |
return transition_region
|
304 |
|
|
|
310 |
with gradio.Blocks() as demo:
|
311 |
with gradio.Row():
|
312 |
with gradio.Column():
|
313 |
+
t1 = gradio.Dropdown(endpoint_options, label="Type 1", value=random.choice(endpoint_options))
|
314 |
d1 = gradio.Dropdown(density_options, label="Density 1", value=random.choice(density_options))
|
315 |
th1 = gradio.Dropdown(thickness_options, label="Thickness 1", value=random.choice(thickness_options))
|
316 |
with gradio.Column():
|
317 |
img1 = gradio.Image(label="Endpoint 1")
|
318 |
with gradio.Column():
|
319 |
+
t2 = gradio.Dropdown(endpoint_options, label="Type 2", value=random.choice(endpoint_options))
|
320 |
d2 = gradio.Dropdown(density_options, label="Density 2", value=random.choice(density_options))
|
321 |
th2 = gradio.Dropdown(thickness_options, label="Thickness 2", value=random.choice(thickness_options))
|
322 |
with gradio.Column():
|
|
|
327 |
lattice_inputs_2 = [t2, d2, th2]
|
328 |
[x.change(fn=generate_unit_cell, inputs=lattice_inputs_2, outputs=[img2]) for x in lattice_inputs_2]
|
329 |
|
330 |
+
steps = gradio.Dropdown(interpolation_options, label="Interpolation Length",
|
331 |
+
value=random.choice(interpolation_options))
|
332 |
btn = gradio.Button("Run")
|
333 |
img = gradio.Image(label="Transition")
|
334 |
btn.click(fn=interpolate, inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img])
|
335 |
+
examples = gradio.Examples(examples=[["hamburger_box", "hot_dog_box", "1.00", "1.00", "2", "2", "20"],
|
336 |
+
["hamburger_box", "hot_dog_box", "0.10", "1.00", "10", "10", "5"]],
|
337 |
+
fn=interpolate,
|
338 |
+
inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img], cache_examples=True)
|
339 |
|
340 |
+
demo.launch()
|