Pytorch搭建ResNet

Pytorch搭建ResNet

1、网络架构

ResNet的网络架构这里就不做过多解释,论文原文网络结构如下图,详细可以参照你必须要知道CNN模型:ResNet

preview

2、环境搭建

pytorch版本:1.10.2

python版本:3.6.15

pytorch的安装教程可以参照pytorch的安装和入门使用

3、模型搭建

3.1 定义ResNet[18,34]基础残差块BasicBlock

img

  1. expansion用来区分残差结构中不同层卷积核的个数,(50,101,152)的残差块中的第三层卷积和个数时是第一层和第二层的4倍。

    class BasicBlock(nn.Module):
        expansion = 1
        # 用来区分残差结构中不同层卷积核的个数
        # (50,101,152的残差块中的第三层卷积和个数时是第一层和第二层的4倍,这里就应该写4)
    
  2. 在init函数中初始化残差块需要用到的结构

    • in_channel:残差块输入的通道数

    • out_channel:残差块输出的通道数

    • stride:卷积核移动的步长

    • downsample:下采样方法,默认为空(例如:网络架构中conv2.x的输出为[56,56,64],但是conv3.x中需要的输入为[28,28,128],所以需要下采样,对应下图虚线处的残差结构)

      image-20220313142847264

    def __init__(self, in_channel, out_channel, stride=1, downsample=None):
            super(BasicBlock, self).__init__()
            self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=3, stride=stride,
                                   padding=1,
                                   bias=False)
            self.bn1 = nn.BatchNorm2d(out_channel)
            self.relu = nn.ReLU()
            self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel, kernel_size=3, stride=1, padding=1,
                                   bias=False)
            self.bn2 = nn.BatchNorm2d(out_channel)
            self.downsample = downsample
    
    
  3. 编写forward函数,定义模型的前向传输过程

    • identity用来表示残差结构的支线
    • out表示残差结构的主线
    def forward(self, x):
            identity = x
            if self.downsample is not None:
                identity = self.downsample(x)
    
            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu(out)
    
            out = self.conv2(out)
            out = self.bn2(out)
    
            out += identity
            out = self.relu(out)
    
            return out
    

3.2 定义ResNet[50,101,152]的基础残差块Bottleneck

image-20220313144022420

  1. 50,101,152的残差块中的第三层卷积和个数时是第一层和第二层的4倍,因此定义expansion为4

    class Bottleneck(nn.Module):
        expansion = 4
    
  2. 在init函数中初始化残差块需要用到的结构

        def __init__(self, in_channel, out_channel, stride=1, downsample=None):
            super(Bottleneck, self).__init__()
            self.conv1 = nn.Conv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=1, stride=stride,
                                   bias=False)
            self.bn1 = nn.BatchNorm2d(out_channel)
    
            self.conv2 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel, kernel_size=3, stride=1, padding=1,
                                   bias=False)
            self.bn2 = nn.BatchNorm2d(out_channel)
    
            self.conv3 = nn.Conv2d(in_channels=out_channel, out_channels=out_channel * self.expansion, kernel_size=1,
                                   stride=1, bias=False)
            self.bn3 = nn.BatchNorm2d(out_channel * self.expansion)
            self.relu = nn.ReLU(inplace=True)
    
            self.downsample = downsample
    
  3. 编写forward函数,定义模型的前向传输过程

        def forward(self, x):
            identity = x
            if self.downsample is not None:
                identity = self.downsample(x)
    
            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu(out)
    
            out = self.conv2(out)
            out = self.bn2(out)
            out = self.relu(out)
    
            out = self.conv3(out)
            out = self.bn3(out)
    
            out += identity
            out = self.relu(out)
    
            return out
    

3.3 定义ResNet整体网络结构

  1. 定义_make_layer函数:用来构建网络架构中的(conv2.x,conv3.x,conv4.x,conv5.x)

    • block:基础残差结构(根据定义的网络层数不同,传入不同的残差结构,[18,34]传BasicBlock,[50,101,152]传Bottleneck)
    • channel:残差结构中第一个卷积层使用的卷积核的个数
    • block_num:该层包含多少个残差结构
    • stride:卷积和移动的步距
    • 什么时候分支需要进行下采样:
      • 对于所有的resnet结构,在conv2.x—>conv3.x—>conv4.x—>conv5.x的途中分支都需要进行下采样(stride=2)
      • 对于resnet[50,101,152]网络结构来说,从maxpool层进入conv2.x时分支也需要进行一次“下采样”,这次“下采样”只改变深度,不改变高度和宽度(stride=1)。
        def __make_layer(self, block, channel, block_num, stride=1):
            downsample = None
            if stride != 1 or self.in_channel != channel * block.expansion:  # 判断是否进行下采样
                downsample = nn.Sequential(
                    nn.Conv2d(self.in_channel, channel * block.expansion, kernel_size=1, stride=stride, bias=False),
                    nn.BatchNorm2d(channel * block.expansion)
                )
            layers = []
            layers.append(block(self.in_channel, channel, downsample=downsample, stride=stride))  # 添加第一个残差基础块
            self.in_channel = channel * block.expansion
            # 对于18,34层的网络,经过第一个残差块后,输出的特征矩阵通道数与第一层的卷积层个数一样
            # 对于50,101,152层的网络,经过第一个残差块后,输出的特征矩阵通道数时第一个卷积层的4倍,因此要将后续残差块的输入特征矩阵通道数调整过来
            for _ in range(1, block_num):  # 添加后续的基础残差模块,后续的基础模块都不需要进行下采样操作
                layers.append(block(self.in_channel, channel))
    
            return nn.Sequential(*layers)
    
  2. 在init函数中初始化网络需要用到的结构

    • block:基础残差结构
    • block_num:列表参数,标注所使用残差结构的数目,对应网络架构图中的数目
    • num_classes:训练集分类个数
    • include_top:resnet网络上接其他网络,构成更复杂的网络
        def __init__(self, block, block_num, num_classes=1000, include_top=True):
            super(ResNet, self).__init__()
            self.include_top = include_top
            self.in_channel = 64  # maxpooling之后得到的特征矩阵的深度
    
            self.conv1 = nn.Conv2d(in_channels=3, out_channels=self.in_channel, kernel_size=7, stride=2, padding=3,
                                   bias=False)
            self.bn1 = nn.BatchNorm2d(self.in_channel)
            self.relu = nn.ReLU(inplace=True)
            self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
    
            self.layer1 = self.__make_layer(block, 64, block_num[0])
            self.layer2 = self.__make_layer(block, 128, block_num[1], stride=2)
            self.layer3 = self.__make_layer(block, 256, block_num[2], stride=2)
            self.layer4 = self.__make_layer(block, 512, block_num[3], stride=2)
            if self.include_top:
                self.avgpool = nn.AdaptiveAvgPool2d(1, 1)
                self.fc = nn.Linear(512 * block.expansion, num_classes)
    
            for m in self.modules():  # 初始化模型参数
                if isinstance(m, nn.Conv2d):
                    nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
    
  3. 编写forward函数,定义模型的前向传输过程

        def forward(self, x):
            x = self.conv1(x)
            x = self.bn1(x)
            x = self.relu(x)
            x = self.maxpool(x)
    
            x = self.layer1(x)
            x = self.layer2(x)
            x = self.layer3(x)
            x = self.layer4(x)
    
            if self.include_top:
                x = self.avgpool(x)
                x = torch.flatten(x, 1)
                x = self.fc(x)
    
            return x
    
  4. 定义resnet不同层数的网络

    def resnet18(num_classes=1000, include_top=True):
        return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=num_classes, include_top=include_top)
    
    
    def resnet34(num_classes=1000, include_top=True):
        return ResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)
    
    
    def resnet50(num_classes=1000, include_top=True):
        return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)
    
    
    def resnet101(num_classes=1000, include_top=True):
        return ResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, include_top=include_top)
    
    
    def resnet34(num_classes=1000, include_top=True):
        return ResNet(Bottleneck, [3, 8, 26, 3], num_classes=num_classes, include_top=include_top)
    

4、训练

4.1 下载官方提供的ResNet网络的与训练模型参数

model_urls = {
    'resnet18': 'https://download.pytorch.org/models/resnet18-f37072fd.pth',
    'resnet34': 'https://download.pytorch.org/models/resnet34-b627a593.pth',
    'resnet50': 'https://download.pytorch.org/models/resnet50-0676ba61.pth',
    'resnet101': 'https://download.pytorch.org/models/resnet101-63fe2227.pth',
    'resnet152': 'https://download.pytorch.org/models/resnet152-394f9c45.pth',
    'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth',
    'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth',
    'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
    'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth',
}

4.2 数据集准备

  1. 获取花分类数据集,放在data_set/flower_data文件夹下

  2. 使用split.py划分训练集和测试集

    import os
    from shutil import copy, rmtree
    import random
    
    
    def mk_file(file_path: str):
        if os.path.exists(file_path):
            # 如果文件夹存在,则先删除原文件夹在重新创建
            rmtree(file_path)
        os.makedirs(file_path)
    
    
    def main():
        # 保证随机可复现
        random.seed(0)
    
        # 将数据集中10%的数据划分到验证集中
        split_rate = 0.1
    
        # 指向你解压后的flower_photos文件夹
        cwd = os.getcwd()
        data_root = os.path.join(cwd, "flower_data")
        origin_flower_path = os.path.join(data_root, "flower_photos")
        assert os.path.exists(origin_flower_path), "path '{}' does not exist.".format(origin_flower_path)
    
        flower_class = [cla for cla in os.listdir(origin_flower_path)
                        if os.path.isdir(os.path.join(origin_flower_path, cla))]
    
        # 建立保存训练集的文件夹
        train_root = os.path.join(data_root, "train")
        mk_file(train_root)
        for cla in flower_class:
            # 建立每个类别对应的文件夹
            mk_file(os.path.join(train_root, cla))
    
        # 建立保存验证集的文件夹
        val_root = os.path.join(data_root, "val")
        mk_file(val_root)
        for cla in flower_class:
            # 建立每个类别对应的文件夹
            mk_file(os.path.join(val_root, cla))
    
        for cla in flower_class:
            cla_path = os.path.join(origin_flower_path, cla)
            images = os.listdir(cla_path)
            num = len(images)
            # 随机采样验证集的索引
            eval_index = random.sample(images, k=int(num*split_rate))
            for index, image in enumerate(images):
                if image in eval_index:
                    # 将分配至验证集中的文件复制到相应目录
                    image_path = os.path.join(cla_path, image)
                    new_path = os.path.join(val_root, cla)
                    copy(image_path, new_path)
                else:
                    # 将分配至训练集中的文件复制到相应目录
                    image_path = os.path.join(cla_path, image)
                    new_path = os.path.join(train_root, cla)
                    copy(image_path, new_path)
                print("\r[{}] processing [{}/{}]".format(cla, index+1, num), end="")  # processing bar
            print()
    
        print("processing done!")
    
    
    if __name__ == '__main__':
        main()
    

    image-20220313172708867

4.3 构建train.py文件

  1. 定义数据标准化处理方式(这里的normalize的参数为官方提供的参数)

        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])])}
    
  2. 载入数据集

        data_root = os.path.abspath(os.path.join(os.getcwd(), "../.."))  # get data root path
        image_path = os.path.join(data_root, "data_set", "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"])
        train_num = len(train_dataset)
        
        # {'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)
            
        batch_size = 16
    
        train_loader = torch.utils.data.DataLoader(train_dataset,
                                                   batch_size=batch_size, shuffle=True,
                                                   num_workers=0)
    
        validate_dataset = datasets.ImageFolder(root=os.path.join(image_path, "val"),
                                                transform=data_transform["val"])
        val_num = len(validate_dataset)
        validate_loader = torch.utils.data.DataLoader(validate_dataset,
                                                      batch_size=batch_size, shuffle=False,
                                                      num_workers=0)
    
        print("using {} images for training, {} images for validation.".format(train_num,
                                                                               val_num))
    
  3. 初始化网络模型并载入预训练参数(由于使用cpu进行训练,为了节省时间使用了迁移学习的方法)

    	net = resnet34()
        model_weight_path = "./pth/resnet34-pre.pth"
        assert os.path.exists(model_weight_path), "file {} does not exist.".format(model_weight_path)
        net.load_state_dict(torch.load(model_weight_path, map_location='cpu'))
        # for param in net.parameters():
        #     param.requires_grad = False
    
  4. 修改最后全连接层的输出类别数量(这里只预测5类)

        # change fc layer structure
        in_channel = net.fc.in_features
        net.fc = nn.Linear(in_channel, 5)
        net.to(device)
    
  5. 定义损失函数和优化器

    
        # define loss function
        loss_function = nn.CrossEntropyLoss()
    
        # construct an optimizer
        params = [p for p in net.parameters() if p.requires_grad]
        optimizer = optim.Adam(params, lr=0.0001)
    
    
  6. 定义一些初始化参数(epoch,模型参数保存路径等)

        epochs = 3
        best_acc = 0.0
        save_path = './resNet34.pth'
        train_steps = len(train_loader)
    
  7. 开始训练,打印训练结果

        for epoch in range(epochs):
            # train
            net.train()
            running_loss = 0.0
            train_bar = tqdm(train_loader, file=sys.stdout)
            for step, data in enumerate(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')
    

    image-20220313205717661

5、预测

注意:预测时,对于数据的标准化处理方式要采用和训练时的一致的处理方式

python
import os
import json

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

from model import resnet34


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 = "./tulipa.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 = resnet34(num_classes=5).to(device)

    # load model weights
    weights_path = "./resNet34.pth"
    assert os.path.exists(weights_path), "file: '{}' dose not exist.".format(weights_path)
    model.load_state_dict(torch.load(weights_path, map_location=device))

    # prediction
    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()

image-20220313205831220

  • 11
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值