import random import gradio import numpy import tensorflow import math from tensorflow.python.framework.ops import disable_eager_execution import huggingface_hub # for loading model # Because important disable_eager_execution() def basic_box_array(image_size): A = numpy.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 = numpy.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 = numpy.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 = numpy.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 = numpy.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 = numpy.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 = numpy.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) endpoint_options = ( "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") 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 generate_unit_cell(t, d, th): return globals()[t](int(th), float(d), 28) def interpolate(t1, t2, d1, d2, th1, th2, steps): # Load the decoder model decoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-decoder") # 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') encoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-encoder") num_internal = int(steps) number_1 = generate_unit_cell(t1, d1, th1) number_2 = generate_unit_cell(t2, d2, th2) # resize the array to match the prediction size requirement number_1_expand = numpy.expand_dims(numpy.expand_dims(number_1, axis=2), axis=0) number_2_expand = numpy.expand_dims(numpy.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 = numpy.linspace(latent_point_1[column], latent_point_2[column], num_interp) latent_matrix.append(new_column) latent_matrix = numpy.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(numpy.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 with gradio.Blocks() as demo: with gradio.Accordion("✨ Read about the ML model here! ✨", open=False): with gradio.Row(): with gradio.Column(): gradio.Markdown("# A Data-Driven Approach for Multi-Lattice Transitions") gradio.HTML("Martha Baldwin, Carnegie Mellon University
Nicholas A. Meisel, Penn State
Christopher McComb, Carnegie Mellon University") gradio.Markdown("_Abstract_: Additive manufacturing is advantageous for producing lightweight components while maintaining function and form. This ability has been bolstered by the introduction of unit lattice cells and the gradation of those cells. In cases where loading varies throughout a part, it may be necessary to use multiple lattice cell types, also known as multi-lattice structures. In such structures, abrupt transitions between geometries may cause stress concentrations, making the boundary a primary failure point; thus, transition regions should be created between each lattice cell type. Although computational approaches have been proposed, smooth transition regions are still difficult to intuit and design, especially between lattices of drastically different geometries. This work demonstrates and assesses a method for using variational autoencoders to automate the creation of transitional lattice cells. In particular, the work focuses on identifying the relationships that exist within the latent space produced by the variational autoencoder. Through computational experimentation, it was found that the smoothness of transition regions was higher when the endpoints were located closer together in the latent space.") with gradio.Column(): download = gradio.HTML("") gradio.Markdown("Lattices are used in 3D-printing to reduce weight, and its usually good to use more than one type of lattice. This demo can create smooth transitions between different lattices to improve the strength of the part. To use the demo, set the characteristics of the lattice unit cells you want to use as the endpoints, select the length of the transitions, and then hit `Interpolate!`") with gradio.Row(): with gradio.Column(min_width=200): t1 = gradio.Dropdown(endpoint_options, label="Type 1", value="hamburger_box") d1 = gradio.Dropdown(density_options, label="Density 1", value="1.00") th1 = gradio.Dropdown(thickness_options, label="Thickness 1", value="2") with gradio.Column(min_width=200): img1 = gradio.Image(label="Endpoint 1", value=generate_unit_cell("hamburger_box", "1.00", "2")) with gradio.Column(min_width=200): t2 = gradio.Dropdown(endpoint_options, label="Type 2", value="hot_dog_box") d2 = gradio.Dropdown(density_options, label="Density 2", value="1.00") th2 = gradio.Dropdown(thickness_options, label="Thickness 2", value="2") with gradio.Column(min_width=200): img2 = gradio.Image(label="Endpoint 2", value=generate_unit_cell("hot_dog_box", "1.00", "2")) lattice_inputs_1 = [t1, d1, th1] [x.change(fn=generate_unit_cell, inputs=lattice_inputs_1, outputs=[img1], show_progress=False) for x in lattice_inputs_1] lattice_inputs_2 = [t2, d2, th2] [x.change(fn=generate_unit_cell, inputs=lattice_inputs_2, outputs=[img2], show_progress=False) for x in lattice_inputs_2] steps = gradio.Dropdown(interpolation_options, label="Interpolation Length", value=random.choice(interpolation_options)) btn = gradio.Button("Interpolate!", variant="primary") 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()