【论文阅读】MobileNet ——MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

论文阅读

感谢p导

论文链接:MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

主要亮点有两个:1、Depthwise separable convlution(a depthwise convolution and a 1*1 convolution) replace standard convolution
2、添加了两个超参数:α、β
在这里插入图片描述
提出来一个有效的模型——mobilenet,用在移动设备和嵌入式设备上,使用深度可分离卷积,引进了两个超参数来权衡延迟和准确率
在这里插入图片描述
使用两个超参数来定义更小更有效的MobileNets

在这里插入图片描述
mobilenet 是基于深度可分离卷积实现的,深度可分离卷积是将普通卷积分为了两部分,一个逐通道卷积和一个1*1卷积
在这里插入图片描述

在这里插入图片描述
普通卷积的计算量:DK是卷积核大小,DF是输入的特征图的大小(本文中特征图长宽一致),M是输入特征图的通道数,N是输出特征图的通道数

在这里插入图片描述
在这里插入图片描述
变换成深度可分离卷积之后的计算量以及两者之比

知乎中关于深度可分离卷积的介绍,感觉挺好

两个超参数
在这里插入图片描述
第一个是改变channels
在这里插入图片描述
第二个是改变每个feature map的size

代码实现

因为霹雳吧啦这次没有实现v1的代码,自己按照论文中的模型表随便瞎写的
使用torchsummary输出模型
在这里插入图片描述
就按照论文中给出的网络结构图来实现即可
在这里插入图片描述

model

from torch import nn


class BasicConv2d_dw(nn.Module):
    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 stride: int):
        super(BasicConv2d_dw, self).__init__()
        self.conv_dw = nn.Sequential(
            nn.Conv2d(in_channels=in_channels, out_channels=in_channels,
                      groups=in_channels, kernel_size=3, stride=stride, padding=1,bias=False),
            nn.BatchNorm2d(in_channels),
            nn.ReLU6(inplace=True),

            nn.Conv2d(in_channels=in_channels, out_channels=out_channels,
                      kernel_size=1, stride=1, padding=0, bias=False),
            nn.BatchNorm2d(out_channels),
            nn.ReLU6(inplace=True),
        )

    def forward(self, x):
        return self.conv_dw(x)


class MobileNet_v1(nn.Module):
    def __init__(self, num_classes=1000):
        super().__init__()
        self.model = nn.Sequential(
            
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3,
                      stride=2, padding=1, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU6(inplace=True),

            BasicConv2d_dw(32, 64, 1),
            BasicConv2d_dw(64, 128, 2),
            BasicConv2d_dw(128, 128, 1),
            BasicConv2d_dw(128, 256, 2),
            BasicConv2d_dw(256, 256, 1),
            BasicConv2d_dw(256, 512, 2),
            BasicConv2d_dw(512, 512, 1),
            BasicConv2d_dw(512, 512, 1),
            BasicConv2d_dw(512, 512, 1),
            BasicConv2d_dw(512, 512, 1),
            BasicConv2d_dw(512, 512, 1),
            BasicConv2d_dw(512, 1024, 2),
            BasicConv2d_dw(1024, 1024, 1),

            nn.AvgPool2d(7),
        )

        self.fc = nn.Linear(1024, 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.model(x)
        x = x.view(-1, 1024)
        x = self.fc(x)
        return x

train

import os
import sys
import json

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import transforms, datasets
from tqdm import tqdm

from model_v1 import MobileNet_v1


def main():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print("using {} device.".format(device))

    batch_size = 16
    epochs = 20

    data_transform = {
        "train": transforms.Compose([transforms.RandomResizedCrop(224),
                                     transforms.RandomHorizontalFlip(),
                                     transforms.ToTensor(),
                                     transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]),
        "val": transforms.Compose([transforms.Resize(256),
                                   transforms.CenterCrop(224),
                                   transforms.ToTensor(),
                                   transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])}


    data_root = os.path.abspath(os.path.join(os.getcwd(), "../"))  # get data root path
    image_path = os.path.join(data_root, "flower_data")  # flower data set path
    assert os.path.exists(image_path), "{} path does not exist.".format(image_path)
    
    train_dataset = datasets.ImageFolder(root=os.path.join(image_path, "train"),
                                         transform=data_transform["train"])    
    validate_dataset = datasets.ImageFolder(root=os.path.join(image_path, "val"),
                                            transform=data_transform["val"])
    
    val_num = len(validate_dataset)
    train_num = len(train_dataset)
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=batch_size, shuffle=True)
    validate_loader = torch.utils.data.DataLoader(validate_dataset,
                                                  batch_size=batch_size, shuffle=False)

    print("using {} images for training, {} images for validation.".format(train_num,
                                                                           val_num))
    # {'daisy':0, 'dandelion':1, 'roses':2, 'sunflower':3, 'tulips':4}
    flower_list = train_dataset.class_to_idx
    cla_dict = dict((val, key) for key, val in flower_list.items())
    # write dict into json file
    json_str = json.dumps(cla_dict, indent=4)
    with open('class_indices.json', 'w') as json_file:
        json_file.write(json_str)   
    

    # create model
    net = MobileNet_v1(num_classes=5).to(device)
    # load pretrain weights,这里我没有去pytorch上下载预训练好的模型,只是有这部分代码,在一开始训练时候把这部分注释了,之后运行就可以取消注释
    model_weight_path = "./MobileNetV1.pth"
    assert os.path.exists(model_weight_path), "file {} dose not exist.".format(model_weight_path)
    pre_weights = torch.load(model_weight_path, map_location=device)
    # delete classifier weights
    pre_dict = {k: v for k, v in pre_weights.items() if net.state_dict()[k].numel() == v.numel()}
    missing_keys, unexpected_keys = net.load_state_dict(pre_dict, strict=False)
    
    # define loss function
    loss_function = nn.CrossEntropyLoss()
    # construct an optimizer
    optimizer = optim.Adam(net.parameters(), lr=0.0001)


    best_acc = 0.0
    save_path = './MobileNetV1.pth'
    train_steps = len(train_loader)
    for epoch in range(epochs):
        # train
        net.train()
        running_loss = 0.0
        train_bar = tqdm(train_loader)
        for data in train_bar:
            images, labels = data
            optimizer.zero_grad()
            logits = net(images.to(device))
            loss = loss_function(logits, labels.to(device))
            loss.backward()
            optimizer.step()

            # print statistics
            running_loss += loss.item()

            train_bar.desc = "train epoch[{}/{}] loss:{:.3f}".format(epoch + 1,
                                                                     epochs,
                                                                     loss)

        # validate
        net.eval()
        acc = 0.0  # accumulate accurate number / epoch
        with torch.no_grad():
            val_bar = tqdm(validate_loader, file=sys.stdout)
            for val_data in val_bar:
                val_images, val_labels = val_data
                outputs = net(val_images.to(device))
                # loss = loss_function(outputs, test_labels)
                predict_y = torch.max(outputs, dim=1)[1]
                acc += torch.eq(predict_y, val_labels.to(device)).sum().item()

                val_bar.desc = "valid epoch[{}/{}]".format(epoch + 1,
                                                           epochs)
        val_accurate = acc / val_num
        print('[epoch %d] train_loss: %.3f  val_accuracy: %.3f' %
              (epoch + 1, running_loss / train_steps, val_accurate))

        if val_accurate > best_acc:
            best_acc = val_accurate
            torch.save(net.state_dict(), save_path)

    print('Finished Training')


if __name__ == '__main__':
    main()

predict

import os
import json

import torch
from PIL import Image
from torchvision import transforms
import matplotlib.pyplot as plt

from model_v1 import MobileNet_v1


def main():
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    data_transform = transforms.Compose(
        [transforms.Resize(256),
         transforms.CenterCrop(224),
         transforms.ToTensor(),
         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

    # load image
    img_path = "../tulip.jpg"
    assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path)
    img = Image.open(img_path)
    plt.imshow(img)
    # [N, C, H, W]
    img = data_transform(img)
    # expand batch dimension
    img = torch.unsqueeze(img, dim=0)

    # read class_indict
    json_path = './class_indices.json'
    assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path)

    json_file = open(json_path, "r")
    class_indict = json.load(json_file)

    # create model
    model = MobileNet_v1(num_classes=5).to(device)
    # load model weights
    model_weight_path = "./MobileNetV1.pth"
    model.load_state_dict(torch.load(model_weight_path, map_location=device))
    model.eval()
    with torch.no_grad():
        # predict class
        output = torch.squeeze(model(img.to(device))).cpu()
        predict = torch.softmax(output, dim=0)
        predict_cla = torch.argmax(predict).numpy()

    print_res = "class: {}   prob: {:.3}".format(class_indict[str(predict_cla)],
                                                 predict[predict_cla].numpy())
    plt.title(print_res)
    for i in range(len(predict)):
        print("class: {:10}   prob: {:.3}".format(class_indict[str(i)],
                                                  predict[i].numpy()))
    plt.show()


if __name__ == '__main__':
    main()

实验结果

训练了20轮,在p导的花分类上面准确率74.5%
在这里插入图片描述
在这里插入图片描述

版权声明:本文为CSDN博主「每个人都是孙笑川」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41488595/article/details/122752071

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐