chethan999 commited on
Commit
83c0733
·
verified ·
1 Parent(s): 6dc2b96

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +151 -0
model.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+
5
+
6
+ class BasicBlock(nn.Module):
7
+ expansion = 1
8
+
9
+ def __init__(self, in_planes, planes, stride=1):
10
+ super(BasicBlock, self).__init__()
11
+ self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
12
+ self.bn1 = nn.BatchNorm2d(planes)
13
+ self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
14
+ self.bn2 = nn.BatchNorm2d(planes)
15
+
16
+ self.shortcut = nn.Sequential()
17
+ if stride != 1 or in_planes != self.expansion * planes:
18
+ self.shortcut = nn.Sequential(
19
+ nn.Conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False),
20
+ nn.BatchNorm2d(self.expansion * planes)
21
+ )
22
+
23
+ def forward(self, x):
24
+ out = F.relu(self.bn1(self.conv1(x)))
25
+ out = self.bn2(self.conv2(out))
26
+ out += self.shortcut(x)
27
+ out = F.relu(out)
28
+ return out
29
+
30
+
31
+ class Bottleneck(nn.Module):
32
+ expansion = 4
33
+
34
+ def __init__(self, in_planes, planes, stride=1):
35
+ super(Bottleneck, self).__init__()
36
+ self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
37
+ self.bn1 = nn.BatchNorm2d(planes)
38
+ self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
39
+ self.bn2 = nn.BatchNorm2d(planes)
40
+ self.conv3 = nn.Conv2d(planes, self.expansion * planes, kernel_size=1, bias=False)
41
+ self.bn3 = nn.BatchNorm2d(self.expansion * planes)
42
+
43
+ self.shortcut = nn.Sequential()
44
+ if stride != 1 or in_planes != self.expansion * planes:
45
+ self.shortcut = nn.Sequential(
46
+ nn.Conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False),
47
+ nn.BatchNorm2d(self.expansion * planes)
48
+ )
49
+
50
+ def forward(self, x):
51
+ out = F.relu(self.bn1(self.conv1(x)))
52
+ out = F.relu(self.bn2(self.conv2(out)))
53
+ out = self.bn3(self.conv3(out))
54
+ out += self.shortcut(x)
55
+ out = F.relu(out)
56
+ return out
57
+
58
+
59
+ class ResNet(nn.Module):
60
+ def __init__(self, block, num_blocks, num_classes=100):
61
+ super(ResNet, self).__init__()
62
+ self.in_planes = 64
63
+
64
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
65
+ self.bn1 = nn.BatchNorm2d(64)
66
+
67
+ self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
68
+ self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
69
+ self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
70
+ self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
71
+
72
+ self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
73
+ self.fc = nn.Linear(512 * block.expansion, num_classes)
74
+
75
+ self._initialize_weights()
76
+
77
+ def _make_layer(self, block, planes, num_blocks, stride):
78
+ strides = [stride] + [1] * (num_blocks - 1)
79
+ layers = []
80
+ for stride in strides:
81
+ layers.append(block(self.in_planes, planes, stride))
82
+ self.in_planes = planes * block.expansion
83
+ return nn.Sequential(*layers)
84
+
85
+ def _initialize_weights(self):
86
+ for m in self.modules():
87
+ if isinstance(m, nn.Conv2d):
88
+ nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
89
+ elif isinstance(m, nn.BatchNorm2d):
90
+ nn.init.constant_(m.weight, 1)
91
+ nn.init.constant_(m.bias, 0)
92
+ elif isinstance(m, nn.Linear):
93
+ nn.init.normal_(m.weight, 0, 0.01)
94
+ nn.init.constant_(m.bias, 0)
95
+
96
+ def forward(self, x):
97
+ out = F.relu(self.bn1(self.conv1(x)))
98
+
99
+ out = self.layer1(out)
100
+ out = self.layer2(out)
101
+ out = self.layer3(out)
102
+ out = self.layer4(out)
103
+
104
+ out = self.avgpool(out)
105
+ out = out.view(out.size(0), -1)
106
+ out = self.fc(out)
107
+
108
+ return out
109
+
110
+
111
+ def ResNet18(num_classes=100):
112
+ return ResNet(BasicBlock, [2, 2, 2, 2], num_classes)
113
+
114
+
115
+ def ResNet34(num_classes=100):
116
+ return ResNet(BasicBlock, [3, 4, 6, 3], num_classes)
117
+
118
+
119
+ def ResNet50(num_classes=100):
120
+ return ResNet(Bottleneck, [3, 4, 6, 3], num_classes)
121
+
122
+
123
+ def ResNet101(num_classes=100):
124
+ return ResNet(Bottleneck, [3, 4, 23, 3], num_classes)
125
+
126
+
127
+ def ResNet152(num_classes=100):
128
+ return ResNet(Bottleneck, [3, 8, 36, 3], num_classes)
129
+
130
+
131
+ def count_parameters(model):
132
+ return sum(p.numel() for p in model.parameters() if p.requires_grad)
133
+
134
+
135
+ if __name__ == "__main__":
136
+ # Test different ResNet models
137
+ models = {
138
+ 'ResNet18': ResNet18(),
139
+ 'ResNet34': ResNet34(),
140
+ 'ResNet50': ResNet50()
141
+ }
142
+
143
+ for name, model in models.items():
144
+ total_params = count_parameters(model)
145
+ print(f"{name} - Total parameters: {total_params:,}")
146
+
147
+ # Test forward pass
148
+ x = torch.randn(1, 3, 32, 32)
149
+ output = model(x)
150
+ print(f"{name} - Output shape: {output.shape}")
151
+ print()