File size: 6,046 Bytes
c701ef5
c1a62e7
 
 
c701ef5
94d435b
 
 
 
 
 
c1a62e7
94d435b
 
a96981a
52f8d86
 
 
a96981a
 
 
8b22a58
52f8d86
 
94d435b
c1a62e7
 
94d435b
 
 
 
 
c1a62e7
94d435b
 
 
 
 
c1a62e7
1408fc3
8b22a58
1408fc3
c1a62e7
 
dc3fab6
108db48
61b1ddc
 
c1a62e7
 
 
 
 
 
 
61b1ddc
 
 
 
 
 
 
 
c1a62e7
 
 
 
 
 
 
 
 
 
61b1ddc
 
c1a62e7
 
 
 
 
 
c3ef54d
c1a62e7
 
 
 
 
 
 
 
61b1ddc
eca074e
 
94d435b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c701ef5
b2dd653
c269260
5527c3c
4643a51
 
c269260
f7889c7
c269260
f7889c7
 
 
 
b2dd653
c269260
4643a51
 
 
 
 
b2dd653
f7889c7
 
 
 
 
 
 
b2dd653
f7889c7
4643a51
 
17a836b
7e50272
 
 
 
 
 
 
17a836b
aa0f7e8
0fb0eb0
 
f33d4dd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
language:
- en
- th
license: mit
tags:
- medical
- mri
- brain-tumor-detection
- computer-vision
- tensorflow
base_model: google/efficientnet-b0
---

🧠 Brain Tumor MRI Classification Model (โมเดล AI ตรวจวิเคราะห์และแยกแยะประเภทเนื้องอกในสมองจากภาพสแกน MRI)

This Artificial Intelligence (AI) model is fine-tuned to analyze brain MRI scans and classify them into 4 distinct categories:

    Glioma Tumor (เนื้องอกในสมองชนิดกลิโอมา)— A type of tumor that occurs in the brain and spinal cord.
    Meningioma Tumor (เนื้องอกเยื่อหุ้มสมอง) — A tumor that arises from the meninges (membranes surrounding the brain).
    Pituitary Tumor (เนื้องอกต่อมใต้สมอง) — An abnormal growth that develops in the pituitary gland.
    No Tumor (สมองปกติ ไม่พบเนื้องอก) — Healthy brain scan with no detected tumors.

    💡 In Short: An AI-powered image classification model designed to assist in detecting and identifying brain tumor types from MRI scans.

---

## 📋 Model Overview
- **Base Architecture:** EfficientNetB0 (Pre-trained on ImageNet)
- **Task:** Multi-class Image Classification (4 Classes)
- **Input Image Size:** 224 x 224 x 3
- **Framework:** TensorFlow 2.x / Keras
---
## 🏷️ Target Classes
1. **Glioma Tumor**
2. **Meningioma Tumor**
3. **No Tumor**
4. **Pituitary Tumor**
---
### 🛠️ 1. Prerequisites & Installation
Before using, you need to install the required basic libraries using this command in your Terminal or Command Prompt:
ก่อนเริ่มใช้งาน คุณจำเป็นต้องติดตั้งไลบรารีพื้นฐาน
```bash
pip install tensorflow pillow requests numpy
```
```
import os
import urllib.request
import numpy as np
import tensorflow as tf
from PIL import Image
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization
from tensorflow.keras.models import Model


model_url = "[https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/efficientnetb0_finetuned_brain_mri.keras](https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/efficientnetb0_finetuned_brain_mri.keras)"
weights_path = "model_weights.keras"

if not os.path.exists(weights_path):
    print("Downloading model weights...")
    urllib.request.urlretrieve(model_url, weights_path)
    print("Download completed!")
base_model = EfficientNetB0(weights=None, include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = BatchNormalization()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.4)(x)
outputs = Dense(4, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=outputs)

model.load_weights(weights_path)
print("✅ Model is ready to use!")
class_names = ['Glioma', 'Meningioma', 'No Tumor', 'Pituitary']

def predict_mri(image_path):
    img = Image.open(image_path).convert('RGB')
    img = img.resize((224, 224))
    img_array = np.array(img, dtype=np.float32)
    img_array = np.expand_dims(img_array, axis=0)  
    
    predictions = model.predict(img_array)
    predicted_class = class_names[np.argmax(predictions[0])]
    confidence = np.max(predictions[0]) * 100
    
    return predicted_class, confidence

# class_label, conf = predict_mri("path/to/your/mri_scan.jpg")
# print(f"Result: {class_label} ({conf:.2f}%)")
```
```
## ⚙️ Training Details & Hyperparameters

- **Optimizer:** Adam (Learning Rate = `1e-4`)
- **Loss Function:** Categorical Crossentropy
- **Batch Size:** 16
- **Data Augmentation:**
  - Rotation Range: 15°
  - Width & Height Shift: 10%
  - Zoom Range: 15%
  - Horizontal Flip: True
- **Fine-Tuning Strategy:** Unfroze top 40 layers of EfficientNetB0 for fine-tuning.

## 📊 Model Architecture Summary

```text
Input (224, 224, 3) 
  ↳ EfficientNetB0 Base (Top 40 layers unfrozen)
  ↳ GlobalAveragePooling2D
  ↳ BatchNormalization
  ↳ Dense(256, activation='relu')
  ↳ Dropout(0.4)
  ↳ Dense(4, activation='softmax')
---
```
## 🚀 How to Load and Use
```
import os
import urllib.request
import numpy as np
import tensorflow as tf
from PIL import Image
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization
from tensorflow.keras.models import Model

model_url = "[https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/efficientnetb0_finetuned_brain_mri.keras](https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/efficientnetb0_finetuned_brain_mri.keras)"
weights_path = "model_weights.keras"

if not os.path.exists(weights_path):
    print("Downloading model weights...")
    urllib.request.urlretrieve(model_url, weights_path)
    print("Download completed!")

base_model = EfficientNetB0(weights=None, include_top=False, input_shape=(224, 224, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = BatchNormalization()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.4)(x)
outputs = Dense(4, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=outputs)
model.load_weights(weights_path)
print("Model is ready for use!")
```
📊 Dataset & Training Data

    Source: Brain Tumor MRI Dataset (Kaggle)
    Data Distribution:
        Training Set: MRI images augmented with rotation, shift, zoom, and horizontal flips.
        Testing Set: Independent brain MRI scans for evaluation.
        Classes: 4 categories (Glioma, Meningioma, No Tumor, Pituitary).
        
### Confusion Matrix & Training Performance
![Confusion Matrix](https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/Figure_1.png)
![Training Performance](https://huggingface.co/starpreeda/BrainTumorTest/resolve/main/Figure_2.png)