Kalbe-x-Bangkit commited on
Commit
8d5bc8b
1 Parent(s): 91d9ac6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -12
app.py CHANGED
@@ -36,7 +36,7 @@ enhancement_type = st.sidebar.selectbox(
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,7 +76,7 @@ model = load_model()
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(
@@ -198,7 +198,7 @@ def upload_folder_images(original_image_path, enhanced_image_path):
198
  upload_to_gcs(enhanced_dicom_bytes, folder_name + '/' + enhancement_name + '.dcm', content_type='application/dicom')
199
 
200
 
201
- def get_mean_std_per_batch(image_path, df, H=320, W=320):
202
  sample_data = []
203
  for idx, img in enumerate(df.sample(100)["Image Index"].values):
204
  # path = image_dir + img
@@ -209,9 +209,8 @@ 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, 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,7 +250,7 @@ def grad_cam(input_model, img_array, cls, layer_name):
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,9 +263,7 @@ def compute_gradcam(model_gradcam, img_path, df, layer_name='bn'):
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)
@@ -520,7 +517,6 @@ if uploaded_file is not None:
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}")
 
36
  H = 224
37
  W = 224
38
 
39
+ @st.cache_resource(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_resource(allow_output_mutation=True)
80
  def load_gradcam_model():
81
  model = keras.models.load_model('./model_renamed.h5', compile=False)
82
  model.compile(
 
198
  upload_to_gcs(enhanced_dicom_bytes, folder_name + '/' + enhancement_name + '.dcm', content_type='application/dicom')
199
 
200
 
201
+ def get_mean_std_per_batch(image_path, H=320, W=320):
202
  sample_data = []
203
  for idx, img in enumerate(df.sample(100)["Image Index"].values):
204
  # path = image_dir + img
 
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
+ mean, std = get_mean_std_per_batch(img_path, height, width)
 
214
  x = keras.utils.load_img(img_path, target_size=(height, width))
215
  x = keras.utils.img_to_array(x)
216
  if preprocess:
 
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
 
264
  # Now use this modified model in your application
265
  model_gradcam = load_gradcam_model()
266
+ preprocessed_input = load_image(img_path)
 
 
267
  predictions = model_gradcam.predict(preprocessed_input)
268
 
269
  original_image = load_image(img_path, preprocess=False)
 
517
  # Compute and show Grad-CAM
518
  st.write("Generating Grad-CAM visualizations")
519
  try:
520
+ compute_gradcam(model_gradcam, uploaded_file)
 
521
  except Exception as e:
522
  st.error(f"Error generating Grad-CAM: {e}")