This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest_dbblock.py
180 lines (146 loc) · 4.69 KB
/
test_dbblock.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: utf-8 -*-
"""
@date: 2021/7/28 下午6:10
@file: test_dbblock.py
@author: zj
@description:
"""
import torch
import torch.nn as nn
from torchvision.models import resnet50
from zcls.model.layers.diverse_branch_block import DiverseBranchBlock
from zcls.model.conv_helper import insert_dbblock, fuse_dbblock
def test_dbblock():
in_channels = 32
out_channels = 64
dilation = 1
groups = 1
# inputs == outputs
kernel_size = 3
stride = 1
padding = 1
dbblock = DiverseBranchBlock(in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups)
print(dbblock)
data = torch.randn(1, in_channels, 56, 56)
outputs = dbblock.forward(data)
_, _, h, w = data.shape[:4]
_, _, h2, w2 = outputs.shape[:4]
assert h == h2 and w == w2
# 下采样
kernel_size = 3
stride = 2
padding = 1
dbblock = DiverseBranchBlock(in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups)
print(dbblock)
data = torch.randn(1, in_channels, 56, 56)
outputs = dbblock.forward(data)
_, _, h, w = data.shape[:4]
_, _, h2, w2 = outputs.shape[:4]
assert h / 2 == h2 and w / 2 == w2
# 下采样 + 分组卷积
kernel_size = 3
stride = 2
padding = 1
groups = 8
dbblock = DiverseBranchBlock(in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups)
print(dbblock)
data = torch.randn(1, in_channels, 56, 56)
outputs = dbblock.forward(data)
_, _, h, w = data.shape[:4]
_, _, h2, w2 = outputs.shape[:4]
assert h / 2 == h2 and w / 2 == w2
# 下采样 + 深度卷积
kernel_size = 3
stride = 2
padding = 1
in_channels = 32
out_channels = 32
groups = 32
dbblock = DiverseBranchBlock(in_channels,
out_channels,
kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups)
print(dbblock)
data = torch.randn(1, in_channels, 56, 56)
outputs = dbblock.forward(data)
_, _, h, w = data.shape[:4]
_, _, h2, w2 = outputs.shape[:4]
assert h / 2 == h2 and w / 2 == w2
def test_dbb_helper():
in_channels = 32
out_channels = 64
dilation = 1
# 下采样 + 分组卷积
kernel_size = 3
stride = 2
padding = 1
groups = 8
model = nn.Sequential(
nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=(kernel_size, kernel_size),
stride=(stride, stride), padding=padding, dilation=(dilation, dilation), groups=groups),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
print(model)
data = torch.randn(1, in_channels, 56, 56)
model.train()
insert_dbblock(model)
print(model)
model.eval()
train_outputs = model(data)
model.train()
fuse_dbblock(model)
model.eval()
eval_outputs = model(data)
print(model)
print(torch.sqrt(torch.sum((train_outputs - eval_outputs) ** 2)))
print(torch.allclose(train_outputs, eval_outputs, atol=1e-6))
assert torch.allclose(train_outputs, eval_outputs, atol=1e-6)
def test_resnet50_dbb():
model = resnet50()
print('src model: ' + '*' * 10)
print(model)
data = torch.randn(1, 3, 224, 224)
insert_dbblock(model)
model.eval()
train_outputs = model(data)
print('inserted model: ' + '*' * 10)
print(model)
model.train()
fuse_dbblock(model)
model.eval()
eval_outputs = model(data)
print('fused model: ' + '*' * 10)
print(model)
print(torch.sqrt(torch.sum((train_outputs - eval_outputs) ** 2)))
print(torch.allclose(train_outputs, eval_outputs, atol=1e-4))
assert torch.allclose(train_outputs, eval_outputs, atol=1e-4)
if __name__ == '__main__':
# test_dbblock()
# test_dbb_helper()
test_resnet50_dbb()