chore: model class
Browse files
model.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class CNN(nn.Module):
|
| 7 |
+
def __init__(self):
|
| 8 |
+
super(CNN, self).__init__()
|
| 9 |
+
self.conv1 = nn.Conv2d(3, 32, 3, stride=1, padding=1) # 32x32 -> 16x16
|
| 10 |
+
self.bn1 = nn.BatchNorm2d(32)
|
| 11 |
+
self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1) # 16x16 -> 8x8
|
| 12 |
+
self.bn2 = nn.BatchNorm2d(64)
|
| 13 |
+
self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1) # 8x8 -> 4x4
|
| 14 |
+
self.bn3 = nn.BatchNorm2d(128)
|
| 15 |
+
self.pool = nn.MaxPool2d(stride=2, kernel_size=2)
|
| 16 |
+
self.fc1 = nn.Linear(128 * 4 * 4, 512)
|
| 17 |
+
self.fc2 = nn.Linear(512, 10)
|
| 18 |
+
self.dropout = nn.Dropout(0.5)
|
| 19 |
+
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
x = self.pool(F.relu(self.bn1(self.conv1(x))))
|
| 22 |
+
x = self.pool(F.relu(self.bn2(self.conv2(x))))
|
| 23 |
+
x = self.pool(F.relu(self.bn3(self.conv3(x))))
|
| 24 |
+
x = x.view(x.size(0), -1)
|
| 25 |
+
x = self.dropout(x)
|
| 26 |
+
x = F.relu(self.fc1(x))
|
| 27 |
+
x = self.dropout(x)
|
| 28 |
+
x = self.fc2(x)
|
| 29 |
+
return x
|