Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Custom ResNet-18 for 7-Class Classification
|
2 |
+
|
3 |
+
This is a fine-tuned **`ResNet-18`** model designed for a 7-class classification task. The model replaces all **ReLU** activation functions with **PReLU**, introduces **Dropout2D** layers for better generalization, and was trained on a custom dataset with various augmentations.
|
4 |
+
|
5 |
+
---
|
6 |
+
|
7 |
+
## 📜 Model Details
|
8 |
+
|
9 |
+
- **Base Model:** ResNet-18 (pre-trained on ImageNet).
|
10 |
+
- **Activations:** ReLU layers replaced with PReLU.
|
11 |
+
- **Dropout:** Dropout2D applied to enhance generalization.
|
12 |
+
- **Classes:** 7 output classes.
|
13 |
+
- **Input Size:** Images with customizable dimensions (default: `[100, 100]`).
|
14 |
+
- **Normalization:** Input images are normalized using the following statistics:
|
15 |
+
- Mean: `[0.485, 0.456, 0.406]`
|
16 |
+
- Std: `[0.229, 0.224, 0.225]`
|
17 |
+
|
18 |
+
---
|
19 |
+
|
20 |
+
## 📈 Evaluation Metrics on Test Data
|
21 |
+
![Confusion Matrix](Test Accuracy 80.31%.png)
|
22 |
+
|
23 |
+
|
24 |
+
## 🧑💻 How to Use
|
25 |
+
|
26 |
+
You can load the model weights and architecture for inference or fine-tuning with the provided files:
|
27 |
+
|
28 |
+
### **Using PyTorch**
|
29 |
+
```
|
30 |
+
|
31 |
+
def get_out_channels(module):
|
32 |
+
"""تابعی برای یافتن تعداد کانالهای خروجی از لایههای کانولوشن و BatchNorm"""
|
33 |
+
if isinstance(module, nn.Conv2d):
|
34 |
+
return module.out_channels
|
35 |
+
elif isinstance(module, nn.BatchNorm2d):
|
36 |
+
return module.num_features
|
37 |
+
elif isinstance(module, nn.Linear):
|
38 |
+
return module.out_features
|
39 |
+
return None
|
40 |
+
|
41 |
+
def replace_relu_with_prelu_and_dropout(module, inplace=True):
|
42 |
+
for name, child in module.named_children():
|
43 |
+
replace_relu_with_prelu_and_dropout(child, inplace)
|
44 |
+
|
45 |
+
if isinstance(child, nn.ReLU):
|
46 |
+
out_channels = None
|
47 |
+
for prev_name, prev_child in module.named_children():
|
48 |
+
if prev_name == name:
|
49 |
+
break
|
50 |
+
out_channels = get_out_channels(prev_child) or out_channels
|
51 |
+
|
52 |
+
if out_channels is None:
|
53 |
+
raise ValueError(f"Cannot determine `out_channels` for {child}. Please check the model structure.")
|
54 |
+
|
55 |
+
prelu = PReLU(device=device, num_parameters=out_channels)
|
56 |
+
dropout = nn.Dropout2d(p=0.2)
|
57 |
+
setattr(module, name, nn.Sequential(prelu, dropout).to(device))
|
58 |
+
model = models.resnet18(weights = models.ResNet18_Weights.IMAGENET1K_V1).train(True).to(device)
|
59 |
+
replace_relu_with_prelu_and_dropout(model)
|
60 |
+
# print(model.fc.in_features)
|
61 |
+
|
62 |
+
|
63 |
+
number = model.fc.in_features
|
64 |
+
module = []
|
65 |
+
|
66 |
+
module.append(LazyLinear(7))
|
67 |
+
model.fc = Sequential(*module).to(device)
|
68 |
+
|
69 |
+
state_dict = load_file("model.safetensors")
|
70 |
+
model.load_state_dict(state_dict)
|
71 |
+
model.eval()
|
72 |
+
```
|
73 |
+
|
74 |
+
## ⚠️ Limitations and Considerations
|
75 |
+
Input Dimensions: Make sure your input images are resized to the expected dimensions (100x100) before inference.
|
76 |
+
Number of Classes: The trained model supports exactly 7 classes as defined in the training dataset.
|
77 |
+
|
78 |
+
|
79 |
+
|