Spaces:
Sleeping
Sleeping
balajib197
commited on
Commit
•
a72e0bf
1
Parent(s):
06828c6
Create resnet.py
Browse files
resnet.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from pytorch_lightning import LightningModule
|
5 |
+
|
6 |
+
class BasicBlock(LightningModule):
|
7 |
+
expansion = 1
|
8 |
+
|
9 |
+
def __init__(self, in_planes, planes, stride=1):
|
10 |
+
super().__init__()
|
11 |
+
self.conv1 = torch.nn.Conv2d(in_planes, planes, (3,3), stride=stride, padding=1, bias=False)
|
12 |
+
self.bn1 = torch.nn.BatchNorm2d(planes)
|
13 |
+
self.conv2 = torch.nn.Conv2d(planes, planes, (3,3), stride=1, padding=1, bias=False)
|
14 |
+
self.bn2 = torch.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 |
+
torch.nn.Conv2d(in_planes, self.expansion*planes, (1,1), stride=stride, bias=False),
|
20 |
+
torch.nn.BatchNorm2d(self.expansion*planes)
|
21 |
+
)
|
22 |
+
|
23 |
+
def forward(self, x):
|
24 |
+
out = torch.relu(self.bn1(self.conv1(x)))
|
25 |
+
out = self.bn2(self.conv2(out))
|
26 |
+
out += self.shortcut(x)
|
27 |
+
out = torch.relu(out)
|
28 |
+
return out
|
29 |
+
|
30 |
+
class LitResnet(LightningModule):
|
31 |
+
def __init__(self, block, num_blocks, num_classes=10, lr=0.05):
|
32 |
+
super().__init__()
|
33 |
+
|
34 |
+
self.save_hyperparameters()
|
35 |
+
self.in_planes = 64
|
36 |
+
|
37 |
+
self.conv1 = torch.nn.Conv2d(3, 64, (3,3), stride=1, padding=1, bias=False)
|
38 |
+
self.bn1 = torch.nn.BatchNorm2d(64)
|
39 |
+
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)
|
40 |
+
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)
|
41 |
+
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)
|
42 |
+
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)
|
43 |
+
self.linear = torch.nn.Linear(512*block.expansion, num_classes)
|
44 |
+
|
45 |
+
def _make_layer(self, block, planes, num_blocks, stride):
|
46 |
+
strides = [stride] + [1]*(num_blocks-1)
|
47 |
+
layers = []
|
48 |
+
for stride in strides:
|
49 |
+
layers.append(block(self.in_planes, planes, stride))
|
50 |
+
self.in_planes = planes * block.expansion
|
51 |
+
return torch.nn.Sequential(*layers)
|
52 |
+
|
53 |
+
def forward(self, x):
|
54 |
+
out = torch.relu(self.bn1(self.conv1(x)))
|
55 |
+
out = self.layer1(out)
|
56 |
+
out = self.layer2(out)
|
57 |
+
out = self.layer3(out)
|
58 |
+
out = self.layer4(out)
|
59 |
+
out = F.avg_pool2d(out, 4)
|
60 |
+
out = out.view(out.size(0), -1)
|
61 |
+
# out = self.linear(out)
|
62 |
+
return F.log_softmax(out, dim=1)
|
63 |
+
|
64 |
+
|
65 |
+
# def ResNet18():
|
66 |
+
# return ResNet(BasicBlock, [2, 2, 2, 2])
|
67 |
+
|
68 |
+
|
69 |
+
# def ResNet34():
|
70 |
+
# return ResNet(BasicBlock, [3, 4, 6, 3])
|