-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcnnNet.py
63 lines (46 loc) · 1.31 KB
/
cnnNet.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
import torch
from torch import nn
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
self.func = nn.Sequential(
nn.Conv2d(3, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(True),
nn.Conv2d(32, 32, 3, 1, 1),
nn.BatchNorm2d(32),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=2),
nn.Conv2d(32, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.Conv2d(64, 128, 3, 1, 1),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.MaxPool2d(kernel_size=2),
)
self.linear = nn.Sequential(
nn.Linear(128 * 21 * 21, 512),
nn.BatchNorm1d(512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.BatchNorm1d(128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, 2)
)
def forward(self, x):
batchsz = x.size(0)
x = self.func(x)
# print(x.shape)
x = x.view(batchsz, 128 * 21 * 21)
logits = self.linear(x)
return logits
def main():
data = torch.randn(2, 3, 84, 84)
net = ResNet()
out = net(data)
print(out.shape)
if __name__ == '__main__':
main()