Upload model.py with huggingface_hub
Browse files
model.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SimpleNet — lightweight CNN for EuroSAT satellite image classification.
|
| 3 |
+
4 convolutional blocks (double-and-halve pattern) + FC classifier.
|
| 4 |
+
Input: 3×64×64 RGB | Output: 10 land-use classes | ~850K parameters
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
CLASS_NAMES = [
|
| 12 |
+
"AnnualCrop", "Forest", "HerbaceousVegetation", "Highway",
|
| 13 |
+
"Industrial", "Pasture", "PermanentCrop", "Residential",
|
| 14 |
+
"River", "SeaLake"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class SimpleNet(nn.Module):
|
| 19 |
+
def __init__(self, num_classes: int = 10):
|
| 20 |
+
super().__init__()
|
| 21 |
+
|
| 22 |
+
# 64×64 → 32×32
|
| 23 |
+
self.block1 = nn.Sequential(
|
| 24 |
+
nn.Conv2d(3, 32, kernel_size=3, padding=1),
|
| 25 |
+
nn.BatchNorm2d(32),
|
| 26 |
+
nn.ReLU(),
|
| 27 |
+
nn.MaxPool2d(2),
|
| 28 |
+
)
|
| 29 |
+
# 32×32 → 16×16
|
| 30 |
+
self.block2 = nn.Sequential(
|
| 31 |
+
nn.Conv2d(32, 64, kernel_size=3, padding=1),
|
| 32 |
+
nn.BatchNorm2d(64),
|
| 33 |
+
nn.ReLU(),
|
| 34 |
+
nn.MaxPool2d(2),
|
| 35 |
+
)
|
| 36 |
+
# 16×16 → 8×8
|
| 37 |
+
self.block3 = nn.Sequential(
|
| 38 |
+
nn.Conv2d(64, 128, kernel_size=3, padding=1),
|
| 39 |
+
nn.BatchNorm2d(128),
|
| 40 |
+
nn.ReLU(),
|
| 41 |
+
nn.MaxPool2d(2),
|
| 42 |
+
)
|
| 43 |
+
# 8×8 → 4×4
|
| 44 |
+
self.block4 = nn.Sequential(
|
| 45 |
+
nn.Conv2d(128, 256, kernel_size=3, padding=1),
|
| 46 |
+
nn.BatchNorm2d(256),
|
| 47 |
+
nn.ReLU(),
|
| 48 |
+
nn.MaxPool2d(2),
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
self.classifier = nn.Sequential(
|
| 52 |
+
nn.Flatten(),
|
| 53 |
+
nn.Linear(256 * 4 * 4, 512),
|
| 54 |
+
nn.ReLU(),
|
| 55 |
+
nn.Dropout(0.3),
|
| 56 |
+
nn.Linear(512, num_classes),
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 60 |
+
x = self.block1(x)
|
| 61 |
+
x = self.block2(x)
|
| 62 |
+
x = self.block3(x)
|
| 63 |
+
x = self.block4(x)
|
| 64 |
+
return self.classifier(x)
|