1. 图像增广
import matplotlib.pyplot as plt
import torch
import torchvision
from d2l import torch as d2l
all_imags = torchvision.datasets.CIFAR10(train=True, root="../data_new", download=True)
train_augs = torchvision.transforms.Compose([
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.ToTensor()
])
test_augs = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
def load_cifar10(is_train, augs, batch_size):
dataset = torchvision.datasets.CIFAR10(root="../data", train=is_train,
transform=augs, download=True)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size,
shuffle=is_train, num_workers=d2l.get_dataloader_workers())
return dataloader
def train_batch_ch13(net, X, y, loss, trainer, devices):
"""
function :用多 GPU 进行小批量训练
:param net: 神经网络 net
:param X: 输入样本 X
:param y: 实际标签 Y
:param loss: 损失函数
:param trainer: 迭代器
:param devices: GPU 或 CPU
:return: train_loss_sum 训练损失值,train_acc_sum 训练精度值
"""
if isinstance(X, list):
X = [x.to(devices[0]) for x in X]
else:
X = X.to(devices[0])
y = y.to(devices[0])
net.train()
trainer.zero_grad()
pred = net(X)
l = loss(pred, y)
l.sum().backward()
trainer.step()
train_loss_sum = l.sum()
train_acc_sum = d2l.accuracy(pred, y)
return train_loss_sum, train_acc_sum
plt.show()
2. 微调
3. 目标检测和边界框
4. 锚框
5. 多尺度目标检测
6. 目标检测数据集
|