天天看點

Pytorch實戰使用ResNet50/101/152實作Cifar-10的分類任務

編譯器pycharm pytorch版本0.4 python版本3.6

為适應cifar-10中32×32的圖檔尺寸,對resnet中進行修改,所有層的channel數量都沒有進行修改,其中conv1中的(k=7,s=2,p=3)改為(3,1,1),conv2中的maxpool(3,2,1)改為(3,1,1),fc之前的avgpool也進行更改以适應conv5的輸出尺寸(4×4在本層輸出為1×1)。

最後加了tensorboard輸出loss和train acc、test acc曲線。

學習率衰減代碼,可調節stepsize與gamma,也可更換其他學習率衰減的方式,pytorch提供了六種

scheduler = lr_scheduler.StepLR(optimizer, step_size = 30, gamma = 0.2)
           

跟改下面一行代碼可以分别調用ResNet 50/101/152

model = ResNet50().to(device)

下面是全部代碼:

import os
import torch
import torchvision
import numpy as np
import matplotlib.pyplot as plt
from torch import nn, optim
from torch.autograd import Variable
from torch.optim import lr_scheduler
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from tensorboardX import SummaryWriter
import torch.nn.functional as F


__all__ = ['ResNet50', 'ResNet101','ResNet152']

def Conv1(in_planes, places, stride=1):
    return nn.Sequential(
        nn.Conv2d(in_channels=in_planes,out_channels=places,kernel_size=3,stride=stride,padding=1, bias=False),
        nn.BatchNorm2d(places),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
    )

class Bottleneck(nn.Module):
    def __init__(self,in_places,places, stride=1,downsampling=False, expansion = 4):
        super(Bottleneck,self).__init__()
        self.expansion = expansion
        self.downsampling = downsampling

        self.bottleneck = nn.Sequential(
            nn.Conv2d(in_channels=in_places,out_channels=places,kernel_size=1,stride=1, bias=False),
            nn.BatchNorm2d(places),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=places, out_channels=places, kernel_size=3, stride=stride, padding=1, bias=False),
            nn.BatchNorm2d(places),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=places, out_channels=places*self.expansion, kernel_size=1, stride=1, bias=False),
            nn.BatchNorm2d(places*self.expansion),
        )

        if self.downsampling:
            self.downsample = nn.Sequential(
                nn.Conv2d(in_channels=in_places, out_channels=places*self.expansion, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(places*self.expansion)
            )
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        residual = x
        out = self.bottleneck(x)

        if self.downsampling:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)
        return out

class ResNet(nn.Module):
    def __init__(self,blocks, num_classes=10, expansion = 4):
        super(ResNet,self).__init__()
        self.expansion = expansion

        self.conv1 = Conv1(in_planes = 3, places= 64)

        self.layer1 = self.make_layer(in_places = 64, places= 64, block=blocks[0], stride=1)
        self.layer2 = self.make_layer(in_places = 256,places=128, block=blocks[1], stride=2)
        self.layer3 = self.make_layer(in_places=512,places=256, block=blocks[2], stride=2)
        self.layer4 = self.make_layer(in_places=1024,places=512, block=blocks[3], stride=2)

        self.fc = nn.Linear(2048,num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

    def make_layer(self, in_places, places, block, stride):
        layers = []
        layers.append(Bottleneck(in_places, places,stride, downsampling =True))
        for i in range(1, block):
            layers.append(Bottleneck(places*self.expansion, places))

        return nn.Sequential(*layers)


    def forward(self, x):
        x = self.conv1(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = F.avg_pool2d(x, 4)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

def ResNet50():
    return ResNet([3, 4, 6, 3])

def ResNet101():
    return ResNet([3, 4, 23, 3])

def ResNet152():
    return ResNet([3, 8, 36, 3])



def train_accuracy():
    correct = 0
    total = 0
    with torch.no_grad():
        for data in trainloader:
            img, labels = data
            img, labels = img.to(device), labels.to(device)
            d = img.size()
            e = labels.size()
            out = model(img)
            f = out.size()
            _,pred = torch.max(out.data, 1)
            total += labels.size(0)
            correct += (pred == labels).sum().item()
    print('Accuracy of the network on the train image: %d %%' % (100 * correct / total))
    return 100.0 * correct / total

def test_accuracy():
    correct = 0
    total = 0
    with torch.no_grad():
        for data in testloader:
            img, labels = data
            img, labels = img.to(device), labels.to(device)
            d = img.size()
            e = labels.size()
            out = model(img)
            f = out.size()
            _,pred = torch.max(out.data, 1)
            total += labels.size(0)
            correct += (pred == labels).sum().item()
    print('Accuracy of the network on the 10000 test image: %d %%' % (100 * correct / total))
    return 100.0 * correct / total



def train():
    #定義損失函數和優化器
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.parameters(), lr = LR, momentum=0.9, weight_decay=5e-4)  # 優化方式為mini-batch momentum-SGD,并采用L2正則化(權重衰減)
#    optimizer = optim.SGD(model.parameters(), lr = LR, momentum=0.9)
#    optimizer = optim.Adam(model.parameters(), lr=LR, betas=(0.9, 0.99))
    scheduler = lr_scheduler.StepLR(optimizer, step_size = 30, gamma = 0.2)
#    scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=0.9)
    iter = 0
    num = 1
    #訓練網絡
    for epoch in range(num_epoches):
        running_loss = 0.0
        for i, data in enumerate(trainloader, 0):
            iter = iter + 1
            img, labels = data
            img, labels = img.to(device), labels.to(device)
            a = img.size()
            b = labels.size()
            optimizer.zero_grad()
            #訓練
            out = model(img)
            c = out.size()
            loss = criterion(out, labels).to(device)
            loss.backward()
            writer.add_scalar('scalar/loss', loss.item(), iter)
            optimizer.step()
            running_loss += loss.item()

        scheduler.step()  # 這一步隻是學習率更新,其實應該放在epoch的循環當中

        print('epoch: %d\t batch: %d\t lr: %g\t loss: %.6f' % (epoch + 1, i + 1, scheduler.get_lr()[0], running_loss / (batchSize * (i + 1))))
        writer.add_scalar('scalar/train_accuracy', train_accuracy(), num + 1)
        writer.add_scalar('scalar/test_accuracy', test_accuracy(), num + 1)
        print('\n')
        num = num + 1

        torch.save(model, './model.pkl')




transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4), #padding後随機裁剪
    transforms.RandomHorizontalFlip(0.5),
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

transform_test = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])



modelPath = './model.pkl'
batchSize = 64
LR = 0.1
num_epoches = 200
writer = SummaryWriter(log_dir='scalar')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

trainset = torchvision.datasets.CIFAR10(root='./Cifar-10', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batchSize, shuffle=True)

testset = torchvision.datasets.CIFAR10(root='./Cifar-10', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=batchSize, shuffle=False)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')


model = ResNet50().to(device)

if __name__ == '__main__':
    #如果模型存在,加載模型
    if os.path.exists(modelPath):
        print('model exists')
        model = torch.load(modelPath)
        print('model load')
    else:
        print('model not exists')
    print('Training starts')
    train()
    writer.close()
    print('Training Finished')
           

繼續閱讀