-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
125 lines (99 loc) · 4.99 KB
/
model.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import torch.nn as nn
# bigger one
class CustomCNN(nn.Module):
def __init__(self):
super(CustomCNN, self).__init__()
# Layer 1: Initial layer with a 17x17 filter
self.conv1 = nn.Conv2d(1, 32, kernel_size=(17,17), padding=8, padding_mode='reflect')
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
# Layer 2: Additional layer with an 11x11 filter
self.conv2 = nn.Conv2d(32, 32, kernel_size=11, padding=5, padding_mode='reflect')
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
# Layer 3: Intermediate layers with 5x5 filters
self.conv3 = nn.Conv2d(32, 64, kernel_size=5, padding=2, padding_mode='reflect')
self.relu3 = nn.ReLU()
# Layer 4: Another layer with 5x5 filter and adjusted stride
self.conv4 = nn.Conv2d(64, 64, kernel_size=5, padding=2, padding_mode='reflect')
self.relu4 = nn.ReLU()
# Layer 5: Final layer with 5x5 filter
self.conv5 = nn.Conv2d(64, 64, kernel_size=5, padding=2, padding_mode='reflect')
self.relu5 = nn.ReLU()
num_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
print(f"Number of trainable parameters: {num_params}")
def forward(self, x):
# Forward pass through the layers
x = self.pool1(self.relu1(self.conv1(x)))
x = self.pool2(self.relu2(self.conv2(x)))
x = self.relu3(self.conv3(x))
x = self.relu4(self.conv4(x))
x = self.relu5(self.conv5(x))
return x
# without batch/layer normalization
class CustomCNN2(nn.Module):
def __init__(self):
super(CustomCNN2, self).__init__()
# Layer 1: Initial layer with a 17x17 filter
self.conv1 = nn.Conv2d(1, 4, kernel_size=(17,17), padding=8, padding_mode='reflect')
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
# Layer 2: Additional layer with an 11x11 filter
self.conv2 = nn.Conv2d(4, 16, kernel_size=11, padding=5, padding_mode='reflect')
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
# Layer 3: Intermediate layers with 5x5 filters
self.conv3 = nn.Conv2d(16, 16, kernel_size=5, padding=2, padding_mode='reflect')
self.relu3 = nn.ReLU()
# Layer 4: Another layer with 5x5 filter and adjusted stride
self.conv4 = nn.Conv2d(16, 32, kernel_size=5, padding=2, padding_mode='reflect')
self.relu4 = nn.ReLU()
# Layer 5: Final layer with 5x5 filter
self.conv5 = nn.Conv2d(32, 32, kernel_size=5, padding=2, padding_mode='reflect')
self.relu5 = nn.ReLU()
num_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
print(f"Number of trainable parameters: {num_params}")
def forward(self, x):
# Forward pass through the layers
x = self.pool1(self.relu1(self.conv1(x)))
x = self.pool2(self.relu2(self.conv2(x)))
x = self.relu3(self.conv3(x))
x = self.relu4(self.conv4(x))
x = self.relu5(self.conv5(x))
return x
# With batch normalization
class CustomCNN3(nn.Module):
def __init__(self):
super(CustomCNN3, self).__init__()
# Layer 1: Initial layer with a 17x17 filter
self.conv1 = nn.Conv2d(1, 4, kernel_size=(17,17), padding=8, padding_mode='reflect')
self.bn1 = nn.BatchNorm2d(4)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
# Layer 2: Additional layer with an 11x11 filter
self.conv2 = nn.Conv2d(4, 16, kernel_size=11, padding=5, padding_mode='reflect')
self.bn2 = nn.BatchNorm2d(16)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
# Layer 3: Intermediate layers with 5x5 filters
self.conv3 = nn.Conv2d(16, 16, kernel_size=5, padding=2, padding_mode='reflect')
self.bn3 = nn.BatchNorm2d(16)
self.relu3 = nn.ReLU()
# Layer 4: Another layer with 5x5 filter and adjusted stride
self.conv4 = nn.Conv2d(16, 32, kernel_size=5, padding=2, padding_mode='reflect')
self.bn4 = nn.BatchNorm2d(32)
self.relu4 = nn.ReLU()
# Layer 5: Final layer with 5x5 filter
self.conv5 = nn.Conv2d(32, 32, kernel_size=5, padding=2, padding_mode='reflect')
self.bn5 = nn.BatchNorm2d(32)
self.relu5 = nn.ReLU()
num_params = sum(p.numel() for p in self.parameters() if p.requires_grad)
print(f"Number of trainable parameters: {num_params}")
def forward(self, x):
# Forward pass through the layers
x = self.pool1(self.relu1(self.ln1(self.conv1(x))))
x = self.pool2(self.relu2(self.ln2(self.conv2(x))))
x = self.relu3(self.ln3(self.conv3(x)))
x = self.relu4(self.ln4(self.conv4(x)))
x = self.relu5(self.ln5(self.conv5(x)))
return x