nitinvig commited on
Commit
e7e845c
·
verified ·
1 Parent(s): 87de259

Create model.py

Browse files
Files changed (1) hide show
  1. model.py +121 -0
model.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torch.nn as nn
4
+ import torch.optim as optim
5
+ from torch.utils.data import DataLoader
6
+
7
+
8
+ # Bottleneck block (same as before)
9
+ class Bottleneck(nn.Module):
10
+ expansion = 4
11
+
12
+ def __init__(self, in_channels, out_channels, stride=1, downsample=None):
13
+ super(Bottleneck, self).__init__()
14
+ self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False)
15
+ self.bn1 = nn.BatchNorm2d(out_channels)
16
+
17
+ self.conv2 = nn.Conv2d(
18
+ out_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False
19
+ )
20
+ self.bn2 = nn.BatchNorm2d(out_channels)
21
+
22
+ self.conv3 = nn.Conv2d(
23
+ out_channels, out_channels * self.expansion, kernel_size=1, bias=False
24
+ )
25
+ self.bn3 = nn.BatchNorm2d(out_channels * self.expansion)
26
+
27
+ self.relu = nn.ReLU(inplace=True)
28
+ self.downsample = downsample
29
+
30
+ def forward(self, x):
31
+ identity = x
32
+
33
+ out = self.conv1(x)
34
+ out = self.bn1(out)
35
+ out = self.relu(out)
36
+
37
+ out = self.conv2(out)
38
+ out = self.bn2(out)
39
+ out = self.relu(out)
40
+
41
+ out = self.conv3(out)
42
+ out = self.bn3(out)
43
+
44
+ if self.downsample is not None:
45
+ identity = self.downsample(x)
46
+
47
+ out += identity
48
+ out = self.relu(out)
49
+ return out
50
+
51
+
52
+ # ResNet tailored for CIFAR (no initial 7x7 stride-2 conv + no maxpool)
53
+ class ResNetCIFAR(nn.Module):
54
+ def __init__(self, block, layers, num_classes=100):
55
+ super(ResNetCIFAR, self).__init__()
56
+ self.in_channels = 64
57
+
58
+ # Adjusted first conv for CIFAR (3x3, stride=1)
59
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
60
+ self.bn1 = nn.BatchNorm2d(64)
61
+ self.relu = nn.ReLU(inplace=True)
62
+
63
+ # NOTE: we do NOT use the 7x7 stride-2 conv or the 3x3 maxpool used for ImageNet
64
+ # Stage layers
65
+ self.layer1 = self._make_layer(block, 64, layers[0], stride=1)
66
+ self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
67
+ self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
68
+ self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
69
+
70
+ self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
71
+ self.fc = nn.Linear(512 * block.expansion, num_classes)
72
+
73
+ # Weight initialization
74
+ for m in self.modules():
75
+ if isinstance(m, nn.Conv2d):
76
+ nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
77
+ elif isinstance(m, nn.BatchNorm2d):
78
+ nn.init.constant_(m.weight, 1)
79
+ nn.init.constant_(m.bias, 0)
80
+
81
+ def _make_layer(self, block, out_channels, blocks, stride=1):
82
+ downsample = None
83
+ if stride != 1 or self.in_channels != out_channels * block.expansion:
84
+ downsample = nn.Sequential(
85
+ nn.Conv2d(
86
+ self.in_channels,
87
+ out_channels * block.expansion,
88
+ kernel_size=1,
89
+ stride=stride,
90
+ bias=False,
91
+ ),
92
+ nn.BatchNorm2d(out_channels * block.expansion),
93
+ )
94
+
95
+ layers = []
96
+ layers.append(block(self.in_channels, out_channels, stride, downsample))
97
+ self.in_channels = out_channels * block.expansion
98
+ for _ in range(1, blocks):
99
+ layers.append(block(self.in_channels, out_channels))
100
+
101
+ return nn.Sequential(*layers)
102
+
103
+ def forward(self, x):
104
+ x = self.conv1(x)
105
+ x = self.bn1(x)
106
+ x = self.relu(x)
107
+ # no maxpool
108
+
109
+ x = self.layer1(x)
110
+ x = self.layer2(x)
111
+ x = self.layer3(x)
112
+ x = self.layer4(x)
113
+
114
+ x = self.avgpool(x)
115
+ x = torch.flatten(x, 1)
116
+ x = self.fc(x)
117
+ return x
118
+
119
+
120
+ def get_model(num_classes=100):
121
+ return ResNetCIFAR(Bottleneck, [3, 4, 6, 3], num_classes=num_classes)