chainer-图像分类-MobileNetV2代码重构【附源码】

文章目录

  • 前言
  • 代码实现
  • 调用方式


前言

本文基于chainer实现MobileNetV2网络结构,并基于torch的结构方式构建chainer版的,并计算MobileNetV2的参数量。


代码实现


class ConvBNReLU(chainer.Chain):
    def __init__(self, in_channel, out_channel, kernel_size=3, stride=1, groups=1):
        super(ConvBNReLU, self).__init__()
        padding = (kernel_size - 1) // 2
        self.layers = []
        self.layers += [('conv',L.Convolution2D(in_channels=in_channel,out_channels=out_channel,ksize=kernel_size,stride=stride,pad=padding,groups=groups,nobias=True))]
        self.layers += [('bn',L.BatchNormalization(out_channel))]
        self.layers += [('_relu6',ClippedReLU(6.0))]
        
        with self.init_scope():
            for n in self.layers:
                if not n[0].startswith('_'):
                    setattr(self, n[0], n[1])
    
    def __call__(self, x):
        for n, f in self.layers:
            if not n.startswith('_'):
                x = getattr(self, n)(x)
            else:
                x = f.apply((x,))[0]
        return x

# 倒残差结构
class InvertedResidual(chainer.Chain):
    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

        self.layers = []
        if expand_ratio != 1:
            self.layers += [('conv_bn_relu',ConvBNReLU(in_channel, hidden_channel, kernel_size=1))]
        self.layers += [('dw_conv',ConvBNReLU(hidden_channel, hidden_channel, stride=stride, groups=hidden_channel))]
        self.layers += [('pw_conv',L.Convolution2D(hidden_channel, out_channel, ksize=1, nobias=True))]
        self.layers += [('bn',L.BatchNormalization(out_channel))]

        with self.init_scope():
            for n in self.layers:
                if not n[0].startswith('_'):
                    setattr(self, n[0], n[1])
        
    def __call__(self, x):
        shortcut = x
        for n, f in self.layers:
            if not n.startswith('_'):
                x = getattr(self, n)(x)
            else:
                x = f.apply((x,))[0]
                
        if self.use_shortcut:
            return shortcut + x
        else:
            return x

def _make_divisible(ch, divisor=8, min_ch=None):
    if min_ch is None:
        min_ch = divisor
    new_ch = max(min_ch, int(ch + divisor / 2) // divisor * divisor)
    if new_ch < 0.9 * ch:
        new_ch += divisor
    return new_ch

class MobileNet_V2(chainer.Chain):
    cfgs={
        'mobilenetv2_1.0':{'alpha':1.0},
        'mobilenetv2_0.75':{'alpha':0.75},
        'mobilenetv2_0.5':{'alpha':0.5},
        'mobilenetv2_0.25':{'alpha':0.25}
    }
    def __init__(self, model_name='mobilenetv2_1.0',num_classes=1000,batch_size=4,image_size=224,round_nearest=8,**kwargs):
        super(MobileNet_V2, self).__init__()
        self.image_size = image_size
        block = InvertedResidual
        input_channel = _make_divisible(32 * self.cfgs[model_name]['alpha'], round_nearest)
        last_channel = _make_divisible(1280 * self.cfgs[model_name]['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],
        ]

        self.layers = []
        # 第一个卷积层,input:224*224*3 output:112*112*32
        self.layers += [('conv_bn_relu1',ConvBNReLU(3, input_channel, stride=2))]
        output_size = int((self.image_size-3+2*((3 - 1) // 2))/2 +1)
        
        # 定义一系列block结构   input:112*112*32 output: 7*7*320
        inverted_residual_num=0
        for t, c, n, s in inverted_residual_setting:
            inverted_residual_num +=1
            output_channel = _make_divisible(c * self.cfgs[model_name]['alpha'], round_nearest)
            for i in range(n):
                stride = s if i == 0 else 1   # 判断是否是block中的第一层,因为参数s只是表示第一层的stride,其他层都是1
                self.layers += [('inverted_residual{0}_block{1}'.format(inverted_residual_num,i),block(input_channel, output_channel, stride, expand_ratio=t))]
                input_channel = output_channel
                output_size = math.ceil(output_size/stride)
        # 定义bottlenet的下边一层    input:7*7*320 output: 7*7*1280
        self.layers += [('conv_bn_relu2',ConvBNReLU(input_channel, last_channel, 1))]
        output_size = math.ceil((output_size-1+2*((1 - 1) // 2))/1 +1)
        
        # input:7*7*1280 output: 1*1*1280
        self.layers += [('_avgpool',AveragePooling2D(ksize=output_size,stride=2,pad=0))]

        self.layers += [('_reshape',Reshape((batch_size,last_channel)))]
        
        self.layers += [("_dropout1",Dropout(0.2))]
        
        self.layers += [('fc',L.Linear(last_channel, num_classes))]
        
        with self.init_scope():
            for n in self.layers:
                if not n[0].startswith('_'):
                    setattr(self, n[0], n[1])

    def __call__(self, x):
        for n, f in self.layers:
            origin_size = x.shape
            if not n.startswith('_'):
                x = getattr(self, n)(x)
            else:
                x = f.apply((x,))[0]
            print(n,origin_size,x.shape)
        if chainer.config.train:
            return x
        return F.softmax(x)

注意此类就是MobileNetV2的实现过程,注意网络的前向传播过程中,分了训练以及测试。
训练过程中直接返回x,测试过程中会进入softmax得出概率

调用方式


if __name__ == '__main__':
    batch_size = 4
    n_channels = 3
    image_size = 224
    num_classes = 123
    
    model = MobileNet_V2(num_classes=num_classes, channels=n_channels,image_size=image_size,batch_size=batch_size)
    print("参数量",model.count_params())

    x = np.random.rand(batch_size, n_channels, image_size, image_size).astype(np.float32)
    t = np.random.randint(0, num_classes, size=(batch_size,)).astype(np.int32)
    with chainer.using_config('train', True):
        y1 = model(x)
    loss1 = F.softmax_cross_entropy(y1, t)

chainer-图像分类-MobileNetV2代码重构【附源码】_第1张图片

你可能感兴趣的:(深度学习-chainer,分类,重构,python,chainer,mobilenetV2)