Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import torch
- import torchvision
- import matplotlib.pyplot as plt
- import numpy as np
- import torch.nn as nn
- import torch.nn.functional as F
- import torch.optim as optim
- from torch.utils.data import Dataset
- import time
- class Net(nn.Module):
- def __init__(self):
- super(Net, self).__init__()
- self.conv1 = nn.Conv2d(3, 6, 5, 2, 2)
- self.pool = nn.MaxPool2d(3, 2)
- self.conv2 = nn.Conv2d(6, 16, 5, 2, 2)
- self.fc1 = nn.Linear(784, 240)
- self.fc2 = nn.Linear(240, 84)
- self.fc3 = nn.Linear(84, 12)
- self.dropout = nn.Dropout(p=0.5)
- def forward(self, x):
- x = self.pool(F.relu(self.conv1(x)))
- x = self.pool(F.relu(self.conv2(x)))
- x = x.view(-1, self.num_flat_features(x))
- x = self.dropout(x)
- x = F.relu(self.fc1(x))
- x = F.relu(self.fc2(x))
- x = self.fc3(x)
- return x
- def num_flat_features(self, x):
- size = x.size()[1:] # all dimensions except the batch dimension
- num_features = 1
- for s in size:
- num_features *= s
- return num_features
- class Memory(Dataset):
- def __init__(self, dataset_array, dataset_labels):
- self.labels = dataset_labels.astype(np.float64)
- self.images = dataset_array.astype(np.float64)
- def __len__(self):
- return self.images.shape[0]
- def __getitem__(self, idx):
- return self.images[idx], self.labels[idx]
- def dataloader(batch_size, num_workers=4):
- print("Data loading...")
- training_images = np.load("training_images.npy")
- training_labels = np.load("training_labels.npy")
- trainset = Memory(training_images, training_labels)
- trainset_length = trainset.__len__()
- trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=num_workers, pin_memory=False)
- cv_images = np.load("cv_images.npy")
- cv_labels = np.load("cv_labels.npy")
- cvset = Memory(cv_images, cv_labels)
- cvset_length = cvset.__len__()
- cvloader = torch.utils.data.DataLoader(cvset, batch_size=cvset_length, shuffle=False, num_workers=num_workers, pin_memory=False)
- test_images = np.load("test_images.npy")
- test_labels = np.load("test_labels.npy")
- testset = Memory(test_images, test_labels)
- testset_length = testset.__len__()
- testloader = torch.utils.data.DataLoader(testset, batch_size=testset_length, shuffle=False, num_workers=num_workers, pin_memory=False)
- trainloader2 = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=num_workers, pin_memory=False)
- testloader2 = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=True, num_workers=num_workers, pin_memory=False)
- print("Data loaded.")
- return trainloader, cvloader, testloader, trainset_length, trainloader2, testloader2
- def training(trainloader, cvloader, testloader, trainset_length, batch_size, net, opt, epochs, lr, weight_decay, step_size, gamma=0.1):
- criterion = nn.CrossEntropyLoss()
- adam = optim.Adam(net.parameters(), lr=lr, weight_decay=weight_decay)
- adagrad = optim.Adagrad(net.parameters(), lr=lr, weight_decay=weight_decay)
- sgd = optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=weight_decay)
- if opt == 1:
- optimizer = adam
- elif opt == 2:
- optimizer = adagrad
- else:
- optimizer = sgd
- scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=step_size, gamma=gamma)
- best_acc = 0
- # print("Training started...")
- since = time.time()
- for epoch in range(epochs): # loop over the dataset multiple times
- net.train()
- running_loss = 0.0
- for i, data in enumerate(trainloader, 0):
- # get the inputs
- inputs, labels = data
- inputs = inputs.float().to(device)
- labels = labels.long().to(device)
- # zero the parameter gradients
- optimizer.zero_grad()
- # forward + backward + optimize
- outputs = net(inputs)
- loss = criterion(outputs, labels)
- loss.backward()
- optimizer.step()
- # print statistics
- running_loss += loss.item()
- running_loss = running_loss / (trainset_length // batch_size)
- # print('Epoch: %d Loss: %.5f' % (epoch + 1, running_loss))
- cv_acc = accshow(cvloader, net)
- if cv_acc > best_acc:
- best_acc = cv_acc
- torch.save(net, "train_model.pth")
- best_epoch = epoch + 1
- best_epoch_lr = epoch + 1
- best_lr = lr
- if (epoch + 1) - best_epoch_lr >= 50:
- lr = lr / 10
- if opt == 1:
- optimizer = adam
- elif opt == 2:
- optimizer = adagrad
- else:
- optimizer = sgd
- best_epoch_lr = best_epoch_lr + 30
- # print("Learning rate changed to %f" % lr)
- if (epoch + 1) - best_epoch >= 100:
- break
- # if epoch % 20 == 19:
- # print('Train images accuracy: %d%s' % (accshow(trainloader, net), "%"))
- # print('Validation images accuracy: %d%s' % (cv_acc, "%"))
- # print('Test images accuracy: %d%s' % (accshow(testloader, net), "%"))
- scheduler.step()
- time_elapsed = time.time() - since
- # print('Training completed in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
- best_model = torch.load("train_model.pth")
- return best_model, best_epoch, best_lr
- def checking(trainloader, cvloader, testloader, trainset_length, batch_size, epochs, initial_lr, step_size, loops):
- cv_score, test_score = [0, 0]
- opt_list = [1, 2, 3]
- decay_list = [0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001]
- print("Checking started...")
- since = time.time()
- for opt in opt_list:
- for weight in decay_list:
- for i in range(loops):
- net = Net()
- net = net.to(device)
- trained_net, best_epoch, lr = training(trainloader, cvloader, testloader, trainset_length, batch_size, net, opt, epochs, initial_lr, weight, step_size)
- trained_net = trained_net.to(device)
- train_acc = accshow(trainloader, trained_net)
- cv_acc = accshow(cvloader, trained_net)
- test_acc = accshow(testloader, trained_net)
- print("Train: %d%s CV: %d%s Test: %d%s Opt: %d Rate: %.4f Decay: %.4f Loop: %d Epoch: %d" % (train_acc, "%", cv_acc, "%", test_acc, "%", opt, lr, weight, i+1, best_epoch))
- if cv_acc > cv_score:
- cv_score = cv_acc
- cv_train = train_acc
- cv_test = test_acc
- cv_opt = opt
- cv_rate = lr
- cv_decay = weight
- cv_epoch = best_epoch
- torch.save(trained_net, "check_model_val.pth")
- if test_acc > test_score:
- test_score = test_acc
- test_train = train_acc
- test_cv = cv_acc
- test_opt = opt
- test_rate = lr
- test_decay = weight
- test_epoch = best_epoch
- torch.save(trained_net, "check_model_test.pth")
- time_elapsed = time.time() - since
- print('Checking completed in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
- print("Best cross-validation set accuracy:")
- print("Train: %d%s CV: %d%s Test: %d%s Opt: %d Rate: %.4f Decay: %.4f Epoch: %d" % (cv_train, "%", cv_score, "%", cv_test, "%", cv_opt, cv_rate, cv_decay, cv_epoch))
- print("Best test set accuracy:")
- print("Train: %d%s CV: %d%s Test: %d%s Opt: %d Rate: %.4f Decay: %.4f Epoch: %d" % (test_train, "%", test_cv, "%", test_score, "%", test_opt, test_rate, test_decay, test_epoch))
- cv_net = torch.load("check_model_val.pth")
- test_net = torch.load("check_model_test.pth")
- return cv_net, test_net
- def accshow(loader, net):
- net.eval()
- correct = 0
- total = 0
- with torch.no_grad():
- for data in loader:
- inputs, labels = data
- inputs = inputs.float().to(device)
- labels = labels.long().to(device)
- outputs = net(inputs)
- _, predicted = torch.max(outputs.data, 1)
- total += labels.size(0)
- correct += (predicted == labels).sum().item()
- return (100 * correct / total)
- def classaccshow(loader, net, classes, classes_length):
- net.eval()
- class_correct = list(0. for i in range(classes_length))
- class_total = list(0. for i in range(classes_length))
- with torch.no_grad():
- for data in loader:
- inputs, labels = data
- inputs = inputs.float().to(device)
- labels = labels.long().to(device)
- outputs = net(inputs)
- _, predicted = torch.max(outputs, 1)
- c = (predicted == labels).squeeze()
- try:
- for i in range(100):
- label = labels[i]
- class_correct[label] += c[i].item()
- class_total[label] += 1
- except IndexError:
- pass
- for i in range(classes_length):
- print('Accuracy of %5s : %2d %%' % (classes[i], 100 * class_correct[i] / class_total[i]))
- def imshow(img):
- img = img * 0.13 + 0.5 # unnormalize
- npimg = img.numpy()
- plt.imshow(np.transpose(npimg, (1, 2, 0)))
- plt.show()
- def imgshow(loader, net, classes):
- net.eval()
- dataiter = iter(loader)
- inputs, labels = dataiter.next()
- imshow(torchvision.utils.make_grid(inputs))
- print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))
- inputs = inputs.to(device)
- outputs = net(inputs)
- _, predicted = torch.max(outputs, 1)
- print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
- if __name__ == '__main__':
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
- classes = ('alder', 'beech', 'birch', 'chestnut', 'gingko biloba', 'hornbeam', 'horse chestnut', 'linden', 'oak', 'oriental plane', 'pine', 'spruce')
- classes_length = len(classes)
- batch_size = 35
- trainloader, cvloader, testloader, trainset_length, trainloader2, testloader2 = dataloader(batch_size)
- net = Net()
- net = net.to(device)
- epochs = 999
- step_size = epochs
- opt = 2
- learning_rate = 0.01
- weight_decay = 0.0005
- # model, best_epoch, best_lr = training(trainloader, cvloader, testloader, trainset_length, batch_size, net, opt, epochs, learning_rate, weight_decay, step_size)
- # print("Best net score was in %d epoch with %f learning rate." % (best_epoch, best_lr))
- model, model_test = checking(trainloader, cvloader, testloader, trainset_length, batch_size, epochs, learning_rate, step_size, loops=3)
- # model = torch.load("best_val.pth", map_location=device)
- model = model.to(device)
- print("Final model:")
- print('Train images accuracy: %d%s' % (accshow(trainloader, model), "%"))
- print('Validation images accuracy: %d%s' % (accshow(cvloader, model), "%"))
- print('Test images accuracy: %d%s' % (accshow(testloader, model), "%"))
- # classaccshow(cvloader, model, classes, classes_length)
- # classaccshow(testloader, model, classes, classes_length)
- # imgshow(testloader2, model, classes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement