Spaces:
Sleeping
Sleeping
Commit
·
488f10b
1
Parent(s):
42091c4
Updating to get the model imports to work
Browse files- app.py +197 -53
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -7,78 +7,210 @@ from transformers import TFAutoModel
|
|
| 7 |
# Needed for importing torch to use in the transformers model
|
| 8 |
import torch
|
| 9 |
import tensorflow
|
|
|
|
| 10 |
# HELLO HUGGING FACE
|
| 11 |
|
| 12 |
|
| 13 |
-
def basic_box_array(image_size
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
A = np.ones((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 21 |
-
A[1:-1, 1:-1] = 0 # replaces all internal rows/columns with 0's
|
| 22 |
-
A = add_thickness(A, thickness)
|
| 23 |
return A
|
| 24 |
|
| 25 |
|
| 26 |
-
def back_slash_array(image_size
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
"""
|
| 33 |
-
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 34 |
-
np.fill_diagonal(A, 1) # fills the diagonal with 1 values
|
| 35 |
-
A = add_thickness(A, thickness)
|
| 36 |
return A
|
| 37 |
|
| 38 |
|
| 39 |
-
def forward_slash_array(image_size
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
A = np.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 47 |
-
np.fill_diagonal(np.fliplr(A), 1) # Flips the array to then fill the diagonal the opposite direction
|
| 48 |
-
A = add_thickness(A, thickness)
|
| 49 |
return A
|
| 50 |
|
| 51 |
|
| 52 |
-
def hot_dog_array(image_size
|
| 53 |
-
"""
|
| 54 |
-
:param image_size: [int] - the size of the image that will be produced
|
| 55 |
-
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
| 56 |
-
:return: [ndarray] - the output is a unit cell with outer pixel activated from the vertical center based on the
|
| 57 |
-
desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
| 58 |
-
"""
|
| 59 |
# Places pixels down the vertical axis to split the box
|
| 60 |
-
A =
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
return A
|
| 65 |
|
| 66 |
|
| 67 |
-
def hamburger_array(image_size
|
| 68 |
-
"""
|
| 69 |
-
:param image_size: [int] - the size of the image that will be produced
|
| 70 |
-
:param thickness: [int] - the number of pixels to be activated surrounding the base shape
|
| 71 |
-
:return: [ndarray] - the output is a unit cell with outer pixel activated from the horizontal center based on the
|
| 72 |
-
desired thickness. The activated pixels are 1 (white) and the deactivated pixels are 0 (black)
|
| 73 |
-
"""
|
| 74 |
# Places pixels across the horizontal axis to split the box
|
| 75 |
-
A =
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 79 |
return A
|
| 80 |
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
########################################################################################################################
|
| 83 |
# The function to add thickness to struts in an array
|
| 84 |
def add_thickness(array_original, thickness: int) -> np.ndarray:
|
|
@@ -135,6 +267,18 @@ thickness_2 = st.selectbox("Thickness 2", thickness_options)
|
|
| 135 |
interp_length = st.selectbox("Interpolation Length", interpolation_options)
|
| 136 |
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
# Load the models from existing huggingface model
|
| 139 |
# Load the encoder model
|
| 140 |
# encoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-encoder")
|
|
@@ -142,5 +286,5 @@ encoder_model = TFAutoModel.from_pretrained("cmudrc/2d-lattice-encoder")
|
|
| 142 |
# Load the decoder model
|
| 143 |
# decoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-decoder")
|
| 144 |
decoder_model = TFAutoModel.from_pretrained("cmudrc/2d-lattice-decoder")
|
| 145 |
-
|
| 146 |
|
|
|
|
| 7 |
# Needed for importing torch to use in the transformers model
|
| 8 |
import torch
|
| 9 |
import tensorflow
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
# HELLO HUGGING FACE
|
| 12 |
|
| 13 |
|
| 14 |
+
def basic_box_array(image_size):
|
| 15 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 16 |
+
# Creates the outside edges of the box
|
| 17 |
+
for i in range(image_size):
|
| 18 |
+
for j in range(image_size):
|
| 19 |
+
if i == 0 or j == 0 or i == image_size - 1 or j == image_size - 1:
|
| 20 |
+
A[i][j] = 1
|
|
|
|
|
|
|
|
|
|
| 21 |
return A
|
| 22 |
|
| 23 |
|
| 24 |
+
def back_slash_array(image_size):
|
| 25 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 26 |
+
for i in range(image_size):
|
| 27 |
+
for j in range(image_size):
|
| 28 |
+
if i == j:
|
| 29 |
+
A[i][j] = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return A
|
| 31 |
|
| 32 |
|
| 33 |
+
def forward_slash_array(image_size):
|
| 34 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 35 |
+
for i in range(image_size):
|
| 36 |
+
for j in range(image_size):
|
| 37 |
+
if i == (image_size - 1) - j:
|
| 38 |
+
A[i][j] = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return A
|
| 40 |
|
| 41 |
|
| 42 |
+
def hot_dog_array(image_size):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# Places pixels down the vertical axis to split the box
|
| 44 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 45 |
+
for i in range(image_size):
|
| 46 |
+
for j in range(image_size):
|
| 47 |
+
if j == math.floor((image_size - 1) / 2) or j == math.ceil((image_size - 1) / 2):
|
| 48 |
+
A[i][j] = 1
|
| 49 |
return A
|
| 50 |
|
| 51 |
|
| 52 |
+
def hamburger_array(image_size):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Places pixels across the horizontal axis to split the box
|
| 54 |
+
A = numpy.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 == math.floor((image_size - 1) / 2) or i == math.ceil((image_size - 1) / 2):
|
| 58 |
+
A[i][j] = 1
|
| 59 |
return A
|
| 60 |
|
| 61 |
|
| 62 |
+
def center_array(image_size):
|
| 63 |
+
A = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 64 |
+
for i in range(image_size):
|
| 65 |
+
for j in range(image_size):
|
| 66 |
+
if i == math.floor((image_size - 1) / 2) and j == math.ceil((image_size - 1) / 2):
|
| 67 |
+
A[i][j] = 1
|
| 68 |
+
if i == math.floor((image_size - 1) / 2) and j == math.floor((image_size - 1) / 2):
|
| 69 |
+
A[i][j] = 1
|
| 70 |
+
if j == math.ceil((image_size - 1) / 2) and i == math.ceil((image_size - 1) / 2):
|
| 71 |
+
A[i][j] = 1
|
| 72 |
+
if j == math.floor((image_size - 1) / 2) and i == math.ceil((image_size - 1) / 2):
|
| 73 |
+
A[i][j] = 1
|
| 74 |
+
return A
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def update_array(array_original, array_new, image_size):
|
| 78 |
+
A = array_original
|
| 79 |
+
for i in range(image_size):
|
| 80 |
+
for j in range(image_size):
|
| 81 |
+
if array_new[i][j] == 1:
|
| 82 |
+
A[i][j] = 1
|
| 83 |
+
return A
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def add_pixels(array_original, additional_pixels, image_size):
|
| 87 |
+
# Adds pixels to the thickness of each component of the box
|
| 88 |
+
A = array_original
|
| 89 |
+
A_updated = numpy.zeros((int(image_size), int(image_size))) # Initializes A matrix with 0 values
|
| 90 |
+
for dens in range(additional_pixels):
|
| 91 |
+
for i in range(1, image_size - 1):
|
| 92 |
+
for j in range(1, image_size - 1):
|
| 93 |
+
if A[i - 1][j] + A[i + 1][j] + A[i][j - 1] + A[i][j + 1] > 0:
|
| 94 |
+
A_updated[i][j] = 1
|
| 95 |
+
A = update_array(A, A_updated, image_size)
|
| 96 |
+
return A
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def basic_box(additional_pixels, density, image_size):
|
| 100 |
+
A = basic_box_array(image_size) # Creates the outside edges of the box
|
| 101 |
+
# Increase the thickness of each part of the box
|
| 102 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 103 |
+
return A * density
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def horizontal_vertical_box_split(additional_pixels, density, image_size):
|
| 107 |
+
A = basic_box_array(image_size) # Creates the outside edges of the box
|
| 108 |
+
# Place pixels across the horizontal and vertical axes to split the box
|
| 109 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
| 110 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
| 111 |
+
# Increase the thickness of each part of the box
|
| 112 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 113 |
+
return A * density
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def diagonal_box_split(additional_pixels, density, image_size):
|
| 117 |
+
A = basic_box_array(image_size) # Creates the outside edges of the box
|
| 118 |
+
|
| 119 |
+
# Add pixels along the diagonals of the box
|
| 120 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
| 121 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
| 122 |
+
|
| 123 |
+
# Adds pixels to the thickness of each component of the box
|
| 124 |
+
# Increase the thickness of each part of the box
|
| 125 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 126 |
+
return A * density
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def back_slash_box(additional_pixels, density, image_size):
|
| 130 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 131 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
| 132 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 133 |
+
return A * density
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
def forward_slash_box(additional_pixels, density, image_size):
|
| 137 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 138 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
| 139 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 140 |
+
return A * density
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def hot_dog_box(additional_pixels, density, image_size):
|
| 144 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 145 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
| 146 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 147 |
+
return A * density
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def hamburger_box(additional_pixels, density, image_size):
|
| 151 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 152 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
| 153 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 154 |
+
return A * density
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def x_plus_box(additional_pixels, density, image_size):
|
| 158 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 159 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
| 160 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
| 161 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
| 162 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
| 163 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 164 |
+
return A * density
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def forward_slash_plus_box(additional_pixels, density, image_size):
|
| 168 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 169 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
| 170 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
| 171 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
| 172 |
+
# A = update_array(A, back_slash_array(image_size), image_size)
|
| 173 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 174 |
+
return A * density
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def back_slash_plus_box(additional_pixels, density, image_size):
|
| 178 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 179 |
+
A = update_array(A, hot_dog_array(image_size), image_size)
|
| 180 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
| 181 |
+
# A = update_array(A, forward_slash_array(image_size), image_size)
|
| 182 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
| 183 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 184 |
+
return A * density
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def x_hot_dog_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 |
+
|
| 197 |
+
def x_hamburger_box(additional_pixels, density, image_size):
|
| 198 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 199 |
+
# A = update_array(A, hot_dog_array(image_size), image_size)
|
| 200 |
+
A = update_array(A, hamburger_array(image_size), image_size)
|
| 201 |
+
A = update_array(A, forward_slash_array(image_size), image_size)
|
| 202 |
+
A = update_array(A, back_slash_array(image_size), image_size)
|
| 203 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 204 |
+
return A * density
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
def center_box(additional_pixels, density, image_size):
|
| 208 |
+
A = basic_box_array(image_size) # Initializes A matrix with 0 values
|
| 209 |
+
A = update_array(A, center_array(image_size), image_size)
|
| 210 |
+
A = add_pixels(A, additional_pixels, image_size)
|
| 211 |
+
return A * density
|
| 212 |
+
|
| 213 |
+
|
| 214 |
########################################################################################################################
|
| 215 |
# The function to add thickness to struts in an array
|
| 216 |
def add_thickness(array_original, thickness: int) -> np.ndarray:
|
|
|
|
| 267 |
interp_length = st.selectbox("Interpolation Length", interpolation_options)
|
| 268 |
|
| 269 |
|
| 270 |
+
def generate_unit_cell(shape, density, thickness):
|
| 271 |
+
return globals()[shape](int(thickness), float(density), 28)
|
| 272 |
+
|
| 273 |
+
if st.button("Generate Endpoint Images"):
|
| 274 |
+
plt.figure(1)
|
| 275 |
+
st.header("Endpoints to be generated:")
|
| 276 |
+
plt.subplot(1, 2, 1), plt.imshow(generate_unit_cell(shape_1, density_1, thickness_1), cmap='gray', vmin=0, vmax=1)
|
| 277 |
+
plt.subplot(1, 2, 2), plt.imshow(generate_unit_cell(shape_2, density_2, thickness_2), cmap='gray', vmin=0, vmax=1)
|
| 278 |
+
plt.figure(1)
|
| 279 |
+
st.pyplot(plt.figure(1))
|
| 280 |
+
|
| 281 |
+
'''
|
| 282 |
# Load the models from existing huggingface model
|
| 283 |
# Load the encoder model
|
| 284 |
# encoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-encoder")
|
|
|
|
| 286 |
# Load the decoder model
|
| 287 |
# decoder_model_boxes = huggingface_hub.from_pretrained_keras("cmudrc/2d-lattice-decoder")
|
| 288 |
decoder_model = TFAutoModel.from_pretrained("cmudrc/2d-lattice-decoder")
|
| 289 |
+
'''
|
| 290 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
huggingface_hub==0.12.0
|
|
|
|
| 2 |
numpy==1.21.5
|
| 3 |
scipy==1.9.1
|
| 4 |
streamlit==1.18.1
|
|
|
|
| 1 |
huggingface_hub==0.12.0
|
| 2 |
+
matplotlib==3.5.2
|
| 3 |
numpy==1.21.5
|
| 4 |
scipy==1.9.1
|
| 5 |
streamlit==1.18.1
|