nragrawal commited on
Commit
c802290
·
1 Parent(s): d6e2ebe

Add network

Browse files
Files changed (1) hide show
  1. network.py +129 -0
network.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torchvision.models as models
4
+
5
+ class Bottleneck(nn.Module):
6
+ expansion = 4
7
+
8
+ def __init__(self, in_channels, out_channels, stride=1, downsample=None):
9
+ super(Bottleneck, self).__init__()
10
+
11
+ self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False)
12
+ self.bn1 = nn.BatchNorm2d(out_channels)
13
+
14
+ self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3,
15
+ stride=stride, padding=1, bias=False)
16
+ self.bn2 = nn.BatchNorm2d(out_channels)
17
+
18
+ self.conv3 = nn.Conv2d(out_channels, out_channels * self.expansion,
19
+ kernel_size=1, bias=False)
20
+ self.bn3 = nn.BatchNorm2d(out_channels * self.expansion)
21
+
22
+ self.relu = nn.ReLU(inplace=True)
23
+ self.downsample = downsample
24
+
25
+ def forward(self, x):
26
+ identity = x
27
+
28
+ out = self.conv1(x)
29
+ out = self.bn1(out)
30
+ out = self.relu(out)
31
+
32
+ out = self.conv2(out)
33
+ out = self.bn2(out)
34
+ out = self.relu(out)
35
+
36
+ out = self.conv3(out)
37
+ out = self.bn3(out)
38
+
39
+ if self.downsample is not None:
40
+ identity = self.downsample(x)
41
+
42
+ out += identity
43
+ out = self.relu(out)
44
+
45
+ return out
46
+
47
+ class ResNet50(nn.Module):
48
+ def __init__(self, num_classes=1000):
49
+ super(ResNet50, self).__init__()
50
+
51
+ self.in_channels = 64
52
+
53
+ # Initial layers
54
+ self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
55
+ self.bn1 = nn.BatchNorm2d(64)
56
+ self.relu = nn.ReLU(inplace=True)
57
+ self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
58
+
59
+ # Residual layers
60
+ self.layer1 = self._make_layer(64, 3)
61
+ self.layer2 = self._make_layer(128, 4, stride=2)
62
+ self.layer3 = self._make_layer(256, 6, stride=2)
63
+ self.layer4 = self._make_layer(512, 3, stride=2)
64
+
65
+ # Classification head
66
+ self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
67
+ self.fc = nn.Linear(512 * Bottleneck.expansion, num_classes)
68
+
69
+ # Weight initialization
70
+ self._initialize_weights()
71
+
72
+ def _make_layer(self, out_channels, blocks, stride=1):
73
+ downsample = None
74
+ if stride != 1 or self.in_channels != out_channels * Bottleneck.expansion:
75
+ downsample = nn.Sequential(
76
+ nn.Conv2d(self.in_channels, out_channels * Bottleneck.expansion,
77
+ kernel_size=1, stride=stride, bias=False),
78
+ nn.BatchNorm2d(out_channels * Bottleneck.expansion),
79
+ )
80
+
81
+ layers = []
82
+ layers.append(Bottleneck(self.in_channels, out_channels, stride, downsample))
83
+
84
+ self.in_channels = out_channels * Bottleneck.expansion
85
+ for _ in range(1, blocks):
86
+ layers.append(Bottleneck(self.in_channels, out_channels))
87
+
88
+ return nn.Sequential(*layers)
89
+
90
+ def _initialize_weights(self):
91
+ for m in self.modules():
92
+ if isinstance(m, nn.Conv2d):
93
+ nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
94
+ elif isinstance(m, nn.BatchNorm2d):
95
+ nn.init.constant_(m.weight, 1)
96
+ nn.init.constant_(m.bias, 0)
97
+
98
+ def forward(self, x):
99
+ x = self.conv1(x)
100
+ x = self.bn1(x)
101
+ x = self.relu(x)
102
+ x = self.maxpool(x)
103
+
104
+ x = self.layer1(x)
105
+ x = self.layer2(x)
106
+ x = self.layer3(x)
107
+ x = self.layer4(x)
108
+
109
+ x = self.avgpool(x)
110
+ x = torch.flatten(x, 1)
111
+ x = self.fc(x)
112
+
113
+ return x
114
+
115
+ def create_model(num_classes, pretrained=False):
116
+ """
117
+ Create a ResNet-50 model
118
+ Args:
119
+ num_classes: Number of output classes
120
+ pretrained: Whether to use pretrained weights from ImageNet
121
+ """
122
+ # Load model with or without pretrained weights
123
+ model = models.resnet50(pretrained=pretrained)
124
+
125
+ # Modify the final layer for our number of classes
126
+ num_ftrs = model.fc.in_features
127
+ model.fc = nn.Linear(num_ftrs, num_classes)
128
+
129
+ return model