Update README.md
Browse files
README.md
CHANGED
@@ -4,6 +4,63 @@ tags:
|
|
4 |
- pytorch_model_hub_mixin
|
5 |
---
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
- pytorch_model_hub_mixin
|
5 |
---
|
6 |
|
7 |
+
# Model Card: MRI Brain Tumor Classification Model
|
8 |
+
|
9 |
+
## Model Details
|
10 |
+
- **Architecture**: EfficientNet-B1-based MRI classification model
|
11 |
+
- **Dataset**: [Brain Tumor MRI Dataset](https://www.kaggle.com/datasets/masoudnickparvar/brain-tumor-mri-dataset)
|
12 |
+
- **Batch Size**: 32
|
13 |
+
- **Loss Function**: Triplet Margin Loss with Cosine Similarity
|
14 |
+
- **Optimizer**: Adam (learning rate = 1e-2)
|
15 |
+
|
16 |
+
## Model Architecture
|
17 |
+
This model is based on **EfficientNet-B1** and has been modified for MRI brain tumor classification. The main adaptations include:
|
18 |
+
|
19 |
+
### **Modifications**:
|
20 |
+
- **Input Channel Adjustment**: The first convolutional layer is changed to accept single-channel (grayscale) MRI scans.
|
21 |
+
- **Classifier Head**: The default classifier is replaced with a custom MLP featuring:
|
22 |
+
- Fully connected layers with 1280 → 756 → 256 units.
|
23 |
+
- SiLU activation.
|
24 |
+
- Batch normalization.
|
25 |
+
- Dropout for regularization.
|
26 |
+
|
27 |
+
### **Triplet Loss for Metric Learning**:
|
28 |
+
The model uses **Triplet Margin Loss** with **Cosine Similarity** to learn an embedding space where MRI images of the same class are closer together, while images from different classes are farther apart.
|
29 |
+
|
30 |
+
## Implementation
|
31 |
+
### **Model Definition**
|
32 |
+
```python
|
33 |
+
import torch
|
34 |
+
import torch.nn as nn
|
35 |
+
from torchvision.models import efficientnet_b1
|
36 |
+
from torch.nn import TripletMarginWithDistanceLoss
|
37 |
+
from torch.nn.functional import cosine_similarity
|
38 |
+
|
39 |
+
class MRIModel(nn.Module, PyTorchModelHubMixin):
|
40 |
+
def __init__(self):
|
41 |
+
super(MRIModel, self).__init__()
|
42 |
+
self.base_model = efficientnet_b1(weights=False)
|
43 |
+
self.base_model.features[0] = nn.Sequential(
|
44 |
+
nn.Conv2d(1, 32, kernel_size=(3, 3), stride=(2, 2), bias=False),
|
45 |
+
nn.BatchNorm2d(32),
|
46 |
+
nn.ReLU6(inplace=True),
|
47 |
+
)
|
48 |
+
self.base_model.classifier = nn.Sequential(
|
49 |
+
nn.Linear(1280, 756),
|
50 |
+
nn.SiLU(),
|
51 |
+
nn.BatchNorm1d(756),
|
52 |
+
nn.Dropout(0.2),
|
53 |
+
nn.Linear(756, 256),
|
54 |
+
)
|
55 |
+
|
56 |
+
def forward(self, x):
|
57 |
+
return self.base_model(x)
|
58 |
+
```
|
59 |
+
|
60 |
+
## Training Configuration
|
61 |
+
- Batch Size: 32
|
62 |
+
- Loss Function: Triplet Margin Loss (Cosine Similarity)
|
63 |
+
- Optimizer: Adam (learning rate = 1e-2)
|
64 |
+
|
65 |
+
|
66 |
+
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|