-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN.py
55 lines (46 loc) · 1.51 KB
/
CNN.py
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
from typing import List
import torch.nn as nn
class CNN(nn.Module):
def __init__(self, image_size: int, hidden_dim: List[int], dropout: float, num_classes: int):
super(CNN, self).__init__()
self.conv1 = nn.Sequential(
nn.Dropout(p=dropout),
nn.Conv2d(
in_channels=1,
out_channels=hidden_dim[0],
kernel_size=5,
stride=1,
padding=2,
),
nn.BatchNorm2d(hidden_dim[0]),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2)
)
out_dim = image_size // 2
self.conv2 = nn.Sequential(
nn.Dropout(p=dropout),
nn.Conv2d(
in_channels=hidden_dim[0],
out_channels=hidden_dim[1],
kernel_size=5,
stride=1,
padding=2,
)
)
self.conv3 = nn.Sequential(
nn.Dropout(p=dropout),
nn.Conv2d(hidden_dim[1], hidden_dim[2], 5, 1, 2),
nn.BatchNorm2d(hidden_dim[2]),
nn.ReLU(),
nn.MaxPool2d(2)
)
out_dim = out_dim // 2
self.out = nn.Linear(hidden_dim[2] * out_dim * out_dim, num_classes)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x) + x
x = nn.functional.relu(x)
x = self.conv3(x)
x = x.view(x.size(0), -1)
output = self.out(x)
return output