| | |
| | | |
| | | model = hopenet.AlexNet(66) |
| | | |
| | | print 'Loading snapshot.' |
| | | print('Loading snapshot.') |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(224), |
| | | transforms.CenterCrop(224), transforms.ToTensor(), |
| | |
| | | elif args.dataset == 'AFW': |
| | | pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations) |
| | | else: |
| | | print 'Error: not a valid dataset name' |
| | | print('Error: not a valid dataset name') |
| | | sys.exit() |
| | | test_loader = torch.utils.data.DataLoader(dataset=pose_dataset, |
| | | batch_size=args.batch_size, |
| | |
| | | |
| | | model.cuda(gpu) |
| | | |
| | | print 'Ready to test network.' |
| | | print ('Ready to test network.') |
| | | |
| | | # Test the Model |
| | | model.eval() # Change model to 'eval' mode (BN uses moving mean/var). |
| | |
| | | # ResNet50 structure |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) |
| | | |
| | | print 'Loading snapshot.' |
| | | print('Loading snapshot.') |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(224), |
| | | transforms.CenterCrop(224), transforms.ToTensor(), |
| | |
| | | elif args.dataset == 'AFW': |
| | | pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations) |
| | | else: |
| | | print 'Error: not a valid dataset name' |
| | | print('Error: not a valid dataset name') |
| | | sys.exit() |
| | | test_loader = torch.utils.data.DataLoader(dataset=pose_dataset, |
| | | batch_size=args.batch_size, |
| | |
| | | |
| | | model.cuda(gpu) |
| | | |
| | | print 'Ready to test network.' |
| | | print('Ready to test network.') |
| | | |
| | | # Test the Model |
| | | model.eval() # Change model to 'eval' mode (BN uses moving mean/var). |
| | |
| | | # ResNet50 structure |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) |
| | | |
| | | print 'Loading snapshot.' |
| | | print('Loading snapshot.') |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(224), |
| | | transforms.CenterCrop(224), transforms.ToTensor(), |
| | |
| | | |
| | | model.cuda(gpu) |
| | | |
| | | print 'Ready to test network.' |
| | | print('Ready to test network.') |
| | | |
| | | # Test the Model |
| | | model.eval() # Change model to 'eval' mode (BN uses moving mean/var). |
| | |
| | | line = line.split(' ') |
| | | det_frame_num = int(line[0]) |
| | | |
| | | print frame_num |
| | | print(frame_num) |
| | | |
| | | # Stop at a certain frame number |
| | | if frame_num > args.n_frames: |
| | |
| | | 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 |
| | | # 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) |
| | | utils.draw_axis(frame, yaw_predicted, pitch_predicted, roll_predicted, tdx = (x_min + x_max) / 2, tdy= (y_min + y_max) / 2, size = bbox_height/2) |
| | |
| | | |
| | | # Peek next frame detection |
| | | next_frame_num = int(bbox_line_list[idx+1].strip('\n').split(' ')[0]) |
| | | # print 'next_frame_num ', next_frame_num |
| | | # print('next_frame_num ', next_frame_num |
| | | if next_frame_num == det_frame_num: |
| | | idx += 1 |
| | | line = bbox_line_list[idx].strip('\n').split(' ') |
| | |
| | | # Dlib face detection model |
| | | cnn_face_detector = dlib.cnn_face_detection_model_v1(args.face_model) |
| | | |
| | | print 'Loading snapshot.' |
| | | print('Loading snapshot.') |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print ('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(224), |
| | | transforms.CenterCrop(224), transforms.ToTensor(), |
| | |
| | | |
| | | model.cuda(gpu) |
| | | |
| | | print 'Ready to test network.' |
| | | print('Ready to test network.') |
| | | |
| | | # Test the Model |
| | | model.eval() # Change model to 'eval' mode (BN uses moving mean/var). |
| | | total = 0 |
| | | |
| | | idx_tensor = [idx for idx in xrange(66)] |
| | | idx_tensor = [idx for idx in range(66)] |
| | | idx_tensor = torch.FloatTensor(idx_tensor).cuda(gpu) |
| | | |
| | | video = cv2.VideoCapture(video_path) |
| | |
| | | frame_num = 1 |
| | | |
| | | while frame_num <= args.n_frames: |
| | | print frame_num |
| | | print(frame_num) |
| | | |
| | | ret,frame = video.read() |
| | | if ret == False: |
| | |
| | | x_min = max(x_min, 0); y_min = max(y_min, 0) |
| | | x_max = min(frame.shape[1], x_max); y_max = min(frame.shape[0], y_max) |
| | | # Crop image |
| | | img = cv2_frame[y_min:y_max,x_min:x_max] |
| | | img = cv2_frame[int(y_min):int(y_max),int(x_min):int(x_max)] |
| | | img = Image.fromarray(img) |
| | | |
| | | # Transform |
| | |
| | | # ResNet50 structure |
| | | model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66) |
| | | |
| | | print 'Loading snapshot.' |
| | | print('Loading snapshot.') |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(224), |
| | | transforms.CenterCrop(224), transforms.ToTensor(), |
| | |
| | | |
| | | model.cuda(gpu) |
| | | |
| | | print 'Ready to test network.' |
| | | print('Ready to test network.') |
| | | |
| | | # Test the Model |
| | | model.eval() # Change model to 'eval' mode (BN uses moving mean/var). |
| | |
| | | line = line.split(' ') |
| | | det_frame_num = int(line[0]) |
| | | |
| | | print frame_num |
| | | print(frame_num) |
| | | |
| | | # Stop at a certain frame number |
| | | if frame_num > args.n_frames: |
| | |
| | | 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 |
| | | # 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) |
| | | utils.draw_axis(frame, yaw_predicted, pitch_predicted, roll_predicted, tdx = (x_min + x_max) / 2, tdy= (y_min + y_max) / 2, size = bbox_height/2) |
| | |
| | | |
| | | # Peek next frame detection |
| | | next_frame_num = int(bbox_line_list[idx+1].strip('\n').split(' ')[0]) |
| | | # print 'next_frame_num ', next_frame_num |
| | | # print('next_frame_num ', next_frame_num |
| | | if next_frame_num == det_frame_num: |
| | | idx += 1 |
| | | line = bbox_line_list[idx].strip('\n').split(' ') |
| | |
| | | |
| | | model = hopenet.ResNet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 3) |
| | | |
| | | print 'Loading snapshot.' |
| | | print('Loading snapshot.') |
| | | # Load snapshot |
| | | saved_state_dict = torch.load(snapshot_path) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(224), |
| | | transforms.CenterCrop(224), transforms.ToTensor(), |
| | |
| | | elif args.dataset == 'AFW': |
| | | pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations) |
| | | else: |
| | | print 'Error: not a valid dataset name' |
| | | print('Error: not a valid dataset name') |
| | | sys.exit() |
| | | test_loader = torch.utils.data.DataLoader(dataset=pose_dataset, |
| | | batch_size=args.batch_size, |
| | |
| | | |
| | | model.cuda(gpu) |
| | | |
| | | print 'Ready to test network.' |
| | | print('Ready to test network.') |
| | | |
| | | # Test the Model |
| | | model.eval() # Change model to 'eval' mode (BN uses moving mean/var). |
| | |
| | | model = hopenet.AlexNet(66) |
| | | load_filtered_state_dict(model, model_zoo.load_url(model_urls['alexnet'])) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(240), |
| | | transforms.RandomCrop(224), transforms.ToTensor(), |
| | |
| | | elif args.dataset == 'AFW': |
| | | pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations) |
| | | else: |
| | | print 'Error: not a valid dataset name' |
| | | print('Error: not a valid dataset name') |
| | | sys.exit() |
| | | train_loader = torch.utils.data.DataLoader(dataset=pose_dataset, |
| | | batch_size=batch_size, |
| | |
| | | {'params': get_fc_params(model), 'lr': args.lr * 5}], |
| | | lr = args.lr) |
| | | |
| | | print 'Ready to train network.' |
| | | print('Ready to train network.') |
| | | for epoch in range(num_epochs): |
| | | for i, (images, labels, cont_labels, name) in enumerate(train_loader): |
| | | images = Variable(images).cuda(gpu) |
| | |
| | | optimizer.step() |
| | | |
| | | if (i+1) % 100 == 0: |
| | | print ('Epoch [%d/%d], Iter [%d/%d] Losses: Yaw %.4f, Pitch %.4f, Roll %.4f' |
| | | 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 numbered epochs. |
| | | if epoch % 1 == 0 and epoch < num_epochs: |
| | | print 'Taking snapshot...' |
| | | print('Taking snapshot...') |
| | | torch.save(model.state_dict(), |
| | | 'output/snapshots/' + args.output_string + '_epoch_'+ str(epoch+1) + '.pkl') |
| | |
| | | saved_state_dict = torch.load(args.snapshot) |
| | | model.load_state_dict(saved_state_dict) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(240), |
| | | transforms.RandomCrop(224), transforms.ToTensor(), |
| | |
| | | elif args.dataset == 'AFW': |
| | | pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations) |
| | | else: |
| | | print 'Error: not a valid dataset name' |
| | | print('Error: not a valid dataset name') |
| | | sys.exit() |
| | | |
| | | train_loader = torch.utils.data.DataLoader(dataset=pose_dataset, |
| | |
| | | {'params': get_fc_params(model), 'lr': args.lr * 5}], |
| | | lr = args.lr) |
| | | |
| | | print 'Ready to train network.' |
| | | print('Ready to train network.') |
| | | for epoch in range(num_epochs): |
| | | for i, (images, labels, cont_labels, name) in enumerate(train_loader): |
| | | images = Variable(images).cuda(gpu) |
| | |
| | | |
| | | # Save models at numbered epochs. |
| | | if epoch % 1 == 0 and epoch < num_epochs: |
| | | print 'Taking snapshot...' |
| | | print('Taking snapshot...') |
| | | torch.save(model.state_dict(), |
| | | 'output/snapshots/' + args.output_string + '_epoch_'+ str(epoch+1) + '.pkl') |
| | |
| | | model = hopenet.ResNet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 3) |
| | | load_filtered_state_dict(model, model_zoo.load_url('https://download.pytorch.org/models/resnet50-19c8e357.pth')) |
| | | |
| | | print 'Loading data.' |
| | | print('Loading data.') |
| | | |
| | | transformations = transforms.Compose([transforms.Scale(240), |
| | | transforms.RandomCrop(224), transforms.ToTensor(), |
| | |
| | | elif args.dataset == 'AFW': |
| | | pose_dataset = datasets.AFW(args.data_dir, args.filename_list, transformations) |
| | | else: |
| | | print 'Error: not a valid dataset name' |
| | | print('Error: not a valid dataset name') |
| | | sys.exit() |
| | | train_loader = torch.utils.data.DataLoader(dataset=pose_dataset, |
| | | batch_size=batch_size, |
| | |
| | | {'params': get_fc_params(model), 'lr': args.lr * 5}], |
| | | lr = args.lr) |
| | | |
| | | print 'Ready to train network.' |
| | | print 'First phase of training.' |
| | | print('Ready to train network.') |
| | | print('First phase of training.') |
| | | for epoch in range(num_epochs): |
| | | for i, (images, labels, cont_labels, name) in enumerate(train_loader): |
| | | images = Variable(images).cuda(gpu) |
| | |
| | | |
| | | # Save models at numbered epochs. |
| | | if epoch % 1 == 0 and epoch < num_epochs: |
| | | print 'Taking snapshot...' |
| | | print('Taking snapshot...') |
| | | torch.save(model.state_dict(), |
| | | 'output/snapshots/' + args.output_string + '_epoch_'+ str(epoch+1) + '.pkl') |
New file |
| | |
| | | python code/test_on_video_dlib.py --snapshot ./snapshots/hopenet_robust_alpha1.pkl --face_model ./models/mmod_human_face_detector.dat --video ./videos/out2.mp4 --output_string "something you want to add to the output video name and result file name" --n_frames 500 --fps 30 |