--- 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)