-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
165 lines (138 loc) · 5.49 KB
/
resnet.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
__author__="xu hongtao"
__email__="[email protected]"
from res_block import basic_2d,bottlneck_2d
import torch.nn as nn
import torch.nn.functional as F
import torch
class ResNet(nn.Module):
def __init__(self,
block,#残差块
blocks,#每个stage的残差块数量
include_top=True,#是否包含分类层头部
class_num=1000,#分类类别个数
per_block_exp=1):#使用的残差块通道扩展倍数,basic块是1,bottleneck块是4
super(ResNet,self).__init__()
self.pre=nn.Sequential(
nn.Conv2d(3, 64, (7,7), stride=(2,2), padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d((3,3), stride=(2,2),padding=1)
)
self.blocks=blocks
self.include_top=include_top
self.class_num=class_num
self.per_block_exp=per_block_exp
layers=[]
in_features=64
out_features=64
for stage_id,iterations in enumerate(self.blocks):
for block_id in range(iterations):
if block_id==0 and stage_id>0:
in_features=out_features*self.per_block_exp//2
elif block_id==0 and stage_id==0:
in_features=out_features
elif block_id>0 :
in_features=out_features*self.per_block_exp
# self.__dict__["stage%s_block%s"%(str(stage_id),str(block_id))]=block(in_features,out_features,stage=stage_id,block=block_id)
layers.append(block(in_features,out_features,stage=stage_id,block=block_id))
if stage_id!=(len(self.blocks)-1):
out_features*=2
self.layers=nn.Sequential(*layers)
if self.include_top:
assert self.class_num>0
self.fc_with_softmax=nn.Sequential(
nn.Linear(out_features*self.per_block_exp,self.class_num),
nn.Softmax()
)
self.initialize_weights()
def forward(self,x):
x=self.pre(x)
if self.include_top:
x=self.layers(x)
x=F.adaptive_avg_pool2d(x,1)
x=x.view(x.size(0),-1)
x=self.fc_with_softmax(x)
return x
else:
output=[]
n=0
for i ,num in enumerate(self.blocks):
x=self.layers[n:n+num](x)
n+=num
# print("x:%d"%(i),x.size(),n)
output.append(x)
return output
def initialize_weights(self):
for m in self.modules():
if isinstance(m,nn.Conv2d):
torch.nn.init.xavier_normal_(m.weight.data)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m,nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m,nn.Linear):
torch.nn.init.normal_(m.weight.data,0,0.01)
m.bias.data.zero_()
class ResNet18(ResNet):
def __init__(self,
blocks=None,#每个stage的残差块数量
include_top=True,
class_num=1000,
):
if blocks is None:
blocks=[2,2,2,2]
super(ResNet18,self).__init__(basic_2d,
blocks,
include_top=include_top,
class_num=class_num,
per_block_exp=1)
class ResNet34(ResNet):
def __init__(self,
blocks=None,#每个stage的残差块数量
include_top=True,
class_num=1000,
):
if blocks is None:
blocks=[3,4,6,3]
super(ResNet34,self).__init__(basic_2d,
blocks,
include_top=include_top,
class_num=class_num,
per_block_exp=1)
class ResNet50(ResNet):
def __init__(self,
blocks=None,#每个stage的残差块数量
include_top=True,
class_num=1000,
):
if blocks is None:
blocks=[3,4,6,3]
super(ResNet50,self).__init__(bottlneck_2d,
blocks,
include_top=include_top,
class_num=class_num,
per_block_exp=4)
class ResNet101(ResNet):
def __init__(self,
blocks=None,#每个stage的残差块数量
include_top=True,
class_num=1000,
):
if blocks is None:
blocks=[3,4,23,3]
super(ResNet101,self).__init__(bottlneck_2d,
blocks,
include_top=include_top,
class_num=class_num,
per_block_exp=4)
if __name__=="__main__":
import cv2 ,torch
import numpy as np
input1=np.ones((128,128,3))
input1=input1[np.newaxis,...]
input1=torch.Tensor(input1)
input1=input1.permute(0,3,1,2)
net=ResNet18(include_top=True,class_num=3)
out=net(input1)
print(out)