NiksheyYadav commited on
Commit
30ad36b
·
1 Parent(s): 6840f51

Add MRI brain classification models (93.95% tumor accuracy)

Browse files
Files changed (5) hide show
  1. README.md +160 -0
  2. best_model.pth +3 -0
  3. config.json +57 -0
  4. ixi_3dcnn_best.pth +3 -0
  5. kaggle_tumor_2dcnn_best.pth +3 -0
README.md ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # MRI Brain Tumor Classification Models
2
+
3
+ This repository contains trained deep learning models for MRI brain scan classification, developed using Mojo and PyTorch.
4
+
5
+ ## Models
6
+
7
+ ### 1. Brain Tumor 2D CNN (`kaggle_tumor_2dcnn_best.pth`)
8
+
9
+ **Task:** Multi-class brain tumor classification from MRI slices
10
+
11
+ | Metric | Value |
12
+ |--------|-------|
13
+ | **Accuracy** | 93.95% |
14
+ | **Precision** | 0.94 |
15
+ | **Recall** | 0.94 |
16
+ | **F1 Score** | 0.94 |
17
+
18
+ **Classes:**
19
+ - `glioma` - 98.1% accuracy
20
+ - `meningioma` - 83.9% accuracy
21
+ - `notumor` - 98.5% accuracy
22
+ - `pituitary` - 94.3% accuracy
23
+
24
+ **Architecture:**
25
+ - 2D CNN with 4 convolutional blocks
26
+ - BatchNorm + Dropout regularization
27
+ - AdaptiveAvgPool + 3-layer classifier
28
+ - 4.85M parameters
29
+
30
+ **Training:**
31
+ - Dataset: Kaggle Brain Tumor MRI (7,023 images)
32
+ - Input: 224x224 RGB images
33
+ - Epochs: 50
34
+ - Optimizer: AdamW with OneCycleLR
35
+ - Augmentation: RandomCrop, Flip, Rotation, ColorJitter, GaussianBlur
36
+
37
+ ### 2. IXI 3D Brain CNN (`ixi_3dcnn_best.pth`)
38
+
39
+ **Task:** 3D brain MRI volume classification
40
+
41
+ **Architecture:**
42
+ - 3D CNN with 4 Conv3D blocks
43
+ - BatchNorm3D + Dropout3D
44
+ - Global Average Pooling
45
+ - 1.2M parameters
46
+
47
+ **Training:**
48
+ - Dataset: IXI Brain MRI (681 NIfTI volumes)
49
+ - Input: 64x64x64 3D volumes
50
+ - Epochs: 30
51
+ - GPU: NVIDIA RTX 4090
52
+
53
+ ## Usage
54
+
55
+ ### Load Tumor Model (PyTorch)
56
+
57
+ ```python
58
+ import torch
59
+ import torch.nn as nn
60
+
61
+ class TumorCNN(nn.Module):
62
+ def __init__(self, num_classes=4):
63
+ super(TumorCNN, self).__init__()
64
+ self.features = nn.Sequential(
65
+ nn.Conv2d(3, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(),
66
+ nn.Conv2d(64, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(),
67
+ nn.MaxPool2d(2, 2), nn.Dropout2d(0.25),
68
+
69
+ nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(),
70
+ nn.Conv2d(128, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(),
71
+ nn.MaxPool2d(2, 2), nn.Dropout2d(0.25),
72
+
73
+ nn.Conv2d(128, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(),
74
+ nn.Conv2d(256, 256, 3, padding=1), nn.BatchNorm2d(256), nn.ReLU(),
75
+ nn.MaxPool2d(2, 2), nn.Dropout2d(0.25),
76
+
77
+ nn.Conv2d(256, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(),
78
+ nn.Conv2d(512, 512, 3, padding=1), nn.BatchNorm2d(512), nn.ReLU(),
79
+ nn.AdaptiveAvgPool2d((1, 1)),
80
+ )
81
+ self.classifier = nn.Sequential(
82
+ nn.Flatten(),
83
+ nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.5),
84
+ nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.3),
85
+ nn.Linear(128, num_classes),
86
+ )
87
+
88
+ def forward(self, x):
89
+ return self.classifier(self.features(x))
90
+
91
+ # Load model
92
+ model = TumorCNN(num_classes=4)
93
+ model.load_state_dict(torch.load("kaggle_tumor_2dcnn_best.pth"))
94
+ model.eval()
95
+
96
+ # Inference
97
+ from torchvision import transforms
98
+ from PIL import Image
99
+
100
+ transform = transforms.Compose([
101
+ transforms.Resize((224, 224)),
102
+ transforms.ToTensor(),
103
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
104
+ ])
105
+
106
+ image = Image.open("brain_mri.jpg").convert("RGB")
107
+ input_tensor = transform(image).unsqueeze(0)
108
+
109
+ with torch.no_grad():
110
+ output = model(input_tensor)
111
+ pred = output.argmax(1).item()
112
+
113
+ classes = ['glioma', 'meningioma', 'notumor', 'pituitary']
114
+ print(f"Prediction: {classes[pred]}")
115
+ ```
116
+
117
+ ## Training
118
+
119
+ ```bash
120
+ # Clone repository
121
+ git clone https://huggingface.co/YOUR_USERNAME/mri-brain-classification
122
+
123
+ # Train tumor model
124
+ mojo run scripts/train_tumor.mojo
125
+
126
+ # Train 3D model
127
+ mojo run scripts/train_advanced.mojo
128
+ ```
129
+
130
+ ## Evaluation
131
+
132
+ ```bash
133
+ # Evaluate tumor model
134
+ mojo run scripts/evaluate_tumor.mojo
135
+
136
+ # Evaluate IXI model
137
+ mojo run scripts/evaluate_ixi.mojo
138
+ ```
139
+
140
+ ## Citation
141
+
142
+ ```bibtex
143
+ @misc{mri-brain-classification,
144
+ author = {Meidverse},
145
+ title = {MRI Brain Tumor Classification Models},
146
+ year = {2024},
147
+ publisher = {Hugging Face},
148
+ url = {https://huggingface.co/Meidverse/mri-brain-classification}
149
+ }
150
+ ```
151
+
152
+ ## License
153
+
154
+ MIT License
155
+
156
+ ## Acknowledgments
157
+
158
+ - [Kaggle Brain Tumor MRI Dataset](https://www.kaggle.com/datasets/masoudnickparvar/brain-tumor-mri-dataset)
159
+ - [IXI Dataset](https://brain-development.org/ixi-dataset/)
160
+ - Built with Mojo 🔥 and PyTorch
best_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:de1927dcca7dbf627c90128a2610b0d44b0f7b78018a878c73616c870f7b0112
3
+ size 4801205
config.json ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "tumor_model": {
3
+ "name": "kaggle_tumor_2dcnn_best",
4
+ "task": "image-classification",
5
+ "architecture": "2D-CNN",
6
+ "num_classes": 4,
7
+ "classes": ["glioma", "meningioma", "notumor", "pituitary"],
8
+ "input_size": [224, 224],
9
+ "channels": 3,
10
+ "parameters": 4853956,
11
+ "metrics": {
12
+ "accuracy": 0.9395,
13
+ "precision": 0.94,
14
+ "recall": 0.94,
15
+ "f1_score": 0.94
16
+ },
17
+ "per_class_accuracy": {
18
+ "glioma": 0.981,
19
+ "meningioma": 0.839,
20
+ "notumor": 0.985,
21
+ "pituitary": 0.943
22
+ },
23
+ "training": {
24
+ "dataset": "Kaggle Brain Tumor MRI",
25
+ "samples": 7023,
26
+ "epochs": 50,
27
+ "batch_size": 32,
28
+ "optimizer": "AdamW",
29
+ "learning_rate": 0.0001,
30
+ "scheduler": "OneCycleLR",
31
+ "gpu": "NVIDIA RTX 4090"
32
+ }
33
+ },
34
+ "ixi_model": {
35
+ "name": "ixi_3dcnn_best",
36
+ "task": "3d-volume-classification",
37
+ "architecture": "3D-CNN",
38
+ "num_classes": 2,
39
+ "classes": ["healthy", "diseased"],
40
+ "input_size": [64, 64, 64],
41
+ "channels": 1,
42
+ "parameters": 1196674,
43
+ "training": {
44
+ "dataset": "IXI Brain MRI",
45
+ "samples": 681,
46
+ "epochs": 30,
47
+ "batch_size": 2,
48
+ "optimizer": "Adam",
49
+ "learning_rate": 0.0001
50
+ }
51
+ },
52
+ "framework": {
53
+ "primary": "Mojo",
54
+ "inference": "PyTorch",
55
+ "version": "2.0+"
56
+ }
57
+ }
ixi_3dcnn_best.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6e3647054a8919ace03f3d9c15012b8aa9263888fe1022332b670de12e217307
3
+ size 4801205
kaggle_tumor_2dcnn_best.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c20abe0c57f018dda98486712fdf4e47d5cb118e1aa5ee0d03109e927ec48218
3
+ size 19452813