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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import huggingface_hub # for loading model
|
8 |
+
import matplotlib.pyplot
|
9 |
+
|
10 |
+
# Because important
|
11 |
+
# disable_eager_execution()
|
12 |
+
|
13 |
+
def basic_box_array(image_size):
|
14 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
15 |
+
# Creates the outside edges of the box
|
16 |
+
for i in range(image_size):
|
17 |
+
for j in range(image_size):
|
18 |
+
if i == 0 or j == 0 or i == image_size - 1 or j == image_size - 1:
|
19 |
+
A[i][j] = 1
|
20 |
+
return A
|
21 |
+
|
22 |
+
|
23 |
+
def back_slash_array(image_size):
|
24 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
25 |
+
for i in range(image_size):
|
26 |
+
for j in range(image_size):
|
27 |
+
if i == j:
|
28 |
+
A[i][j] = 1
|
29 |
+
return A
|
30 |
+
|
31 |
+
|
32 |
+
def forward_slash_array(image_size):
|
33 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
34 |
+
for i in range(image_size):
|
35 |
+
for j in range(image_size):
|
36 |
+
if i == (image_size - 1) - j:
|
37 |
+
A[i][j] = 1
|
38 |
+
return A
|
39 |
+
|
40 |
+
|
41 |
+
def hot_dog_array(image_size):
|
42 |
+
# Places pixels down the vertical axis to split the box
|
43 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
44 |
+
for i in range(image_size):
|
45 |
+
for j in range(image_size):
|
46 |
+
if j == math.floor((image_size - 1) / 2) or j == math.ceil((image_size - 1) / 2):
|
47 |
+
A[i][j] = 1
|
48 |
+
return A
|
49 |
+
|
50 |
+
|
51 |
+
def hamburger_array(image_size):
|
52 |
+
# Places pixels across the horizontal axis to split the box
|
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 == math.floor((image_size - 1) / 2) or i == math.ceil((image_size - 1) / 2):
|
57 |
+
A[i][j] = 1
|
58 |
+
return A
|
59 |
+
|
60 |
+
|
61 |
+
def center_array(image_size):
|
62 |
+
A = numpy.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 == math.floor((image_size - 1) / 2) and j == math.ceil((image_size - 1) / 2):
|
66 |
+
A[i][j] = 1
|
67 |
+
if i == math.floor((image_size - 1) / 2) and j == math.floor((image_size - 1) / 2):
|
68 |
+
A[i][j] = 1
|
69 |
+
if j == math.ceil((image_size - 1) / 2) and i == math.ceil((image_size - 1) / 2):
|
70 |
+
A[i][j] = 1
|
71 |
+
if j == math.floor((image_size - 1) / 2) and i == math.ceil((image_size - 1) / 2):
|
72 |
+
A[i][j] = 1
|
73 |
+
return A
|
74 |
+
|
75 |
+
|
76 |
+
def update_array(array_original, array_new, image_size):
|
77 |
+
A = array_original
|
78 |
+
for i in range(image_size):
|
79 |
+
for j in range(image_size):
|
80 |
+
if array_new[i][j] == 1:
|
81 |
+
A[i][j] = 1
|
82 |
+
return A
|
83 |
+
|
84 |
+
|
85 |
+
def add_pixels(array_original, additional_pixels, image_size):
|
86 |
+
# Adds pixels to the thickness of each component of the box
|
87 |
+
A = array_original
|
88 |
+
A_updated = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
89 |
+
for dens in range(additional_pixels):
|
90 |
+
for i in range(1, image_size - 1):
|
91 |
+
for j in range(1, image_size - 1):
|
92 |
+
if A[i - 1][j] + A[i + 1][j] + A[i][j - 1] + A[i][j + 1] > 0:
|
93 |
+
A_updated[i][j] = 1
|
94 |
+
A = update_array(A, A_updated, image_size)
|
95 |
+
return A
|
96 |
+
|
97 |
+
|
98 |
+
def basic_box(additional_pixels, density, image_size):
|
99 |
+
A = basic_box_array(image_size) # Creates the outside edges of the box
|
100 |
+
# Increase the thickness of each part of the box
|
101 |
+
A = add_pixels(A, additional_pixels, image_size)
|
102 |
+
return A * density
|
103 |
+
|
104 |
+
|
105 |
+
def horizontal_vertical_box_split(additional_pixels, density, image_size):
|
106 |
+
A = basic_box_array(image_size) # Creates the outside edges of the box
|
107 |
+
# Place pixels across the horizontal and vertical axes to split the box
|
108 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
109 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
110 |
+
# Increase the thickness of each part of the box
|
111 |
+
A = add_pixels(A, additional_pixels, image_size)
|
112 |
+
return A * density
|
113 |
+
|
114 |
+
|
115 |
+
def diagonal_box_split(additional_pixels, density, image_size):
|
116 |
+
A = basic_box_array(image_size) # Creates the outside edges of the box
|
117 |
+
|
118 |
+
# Add pixels along the diagonals of the box
|
119 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
120 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
121 |
+
|
122 |
+
# Adds pixels to the thickness of each component of the box
|
123 |
+
# Increase the thickness of each part of the box
|
124 |
+
A = add_pixels(A, additional_pixels, image_size)
|
125 |
+
return A * density
|
126 |
+
|
127 |
+
|
128 |
+
def back_slash_box(additional_pixels, density, image_size):
|
129 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
130 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
131 |
+
A = add_pixels(A, additional_pixels, image_size)
|
132 |
+
return A * density
|
133 |
+
|
134 |
+
|
135 |
+
def forward_slash_box(additional_pixels, density, image_size):
|
136 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
137 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
138 |
+
A = add_pixels(A, additional_pixels, image_size)
|
139 |
+
return A * density
|
140 |
+
|
141 |
+
|
142 |
+
def hot_dog_box(additional_pixels, density, image_size):
|
143 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
144 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
145 |
+
A = add_pixels(A, additional_pixels, image_size)
|
146 |
+
return A * density
|
147 |
+
|
148 |
+
|
149 |
+
def hamburger_box(additional_pixels, density, image_size):
|
150 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
151 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
152 |
+
A = add_pixels(A, additional_pixels, image_size)
|
153 |
+
return A * density
|
154 |
+
|
155 |
+
|
156 |
+
def x_plus_box(additional_pixels, density, image_size):
|
157 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
158 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
159 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
160 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
161 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
162 |
+
A = add_pixels(A, additional_pixels, image_size)
|
163 |
+
return A * density
|
164 |
+
|
165 |
+
|
166 |
+
def forward_slash_plus_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 = update_array(A, hamburger_array(image_size), image_size)
|
170 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
171 |
+
# A = update_array(A, back_slash_array(image_size), image_size)
|
172 |
+
A = add_pixels(A, additional_pixels, image_size)
|
173 |
+
return A * density
|
174 |
+
|
175 |
+
|
176 |
+
def back_slash_plus_box(additional_pixels, density, image_size):
|
177 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
178 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
179 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
180 |
+
# A = update_array(A, forward_slash_array(image_size), image_size)
|
181 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
182 |
+
A = add_pixels(A, additional_pixels, image_size)
|
183 |
+
return A * density
|
184 |
+
|
185 |
+
|
186 |
+
def x_hot_dog_box(additional_pixels, density, image_size):
|
187 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
188 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
189 |
+
# A = update_array(A, hamburger_array(image_size), image_size)
|
190 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
191 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
192 |
+
A = add_pixels(A, additional_pixels, image_size)
|
193 |
+
return A * density
|
194 |
+
|
195 |
+
|
196 |
+
def x_hamburger_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 |
+
|
206 |
+
def center_box(additional_pixels, density, image_size):
|
207 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
208 |
+
A = update_array(A, center_array(image_size), image_size)
|
209 |
+
A = add_pixels(A, additional_pixels, image_size)
|
210 |
+
return A * density
|
211 |
+
|
212 |
+
|
213 |
+
# import tensorflow as tf
|
214 |
+
#
|
215 |
+
# sess = tf.compat.v1.Session()
|
216 |
+
#
|
217 |
+
# from keras import backend as K
|
218 |
+
#
|
219 |
+
# K.set_session(sess)
|
220 |
+
|
221 |
+
|
222 |
+
|
223 |
+
endpoint_options = (
|
224 |
+
"basic_box", "diagonal_box_split", "horizontal_vertical_box_split", "back_slash_box", "forward_slash_box",
|
225 |
+
"back_slash_plus_box", "forward_slash_plus_box", "hot_dog_box", "hamburger_box", "x_hamburger_box",
|
226 |
+
"x_hot_dog_box", "x_plus_box")
|
227 |
+
density_options = ["{:.2f}".format(x) for x in numpy.linspace(0.1, 1, 10)]
|
228 |
+
thickness_options = [str(int(x)) for x in numpy.linspace(0, 10, 11)]
|
229 |
+
interpolation_options = [str(int(x)) for x in [3, 5, 10, 20]]
|
230 |
+
|
231 |
+
|
232 |
+
def generate_unit_cell(t, d, th):
|
233 |
+
return globals()[t](int(th), float(d), 28)
|
234 |
+
|
235 |
+
|
236 |
+
def interpolate(t1, t2, d1, d2, th1, th2, steps):
|
237 |
+
# Load the decoder model
|
238 |
+
decoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-decoder")
|
239 |
+
# decoder_model_boxes = tensorflow.keras.models.load_model('data/decoder_model_boxes', compile=False)
|
240 |
+
|
241 |
+
# # import the encoder model architecture
|
242 |
+
# json_file_loaded = open('data/model.json', 'r')
|
243 |
+
# loaded_model_json = json_file_loaded.read()
|
244 |
+
|
245 |
+
# load model using the saved json file
|
246 |
+
# encoder_model_boxes = tensorflow.keras.models.model_from_json(loaded_model_json)
|
247 |
+
|
248 |
+
# load weights into newly loaded_model
|
249 |
+
# encoder_model_boxes.load_weights('data/model_tf')
|
250 |
+
encoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-encoder")
|
251 |
+
|
252 |
+
num_internal = int(steps)
|
253 |
+
number_1 = generate_unit_cell(t1, d1, th1)
|
254 |
+
number_2 = generate_unit_cell(t2, d2, th2)
|
255 |
+
|
256 |
+
# resize the array to match the prediction size requirement
|
257 |
+
number_1_expand = numpy.expand_dims(numpy.expand_dims(number_1, axis=2), axis=0)
|
258 |
+
number_2_expand = numpy.expand_dims(numpy.expand_dims(number_2, axis=2), axis=0)
|
259 |
+
|
260 |
+
# Determine the latent point that will represent our desired number
|
261 |
+
latent_point_1 = encoder_model_boxes.predict(number_1_expand)[0]
|
262 |
+
latent_point_2 = encoder_model_boxes.predict(number_2_expand)[0]
|
263 |
+
|
264 |
+
latent_dimensionality = len(latent_point_1) # define the dimensionality of the latent space
|
265 |
+
num_interp = num_internal # the number of images to be pictured
|
266 |
+
latent_matrix = [] # This will contain the latent points of the interpolation
|
267 |
+
for column in range(latent_dimensionality):
|
268 |
+
new_column = numpy.linspace(latent_point_1[column], latent_point_2[column], num_interp)
|
269 |
+
latent_matrix.append(new_column)
|
270 |
+
latent_matrix = numpy.array(latent_matrix).T # Transposes the matrix so that each row can be easily indexed
|
271 |
+
|
272 |
+
# plot_rows = 2
|
273 |
+
# plot_columns = num_interp + 2
|
274 |
+
|
275 |
+
predicted_interps = [number_1_expand[0, :, :, 0]]
|
276 |
+
|
277 |
+
for latent_point in range(2, num_interp + 2): # cycles the latent points through the decoder model to create images
|
278 |
+
generated_image = decoder_model_boxes.predict(numpy.array([latent_matrix[latent_point - 2]]))[
|
279 |
+
0] # generates an interpolated image based on the latent point
|
280 |
+
predicted_interps.append(generated_image[:, :, -1])
|
281 |
+
|
282 |
+
predicted_interps.append(number_2_expand[0, :, :, 0])
|
283 |
+
|
284 |
+
transition_region = predicted_interps[0]
|
285 |
+
for i in range(len(predicted_interps) - 1):
|
286 |
+
transition_region = numpy.hstack((transition_region, predicted_interps[i + 1]))
|
287 |
+
|
288 |
+
return numpy.round(transition_region)
|
289 |
+
|
290 |
+
|
291 |
+
t1 = gradio.Dropdown(endpoint_options, label="Type 1", value="hamburger_box")
|
292 |
+
d1 = gradio.Dropdown(density_options, label="Density 1", value="1.00")
|
293 |
+
th1 = gradio.Dropdown(thickness_options, label="Thickness 1", value="2")
|
294 |
+
|
295 |
+
t2 = gradio.Dropdown(endpoint_options, label="Type 2", value="hot_dog_box")
|
296 |
+
d2 = gradio.Dropdown(density_options, label="Density 2", value="1.00")
|
297 |
+
th2 = gradio.Dropdown(thickness_options, label="Thickness 2", value="2")
|
298 |
+
|
299 |
+
steps = gradio.Dropdown(interpolation_options, label="Interpolation Length", value=random.choice(interpolation_options))
|
300 |
+
img = gradio.Image(label="Transition")
|
301 |
+
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])
|
302 |
+
|
303 |
+
lattice_interpolation_inetrface = gradio.Interface(
|
304 |
+
fn = interpolate, inputs=[t1, t2, d1, d2, th1, th2, steps], outputs=[img]
|
305 |
+
)
|
306 |
+
|
307 |
+
# Loadin the model
|
308 |
+
model1 = huggingface_hub.from_pretrained_keras("cmudrc/microstructure-colorization")
|
309 |
+
|
310 |
+
# Get the color map by name:
|
311 |
+
cm = matplotlib.pyplot.get_cmap('RdBu')
|
312 |
+
|
313 |
+
#simple image scaling to (nR x nC) size
|
314 |
+
def scale(im, nR, nC):
|
315 |
+
nR0 = len(im) # source number of rows
|
316 |
+
nC0 = len(im[0]) # source number of columns
|
317 |
+
return numpy.array([[ im[int(nR0 * r / nR)][int(nC0 * c / nC)]
|
318 |
+
for c in range(nC)] for r in range(nR)])
|
319 |
+
|
320 |
+
|
321 |
+
# Prediction function
|
322 |
+
def predict(mask):
|
323 |
+
print(mask)
|
324 |
+
scaled_mask = numpy.ones((101, 636)) if mask is None else numpy.round(scale(mask, 101, 636)/255.0)
|
325 |
+
print(scaled_mask)
|
326 |
+
X = scaled_mask[numpy.newaxis, :, :, numpy.newaxis]
|
327 |
+
print(X.shape)
|
328 |
+
v = model1.predict(X)
|
329 |
+
print(v)
|
330 |
+
measure = max(v.max(), -v.min())
|
331 |
+
output = (v / measure)
|
332 |
+
legend = "<h2>Strain</h2><table style=\"width:100%\"><tr>"
|
333 |
+
for i in range(11):
|
334 |
+
color = cm(i/10.0)[:3]
|
335 |
+
value = -measure + i*2*measure/10
|
336 |
+
print(sum(list(color)))
|
337 |
+
hex = matplotlib.colors.to_hex(list(color))
|
338 |
+
text_color = "black" if sum(list(color)) > 2.0 else "white"
|
339 |
+
legend = legend + f"<td style=\"background-color: {hex}; color: {text_color}\">{value:+.2e}</td>"
|
340 |
+
legend = legend + "</tr></table>"
|
341 |
+
return cm((numpy.multiply(output[0, :, :, 0], scaled_mask)+1.0)/2.0), cm((numpy.multiply(output[0, :, :, 1], scaled_mask)+1.0)/2.0), cm((numpy.multiply(output[0, :, :, 2], scaled_mask)+1.0)/2.0), legend
|
342 |
+
|
343 |
+
mask = gradio.Image(image_mode="L", source="canvas", label="microstructure")
|
344 |
+
|
345 |
+
exx = gradio.Image(label="ε-xx")
|
346 |
+
eyy = gradio.Image(label="ε-yy")
|
347 |
+
exy = gradio.Image(label="ε-xy")
|
348 |
+
legend = gradio.HTML(label="", value="")
|
349 |
+
|
350 |
+
microstructure_interface = gradio.Interface(
|
351 |
+
fn=predict,
|
352 |
+
inputs=[mask],
|
353 |
+
outputs=[exx, eyy, exy, legend]
|
354 |
+
)
|
355 |
+
|
356 |
+
gradio.Series(
|
357 |
+
lattice_interpolation_inetrface,
|
358 |
+
microstructure_interface
|
359 |
+
).launch(debug=True)
|