File size: 2,270 Bytes
23552e8
 
 
 
 
 
b51c3f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
tags:
- model_hub_mixin
- pytorch_model_hub_mixin
---

# Model Card: MRI Brain Tumor Classification (ResNet-18)

## Model Details
- **Model Name**: `MRIResnet`
- **Architecture**: ResNet-18-based model for MRI brain tumor classification  
- **Dataset**: [Brain Tumor MRI Dataset](https://www.kaggle.com/datasets/masoudnickparvar/brain-tumor-mri-dataset)  
- **Batch Size**: 32  
- **Loss Function**: CrossEntropy Loss  
- **Optimizer**: Adam (learning rate = 1e-3)  
- **Transfer Learning**: Yes (pretrained ResNet-18 with modified layers)  

## Model Architecture
This model is based on **ResNet-18**, a widely used convolutional neural network, and has been adapted for **MRI-based brain tumor classification**. 

### **Modifications**
- **Input Channel Adaptation**: The first convolutional layer is modified to accept single-channel (grayscale) MRI scans.
- **Classifier Head**: The fully connected (FC) layer is replaced to output 4 classes (assuming 4 tumor categories).
- **Transfer Learning**:
  - **Frozen Layers**: All pre-trained weights are frozen except for the modified layers.
  - **Trainable Layers**:
    - First convolutional layer (`conv1`)  
    - Fully connected classification layer (`fc`)  

## Implementation
### **Model Definition**
```python
import torch
import torch.nn as nn
from torchvision.models import resnet18

class MRIResnet(nn.Module, PyTorchModelHubMixin):
    def __init__(self):
        super().__init__()
        self.base_model = resnet18(weights=True)
        self.base_model.conv1 = nn.Conv2d(
            1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False
        )
        self.base_model.fc = nn.Linear(512, 4)

        # Freeze all layers except the modified ones
        for param in self.base_model.parameters():
            param.requires_grad = False

        for param in self.base_model.conv1.parameters():
            param.requires_grad = True
        for param in self.base_model.fc.parameters():
            param.requires_grad = True

    def forward(self, x):
        return self.base_model(x)
```

This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration: