-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresnet.py
More file actions
145 lines (120 loc) · 4.39 KB
/
resnet.py
File metadata and controls
145 lines (120 loc) · 4.39 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
--------------------- RESNET FILE ---------------------
Author: Reza Tanakizadeh
Year : 2022
P_name: Sima face verification project
Desc : This file a an Inplemented of resnet network (Resnet18, ...)
-------------------------------------------------------
"""
import torch.nn as nn
# --- Define Residual block
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, downsample=1):
super().__init__()
# --- Variables
self.in_channels = in_channels
self.out_channels = out_channels
self.downsample = downsample
# --- Residual parts
# --- Conv part
self.blocks = nn.Sequential(OrderedDict(
{
# --- First Conv
'conv1' : nn.Conv2d(self.in_channels, self.out_channels, kernel_size=3, stride=self.downsample, padding=1, bias=False),
'bn1' : nn.BatchNorm2d(self.out_channels),
'Relu1' : nn.ReLU(),
# --- Secound Conv
'conv2' : nn.Conv2d(self.out_channels, self.out_channels, kernel_size=3, stride=1, padding=1, bias=False),
'bn2' : nn.BatchNorm2d(self.out_channels)
}
))
# --- shortcut part
self.shortcut = nn.Sequential(OrderedDict(
{
'conv' : nn.Conv2d(self.in_channels, self.out_channels, kernel_size=1, stride=self.downsample, bias=False),
'bn' : nn.BatchNorm2d(self.out_channels)
}
))
# --- Forward Inplementation
def forward(self, x):
residual = x
if (self.in_channels != self.out_channels) : residual = self.shortcut(x)
x = self.blocks(x)
x += residual
return x
# --- Make ResNet18
class ResNet18(nn.Module):
def __init__(self):
super().__init__()
# --- Pre layers with 7*7 conv with stride2 and a max-pooling
self.PreBlocks = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, padding=3, stride=2, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
# --- Define all Residual Blocks here
self.CoreBlocka = nn.Sequential(
ResidualBlock(64,64 ,downsample=1),
ResidualBlock(64,64 ,downsample=1),
ResidualBlock(64,128 ,downsample=2),
ResidualBlock(128,128 ,downsample=1),
ResidualBlock(128,256 ,downsample=2),
ResidualBlock(256,256 ,downsample=1),
ResidualBlock(256,512 ,downsample=2),
ResidualBlock(512,512 ,downsample=1)
)
# --- Make Average pooling
self.avg = nn.AdaptiveAvgPool2d((1,1))
# --- FC layer for output
self.fc = nn.Linear(512, 512, bias=False)
def forward(self, x):
x = self.PreBlocks(x)
x = self.CoreBlocka(x)
x = self.avg(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
x = F.normalize(x, p=2, dim=1)
return x
# --- Make ResNet18
class ResNet34(nn.Module):
def __init__(self):
super().__init__()
# --- Pre layers with 7*7 conv with stride2 and a max-pooling
self.PreBlocks = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, padding=3, stride=2, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
# --- Define all Residual Blocks here
self.CoreBlocka = nn.Sequential(
ResidualBlock(64,64 ,downsample=1),
ResidualBlock(64,64 ,downsample=1),
ResidualBlock(64,64 ,downsample=1),
ResidualBlock(64,128 ,downsample=2),
ResidualBlock(128,128 ,downsample=1),
ResidualBlock(128,128 ,downsample=1),
ResidualBlock(128,128 ,downsample=1),
ResidualBlock(128,256 ,downsample=2),
ResidualBlock(256,256 ,downsample=1),
ResidualBlock(256,256 ,downsample=1),
ResidualBlock(256,256 ,downsample=1),
ResidualBlock(256,256 ,downsample=1),
ResidualBlock(256,256 ,downsample=1),
ResidualBlock(256,512 ,downsample=2),
ResidualBlock(512,512 ,downsample=1),
ResidualBlock(512,512 ,downsample=1)
)
# --- Make Average pooling
self.avg = nn.AdaptiveAvgPool2d((1,1))
# --- FC layer for output
self.fc = nn.Linear(512, 512, bias=False)
def forward(self, x):
x = self.PreBlocks(x)
x = self.CoreBlocka(x)
x = self.avg(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
x = F.normalize(x, p=2, dim=1)
return x