| | |
| | | self.fc_pitch = nn.Linear(512 * block.expansion, num_bins) |
| | | self.fc_roll = nn.Linear(512 * block.expansion, num_bins) |
| | | |
| | | # Vestigial layer from previous experiments |
| | | self.fc_finetune = nn.Linear(512 * block.expansion + 3, 3) |
| | | |
| | | # Used to get the expected value of angle from bins |
| | | self.softmax = nn.Softmax() |
| | | self.idx_tensor = Variable(torch.FloatTensor(range(66))).cuda() |
| | | |
| | | for m in self.modules(): |
| | | if isinstance(m, nn.Conv2d): |
| | |
| | | pre_pitch = self.fc_pitch(x) |
| | | pre_roll = self.fc_roll(x) |
| | | |
| | | yaw = self.softmax(pre_yaw) |
| | | yaw = Variable(torch.sum(yaw.data * self.idx_tensor.data, 1), requires_grad=True) |
| | | pitch = self.softmax(pre_pitch) |
| | | pitch = Variable(torch.sum(pitch.data * self.idx_tensor.data, 1), requires_grad=True) |
| | | roll = self.softmax(pre_roll) |
| | | roll = Variable(torch.sum(roll.data * self.idx_tensor.data, 1), requires_grad=True) |
| | | yaw = yaw.view(yaw.size(0), 1) |
| | | pitch = pitch.view(pitch.size(0), 1) |
| | | roll = roll.view(roll.size(0), 1) |
| | | preangles = torch.cat([yaw, pitch, roll], 1) |
| | | |
| | | return pre_yaw, pre_pitch, pre_roll, preangles |
| | | return pre_yaw, pre_pitch, pre_roll |
| | | |
| | | class ResNet(nn.Module): |
| | | # ResNet for regression of 3 Euler angles. |
| | |
| | | |
| | | return args |
| | | |
| | | def load_filtered_state_dict(model, snapshot): |
| | | # By user apaszke from discuss.pytorch.org |
| | | model_dict = model.state_dict() |
| | | snapshot = {k: v for k, v in snapshot.items() if k in model_dict} |
| | | model_dict.update(snapshot) |
| | | model.load_state_dict(model_dict) |
| | | |
| | | if __name__ == '__main__': |
| | | args = parse_args() |
| | | |
| | |
| | | print 'Loading snapshot.' |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | load_filtered_state_dict(model, saved_state_dict) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | |
| | |
| | | if not os.path.exists(args.video_path): |
| | | sys.exit('Video does not exist') |
| | | |
| | | # ResNet50 |
| | | # ResNet50 structure |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) |
| | | |
| | | print 'Loading snapshot.' |
| | |
| | | img = img.view(1, img_shape[0], img_shape[1], img_shape[2]) |
| | | img = Variable(img).cuda(gpu) |
| | | |
| | | yaw, pitch, roll, angles = model(img) |
| | | yaw, pitch, roll = model(img) |
| | | |
| | | yaw_predicted = angles[:,0].data[0].cpu() |
| | | pitch_predicted = angles[:,1].data[0].cpu() |
| | | roll_predicted = angles[:,2].data[0].cpu() |
| | | yaw_predicted = F.softmax(yaw) |
| | | pitch_predicted = F.softmax(pitch) |
| | | roll_predicted = F.softmax(roll) |
| | | # Get continuous predictions in degrees. |
| | | yaw_predicted = torch.sum(yaw_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | pitch_predicted = torch.sum(pitch_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | roll_predicted = torch.sum(roll_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | |
| | | # Print new frame with cube and axis |
| | | txt_out.write(str(frame_num) + ' %f %f %f\n' % (yaw_predicted, pitch_predicted, roll_predicted)) |
| | | # utils.plot_pose_cube(frame, yaw_predicted, pitch_predicted, roll_predicted, (x_min + x_max) / 2, (y_min + y_max) / 2, size = bbox_width) |
| | |
| | | img_shape = img.size() |
| | | img = img.view(1, img_shape[0], img_shape[1], img_shape[2]) |
| | | img = Variable(img).cuda(gpu) |
| | | |
| | | yaw, pitch, roll, angles = model(img) |
| | | |
| | | yaw_predicted = angles[:,0].data[0].cpu() |
| | | pitch_predicted = angles[:,1].data[0].cpu() |
| | | roll_predicted = angles[:,2].data[0].cpu() |
| | | yaw, pitch, roll = model(img) |
| | | |
| | | yaw_predicted = F.softmax(yaw) |
| | | pitch_predicted = F.softmax(pitch) |
| | | roll_predicted = F.softmax(roll) |
| | | # Get continuous predictions in degrees. |
| | | yaw_predicted = torch.sum(yaw_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | pitch_predicted = torch.sum(pitch_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | roll_predicted = torch.sum(roll_predicted.data[0] * idx_tensor) * 3 - 99 |
| | | |
| | | # Print new frame with cube and axis |
| | | txt_out.write(str(frame_num) + ' %f %f %f\n' % (yaw_predicted, pitch_predicted, roll_predicted)) |
| | |
| | | |
| | | return args |
| | | |
| | | def load_filtered_state_dict(model, snapshot): |
| | | # By user apaszke from discuss.pytorch.org |
| | | model_dict = model.state_dict() |
| | | snapshot = {k: v for k, v in snapshot.items() if k in model_dict} |
| | | model_dict.update(snapshot) |
| | | model.load_state_dict(model_dict) |
| | | |
| | | if __name__ == '__main__': |
| | | args = parse_args() |
| | | |
| | |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | # load_filtered_state_dict(model, saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | |
| | |
| | | |
| | | l1loss = torch.nn.L1Loss(size_average=False) |
| | | |
| | | |
| | | |
| | | for i, (images, labels, cont_labels, name) in enumerate(test_loader): |
| | | images = Variable(images).cuda(gpu) |
| | | total += cont_labels.size(0) |
| | |
| | | label_pitch = cont_labels[:,1].float() |
| | | label_roll = cont_labels[:,2].float() |
| | | |
| | | yaw, pitch, roll, angles = model(images) |
| | | yaw, pitch, roll = model(images) |
| | | |
| | | # Binned predictions |
| | | _, yaw_bpred = torch.max(yaw.data, 1) |
| | |
| | | |
| | | return args |
| | | |
| | | def load_filtered_state_dict(model, snapshot): |
| | | # By user apaszke from discuss.pytorch.org |
| | | model_dict = model.state_dict() |
| | | snapshot = {k: v for k, v in snapshot.items() if k in model_dict} |
| | | model_dict.update(snapshot) |
| | | model.load_state_dict(model_dict) |
| | | |
| | | if __name__ == '__main__': |
| | | args = parse_args() |
| | | |
| | |
| | | print 'Loading snapshot.' |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | load_filtered_state_dict(model, saved_state_dict) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | |
| | |
| | | # Regression loss coefficient |
| | | alpha = args.alpha |
| | | |
| | | softmax = nn.Softmax().cuda(gpu) |
| | | idx_tensor = [idx for idx in xrange(66)] |
| | | idx_tensor = Variable(torch.FloatTensor(idx_tensor)).cuda(gpu) |
| | | |
| | | optimizer = torch.optim.Adam([{'params': get_ignored_params(model), 'lr': 0}, |
| | | {'params': get_non_ignored_params(model), 'lr': args.lr}, |
| | | {'params': get_fc_params(model), 'lr': args.lr * 5}], |
| | |
| | | loss_roll = criterion(roll, label_roll) |
| | | |
| | | # MSE loss |
| | | yaw_predicted = angles[:,0] |
| | | pitch_predicted = angles[:,1] |
| | | roll_predicted = angles[:,2] |
| | | yaw_predicted = softmax(yaw) |
| | | pitch_predicted = softmax(pitch) |
| | | roll_predicted = softmax(roll) |
| | | |
| | | yaw_predicted = torch.sum(yaw_predicted * idx_tensor, 1) * 3 - 99 |
| | | pitch_predicted = torch.sum(pitch_predicted * idx_tensor, 1) * 3 - 99 |
| | | roll_predicted = torch.sum(roll_predicted * idx_tensor, 1) * 3 - 99 |
| | | |
| | | loss_reg_yaw = reg_criterion(yaw_predicted, label_yaw_cont) |
| | | loss_reg_pitch = reg_criterion(pitch_predicted, label_pitch_cont) |