犬と猫もやろう!!!
VGG16のFine-tuningによる犬猫認識 (2)
In [64]:
%matplotlib inline
In [106]:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
import torchvision
from torchvision import datasets, models, transforms
import os
import time
import copy
import numpy as np
import matplotlib.pyplot as plt
In [3]:
!wget https://download.pytorch.org/tutorial/hymenoptera_data.zip -P data/
In [4]:
!ls data
In [ ]:
!cd data/
!unzip data/hymenoptera_data.zip -d data/
ImageFolder
In [47]:
data_dir = os.path.join('data', 'hymenoptera_data')
image_dataset = datasets.ImageFolder(os.path.join(data_dir, 'train'))
print(len(image_dataset))
image, label = image_dataset[0]
In [48]:
plt.figure()
plt.imshow(image)
t = transforms.RandomResizedCrop(224)
trans_image = t(image)
plt.figure()
plt.imshow(trans_image)
Out[48]:
In [53]:
plt.figure()
plt.imshow(image)
t = transforms.RandomHorizontalFlip()
trans_image = t(image)
plt.figure()
plt.imshow(trans_image)
Out[53]:
In [57]:
plt.figure()
plt.imshow(image)
t = transforms.Resize((256, 256))
trans_image = t(image)
plt.figure()
plt.imshow(trans_image)
Out[57]:
In [63]:
plt.figure()
plt.imshow(image)
t = transforms.CenterCrop(224)
trans_image = t(image)
plt.figure()
plt.imshow(trans_image)
Out[63]:
In [66]:
data_transforms = {
'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, 256)),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
ImageFolder
はKerasのImageDataGeneratorのflow_from_directory()に近い
In [84]:
data_dir = os.path.join('data', 'hymenoptera_data')
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
for x in ['train', 'val']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x],
batch_size=4,
shuffle=True,
num_workers=4) for x in ['train', 'val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
class_names = image_datasets['train'].classes
In [86]:
print(image_datasets['train'])
print(image_datasets['val'])
print(dataloaders['train'])
print(dataloaders['val'])
print(dataset_sizes)
print(class_names)
In [92]:
def imshow(images, title=None):
images = images.numpy().transpose((1, 2, 0)) # (h, w, c)
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
images = std * images + mean
images = np.clip(images, 0, 1)
plt.imshow(images)
if title is not None:
plt.title(title)
images, classes = next(iter(dataloaders['train']))
print(images.size(), classes.size()) # torch.Size([4, 3, 224, 224]) torch.Size([4])
images = torchvision.utils.make_grid(images)
imshow(images, title=[class_names[x] for x in classes])
In [107]:
use_gpu = torch.cuda.is_available()
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# 各エポックで訓練+バリデーションを実行
for phase in ['train', 'val']:
if phase == 'train':
scheduler.step()
model.train(True) # training mode
else:
model.train(False) # evaluate mode
running_loss = 0.0
running_corrects = 0
for data in dataloaders[phase]:
inputs, labels = data
if use_gpu:
inputs = Variable(inputs.cuda())
labels = Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
optimizer.zero_grad()
# forward
outputs = model(inputs)
_, preds = torch.max(outputs.data, 1)
loss = criterion(outputs, labels)
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.data[0] * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
# サンプル数で割って平均を求める
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
# deep copy the model
# 精度が改善したらモデルを保存する
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
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))
# load best model weights
model.load_state_dict(best_model_wts)
return model
In [95]:
model_ft = models.resnet18(pretrained=True)
In [96]:
model_ft
Out[96]:
In [99]:
num_features = model_ft.fc.in_features
print(num_features)
In [100]:
# fc層を置き換える
model_ft.fc = nn.Linear(num_features, 2)
In [101]:
model_ft
Out[101]:
In [ ]:
if use_gpu:
model_ft = model_ft.cuda()
criterion = nn.CrossEntropyLoss()
optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)
# 7エポックごとに学習率を0.1倍する
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)
model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler, num_epochs=25)
torch.save(model_ft.state_dict(), 'model_ft.pkl')
Epoch 20/24
----------
train Loss: 0.2950 Acc: 0.8689
val Loss: 0.2302 Acc: 0.9020
Epoch 21/24
----------
train Loss: 0.2466 Acc: 0.8893
val Loss: 0.2107 Acc: 0.9281
Epoch 22/24
----------
train Loss: 0.3057 Acc: 0.8648
val Loss: 0.2204 Acc: 0.9216
Epoch 23/24
----------
train Loss: 0.2220 Acc: 0.9180
val Loss: 0.2031 Acc: 0.9281
Epoch 24/24
----------
train Loss: 0.3338 Acc: 0.8648
val Loss: 0.2066 Acc: 0.9216
Training complete in 1m 60s
Best val acc: 0.9281
AssertionError: Torch not compiled with CUDA enabled
In [115]:
# GPUで学習したモデルのロード
model_ft.load_state_dict(torch.load('model_ft.pkl', map_location=lambda storage, loc: storage))
In [128]:
def visualize_model(model, num_images=6):
images_so_far = 0
fig = plt.figure()
for i, data in enumerate(dataloaders['val']):
inputs, labels = data
if use_gpu:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
outputs = model(inputs)
_, preds = torch.max(outputs.data, 1)
for j in range(inputs.size()[0]):
images_so_far += 1
ax = plt.subplot(num_images // 2, 2, images_so_far)
ax.axis('off')
ax.set_title('predicted: {}'.format(class_names[preds[j]]))
imshow(inputs.cpu().data[j])
if images_so_far == num_images:
return
In [129]:
visualize_model(model_ft)
requires_grad = False
とする
In [133]:
# 訓練済みResNet18をロード
model_conv = torchvision.models.resnet18(pretrained=True)
# すべてのパラメータを固定
for param in model_conv.parameters():
param.requires_grad = False
# 最後のfc層を置き換える
# これはデフォルトの requires_grad=True のままなのでパラメータ更新対象
num_features = model_conv.fc.in_features
model_conv.fc = nn.Linear(num_features, 2)
if use_gpu:
model_conv = model_conv.cuda()
criterion = nn.CrossEntropyLoss()
# Optimizerの第1引数には更新対象のfc層のパラメータのみ指定
optimizer_conv = optim.SGD(model_conv.fc.parameters(), lr=0.001, momentum=0.9)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)
In [134]:
model_conv = train_model(model_conv, criterion, optimizer_conv,
exp_lr_scheduler, num_epochs=25)
In [135]:
visualize_model(model_conv)