From 61526433d2f56a669dd077de7920ada32b6008ad Mon Sep 17 00:00:00 2001 From: natanielruiz <nataniel777@hotmail.com> Date: 星期二, 11 七月 2017 11:21:08 +0800 Subject: [PATCH] next --- code/test_resnet_bins.py | 88 ++++++++++++++++++++++++++++++++++---------- 1 files changed, 68 insertions(+), 20 deletions(-) diff --git a/code/test_resnet_bins.py b/code/test_resnet_bins.py index 0a093ee..8d0eaec 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 @@ -30,6 +31,8 @@ default='', type=str) parser.add_argument('--batch_size', dest='batch_size', help='Batch size.', default=1, type=int) + parser.add_argument('--save_viz', dest='save_viz', help='Save images with pose cube.', + default=False, type=bool) args = parser.parse_args() @@ -43,10 +46,9 @@ 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) + # model = hopenet.Hopenet(torchvision.models.resnet.BasicBlock, [2, 2, 2, 2], 66) print 'Loading snapshot.' # Load snapshot @@ -70,25 +72,71 @@ # 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) - 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)) + # Binned predictions + _, yaw_bpred = torch.max(yaw.data, 1) + _, pitch_bpred = torch.max(pitch.data, 1) + _, roll_bpred = torch.max(roll.data, 1) + + yaw_predicted = F.softmax(yaw) + pitch_predicted = F.softmax(pitch) + roll_predicted = F.softmax(roll) + + # Continuous predictions + 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) + + # Mean absolute error + 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 + + # Binned Accuracy + # for er in xrange(n_margins): + # yaw_bpred[er] += (label_yaw[0] in range(yaw_bpred[0,0] - er, yaw_bpred[0,0] + er + 1)) + # pitch_bpred[er] += (label_pitch[0] in range(pitch_bpred[0,0] - er, pitch_bpred[0,0] + er + 1)) + # roll_bpred[er] += (label_roll[0] in range(roll_bpred[0,0] - er, roll_bpred[0,0] + er + 1)) + + # print label_yaw[0], yaw_bpred[0,0] + + # Save images with pose cube. + if args.save_viz: + name = name[0] + cv2_img = cv2.imread(os.path.join(args.data_dir, name + '.jpg')) + #cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_RGB2BGR) + #print name + #print label_yaw[0] * 3 - 99, label_pitch[0] * 3 - 99, label_roll[0] * 3 - 99 + #print yaw_predicted * 3 - 99, pitch_predicted * 3 - 99, roll_predicted * 3 - 99 + utils.plot_pose_cube(cv2_img, yaw_predicted * 3 - 99, pitch_predicted * 3 - 99, roll_predicted * 3 - 99) + cv2.imwrite(os.path.join('output/images', name + '.jpg'), cv2_img) + + 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)) + + # Binned accuracy + # for idx in xrange(len(yaw_correct)): + # print yaw_correct[idx] / total, pitch_correct[idx] / total, roll_correct[idx] / total -- Gitblit v1.8.0