rohan-16 commited on
Commit
a7aa4da
·
verified ·
1 Parent(s): 164c665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -56
app.py CHANGED
@@ -310,62 +310,10 @@ elif selected_menu == "Explainable AI":
310
  # Define the Streamlit app
311
  st.title('SHAP Explanation for Image Classification')
312
 
313
- # Define the base directory containing the subfolders
314
- dataset_folder = 'Validation Set'
315
-
316
- images = []
317
-
318
- # Iterate over files in the folder
319
- for image_file in os.listdir(dataset_folder):
320
- image_path = os.path.join(dataset_folder, image_file)
321
- if os.path.isfile(image_path):
322
- image = cv2.imread(image_path)
323
- if image is not None:
324
- image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
325
- image = cv2.resize(image, (48, 48))
326
- images.append(image)
327
-
328
- # Convert list to numpy array
329
- images = np.array(images)
330
-
331
- # Normalize pixel values and expand dimension for grayscale
332
- images = images / 255.0
333
- images = np.expand_dims(images, -1)
334
-
335
- val_images = images
336
-
337
- # Function to generate SHAP explanations
338
- def generate_shap_explanations(model, images, index):
339
- def f(x):
340
- tmp = x.copy()
341
- return model(tmp)
342
-
343
- masker_blur = shap.maskers.Image("blur(32,32)", images[0].shape)
344
-
345
- # Resize images to the expected shape
346
- resized_images = np.array([cv2.resize(img, (48, 48)) for img in images])
347
- resized_images = np.expand_dims(resized_images, axis=-1) # Add channel dimension
348
-
349
- explainer = shap.Explainer(f, masker_blur, output_names=list(range(3)))
350
- shap_values = explainer(resized_images[index:index+1], max_evals=5000, batch_size=50) # Use slicing to ensure single image
351
- return shap_values
352
-
353
- # Choose a valid index within the bounds of your dataset
354
- index = st.slider('Choose an index', 0, len(val_images)-1, 0)
355
-
356
- # Generate SHAP explanations when a button is clicked
357
- if st.button('Generate SHAP Explanations'):
358
- # Generate SHAP explanations
359
- shap_values_ = generate_shap_explanations(model, val_images, index)
360
-
361
- # Display the SHAP values
362
- if shap_values_ is not None:
363
- plt.figure(figsize=(10, 5))
364
- shap.image_plot(shap_values_[0], val_images[index], show=False)
365
- st.set_option('deprecation.showPyplotGlobalUse', False)
366
- st.pyplot()
367
- else:
368
- st.write("Error: SHAP values are None")
369
 
370
  elif selected_menu == "MLFlow":
371
  st.title("MLFlow")
 
310
  # Define the Streamlit app
311
  st.title('SHAP Explanation for Image Classification')
312
 
313
+ st.write(" ")
314
+ st.write(" ")
315
+
316
+ st.image("Screenshot 2024-05-03 at 3.41.00 PM.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
  elif selected_menu == "MLFlow":
319
  st.title("MLFlow")