feature map经卷积后的大小:
conv:
h
−
f
+
2
p
s
+
1
\frac{h-f+2p}{s}+1
sh−f+2p+1 —
f
f
f: kernel size;
p
p
p: padding;
s
s
s: stride
dilated conv:
h
+
2
p
−
(
f
−
1
)
×
d
s
+
1
\frac{h+2p-(f-1)×d}{s}+1
sh+2p−(f−1)×d+1 —
d
d
d: dilated
pooling:
h
−
f
s
+
1
\frac{h-f}{s}+1
sh−f+1
receptive filed计算(从前往后):
L
k
=
L
k
−
1
+
(
f
k
−
1
)
×
∏
i
=
1
k
−
1
s
i
L_k=L_{k-1} + (f_k-1)×\displaystyle\prod_{i=1}^{k-1} s_i
Lk=Lk−1+(fk−1)×i=1∏k−1si
其中:
L
k
−
1
L_{k-1}
Lk−1–上一层的感受野,
f
k
f_k
fk—核大小,
s
i
s_i
si—stride
普通卷积
参数量是参与计算参数的个数,占用内存空间。
考虑输入通道
C
i
n
C_{in}
Cin和输出通道
C
o
u
t
C_{out}
Cout, 参数量
(
C
i
n
×
(
K
×
K
)
+
1
)
×
C
o
u
t
(C_{in}×(K×K)+1)×C_{out}
(Cin×(K×K)+1)×Cout
计算量(乘加次数)
MAC(Multiply Accumulate),需要考虑输出map的大小,1个MAC算两次操作。
考虑输入通道
C
i
n
C_{in}
Cin和输出通道
C
o
u
t
C_{out}
Cout,计算量
(
C
i
n
×
(
K
×
K
)
×
H
×
W
)
×
C
o
u
t
(C_{in}×(K×K)×H×W)×C_{out}
(Cin×(K×K)×H×W)×Cout
其中,
K
K
K—kernel size;
H
,
W
H,W
H,W—the size of output feature map;
FLOPS:注意全大写,是floating point operations per second的缩写,意指每秒浮点运算次数,理解为计算速度。----是一个衡量硬件性能的指标。
FLOPs:注意s小写,是floating point operations的缩写,意指浮点运算数,理解为计算量。----衡量算法/模型的复杂度。
不考虑activation function的运算:
则卷积层:
(
2
×
C
i
n
×
K
2
−
1
)
×
H
×
W
×
C
o
u
t
(2×C_{in}×K^2-1)×H×W×C_{out}
(2×Cin×K2−1)×H×W×Cout <不考虑bias时有-1,有bias时没有-1>
其中,
C
i
n
C_{in}
Cin—input channel;
C
o
u
t
C_{out}
Cout—output channel;
K
K
K—kernel size;
H
,
W
H,W
H,W—the size of output feature map; 2表示一个MAC操作。
全连接层:
(
2
×
I
−
1
)
×
O
(2×I-1)×O
(2×I−1)×O
其中,
I
I
I—input neuron numbers;
O
O
O—output neuron numbers。
深度可分离卷积
对于不同的输入channel采取不同的卷积核进行卷积,它将普通的卷积操作分解为
Depthwise 过程(指将 N×H×W×C的输入分为 group=c 组,然后每一组做 3×3 卷积。这样相当于收集了每个Channel的空间特征,即Depthwise特征)
和Pointwise 过程(对 N×H×W×C 的输入做 n个普通的 1×1 卷积。这样相当于收集了每个点的特征,即Pointwise特征)。
Depthwise+Pointwise最终输出也是 N×H× W× n。
Depthwise计算量:
C
i
n
×
H
×
W
×
K
2
C_{in}×H×W×K^2
Cin×H×W×K2 --K=3
Pointwise计算量:
C
i
n
×
H
×
W
×
C
o
u
t
C_{in}×H×W×C_{out}
Cin×H×W×Cout
相当于将普通卷积的计算量压缩为:
D
e
p
t
h
w
i
s
e
+
P
o
i
n
t
w
i
s
e
C
o
n
v
=
C
i
n
×
H
×
W
×
K
2
+
C
i
n
×
H
×
W
×
C
o
u
t
C
i
n
×
K
2
×
H
×
W
×
C
o
u
t
=
1
C
o
u
t
+
1
K
2
\frac{Depthwise+Pointwise}{Conv}=\frac{C_{in}×H×W×K^2+C_{in}×H×W×C_{out}}{C_{in}×K^2×H×W×C_{out}}=\frac{1}{C_{out}}+\frac{1}{K^2}
ConvDepthwise+Pointwise=Cin×K2×H×W×CoutCin×H×W×K2+Cin×H×W×Cout=Cout1+K21
- 有一个基于pytorch的torchstat包,可以计算模型的FLOPs数,参数大小等指标。
安装------pip install torchstat
from torchstat import stat
stat(model, (3, 224, 224)) # 模型及输入的大小
虽然torchstat的功能十分强大,但是也有一些缺陷:
1. 限制模型输入仅能为图片
2. 限制模型每一个layer的输入须为单个变量
3. 对Pytorch-0.4.1及以下版本的支持不足
- thop
安装------pip install thop
from thop import profile
from thop import clever_format
input = torch.randn(1, 3, 224, 224)
flops, params = profile(model, inputs=(input, ))
print(flops, params) # 1819066368.0 11689512.0
flops, params = clever_format([flops, params], "%.3f")
print(flops, params) # 1.819G 11.690M
- torchsummary
pip install torchsummary
from torchsummary import summary
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Net.to(device)
summary(model, (3, 256,256))