Add initial Keras model for stroke classification
Browse files- README.md +63 -0
- stroke_classification_model.h5 +3 -0
README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Stroke Classification ResNet Model
|
| 3 |
+
|
| 4 |
+
This is a Keras model for classifying MRI images into:
|
| 5 |
+
- Hemorrhagic Stroke
|
| 6 |
+
- Ischemic Stroke
|
| 7 |
+
- No Stroke
|
| 8 |
+
|
| 9 |
+
The model is a fine-tuned ResNet50.
|
| 10 |
+
|
| 11 |
+
## How to use this model (example in Python)
|
| 12 |
+
|
| 13 |
+
```python
|
| 14 |
+
import tensorflow as tf
|
| 15 |
+
from PIL import Image
|
| 16 |
+
import numpy as np
|
| 17 |
+
import os # Added for os.path.join
|
| 18 |
+
|
| 19 |
+
# Load the model
|
| 20 |
+
# Ensure the model file 'stroke_classification_model.h5' is in the same directory
|
| 21 |
+
# or provide the full path.
|
| 22 |
+
model = tf.keras.models.load_model('stroke_classification_model.h5')
|
| 23 |
+
|
| 24 |
+
# Define your class names (must match how your model was trained)
|
| 25 |
+
CLASS_NAMES = ['hemorrhagic_stroke', 'ischemic_stroke', 'no_stroke'] # Automatically populated from your Colab session
|
| 26 |
+
|
| 27 |
+
def preprocess_image_for_prediction(image_path, target_size=(224, 224), pixel_threshold=40):
|
| 28 |
+
img = Image.open(image_path).convert("L")
|
| 29 |
+
original_width, original_height = img.size
|
| 30 |
+
data = np.array(img)
|
| 31 |
+
rows_with_content = np.any(data > pixel_threshold, axis=1)
|
| 32 |
+
cols_with_content = np.any(data > pixel_threshold, axis=0)
|
| 33 |
+
try:
|
| 34 |
+
min_row = np.where(rows_with_content)[0][0]
|
| 35 |
+
max_row = np.where(rows_with_content)[0][-1]
|
| 36 |
+
min_col = np.where(cols_with_content)[0][0]
|
| 37 |
+
max_col = np.where(cols_with_content)[0][-1]
|
| 38 |
+
except IndexError:
|
| 39 |
+
cropped_img = img
|
| 40 |
+
else:
|
| 41 |
+
buffer = 5
|
| 42 |
+
min_row = max(0, min_row - buffer)
|
| 43 |
+
max_row = min(original_height - 1, max_row + buffer)
|
| 44 |
+
min_col = max(0, min_col - buffer)
|
| 45 |
+
max_col = min(original_width - 1, max_col + buffer)
|
| 46 |
+
cropped_img = img.crop((min_col, min_row, max_col + 1, max_row + 1))
|
| 47 |
+
processed_img = cropped_img.resize(target_size, Image.LANCZOS)
|
| 48 |
+
if processed_img.mode == 'L':
|
| 49 |
+
processed_img = processed_img.convert('RGB')
|
| 50 |
+
img_array = tf.keras.utils.img_to_array(processed_img)
|
| 51 |
+
img_array = tf.expand_dims(img_array, 0)
|
| 52 |
+
return img_array
|
| 53 |
+
|
| 54 |
+
# Example usage:
|
| 55 |
+
# image_path = "path/to/your/new_mri_image.jpg"
|
| 56 |
+
# preprocessed_img = preprocess_image_for_prediction(image_path)
|
| 57 |
+
# if preprocessed_img is not None:
|
| 58 |
+
# predictions = model.predict(preprocessed_img)
|
| 59 |
+
# predicted_class_index = np.argmax(predictions[0])
|
| 60 |
+
# predicted_class_name = CLASS_NAMES[predicted_class_index]
|
| 61 |
+
# confidence = np.max(predictions[0]) * 100
|
| 62 |
+
# print(f"Predicted: {predicted_class_name} with {confidence:.2f}% confidence")
|
| 63 |
+
```
|
stroke_classification_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f68f6e0e5521317725c5385e66d1858d3ff2d6e147494ef30282232334356cf4
|
| 3 |
+
size 286389792
|