Update app.py
Browse files
app.py
CHANGED
|
@@ -201,6 +201,12 @@ def load_image(img_path, preprocess=True, height=320, width=320):
|
|
| 201 |
x = np.expand_dims(x, axis=0)
|
| 202 |
return x
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
def grad_cam(input_model, img_array, cls, layer_name):
|
| 205 |
grad_model = tf.keras.models.Model(
|
| 206 |
[input_model.inputs],
|
|
@@ -241,7 +247,17 @@ def compute_gradcam(model_gradcam, img_path, layer_name='bn'):
|
|
| 241 |
# model_gradcam.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
|
| 242 |
# loss='sparse_categorical_crossentropy')
|
| 243 |
# model.load_weights('./pretrained_model.h5')
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
preprocessed_input = load_image(img_path)
|
| 247 |
predictions = model_gradcam.predict(preprocessed_input)
|
|
@@ -493,7 +509,7 @@ if uploaded_file is not None:
|
|
| 493 |
with col3:
|
| 494 |
if st.button('Generate Grad-CAM'):
|
| 495 |
st.write("Loading model...")
|
| 496 |
-
model_gradcam = keras.models.load_model('./
|
| 497 |
# Compute and show Grad-CAM
|
| 498 |
st.write("Generating Grad-CAM visualizations")
|
| 499 |
try:
|
|
|
|
| 201 |
x = np.expand_dims(x, axis=0)
|
| 202 |
return x
|
| 203 |
|
| 204 |
+
def rename_layers(model):
|
| 205 |
+
for layer in model.layers:
|
| 206 |
+
if '/' in layer.name:
|
| 207 |
+
layer._name = layer.name.replace('/', '_')
|
| 208 |
+
return model
|
| 209 |
+
|
| 210 |
def grad_cam(input_model, img_array, cls, layer_name):
|
| 211 |
grad_model = tf.keras.models.Model(
|
| 212 |
[input_model.inputs],
|
|
|
|
| 247 |
# model_gradcam.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
|
| 248 |
# loss='sparse_categorical_crossentropy')
|
| 249 |
# model.load_weights('./pretrained_model.h5')
|
| 250 |
+
# Load the original model
|
| 251 |
+
original_model = keras.models.load_model('./gradcam_model.h5')
|
| 252 |
+
|
| 253 |
+
# Rename the layers
|
| 254 |
+
modified_model = rename_layers(original_model)
|
| 255 |
+
|
| 256 |
+
# Save the modified model
|
| 257 |
+
modified_model.save('./modified_gradcam_model.h5')
|
| 258 |
+
|
| 259 |
+
# Now use this modified model in your application
|
| 260 |
+
model_gradcam = keras.models.load_model('./modified_gradcam_model.h5')
|
| 261 |
|
| 262 |
preprocessed_input = load_image(img_path)
|
| 263 |
predictions = model_gradcam.predict(preprocessed_input)
|
|
|
|
| 509 |
with col3:
|
| 510 |
if st.button('Generate Grad-CAM'):
|
| 511 |
st.write("Loading model...")
|
| 512 |
+
model_gradcam = keras.models.load_model('./modified_gradcam_model.h5')
|
| 513 |
# Compute and show Grad-CAM
|
| 514 |
st.write("Generating Grad-CAM visualizations")
|
| 515 |
try:
|