此程序为本人以学习pytorch为目的写的第一个练习程序。
在Ubuntu16.04.5的服务器上。
pytorch版本为0.4.1。
数据集是AI_challenger大赛2018年农作物病虫害检测赛道的数据集,数据集具体说明可访问比赛官网查看。
加载预训练好的resnet34作为特征提取器来训练的,超参数全部都是随便给的,准确率最后仅可达80%。
为了用tensorboard实现可视化,需要在程序文件同目录下新建一个logger.py文件,并把这个网址里的代码复制到logger.py里。启动tensorboard方法:在终端输入:tensorboard --logdir=’./logs’,会生成一个链接,用Chrome浏览器打开这个链接就可以打开tensorboard看可视化的效果了。与matplotlib实现的可视化相比,tensorboard是可以动态看可视化效果的。
from __future__ import print_function, division
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from PIL import Image
import pandas as pd
import os
from torchvision import models, transforms
from logger import Logger
import time
num_categories = 61 # 24*2+3+10
annotations_file_train = "/home/yiming_hao/data/AI_Challenger/2018_农作物病虫害检测/\
AgriculturalDisease_trainingset/AgriculturalDisease_train_annotations.json"
images_folder_train = "/home/yiming_hao/data/AI_Challenger/2018_农作物病虫害检测/\
AgriculturalDisease_trainingset/images"
annotations_file_val = "/home/yiming_hao/data/AI_Challenger/2018_农作物病虫害检测/\
AgriculturalDisease_validationset/AgriculturalDisease_validation_annotations.json"
images_folder_val = "/home/yiming_hao/data/AI_Challenger/2018_农作物病虫害检测/\
AgriculturalDisease_validationset/images"
class ImageDataset(torch.utils.data.Dataset):
def __init__(self, annotations_file, images_folder, transforms=None):
self.annotations_train = pd.read_json(annotations_file)
self.images_folder = images_folder
self.transforms = transforms
self.num_examples = self.annotations_train.shape[0]
print('样本数为: {}'.format(self.num_examples))
def __len__(self):
return self.num_examples
def __getitem__(self, index):
label = torch.tensor(self.annotations_train.ix[index]['disease_class'], dtype=torch.long)
feature = Image.open(self.images_folder + '/' + self.annotations_train.ix[index]['image_id'])
if self.transforms:
feature = self.transforms(feature)
sample = (feature, label)
return sample
transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
]),
}
datasets = {
'train':ImageDataset(annotations_file_train, images_folder_train, transforms['train']),
'val':ImageDataset(annotations_file_val, images_folder_val, transforms['val'])
}
dataloaders = {x: torch.utils.data.DataLoader(datasets[x], batch_size=64, shuffle=True, num_workers=4) for x in ['train', 'val']}
dataset_sizes = {x: len(datasets[x]) for x in ['train', 'val']}
device = torch.device("cuda:1" )
def initialize_model(model_name, num_categories, finetuning=False, pretrained=True):
if model_name == 'resnet18':
model = models.resnet18(pretrained=pretrained)
if finetuning == True:
pass
else:
for param in model.parameters():
param.requires_grad = False
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, num_categories)
model = model.to(device)
elif model_name == 'resnet34':
model = models.resnet34(pretrained=pretrained)
if finetuning == True:
pass
else:
for param in model.parameters():
param.requires_grad = False
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, num_categories)
model = model.to(device)
else:
model = None
return model
def train_model(model, criterion, optimizer, scheduler, pre_epoch, num_epochs, logger):
since = time.time()
best_acc = 0.0
for epoch in range(pre_epoch, num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
for phase in ['train', 'val']:
if phase == 'train':
scheduler.step()
model.train()
else:
model.eval()
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / dataset_sizes[phase]
logger.scalar_summary('loss',epoch_loss,epoch)
epoch_acc = running_corrects.double() / dataset_sizes[phase]
logger.scalar_summary('acc', epoch_acc, epoch)
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
checkpoint_path = './checkpoints/state-best.tar'
torch.save({
'epoch':epoch,
'model_state_dict':model.state_dict(),
'optimizer_state_dict':optimizer.state_dict(),
'loss':epoch_loss,
'acc':best_acc
},checkpoint_path)
print()
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))
checkpoint = torch.load('./checkpoints/state-best.tar')
model.load_state_dict(checkpoint['model_state_dict'])
return model
model = initialize_model('resnet34',num_categories)
optimizer = optim.SGD(model.fc.parameters(), lr=0.001, momentum=0.9)
criterion = nn.CrossEntropyLoss()
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
pre_epoch = 0
logger = Logger('./logs')
if os.listdir('./checkpoints'): # 如果checkpoint文件夹非空,即之前已经有保存过的数据(模型参数等),则加载以前保存过的最好的一组状态
print('loading previous state............')
checkpoint = torch.load('./checkpoints/state-best.tar')
model.load_state_dict(checkpoint['model_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
pre_epoch = checkpoint['epoch']
loss = checkpoint['loss'] # 暂时保留未用
model = train_model(model, criterion, optimizer, exp_lr_scheduler, pre_epoch, 1000, logger)#两分种一个epoch,跑8个小时,就是240个epoch