stevafernandes commited on
Commit
3215d3a
·
verified ·
1 Parent(s): 0e469d5

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +20 -6
  2. app.py +60 -0
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,13 +1,27 @@
1
  ---
2
- title: Deven
3
- emoji: 📉
4
- colorFrom: pink
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 6.32.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: "Alzheimer's MRI Classification with CAM"
3
+ emoji: 🧠
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 6.32.0
8
+ python_version: "3.13"
9
  app_file: app.py
10
  pinned: false
11
  ---
12
 
13
+ # Alzheimer's MRI Classification with Class Activation Maps
14
+
15
+ A small convolutional network that classifies an axial brain MRI slice as
16
+ **Demented** or **Non_Demented**, trained on slices from the OASIS dataset.
17
+ Along with the prediction, the app shows a class activation map: the feature
18
+ maps of the last convolutional layer weighted by the output layer, highlighting
19
+ the regions that support the predicted class.
20
+
21
+ ## Files
22
+
23
+ - `app.py`: the Gradio app
24
+ - `requirements.txt`: Python dependencies
25
+ - `project_model.h5`: the trained model saved by `DSC599_Alzheimers_Binary_CAM_final.ipynb`
26
+
27
+ Research demo only. It is not a medical device and must not be used for diagnosis.
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from matplotlib import colormaps
4
+
5
+ # Same order as in training: label 0, then label 1
6
+ CLASSES = ["Demented", "Non_Demented"]
7
+ IMAGE_SIZE = (224, 224)
8
+
9
+ model = tf.keras.models.load_model("project_model.h5", compile=False)
10
+
11
+ # Split the model after the last convolutional layer: its feature maps,
12
+ # weighted by the weights of the output layer, give the class activation map
13
+ conv_layers = tf.keras.Sequential(model.layers[:-2])
14
+ output_layers = tf.keras.Sequential(model.layers[-2:])
15
+ output_weights = model.layers[-1].get_weights()[0]
16
+
17
+
18
+ def predict(path):
19
+ # Same decoding and resize as image_dataset_from_directory in training
20
+ # (the model rescales to 0-1 itself)
21
+ image = tf.io.decode_image(tf.io.read_file(path), channels=3, expand_animations=False)
22
+ image = tf.image.resize(image, IMAGE_SIZE)
23
+ features = conv_layers(image[None])
24
+
25
+ # The sigmoid output is the probability of label 1 (Non_Demented)
26
+ probability = float(output_layers(features)[0, 0])
27
+
28
+ # Positive values support Non_Demented, negative values support Demented.
29
+ # Keep what supports the predicted class
30
+ cam = features @ output_weights
31
+ cam = tf.nn.relu(cam if probability > 0.5 else -cam)
32
+ cam = tf.image.resize(cam, IMAGE_SIZE)[0, :, :, 0].numpy()
33
+ cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-8)
34
+
35
+ # Same overlay as the notebook: jet colormap at 40% opacity
36
+ heatmap = colormaps["jet"](cam)[:, :, :3] * 255
37
+ overlay = (0.6 * image.numpy() + 0.4 * heatmap).astype("uint8")
38
+
39
+ return {CLASSES[0]: 1 - probability, CLASSES[1]: probability}, overlay
40
+
41
+
42
+ demo = gr.Interface(
43
+ fn=predict,
44
+ # filepath + image_mode=None hands the uploaded file to predict unchanged
45
+ inputs=gr.Image(type="filepath", image_mode=None, label="Brain MRI slice"),
46
+ outputs=[
47
+ gr.Label(label="Prediction"),
48
+ gr.Image(label="Class activation map"),
49
+ ],
50
+ title="Alzheimer's MRI Classification with Class Activation Maps",
51
+ description=(
52
+ "Upload an axial brain MRI slice. The model predicts Demented or "
53
+ "Non_Demented, and the heat map shows the regions that support that "
54
+ "prediction. Research demo only, not a diagnostic tool."
55
+ ),
56
+ flagging_mode="never",
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ tensorflow-cpu==2.21.0
2
+ matplotlib==3.11.2