基于经典网络架构训练图像分类模型
import os
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import torch
from torch import nn
import torch.optim as optim
import torchvision
#pip install torchvision
from torchvision import transforms, models, datasets
#https://pytorch.org/docs/stable/torchvision/index.html
import imageio
import time
import warnings
warnings.filterwarnings("ignore")
import random
import sys
import copy
import json
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
os.environ[“KMP_DUPLICATE_LIB_OK”]=“TRUE”,可以不加,这是我运行matplotlib服务器内核会重启加的。
数据读取与预处理操作
data_dir = './flower_data/'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
制作好数据源:
这里的操作有:
指定data.transforms(这是字典结构)有两个key,这两是有区别的,
在预处理当中做的第一件事就是把所有输入数据格式大小都是相同的,
96*96会影响结果,特征会减少,但是96比256计算比较快。
因为样本少所以做了数据增强。
随机旋转、平移、翻转是为了增加数据量,训练集和测试集的均值和标准差是用别人的,因为这里的数据量小,可能导致结果不稳定,这里的均值分别表示RGB三颜色通道的
transforms.ColorJitter(brightness=0.2, contrast=0.1, saturation=0.1, hue=0.1),做的不多,这是再极端恶劣条件下、光照条件下用的
transforms.ToTensor()把数据转化成torch中的基本结构
data_transforms = {
'train':
transforms.Compose([
transforms.Resize([96, 96]),
transforms.RandomRotation(45),#随机旋转,-45到45度之间随机选
transforms.CenterCrop(64),#从中心开始裁剪
transforms.RandomHorizontalFlip(p=0.5),#随机水平翻转 选择一个概率概率
transforms.RandomVerticalFlip(p=0.5),#随机垂直翻转
transforms.ColorJitter(brightness=0.2, contrast=0.1, saturation=0.1, hue=0.1),#参数1为亮度,参数2为对比度,参数3为饱和度,参数4为色相
transforms.RandomGrayscale(p=0.025),#概率转换成灰度率,3通道就是R=G=B
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])#均值,标准差
]),
'valid':
transforms.Compose([
transforms.Resize([64, 64]),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
ImageFolder()假设所有的文件按文件夹保存好,每个文件夹下面存贮同一类别的图片,文件夹的名字为分类的名字。
image_datasets把数据集和预处理操作全部指定好,
shuffle:洗牌默认False.
dataset_sizes 计数在最后算准确率用的。
class_names 每一个索引位置它的类别是谁的
batch_size = 128
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'valid']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True) for x in ['train', 'valid']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'valid']}
class_names = image_datasets['train'].classes
这里我们看一下image_datasets
image_datasets
# 输出
'''
Out:image_datasets
image_datasets
{'train': Dataset ImageFolder
Number of datapoints: 6552
Root location: ./flower_data/train
StandardTransform
Transform: Compose(
Resize(size=[96, 96], interpolation=bilinear, max_size=None, antialias=None)
RandomRotation(degrees=[-45.0, 45.0], interpolation=nearest, expand=False, fill=0)
CenterCrop(size=(64, 64))
RandomHorizontalFlip(p=0.5)
RandomVerticalFlip(p=0.5)
ColorJitter(brightness=[0.8, 1.2], contrast=[0.9, 1.1], saturation=[0.9, 1.1], hue=[-0.1, 0.1])
RandomGrayscale(p=0.025)
ToTensor()
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
),
'valid': Dataset ImageFolder
Number of datapoints: 818
Root location: ./flower_data/valid
StandardTransform
Transform: Compose(
Resize(size=[64, 64], interpolation=bilinear, max_size=None, antialias=None)
ToTensor()
Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
)}'''
读取标签对应的实际名字
找到对应的名字,把id替换成它的value值,可以运行cat_to_name 看看
with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)
加载models中提供的模型,并且直接用训练的好权重当做初始化参数
第一次执行需要下载,可能会比较慢。
model_name = 'resnet' #可选的比较多 ['resnet', 'alexnet', 'vgg', 'squeezenet', 'densenet', 'inception']
#是否用人家训练好的特征来做
feature_extract = True #都用人家特征
所有参数不更新,就是反向传播不更新梯度。用该模型的参数
model_ft 中有每一层的参数和对应的名字
def set_parameter_requires_grad(model, feature_extracting):
if feature_extracting:
for param in model.parameters():
# 反向传播不更新梯度
param.requires_grad = False
model_ft = models.resnet18()#18层的能快点,也可以选152
打印model_ft看一看
输出结果:
'''
ResNet(
(conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
(layer1): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(1): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(layer2): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(layer3): Sequential(
(0): BasicBlock(
(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(layer4): Sequential(
(0): BasicBlock(
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
(fc): Linear(in_features=512, out_features=1000, bias=True)
)
'''
在最后一层全连接层中
要把1000分类改成102分类,因为我们分类任务是102分类 把模型输出层改成自己的
把模型输出层改成自己的
pretrained=True就是用别人训练好的参数当作我们的初始化,然后重新定义fc层
def initialize_model(model_name, num_classes, feature_extract, use_pretrained=True):
# 指定好网络结构
model_ft = models.resnet18(pretrained=use_pretrained)
set_parameter_requires_grad(model_ft, feature_extract)
# 重新定义fc层
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 102)#类别数自己根据自己任务来
input_size = 64#输入大小根据自己配置来
return model_ft, input_size
设置哪些层需要训练
best.pt 保存网络结构,model里面的权重参数保存在本地
model_ft, input_size = initialize_model(model_name, 102, feature_extract, use_pretrained=True)
#GPU计算
model_ft = model_ft.to(device)
# 模型保存,名字自己起
filename='best.pt'
# 每一层权重参数都保存下来
params_to_update = model_ft.parameters()
print("Params to learn:")
if feature_extract:
params_to_update = []
for name,param in model_ft.named_parameters():
if param.requires_grad == True:
params_to_update.append(param)
print("\t",name)
else:
for name,param in model_ft.named_parameters():
if param.requires_grad == True:
print("\t",name)
优化器设置
优化器使用Adam效果比较好
并设置学习率衰减策略
# 优化器设置
optimizer_ft = optim.Adam(params_to_update, lr=1e-2)#要训练啥参数,你来定
scheduler = optim.lr_scheduler.StepLR(optimizer_ft, step_size=10, gamma=0.1)#学习率每10个epoch衰减成原来的1/10
# 交叉熵损失
criterion = nn.CrossEntropyLoss()
训练模块
def train_model(model, dataloaders, criterion, optimizer, num_epochs=25,filename='best.pt'):
#算一下时间
since = time.time()
#也要记录最好的那一次
best_acc = 0
#模型也得放到你的CPU或者GPU
model.to(device)
#训练过程中打印一堆损失和指标
val_acc_history = []
train_acc_history = []
train_losses = []
valid_losses = []
#学习率
LRs = [optimizer.param_groups[0]['lr']]
#最好的那次模型,后续会变的,先初始化
best_model_wts = copy.deepcopy(model.state_dict())
#一个个epoch来遍历
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# 训练和验证
for phase in ['train', 'valid']:
if phase == 'train':
model.train() # 训练
else:
model.eval() # 验证
running_loss = 0.0
running_corrects = 0
# 把数据都取个遍
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)#放到你的CPU或GPU
labels = labels.to(device)
# 清零
optimizer.zero_grad()
# 只有训练的时候计算和更新梯度
outputs = model(inputs)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs, 1)
# 训练阶段更新权重
if phase == 'train':
loss.backward()
optimizer.step()
# 计算损失
running_loss += loss.item() * inputs.size(0)#0表示batch那个维度
running_corrects += torch.sum(preds == labels.data)#预测结果最大的和真实值是否一致
epoch_loss = running_loss / len(dataloaders[phase].dataset)#算平均
epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)
time_elapsed = time.time() - since#一个epoch我浪费了多少时间
print('Time elapsed {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
# 得到最好那次的模型
if phase == 'valid' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
state = {
'state_dict': model.state_dict(),#字典里key就是各层的名字,值就是训练好的权重
'best_acc': best_acc,
'optimizer' : optimizer.state_dict(),
}
torch.save(state, filename)
if phase == 'valid':
val_acc_history.append(epoch_acc)
valid_losses.append(epoch_loss)
#scheduler.step(epoch_loss)#学习率衰减
if phase == 'train':
train_acc_history.append(epoch_acc)
train_losses.append(epoch_loss)
print('Optimizer learning rate : {:.7f}'.format(optimizer.param_groups[0]['lr']))
LRs.append(optimizer.param_groups[0]['lr'])
print()
scheduler.step()#学习率衰减
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# 训练完后用最好的一次当做模型最终的结果,等着一会测试
model.load_state_dict(best_model_wts)
return model, val_acc_history, train_acc_history, valid_losses, train_losses, LRs
开始训练
model_ft, val_acc_history, train_acc_history, valid_losses, train_losses, LRs = train_model(model_ft, dataloaders, criterion, optimizer_ft, num_epochs=20)
'''Epoch 0/19
----------
Time elapsed 0m 49s
train Loss: 3.8838 Acc: 0.2502
Time elapsed 0m 54s
valid Loss: 3.4304 Acc: 0.2958
Optimizer learning rate : 0.0100000
Epoch 1/19
----------
Time elapsed 1m 42s
train Loss: 2.8802 Acc: 0.3887
Time elapsed 1m 46s
valid Loss: 3.6037 Acc: 0.3056
Optimizer learning rate : 0.0100000
Epoch 2/19
----------
Time elapsed 2m 33s
train Loss: 2.8130 Acc: 0.4167
Time elapsed 2m 38s
valid Loss: 3.9233 Acc: 0.2971
Optimizer learning rate : 0.0100000
Epoch 3/19
----------
Time elapsed 3m 25s
train Loss: 2.7754 Acc: 0.4304
Time elapsed 3m 30s
valid Loss: 3.8080 Acc: 0.3215
Optimizer learning rate : 0.0100000
Epoch 4/19
----------
Time elapsed 4m 17s
train Loss: 2.7439 Acc: 0.4434
Time elapsed 4m 22s
valid Loss: 3.8418 Acc: 0.3081
Optimizer learning rate : 0.0100000
Epoch 5/19
----------
Time elapsed 5m 9s
train Loss: 2.7127 Acc: 0.4501
Time elapsed 5m 14s
valid Loss: 3.9059 Acc: 0.3215
Optimizer learning rate : 0.0100000
Epoch 6/19
----------
Time elapsed 6m 2s
train Loss: 2.7832 Acc: 0.4582
Time elapsed 6m 7s
valid Loss: 3.9352 Acc: 0.3374
Optimizer learning rate : 0.0100000
Epoch 7/19
----------
Time elapsed 6m 55s
train Loss: 2.8446 Acc: 0.4600
Time elapsed 6m 60s
valid Loss: 4.1712 Acc: 0.3240
Optimizer learning rate : 0.0100000
Epoch 8/19
----------
Time elapsed 7m 48s
train Loss: 2.7674 Acc: 0.4623
Time elapsed 7m 53s
valid Loss: 4.2523 Acc: 0.3142
Optimizer learning rate : 0.0100000
Epoch 9/19
----------
Time elapsed 8m 39s
train Loss: 2.8622 Acc: 0.4669
Time elapsed 8m 44s
valid Loss: 4.1984 Acc: 0.3264
Optimizer learning rate : 0.0100000
Epoch 10/19
----------
Time elapsed 9m 32s
train Loss: 2.2802 Acc: 0.5250
Time elapsed 9m 37s
valid Loss: 3.7280 Acc: 0.3545
Optimizer learning rate : 0.0010000
Epoch 11/19
----------
Time elapsed 10m 25s
train Loss: 2.1310 Acc: 0.5363
Time elapsed 10m 29s
valid Loss: 3.7047 Acc: 0.3606
Optimizer learning rate : 0.0010000
Epoch 12/19
----------
Time elapsed 11m 17s
train Loss: 2.0519 Acc: 0.5427
Time elapsed 11m 22s
valid Loss: 3.6053 Acc: 0.3582
Optimizer learning rate : 0.0010000
Epoch 13/19
----------
Time elapsed 12m 11s
train Loss: 1.9371 Acc: 0.5588
Time elapsed 12m 16s
valid Loss: 3.5352 Acc: 0.3704
Optimizer learning rate : 0.0010000
Epoch 14/19
----------
Time elapsed 13m 5s
train Loss: 1.9963 Acc: 0.5536
Time elapsed 13m 10s
valid Loss: 3.5780 Acc: 0.3582
Optimizer learning rate : 0.0010000
Epoch 15/19
----------
Time elapsed 13m 59s
train Loss: 1.8958 Acc: 0.5731
Time elapsed 14m 4s
valid Loss: 3.4764 Acc: 0.3631
Optimizer learning rate : 0.0010000
Epoch 16/19
----------
Time elapsed 14m 51s
train Loss: 1.8948 Acc: 0.5662
Time elapsed 14m 56s
valid Loss: 3.4679 Acc: 0.3680
Optimizer learning rate : 0.0010000
Epoch 17/19
----------
Time elapsed 15m 45s
train Loss: 1.8866 Acc: 0.5668
Time elapsed 15m 50s
valid Loss: 3.4043 Acc: 0.3802
Optimizer learning rate : 0.0010000
Epoch 18/19
----------
Time elapsed 16m 38s
train Loss: 1.8298 Acc: 0.5702
Time elapsed 16m 44s
valid Loss: 3.3611 Acc: 0.3936
Optimizer learning rate : 0.0010000
Epoch 19/19
----------
Time elapsed 17m 31s
train Loss: 1.7917 Acc: 0.5749
Time elapsed 17m 36s
valid Loss: 3.3750 Acc: 0.3692
Optimizer learning rate : 0.0010000
Training complete in 17m 36s
Best val Acc: 0.393643'''
再继续训练所有层
之前只训练最后一层,这次训练所有层的权重参数
for param in model_ft.parameters():
param.requires_grad = True
# 再继续训练所有的参数,学习率调小一点
optimizer = optim.Adam(model_ft.parameters(), lr=1e-3)
scheduler = optim.lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
# 损失函数
criterion = nn.CrossEntropyLoss()
# 加载之前训练好的权重参数
checkpoint = torch.load(filename)
print(checkpoint)
best_acc = checkpoint['best_acc']
model_ft.load_state_dict(checkpoint['state_dict'])
model_ft, val_acc_history, train_acc_history, valid_losses, train_losses, LRs = train_model(model_ft, dataloaders, criterion, optimizer, num_epochs=15,)
"""Epoch 0/14
----------
Time elapsed 0m 49s
train Loss: 2.8341 Acc: 0.4005
Time elapsed 0m 53s
valid Loss: 3.5476 Acc: 0.4059
Optimizer learning rate : 0.0010000
Epoch 1/14
----------
Time elapsed 1m 43s
train Loss: 1.6343 Acc: 0.5804
Time elapsed 1m 48s
valid Loss: 2.2607 Acc: 0.4633
Optimizer learning rate : 0.0010000
Epoch 2/14
----------
Time elapsed 2m 38s
train Loss: 1.1703 Acc: 0.6789
Time elapsed 2m 42s
valid Loss: 2.0314 Acc: 0.5220
Optimizer learning rate : 0.0010000
Epoch 3/14
----------
Time elapsed 3m 32s
train Loss: 1.0267 Acc: 0.7158
Time elapsed 3m 36s
valid Loss: 1.9762 Acc: 0.5147
Optimizer learning rate : 0.0010000
Epoch 4/14
----------
Time elapsed 4m 25s
train Loss: 0.8864 Acc: 0.7505
Time elapsed 4m 30s
valid Loss: 1.7464 Acc: 0.5733
Optimizer learning rate : 0.0010000
Epoch 5/14
----------
Time elapsed 5m 20s
train Loss: 0.7785 Acc: 0.7718
Time elapsed 5m 25s
valid Loss: 1.6833 Acc: 0.6112
Optimizer learning rate : 0.0010000
Epoch 6/14
----------
Time elapsed 6m 15s
train Loss: 0.6767 Acc: 0.8013
Time elapsed 6m 20s
valid Loss: 1.7561 Acc: 0.6076
Optimizer learning rate : 0.0010000
Epoch 7/14
----------
Time elapsed 7m 8s
train Loss: 0.6331 Acc: 0.8094
Time elapsed 7m 13s
valid Loss: 1.7938 Acc: 0.5966
Optimizer learning rate : 0.0010000
Epoch 8/14
----------
Time elapsed 8m 2s
train Loss: 0.6210 Acc: 0.8205
Time elapsed 8m 7s
valid Loss: 1.8196 Acc: 0.5905
Optimizer learning rate : 0.0010000
Epoch 9/14
----------
Time elapsed 8m 55s
train Loss: 0.6666 Acc: 0.8054
Time elapsed 9m 0s
valid Loss: 2.1736 Acc: 0.5342
Optimizer learning rate : 0.0010000
Epoch 10/14
----------
Time elapsed 9m 52s
train Loss: 0.5718 Acc: 0.8320
Time elapsed 9m 57s
valid Loss: 1.6204 Acc: 0.6222
Optimizer learning rate : 0.0010000
Epoch 11/14
----------
Time elapsed 10m 49s
train Loss: 0.5009 Acc: 0.8555
Time elapsed 10m 54s
valid Loss: 1.8533 Acc: 0.6015
Optimizer learning rate : 0.0010000
Epoch 12/14
----------
Time elapsed 11m 44s
train Loss: 0.5278 Acc: 0.8463
Time elapsed 11m 49s
valid Loss: 1.8048 Acc: 0.5966
Optimizer learning rate : 0.0010000
Epoch 13/14
----------
Time elapsed 12m 38s
train Loss: 0.4753 Acc: 0.8616
Time elapsed 12m 43s
valid Loss: 1.8856 Acc: 0.5966
Optimizer learning rate : 0.0010000
Epoch 14/14
----------
Time elapsed 13m 33s
train Loss: 0.5076 Acc: 0.8498
Time elapsed 13m 38s
valid Loss: 1.8666 Acc: 0.6015
Optimizer learning rate : 0.0010000
Training complete in 13m 38s
Best val Acc: 0.622249"""
加载训练好的模型
model_ft, input_size = initialize_model(model_name, 102, feature_extract, use_pretrained=True)
# GPU模式
model_ft = model_ft.to(device)
# 保存文件的名字
filename='best.pt'
# 加载模型
checkpoint = torch.load(filename)
best_acc = checkpoint['best_acc']
model_ft.load_state_dict(checkpoint['state_dict'])
测试数据预处理
测试数据处理方法需要跟训练时一直才可以
crop操作的目的是保证输入的大小是一致的
标准化操作也是必须的,用跟训练数据相同的mean和std,但是需要注意一点训练数据是在0-1上进行标准化,所以测试数据也需要先归一化
最后一点,PyTorch中颜色通道是第一个维度,跟很多工具包都不一样,需要转换
# 得到一个batch的测试数据
dataiter = iter(dataloaders['valid'])
images, labels = dataiter.next()
model_ft.eval()
if train_on_gpu:
output = model_ft(images.cuda())
else:
output = model_ft(images)
得到概率最大的那个
_, preds_tensor = torch.max(output, 1)
preds = np.squeeze(preds_tensor.numpy()) if not train_on_gpu else np.squeeze(preds_tensor.cpu().numpy())
展示预测结果
def im_convert(tensor):
""" 展示数据"""
image = tensor.to("cpu").clone().detach()
image = image.numpy().squeeze()
image = image.transpose(1,2,0)
image = image * np.array((0.229, 0.224, 0.225)) + np.array((0.485, 0.456, 0.406))
image = image.clip(0, 1)
return image
fig=plt.figure(figsize=(20, 20))
columns =4
rows = 2
for idx in range (columns*rows):
ax = fig.add_subplot(rows, columns, idx+1, xticks=[], yticks=[])
plt.imshow(im_convert(images[idx]))
ax.set_title("{} ({})".format(cat_to_name[str(preds[idx])], cat_to_name[str(labels[idx].item())]),
color=("green" if cat_to_name[str(preds[idx])]==cat_to_name[str(labels[idx].item())] else "red"))
plt.show()
结果: