|
| 1 | +import torch |
| 2 | +import torch.utils.data as Data |
| 3 | +import torchvision |
| 4 | +from lib.network import Network |
| 5 | +from torch import nn |
| 6 | +from torch.cuda import amp |
| 7 | +import time |
| 8 | + |
| 9 | + |
| 10 | +train_data = torchvision.datasets.MNIST(root='./mnist', train=True, |
| 11 | + transform=torchvision.transforms.ToTensor(), |
| 12 | + download=True) |
| 13 | +test_data = torchvision.datasets.MNIST(root='./mnist/', |
| 14 | + transform=torchvision.transforms.ToTensor(), |
| 15 | + train=False) |
| 16 | + |
| 17 | +train_loader = Data.DataLoader(dataset=train_data, batch_size=128 * 50, shuffle=True) |
| 18 | +test_loader = Data.DataLoader(dataset=test_data, batch_size=128 * 50, shuffle=False) |
| 19 | + |
| 20 | +train_batch_num = len(train_loader) |
| 21 | +test_batch_num = len(test_loader) |
| 22 | + |
| 23 | +net = Network() |
| 24 | +if torch.cuda.is_available(): |
| 25 | + # net = nn.DataParallel(net) |
| 26 | + net.cuda() |
| 27 | + |
| 28 | +# +++++++++++++++++++++++++++++++ |
| 29 | +scaler = amp.GradScaler() |
| 30 | +# +++++++++++++++++++++++++++++++ |
| 31 | + |
| 32 | +opt = torch.optim.Adam(net.parameters(), lr=0.001) |
| 33 | +loss_func = nn.CrossEntropyLoss() |
| 34 | + |
| 35 | +for epoch_index in range(10): |
| 36 | + st = time.time() |
| 37 | + |
| 38 | + torch.set_grad_enabled(True) |
| 39 | + net.train() |
| 40 | + for train_batch_index, (img_batch, label_batch) in enumerate(train_loader): |
| 41 | + if torch.cuda.is_available(): |
| 42 | + img_batch = img_batch.cuda() |
| 43 | + label_batch = label_batch.cuda() |
| 44 | + |
| 45 | + # ++++++++++++++++++++++++++++++++++++++++++++++ |
| 46 | + # predict = net(img_batch) |
| 47 | + # loss = loss_func(predict, label_batch) |
| 48 | + with amp.autocast(): |
| 49 | + predict = net(img_batch) |
| 50 | + loss = loss_func(predict, label_batch) |
| 51 | + # ++++++++++++++++++++++++++++++++++++++++++++++ |
| 52 | + |
| 53 | + net.zero_grad() |
| 54 | + # ++++++++++++++++++++++++++++++++++++++++++++++ |
| 55 | + # loss.backward() |
| 56 | + # opt.step() |
| 57 | + scaler.scale(loss).backward() |
| 58 | + scaler.step(opt) |
| 59 | + scaler.update() |
| 60 | + # ++++++++++++++++++++++++++++++++++++++++++++++ |
| 61 | + |
| 62 | + print('(LR:%f) Time of a epoch:%.4fs' % (opt.param_groups[0]['lr'], time.time()-st)) |
| 63 | + |
| 64 | + torch.set_grad_enabled(False) |
| 65 | + net.eval() |
| 66 | + total_loss = [] |
| 67 | + total_acc = 0 |
| 68 | + total_sample = 0 |
| 69 | + |
| 70 | + for test_batch_index, (img_batch, label_batch) in enumerate(test_loader): |
| 71 | + if torch.cuda.is_available(): |
| 72 | + img_batch = img_batch.cuda() |
| 73 | + label_batch = label_batch.cuda() |
| 74 | + |
| 75 | + predict = net(img_batch) |
| 76 | + loss = loss_func(predict, label_batch) |
| 77 | + |
| 78 | + predict = predict.argmax(dim=1) |
| 79 | + acc = (predict == label_batch).sum() |
| 80 | + |
| 81 | + total_loss.append(loss) |
| 82 | + total_acc += acc |
| 83 | + total_sample += img_batch.size(0) |
| 84 | + |
| 85 | + net.train() |
| 86 | + |
| 87 | + mean_acc = total_acc.item() * 1.0 / total_sample |
| 88 | + mean_loss = sum(total_loss) / total_loss.__len__() |
| 89 | + |
| 90 | + print('[Test] epoch[%d/%d] acc:%.4f%% loss:%.4f\n' |
| 91 | + % (epoch_index, 10, mean_acc * 100, mean_loss.item())) |
| 92 | + |
| 93 | +# weight_path = 'weights/net.pth' |
| 94 | +# print('Save Net weights to', weight_path) |
| 95 | +# net.cpu() |
| 96 | +# torch.save(net.state_dict(), weight_path) |
0 commit comments