-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
60 lines (55 loc) · 2.16 KB
/
model.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
import torch
import torch.nn as nn
from module import ConvNorm, LinearNorm
class ResidualUnit(nn.Module):
def __init__(self, in_channel, out_channel, kernel= 3, down_sample= False):
super(ResidualUnit, self).__init__()
self.down_sample = down_sample
self.conv= nn.Sequential(
ConvNorm(in_channel, out_channel, kernel, stride=2 if down_sample else 1),
nn.BatchNorm2d(out_channel),
nn.ReLU(),
ConvNorm(out_channel, out_channel, kernel, 1),
nn.BatchNorm2d(out_channel),
nn.ReLU()
)
if self.down_sample:
self.x_conv = nn.Sequential(
ConvNorm(in_channel, out_channel, kernel, 2),
nn.BatchNorm2d(out_channel)
)
def forward(self, x):
f = self.conv(x)
if self.down_sample:
x = self.x_conv(x)
return f + x
class Resnet(nn.Module):
def __init__(self, n_class= 10):
super(Resnet, self).__init__()
self.prenet = nn.Sequential(
ConvNorm(3, 64, 7, 2),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(3, stride= 2),
)
resnet=[]
resnet += self.make_residual_block(64, 64, 3)
resnet += self.make_residual_block(64, 128, 4, down_sample= True)
resnet += self.make_residual_block(128, 256, 6, down_sample= True)
resnet += self.make_residual_block(256, 512, 3, down_sample= True)
self.resnet = nn.Sequential(*resnet)
self.pool = nn.AdaptiveAvgPool2d((1,1))
self.linear = LinearNorm(512, 10)
def make_residual_block(self, in_channel, out_channel, layer, down_sample=False):
layers = []
layers.append(ResidualUnit(in_channel, out_channel, down_sample= down_sample))
for i in range(1, layer):
layers.append(ResidualUnit(out_channel, out_channel, down_sample= False))
return layers
def forward(self, x):
B = x.shape[0]
x = self.prenet(x)
x = self.resnet(x)
x = self.pool(x)
out = self.linear(x.view(B, -1))
return out