1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
import torch import torch.utils.data as data import torch.nn as nn import torch.optim as optim from torchvision import transforms from torchvision import datasets from torchvision.models import resnet18
def build_dataloader(batch_size):
print("Build dataset CIFAR-10")
CIFAR10_MEAN = [0.4914, 0.4822, 0.4465] CIFAR10_STD = [0.2023, 0.1994, 0.2010]
transform = transforms.Compose([ transforms.Resize((112, 112)), transforms.ToTensor(), transforms.Normalize(CIFAR10_MEAN, CIFAR10_STD) ])
train_data = datasets.CIFAR10('../data/cifar-10-python', train=True, transform=transform) test_data = datasets.CIFAR10('../data/cifar-10-python', train=False, transform=transform)
train_dataloader = data.DataLoader( train_data, sampler=data.RandomSampler(train_data), batch_size=batch_size, num_workers=8, pin_memory=True, drop_last=False, )
test_dataloader = data.DataLoader( test_data, sampler=data.SequentialSampler(test_data), batch_size=batch_size, num_workers=8, pin_memory=True, drop_last=False )
print("Finish: Build dataset CIFAR-10")
return train_dataloader, test_dataloader
def main():
use_cuda = True epochs = 30 lr = 0.01
train_data, test_data = build_dataloader(256) model = resnet18(num_classes=10)
if use_cuda: model = model.cuda()
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9, weight_decay=0.05) lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, epochs, 1e-4)
criterion = nn.CrossEntropyLoss()
best_acc = 0
for epoch in range(1, epochs + 1): train_acc, train_bs = 0, 0 for img, labels in train_data: if use_cuda: img = img.cuda() labels = labels.cuda() optimizer.zero_grad() out = model(img) loss = criterion(out, labels) loss.backward() optimizer.step()
train_acc += out.argmax(dim=1).eq(labels).sum() train_bs += labels.size(0)
lr_scheduler.step()
test_acc, test_bs = 0, 0 with torch.no_grad(): for img, labels in test_data: if use_cuda: img = img.cuda() labels = labels.cuda() out = model(img) test_acc += out.argmax(dim=1).eq(labels).sum() test_bs += labels.size(0)
if test_acc > best_acc: best_acc = test_acc
print("epoch {:^3}: train accuracy {:.2f}% \t test accuracy {:.2f}%".format(epoch, train_acc / train_bs * 100, test_acc / test_bs * 100)) print("Finish training, best accuracy is {:.2f}".format(best_acc / test_bs * 100))
if __name__ == '__main__': main()
|