Kalbe-x-Bangkit
commited on
Commit
•
91d9ac6
1
Parent(s):
494bb13
Update app.py
Browse files
app.py
CHANGED
@@ -36,7 +36,7 @@ enhancement_type = st.sidebar.selectbox(
|
|
36 |
H = 224
|
37 |
W = 224
|
38 |
|
39 |
-
@st.
|
40 |
def load_model():
|
41 |
model = tf.keras.models.load_model("model-detection.h5", compile=False)
|
42 |
model.compile(
|
@@ -76,7 +76,7 @@ model = load_model()
|
|
76 |
# st.image(image, caption='Uploaded Image.', use_column_width=True)
|
77 |
|
78 |
|
79 |
-
@st.
|
80 |
def load_gradcam_model():
|
81 |
model = keras.models.load_model('./model_renamed.h5', compile=False)
|
82 |
model.compile(
|
@@ -209,8 +209,9 @@ def get_mean_std_per_batch(image_path, df, H=320, W=320):
|
|
209 |
std = np.std(sample_data[0])
|
210 |
return mean, std
|
211 |
|
212 |
-
def load_image(img_path, preprocess=True, height=320, width=320):
|
213 |
-
|
|
|
214 |
x = keras.utils.load_img(img_path, target_size=(height, width))
|
215 |
x = keras.utils.img_to_array(x)
|
216 |
if preprocess:
|
@@ -250,7 +251,7 @@ def grad_cam(input_model, img_array, cls, layer_name):
|
|
250 |
|
251 |
|
252 |
# Compute Grad-CAM
|
253 |
-
def compute_gradcam(model_gradcam, img_path, layer_name='bn'):
|
254 |
# base_model = keras.applications.DenseNet121(weights = './densenet.hdf5', include_top = False)
|
255 |
# x = base_model.output
|
256 |
# x = keras.layers.GlobalAveragePooling2D()(x)
|
@@ -263,8 +264,9 @@ def compute_gradcam(model_gradcam, img_path, layer_name='bn'):
|
|
263 |
|
264 |
# Now use this modified model in your application
|
265 |
model_gradcam = load_gradcam_model()
|
|
|
266 |
|
267 |
-
preprocessed_input = load_image(img_path)
|
268 |
predictions = model_gradcam.predict(preprocessed_input)
|
269 |
|
270 |
original_image = load_image(img_path, preprocess=False)
|
@@ -513,11 +515,12 @@ if uploaded_file is not None:
|
|
513 |
|
514 |
with col3:
|
515 |
if st.button('Generate Grad-CAM'):
|
516 |
-
st.
|
517 |
-
|
518 |
# Compute and show Grad-CAM
|
519 |
st.write("Generating Grad-CAM visualizations")
|
520 |
try:
|
521 |
-
|
|
|
522 |
except Exception as e:
|
523 |
st.error(f"Error generating Grad-CAM: {e}")
|
|
|
36 |
H = 224
|
37 |
W = 224
|
38 |
|
39 |
+
@st.cache(allow_output_mutation=True)
|
40 |
def load_model():
|
41 |
model = tf.keras.models.load_model("model-detection.h5", compile=False)
|
42 |
model.compile(
|
|
|
76 |
# st.image(image, caption='Uploaded Image.', use_column_width=True)
|
77 |
|
78 |
|
79 |
+
@st.cache(allow_output_mutation=True)
|
80 |
def load_gradcam_model():
|
81 |
model = keras.models.load_model('./model_renamed.h5', compile=False)
|
82 |
model.compile(
|
|
|
209 |
std = np.std(sample_data[0])
|
210 |
return mean, std
|
211 |
|
212 |
+
def load_image(img_path, df, preprocess=True, height=320, width=320):
|
213 |
+
df_xray = ('./xray_dataset.csv')
|
214 |
+
mean, std = get_mean_std_per_batch(img_path, df_xray, height, width)
|
215 |
x = keras.utils.load_img(img_path, target_size=(height, width))
|
216 |
x = keras.utils.img_to_array(x)
|
217 |
if preprocess:
|
|
|
251 |
|
252 |
|
253 |
# Compute Grad-CAM
|
254 |
+
def compute_gradcam(model_gradcam, img_path, df, layer_name='bn'):
|
255 |
# base_model = keras.applications.DenseNet121(weights = './densenet.hdf5', include_top = False)
|
256 |
# x = base_model.output
|
257 |
# x = keras.layers.GlobalAveragePooling2D()(x)
|
|
|
264 |
|
265 |
# Now use this modified model in your application
|
266 |
model_gradcam = load_gradcam_model()
|
267 |
+
df_xray = ('./xray_dataset.csv')
|
268 |
|
269 |
+
preprocessed_input = load_image(img_path,df)
|
270 |
predictions = model_gradcam.predict(preprocessed_input)
|
271 |
|
272 |
original_image = load_image(img_path, preprocess=False)
|
|
|
515 |
|
516 |
with col3:
|
517 |
if st.button('Generate Grad-CAM'):
|
518 |
+
with st.spinner("Loading Model...."):
|
519 |
+
model=load_gradcam_model()
|
520 |
# Compute and show Grad-CAM
|
521 |
st.write("Generating Grad-CAM visualizations")
|
522 |
try:
|
523 |
+
df_xray = ('./xray_dataset.csv')
|
524 |
+
compute_gradcam(model_gradcam, uploaded_file, df_xray)
|
525 |
except Exception as e:
|
526 |
st.error(f"Error generating Grad-CAM: {e}")
|