File size: 15,289 Bytes
412e24c
 
 
 
 
 
 
7d77c56
 
412e24c
 
 
a99a8d0
412e24c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e23262e
412e24c
e23262e
412e24c
6e082a5
412e24c
6e082a5
a99a8d0
412e24c
 
a99a8d0
412e24c
 
 
 
 
 
a99a8d0
412e24c
 
 
 
 
 
 
 
 
 
 
 
 
 
a7b44f0
412e24c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed154bd
412e24c
6e082a5
 
 
 
 
 
 
 
412e24c
 
 
48aa2d0
 
 
6e082a5
 
 
 
412e24c
48aa2d0
1141b7e
48aa2d0
6e082a5
 
 
 
48aa2d0
412e24c
 
 
5c5bf62
412e24c
284822b
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
import random
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import tensorflow
from tensorflow.python.framework.ops import disable_eager_execution
import pandas as pd
import math


disable_eager_execution()

load_data = np.load('data/train_test_split_data.npz')  # Data saved by the VAE

# Convert Data to Tuples and Assign to respective variables
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'])
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'])
testX = box_matrix_test  # Shows the relationship to the MNIST Dataset vs the Shape Dataset
image_size = np.shape(testX)[-1]  # Determines the size of the images
test_data = np.reshape(testX, (len(testX), image_size, image_size, 1))

# Creates tuples that contain all of the data generated
allX = np.append(box_matrix_train,box_matrix_test, axis=0)
all_box_density = np.append(box_density_train, box_density_test, axis=0)
all_additional_pixels = np.append(additional_pixels_train, additional_pixels_test,axis=0)
all_box_shape = np.append(box_shape_train, box_shape_test,axis=0)
all_data = np.reshape(allX, (len(allX), image_size, image_size, 1))

# train_latent_points = []
# train_data = np.reshape(box_matrix_train, (len(box_matrix_train), image_size, image_size, 1))
# for i in range(len(box_shape_train)):
#     predicted_train = encoder_model_boxes.predict(np.array([train_data[i]]))
#     train_latent_points.append(predicted_train[0])
# train_latent_points = np.array(train_latent_points)

shapes = ("basic_box", "diagonal_box_split", "horizontal_vertical_box_split", "back_slash_box", "forward_slash_box",
          "back_slash_plus_box", "forward_slash_plus_box", "hot_dog_box", "hamburger_box", "x_hamburger_box",
          "x_hot_dog_box", "x_plus_box")

import math

def basic_box_array(image_size):
    A = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    # Creates the outside edges of the box
    for i in range(image_size):
        for j in range(image_size):
            if i == 0 or j == 0 or i == image_size - 1 or j == image_size - 1:
                A[i][j] = 1
    return A

def back_slash_array(image_size):
    A = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    for i in range(image_size):
        for j in range(image_size):
            if i == j:
                A[i][j] = 1
    return A

def forward_slash_array(image_size):
    A = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    for i in range(image_size):
        for j in range(image_size):
            if i == (image_size-1)-j:
                A[i][j] = 1
    return A

def hot_dog_array(image_size):
    # Places pixels down the vertical axis to split the box
    A = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    for i in range(image_size):
        for j in range(image_size):
            if j == math.floor((image_size - 1) / 2) or j == math.ceil((image_size - 1) / 2):
                A[i][j] = 1
    return A

def hamburger_array(image_size):
    # Places pixels across the horizontal axis to split the box
    A = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    for i in range(image_size):
        for j in range(image_size):
            if i == math.floor((image_size - 1) / 2) or i == math.ceil((image_size - 1) / 2):
                A[i][j] = 1
    return A

def center_array(image_size):
    A = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    for i in range(image_size):
        for j in range(image_size):
            if i == math.floor((image_size-1)/2) and j == math.ceil((image_size-1)/2):
                A[i][j] = 1
            if i == math.floor((image_size-1)/2) and j == math.floor((image_size-1)/2):
                A[i][j] = 1
            if j == math.ceil((image_size-1)/2) and i == math.ceil((image_size-1)/2):
                A[i][j] = 1
            if j == math.floor((image_size-1)/2) and i == math.ceil((image_size-1)/2):
                A[i][j] = 1
    return A

def update_array(array_original, array_new, image_size):
    A = array_original
    for i in range(image_size):
        for j in range(image_size):
            if array_new[i][j] == 1:
                A[i][j] = 1
    return A

def add_pixels(array_original, additional_pixels, image_size):
    # Adds pixels to the thickness of each component of the box
    A = array_original
    A_updated = np.zeros((int(image_size), int(image_size)))  # Initializes A matrix with 0 values
    for dens in range(additional_pixels):
        for i in range(1, image_size - 1):
            for j in range(1, image_size - 1):
                if A[i - 1][j] + A[i + 1][j] + A[i][j - 1] + A[i][j + 1] > 0:
                    A_updated[i][j] = 1
        A = update_array(A, A_updated,image_size)
    return A


def basic_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Creates the outside edges of the box
    # Increase the thickness of each part of the box
    A = add_pixels(A, additional_pixels, image_size)
    return A*density

def horizontal_vertical_box_split(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Creates the outside edges of the box
    # Place pixels across the horizontal and vertical axes to split the box
    A = update_array(A, hot_dog_array(image_size), image_size)
    A = update_array(A, hamburger_array(image_size), image_size)
    # Increase the thickness of each part of the box
    A = add_pixels(A, additional_pixels, image_size)
    return A*density

def diagonal_box_split(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Creates the outside edges of the box

    # Add pixels along the diagonals of the box
    A = update_array(A, back_slash_array(image_size), image_size)
    A = update_array(A, forward_slash_array(image_size), image_size)

    # Adds pixels to the thickness of each component of the box
    # Increase the thickness of each part of the box
    A = add_pixels(A, additional_pixels, image_size)
    return A*density

def back_slash_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, back_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def forward_slash_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, forward_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def hot_dog_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, hot_dog_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def hamburger_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, hamburger_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def x_plus_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, hot_dog_array(image_size), image_size)
    A = update_array(A, hamburger_array(image_size), image_size)
    A = update_array(A, forward_slash_array(image_size), image_size)
    A = update_array(A, back_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def forward_slash_plus_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, hot_dog_array(image_size), image_size)
    A = update_array(A, hamburger_array(image_size), image_size)
    A = update_array(A, forward_slash_array(image_size), image_size)
    # A = update_array(A, back_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def back_slash_plus_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, hot_dog_array(image_size), image_size)
    A = update_array(A, hamburger_array(image_size), image_size)
    # A = update_array(A, forward_slash_array(image_size), image_size)
    A = update_array(A, back_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def x_hot_dog_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, hot_dog_array(image_size), image_size)
    # A = update_array(A, hamburger_array(image_size), image_size)
    A = update_array(A, forward_slash_array(image_size), image_size)
    A = update_array(A, back_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def x_hamburger_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    # A = update_array(A, hot_dog_array(image_size), image_size)
    A = update_array(A, hamburger_array(image_size), image_size)
    A = update_array(A, forward_slash_array(image_size), image_size)
    A = update_array(A, back_slash_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density

def center_box(additional_pixels, density, image_size):
    A = basic_box_array(image_size)  # Initializes A matrix with 0 values
    A = update_array(A, center_array(image_size), image_size)
    A = add_pixels(A, additional_pixels, image_size)
    return A * density



import tensorflow as tf
sess = tf.compat.v1.Session()

from keras import backend as K
K.set_session(sess)

# Gradio Interface

import gradio
import numpy

endpoint_types = shapes
density_options = ["{:.2f}".format(x) for x in numpy.linspace(0.1, 1, 10)]
thickness_options = [str(int(x)) for x in numpy.linspace(0, 10, 11)]
interpolation_options = [str(int(x)) for x in [3, 5, 10, 20]]


def interpolate(t1, t2, d1, d2, th1, th2, steps):
    # Load the decoder model
    decoder_model_boxes = tensorflow.keras.models.load_model('data/decoder_model_boxes', compile=False)

    # # import the encoder model architecture
    json_file_loaded = open('data/model.json', 'r')
    loaded_model_json = json_file_loaded.read()

    # load model using the saved json file
    encoder_model_boxes = tensorflow.keras.models.model_from_json(loaded_model_json)

    # load weights into newly loaded_model
    encoder_model_boxes.load_weights('data/model_tf')

    num_internal = int(steps)
    number_1 = globals()[t1](int(th1), float(d1), 28)
    number_2 = globals()[t2](int(th2), float(d2), 28)

    # resize the array to match the prediction size requirement
    number_1_expand = np.expand_dims(np.expand_dims(number_1, axis=2), axis=0)
    number_2_expand = np.expand_dims(np.expand_dims(number_2, axis=2), axis=0)

    # Determine the latent point that will represent our desired number
    latent_point_1 = encoder_model_boxes.predict(number_1_expand)[0]
    latent_point_2 = encoder_model_boxes.predict(number_2_expand)[0]

    latent_dimensionality = len(latent_point_1)  # define the dimensionality of the latent space
    num_interp = num_internal  # the number of images to be pictured
    latent_matrix = []  # This will contain the latent points of the interpolation
    for column in range(latent_dimensionality):
        new_column = np.linspace(latent_point_1[column], latent_point_2[column], num_interp)
        latent_matrix.append(new_column)
    latent_matrix = np.array(latent_matrix).T  # Transposes the matrix so that each row can be easily indexed

    plot_rows = 2
    plot_columns = num_interp + 2

    predicted_interps = [number_1_expand[0, :, :, 0]]

    for latent_point in range(2, num_interp + 2):  # cycles the latent points through the decoder model to create images
        generated_image = decoder_model_boxes.predict(np.array([latent_matrix[latent_point - 2]]))[0]  # generates an interpolated image based on the latent point
        predicted_interps.append(generated_image[:, :, -1])

    predicted_interps.append(number_2_expand[0, :, :, 0])

    transition_region = predicted_interps[0]
    for i in range(len(predicted_interps)-1):
        transition_region = numpy.hstack((transition_region, predicted_interps[i+1]))

    return transition_region

def generate_unit_cell(t, d, th):
    number_1 = globals()[t](int(th), float(d), 28)

    # resize the array to match the prediction size requirement
    number_1_expand = np.expand_dims(np.expand_dims(number_1, axis=2), axis=0)

    return number_1_expand[0, :, :, 0]

with gradio.Blocks() as demo:
    with gradio.Row():
        with gradio.Column():
            t1 = gradio.Dropdown(endpoint_types, label="Type 1", value=random.choice(endpoint_types))
            d1 = gradio.Dropdown(density_options, label="Density 1", value=random.choice(density_options))
            th1 = gradio.Dropdown(thickness_options, label="Thickness 1", value=random.choice(thickness_options))
            img1 = gradio.Image(label="Endpoint 1")
            t1.change(fn=generate_unit_cell, inputs=[t1, d1, th1], outputs=[img1])
            d1.change(fn=generate_unit_cell, inputs=[t1, d1, th1], outputs=[img1])
            th1.change(fn=generate_unit_cell, inputs=[t1, d1, th1], outputs=[img1])
        with gradio.Column():
            t2 = gradio.Dropdown(endpoint_types, label="Type 2", value=random.choice(endpoint_types))
            d2 = gradio.Dropdown(density_options, label="Density 2", value=random.choice(density_options))
            th2 = gradio.Dropdown(thickness_options, label="Thickness 2", value=random.choice(thickness_options))
            img2 = gradio.Image(label="Endpoint 2")
            t2.change(fn=generate_unit_cell, inputs=[t2, d2, th2], outputs=[img1])
            d2.change(fn=generate_unit_cell, inputs=[t2, d2, th2], outputs=[img1])
            th2.change(fn=generate_unit_cell, inputs=[t2, d2, th2], outputs=[img1])
    steps = gradio.Dropdown(interpolation_options, label="Interpolation Length", value=random.choice(interpolation_options))
    btn = gradio.Button("Run")
    img = gradio.Image(label="Transition")
    btn.click(fn=interpolate, inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img])
    examples = gradio.Examples(examples=[["hamburger_box", "hot_dog_box", "1.00", "1.00", "2", "2", "20"], ["hamburger_box", "hot_dog_box", "0.10", "1.00", "10", "10", "5"]], fn=interpolate, inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img], cache_examples = True)

demo.launch()