定义是否使用GPU

可有可无,默认为 cpu

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

训练数据:

BATCH_SIZE = 64 :批处理尺寸,即一次处理图像的张数
加载训练数据 : 以cifar10 为例

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform_train) #训练数据集
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)   #生成一个个batch进行批训练,组成batch的时候顺序打乱取

数据预处理(transform)

做一些增强,旋转,缩放,仿射变换等

测试数据:

加载测试数据 : 以cifar10 为例

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)

模型加载

现在模型文件定义好模型网络结构,以ResNet-34为例:

model = ResNet_34()
net = model.to(device)

定义损失函数和优化方式

#损失函数为交叉熵,多用于多分类问题
criterion = nn.CrossEntropyLoss()
#优化方式为mini-batch momentum-SGD,并采用L2正则化(权重衰减)
optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9, weight_decay=5e-4)
def adj_lr(optimizer):
    for param_group in optimizer.param_groups:
        param_group['lr'] = param_group['lr'] * 0.1

训练过程

for epoch in range(pre_epoch, EPOCH):
	                for i, data in enumerate(trainloader, 0):
                    inputs, labels = data
                    inputs, labels = inputs.to(device), labels.to(device) # 加载数据
					
					optimizer.zero_grad()               #梯度清零
                    outputs = net(inputs)               #数据过网络
                    loss = criterion(outputs, labels)   #计算loss
                    loss.backward()                     #反向传播
                    optimizer.step()                    #更新参数

模型保存

torch.save(model,"resnet34-1-cifar.pkl")
torch.save(model.state_dict(),"resnet34-1-cifar-param.pkl")

总代码

#! --*-- coding:utf-8 --*--
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
import argparse
from resnet import ResNet18
from resnet_2 import ResNet_34
# 定义是否使用GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print "used device =  " , device
# 参数设置,使得我们能够手动输入命令行参数,就是让风格变得和Linux命令行差不多
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--outf', default='./model/', help='folder to output images and model checkpoints') #输出结果保存路径
parser.add_argument('--net', default='./model/Resnet18.pth', help="path to net (to continue training)")  #恢复训练时的模型路径
args = parser.parse_args()

# 超参数设置
EPOCH = 64   #遍历数据集次数
pre_epoch = 0  # 定义已经遍历数据集的次数
BATCH_SIZE = 64      #批处理尺寸(batch_size)
LR = 0.1             #学习率

#LR = LR*gamma**epoch


# 准备数据集并预处理
transform_train = transforms.Compose([
    transforms.RandomCrop(32, padding=4),  #先四周填充0,在吧图像随机裁剪成32*32
    transforms.RandomHorizontalFlip(),  #图像一半的概率翻转,一半的概率不翻转
    transforms.ToTensor(),
    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), #R,G,B每层的归一化用到的均值和方差
])

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

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform_train) #训练数据集
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)   #生成一个个batch进行批训练,组成batch的时候顺序打乱取

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
# Cifar-10的标签
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# 模型定义-ResNet
#model = ResNet18()
model = ResNet_34()
net = model.to(device)


# 定义损失函数和优化方式
#损失函数为交叉熵,多用于多分类问题
criterion = nn.CrossEntropyLoss()
#优化方式为mini-batch momentum-SGD,并采用L2正则化(权重衰减)
optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9, weight_decay=5e-4)

def adj_lr(optimizer):
    for param_group in optimizer.param_groups:
        param_group['lr'] = param_group['lr'] * 0.1

# 训练
if __name__ == "__main__":
    best_acc = 85  #2 初始化best test accuracy
    print("Start Training, Resnet-18!")  # 定义遍历数据集的次数
    for epoch in range(pre_epoch, EPOCH):
        if(epoch in [8,16,32]):
            adj_lr(optimizer)
        net.train()
        sum_loss = 0.0
        correct = 0.0
        total = 0.0
        for i, data in enumerate(trainloader, 0):
            # 准备数据
            length = len(trainloader)
            inputs, labels = data
            inputs, labels = inputs.to(device), labels.to(device)

            optimizer.zero_grad()               #梯度清零
            outputs = net(inputs)               #数据过网络
            loss = criterion(outputs, labels)   #计算loss
            loss.backward()                     #反向传播
            optimizer.step()                    #更新参数

            # 每训练1个batch打印一次loss和准确率
            sum_loss += loss.item()
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += predicted.eq(labels.data).cpu().sum()
            print('[epoch:%d, iter:%d] Loss: %.03f | Acc: %.3f%% '
                  % (epoch + 1, (i + 1 + epoch * length), sum_loss / (i + 1), 100. * correct / total))

        # 每训练完一个epoch测试一下准确率
        print("Waiting Test!")
        with torch.no_grad():   # 不更新参数
            correct = 0
            total = 0
            for data in testloader:
                net.eval()
                images, labels = data
                images, labels = images.to(device), labels.to(device)
                outputs = net(images)
                # 取得分最高的那个类 (outputs.data的索引号)
                _, predicted = torch.max(outputs.data, 1)
                total += labels.size(0)
                correct += (predicted == labels).sum()
            print('测试分类准确率为:%.3f%%' % (100 * correct / total))
            print('Saving model......')
            torch.save(net.state_dict(), '%s/net_%03d.pth' % (args.outf, epoch + 1))
    print("Training Finished, TotalEPOCH=%d" % EPOCH)
    ###save model
    torch.save(model,"resnet34-1-cifar.pkl")
    torch.save(model.state_dict(),"resnet34-1-cifar-param.pkl")