From 93a4f337f2fd0280634024d2ff15790831813bed Mon Sep 17 00:00:00 2001 From: natanielruiz <nataniel777@hotmail.com> Date: 星期五, 07 七月 2017 14:33:47 +0800 Subject: [PATCH] Resnet50, and changed test error --- code/datasets.py | 12 ++--- code/test_resnet_bins.py | 67 +++++++++++++++++++++++---------- code/train_resnet_bins.py | 18 ++++---- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/code/datasets.py b/code/datasets.py index 3750e71..0ab364e 100644 --- a/code/datasets.py +++ b/code/datasets.py @@ -121,19 +121,17 @@ # We get the pose in radians pose = utils.get_ypr_from_mat(os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)) # And convert to degrees. - pitch, yaw, roll = pose * 180 / np.pi + pitch = pose[0] * 180 / np.pi + yaw = pose[1] * 180 / np.pi + roll = pose[2] * 180 / np.pi # Bin values bins = np.array(range(-99, 102, 3)) - binned_pitch = torch.DoubleTensor(np.digitize(pitch, bins) - 1) - binned_yaw = torch.DoubleTensor(np.digitize(yaw, bins) - 1) - binned_roll = torch.DoubleTensor(np.digitize(roll, bins) - 1) - - label = binned_yaw, binned_pitch, binned_roll + labels = torch.LongTensor(np.digitize([yaw, pitch, roll], bins) - 1) if self.transform is not None: img = self.transform(img) - return img, label, self.X_train[index] + return img, labels, self.X_train[index] def __len__(self): # 2,000 diff --git a/code/test_resnet_bins.py b/code/test_resnet_bins.py index 0a093ee..f5be4f8 100644 --- a/code/test_resnet_bins.py +++ b/code/test_resnet_bins.py @@ -6,6 +6,7 @@ from torchvision import transforms import torch.backends.cudnn as cudnn import torchvision +import torch.nn.functional as F import cv2 import matplotlib.pyplot as plt @@ -43,10 +44,8 @@ gpu = args.gpu_id snapshot_path = os.path.join('output/snapshots', args.snapshot + '.pkl') - model = torchvision.models.resnet18() - # Parameters of newly constructed modules have requires_grad=True by default - num_ftrs = model.fc.in_features - model.fc = nn.Linear(num_ftrs, 3) + # ResNet50 with 3 outputs. + model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) print 'Loading snapshot.' # Load snapshot @@ -70,25 +69,53 @@ # Test the Model model.eval() # Change model to 'eval' mode (BN uses moving mean/var). - yaw_correct = 0 - pitch_correct = 0 - roll_correct = 0 total = 0 + n_margins = 20 + yaw_correct = np.zeros(n_margins) + pitch_correct = np.zeros(n_margins) + roll_correct = np.zeros(n_margins) + + idx_tensor = [idx for idx in xrange(66)] + idx_tensor = torch.FloatTensor(idx_tensor).cuda(gpu) + + yaw_error = .0 + pitch_error = .0 + roll_error = .0 + for i, (images, labels, name) in enumerate(test_loader): images = Variable(images).cuda(gpu) - labels = Variable(labels).cuda(gpu) - outputs = model(images) - _, predicted = torch.max(outputs.data, 1) + total += labels.size(0) - # TODO: There are more efficient ways. - yaw_correct += (outputs[:][0] == labels[:][0]) - pitch_correct += (outputs[:][]) - for idx in xrange(len(outputs)): - yaw_correct += (outputs[idx].data[0] == labels[idx].data[0]) - pitch_correct += (outputs[idx].data[1] == labels[idx].data[1]) - roll_correct += (outputs[idx].data[2] == labels[idx].data[2]) + label_yaw = labels[:,0] + label_pitch = labels[:,1] + label_roll = labels[:,2] + yaw, pitch, roll = model(images) + # _, yaw_predicted = torch.max(yaw.data, 1) + # _, pitch_predicted = torch.max(pitch.data, 1) + # _, roll_predicted = torch.max(roll.data, 1) - print('Test accuracies of the model on the ' + str(total) + - ' test images. Yaw: %.4f %%, Pitch: %.4f %%, Roll: %.4f %%' % (yaw_correct / total, - pitch_correct / total, roll_correct / total)) + yaw_predicted = F.softmax(yaw) + pitch_predicted = F.softmax(pitch) + roll_predicted = F.softmax(roll) + + yaw_predicted = torch.sum(yaw_predicted.data[0] * idx_tensor) + pitch_predicted = torch.sum(pitch_predicted.data[0] * idx_tensor) + roll_predicted = torch.sum(roll_predicted.data[0] * idx_tensor) + + yaw_error += abs(yaw_predicted - label_yaw[0]) * 3 + pitch_error += abs(pitch_predicted - label_pitch[0]) * 3 + roll_error += abs(roll_predicted - label_roll[0]) * 3 + + # for er in xrange(0,n_margins): + # yaw_correct[er] += (label_yaw[0] in range(yaw_predicted[0,0] - er, yaw_predicted[0,0] + er + 1)) + # pitch_correct[er] += (label_pitch[0] in range(pitch_predicted[0,0] - er, pitch_predicted[0,0] + er + 1)) + # roll_correct[er] += (label_roll[0] in range(roll_predicted[0,0] - er, roll_predicted[0,0] + er + 1)) + + # print label_yaw[0], yaw_predicted[0,0] + # 4 -> 15 + print('Test error in degrees of the model on the ' + str(total) + + ' test images. Yaw: %.4f, Pitch: %.4f, Roll: %.4f' % (yaw_error / total, + pitch_error / total, roll_error / total)) + # for idx in xrange(len(yaw_correct)): + # print yaw_correct[idx] / total, pitch_correct[idx] / total, roll_correct[idx] / total diff --git a/code/train_resnet_bins.py b/code/train_resnet_bins.py index 1bbf5be..f33ffd6 100644 --- a/code/train_resnet_bins.py +++ b/code/train_resnet_bins.py @@ -91,10 +91,10 @@ if not os.path.exists('output/snapshots'): os.makedirs('output/snapshots') - # ResNet18 with 3 outputs. - model = hopenet.Hopenet(torchvision.models.resnet.BasicBlock, [2, 2, 2, 2], 66) - load_filtered_state_dict(model, model_zoo.load_url(model_urls['resnet18'])) - + # ResNet50 with 3 outputs. + model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) + load_filtered_state_dict(model, model_zoo.load_url(model_urls['resnet50'])) + print 'Loading data.' transformations = transforms.Compose([transforms.Scale(224),transforms.RandomCrop(224), @@ -109,8 +109,8 @@ model.cuda(gpu) criterion = nn.CrossEntropyLoss() - optimizer = torch.optim.Adam([{'params': get_ignored_params(model), 'lr': .0}, - {'params': get_non_ignored_params(model), 'lr': args.lr}], + optimizer = torch.optim.Adam([{'params': get_ignored_params(model), 'lr': args.lr}, + {'params': get_non_ignored_params(model), 'lr': args.lr * 10}], lr = args.lr) print 'Ready to train network.' @@ -137,11 +137,11 @@ print ('Epoch [%d/%d], Iter [%d/%d] Losses: Yaw %.4f, Pitch %.4f, Roll %.4f' %(epoch+1, num_epochs, i+1, len(pose_dataset)//batch_size, loss_yaw.data[0], loss_pitch.data[0], loss_roll.data[0])) - # Save models at even numbered epochs. + # Save models at numbered epochs. if epoch % 1 == 0 and epoch < num_epochs - 1: print 'Taking snapshot...' torch.save(model.state_dict(), - 'output/snapshots/resnet18_binned_epoch_' + str(epoch+1) + '.pkl') + 'output/snapshots/resnet50_binned_epoch_' + str(epoch+1) + '.pkl') # Save the final Trained Model - torch.save(model.state_dict(), 'output/snapshots/resnet18_binned_epoch_' + str(epoch+1) + '.pkl') + torch.save(model.state_dict(), 'output/snapshots/resnet50_binned_epoch_' + str(epoch+1) + '.pkl') -- Gitblit v1.8.0