1. def __init__(groups=1):-》group=1表示是普通卷積,group=2表示Depthwise(DW)卷積。
2.padding = (kernel_size - 1) // 2-》padding由kernel_size來決定。
3.然後定義網絡結構:
super(ConvBNReLU, self).__init__(
nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU6(inplace=True)
)
到這裡ConvBNReLU網絡結構定義完畢!
定義倒參差網絡結構
1.expand_ratio-》表示擴充因子。
2.hidden_channel = in_channel * expand_ratio中的hidden_channel表示輸出深度也是卷積核數量。
3.use_shortcut-》判斷是否在正向傳播過程中使用Mobile的捷徑分支。
4.stride == 1 and in_channel == out_channel-》判斷使用捷徑分支條件:stride == 1并且輸入深度等于輸出深度。
5. if expand_ratio != 1:-》判斷擴充因子是不是等于1,不等于1就添加一個1x1的卷積層。等于1的話就沒有1x1的卷積層。
接下來添加一系列的層結構:
layers.extend([
# 3x3 depthwise conv
ConvBNReLU(hidden_channel, hidden_channel, stride=stride, groups=hidden_channel),
# 1x1 pointwise conv(linear)
nn.Conv2d(hidden_channel, out_channel, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channel),
])
forward前向傳播:
def forward(self, x):
if self.use_shortcut:
return x + self.conv(x)
else:
return self.conv(x)
-》如果use_shortcut為TRUE的話使用捷徑分支,傳回卷積結果和捷徑分支的和。如果為FALSE傳回主分支卷積結果。
定義MobileNetV2的網絡結構:
_make_divisible()作用将卷積核個數調整到8的整數倍。
1.features.append(ConvBNReLU(3, input_channel, stride=2))-》添加第一個卷積層。
2.循環周遊bottleneck的參數清單:
for t, c, n, s in inverted_residual_setting:
output_channel = _make_divisible(c * alpha, round_nearest)
for i in range(n):
stride = s if i == 0 else 1-》stride如果是第一層指派s如果不是第一層指派為1
features.append(block(input_channel, output_channel, stride, expand_ratio=t))
input_channel = output_channel -》搭建每一個倒參差結構
3. features.append(ConvBNReLU(input_channel, last_channel, 1))-》搭建1x1的卷積層。
4.self.features = nn.Sequential(*features)-》特征提取層完畢,定義為features。
5.self.avgpool = nn.AdaptiveAvgPool2d((1, 1))-》平均池化
搭建分類層:
self.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(last_channel, num_classes)
)
前向傳播:
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
features特征提取層-》avgpool池化層-》flatten展平降維-》classifier全連接配接分類
from torch import nn
import torch
def _make_divisible(ch, divisor=8, min_ch=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
"""
if min_ch is None:
min_ch = divisor
new_ch = max(min_ch, int(ch + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_ch < 0.9 * ch:
new_ch += divisor
return new_ch
class ConvBNReLU(nn.Sequential):
def __init__(self, in_channel, out_channel, kernel_size=3, stride=1, groups=1):
padding = (kernel_size - 1) // 2
super(ConvBNReLU, self).__init__(
nn.Conv2d(in_channel, out_channel, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU6(inplace=True)
)
class InvertedResidual(nn.Module):
def __init__(self, in_channel, out_channel, stride, expand_ratio):
super(InvertedResidual, self).__init__()
hidden_channel = in_channel * expand_ratio
self.use_shortcut = stride == 1 and in_channel == out_channel
layers = []
if expand_ratio != 1:
# 1x1 pointwise conv
layers.append(ConvBNReLU(in_channel, hidden_channel, kernel_size=1))
layers.extend([
# 3x3 depthwise conv
ConvBNReLU(hidden_channel, hidden_channel, stride=stride, groups=hidden_channel),
# 1x1 pointwise conv(linear)
nn.Conv2d(hidden_channel, out_channel, kernel_size=1, bias=False),
nn.BatchNorm2d(out_channel),
])
self.conv = nn.Sequential(*layers)
def forward(self, x):
if self.use_shortcut:
return x + self.conv(x)
else:
return self.conv(x)
class MobileNetV2(nn.Module):
def __init__(self, num_classes=1000, alpha=1.0, round_nearest=8):
super(MobileNetV2, self).__init__()
block = InvertedResidual
input_channel = _make_divisible(32 * alpha, round_nearest)
last_channel = _make_divisible(1280 * alpha, round_nearest)
inverted_residual_setting = [
# t, c, n, s
[1, 16, 1, 1],
[6, 24, 2, 2],
[6, 32, 3, 2],
[6, 64, 4, 2],
[6, 96, 3, 1],
[6, 160, 3, 2],
[6, 320, 1, 1],
]
features = []
# conv1 layer
features.append(ConvBNReLU(3, input_channel, stride=2))
# building inverted residual residual blockes
for t, c, n, s in inverted_residual_setting:
output_channel = _make_divisible(c * alpha, round_nearest)
for i in range(n):
stride = s if i == 0 else 1
features.append(block(input_channel, output_channel, stride, expand_ratio=t))
input_channel = output_channel
# building last several layers
features.append(ConvBNReLU(input_channel, last_channel, 1))
# combine feature layers
self.features = nn.Sequential(*features)
# building classifier
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.classifier = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(last_channel, num_classes)
)
# weight initialization
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
nn.init.zeros_(m.bias)
elif isinstance(m, nn.BatchNorm2d):
nn.init.ones_(m.weight)
nn.init.zeros_(m.bias)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.zeros_(m.bias)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x
https://github.com/WZMIAOMIAO/deep-learning-for-image-processing