%matplotlib inline
import os
import time
import torch
from torch import nn, optim
from torch.utils.data import Dataset, DataLoader
import torchvision
from PIL import Image
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import sys
sys.path.append("..")
import d2lzh_pytorch as d2l
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(torch.__version__)
print(device)
1.11.0+cu113 cuda
常用的图像增广方法
翻转和裁剪
torchvision.transforms.RandomHorizontalFlip()
torchvision.transforms.RandomVerticalFlip()
torchvision.transforms.RandomResizedCrop(200, scale=(0.1, 1), ratio=(0.5, 2))
变化颜色
torchvision.transforms.ColorJitter(brightness=0.5, contrast=0, saturation=0, hue=0)
torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0.5)
torchvision.transforms.ColorJitter(brightness=0, contrast=0.5, saturation=0, hue=0)
torchvision.transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
叠加多个图像增广方法
augs = torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(), color_aug, shape_aug])
使用图像增广训练模型
all_imges = torchvision.datasets.CIFAR10(train=True, root="~/Datasets/CIFAR", download=True)
flip_aug = torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor()])
no_aug = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()])
num_workers = 0 if sys.platform.startswith('win32') else 4
def load_cifar10(is_train, augs, batch_size, root="~/Datasets/CIFAR"):
dataset = torchvision.datasets.CIFAR10(root=root, train=is_train, transform=augs, download=True)
return DataLoader(dataset, batch_size=batch_size, shuffle=is_train, num_workers=num_workers)
开始训练
# 本函数已保存在d2lzh_pytorch包中方便以后使用
def train(train_iter, test_iter, net, loss, optimizer, device, num_epochs):
net = net.to(device)
print("training on ", device)
batch_count = 0
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, n, start = 0.0, 0.0, 0, time.time()
for X, y in train_iter:
X = X.to(device)
y = y.to(device)
y_hat = net(X)
l = loss(y_hat, y)
optimizer.zero_grad()
l.backward()
optimizer.step()
train_l_sum += l.cpu().item()
train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
n += y.shape[0]
batch_count += 1
test_acc = d2l.evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'
% (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, test_acc, time.time() - start))
def train_with_data_aug(train_augs, test_augs, lr=0.001):
batch_size, net = 256, d2l.resnet18(10)
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
loss = torch.nn.CrossEntropyLoss()
train_iter = load_cifar10(True, train_augs, batch_size)
test_iter = load_cifar10(False, test_augs, batch_size)
train(train_iter, test_iter, net, loss, optimizer, device, num_epochs=10)
train_with_data_aug(flip_aug, no_aug)
Files already downloaded and verified Files already downloaded and verified training on cuda epoch 1, loss 1.3615, train acc 0.505, test acc 0.493, time 123.2 sec epoch 2, loss 0.5003, train acc 0.645, test acc 0.620, time 123.0 sec epoch 3, loss 0.2811, train acc 0.703, test acc 0.616, time 123.1 sec epoch 4, loss 0.1890, train acc 0.735, test acc 0.686, time 123.0 sec epoch 5, loss 0.1346, train acc 0.765, test acc 0.671, time 123.1 sec epoch 6, loss 0.1029, train acc 0.787, test acc 0.674, time 123.1 sec epoch 7, loss 0.0803, train acc 0.804, test acc 0.749, time 123.1 sec epoch 8, loss 0.0644, train acc 0.822, test acc 0.717, time 123.1 sec epoch 9, loss 0.0526, train acc 0.836, test acc 0.750, time 123.0 sec epoch 10, loss 0.0433, train acc 0.851, test acc 0.754, time 123.1 sec
Comments NOTHING