natanielruiz
2017-10-30 2c764e41e2fde6244b87da58d12c40d09a14fcb4
Next
3个文件已修改
74 ■■■■■ 已修改文件
code/test_alexnet.py 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
code/test_on_video.py 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
code/test_on_video_noconf.py 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
code/test_alexnet.py
@@ -1,4 +1,9 @@
import sys, os, argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
@@ -8,15 +13,7 @@
import torchvision
import torch.nn.functional as F
import cv2
import matplotlib.pyplot as plt
import sys
import os
import argparse
import datasets
import hopenet
import utils
import datasets, hopenet, utils
def parse_args():
    """Parse input arguments."""
@@ -134,8 +131,7 @@
        pitch_error += torch.sum(torch.abs(pitch_predicted - label_pitch))
        roll_error += torch.sum(torch.abs(roll_predicted - label_roll))
        # Save images with pose cube.
        # TODO: fix for larger batch size
        # Save first image in batch with pose cube or axis.
        if args.save_viz:
            name = name[0]
            if args.dataset == 'BIWI':
@@ -145,7 +141,8 @@
            if args.batch_size == 1:
                error_string = 'y %.2f, p %.2f, r %.2f' % (torch.sum(torch.abs(yaw_predicted - label_yaw)), torch.sum(torch.abs(pitch_predicted - label_pitch)), torch.sum(torch.abs(roll_predicted - label_roll)))
                cv2.putText(cv2_img, error_string, (30, cv2_img.shape[0]- 30), fontFace=1, fontScale=1, color=(0,0,255), thickness=1)
            utils.plot_pose_cube(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0])
            # utils.plot_pose_cube(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0], size=100)
            utils.draw_axis(cv2_img, yaw_predicted[0], pitch_predicted[0], roll_predicted[0], tdx = 200, tdy= 200, size=100)
            cv2.imwrite(os.path.join('output/images', name + '.jpg'), cv2_img)
    print('Test error in degrees of the model on the ' + str(total) +
code/test_on_video.py
@@ -1,4 +1,9 @@
import sys, os, argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
@@ -8,10 +13,6 @@
import torchvision
import torch.nn.functional as F
from PIL import Image
import cv2
import matplotlib.pyplot as plt
import sys, os, argparse
import datasets, hopenet, utils
@@ -46,10 +47,8 @@
    if not os.path.exists(args.video_path):
        sys.exit('Video does not exist')
    # ResNet101 with 3 outputs.
    # model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 23, 3], 66)
    # ResNet50
    model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66, 0)
    model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66)
    print 'Loading snapshot.'
    # Load snapshot
@@ -154,15 +153,12 @@
                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 = 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
                yaw_predicted = angles[:,0].data[0].cpu()
                pitch_predicted = angles[:,1].data[0].cpu()
                roll_predicted = angles[:,2].data[0].cpu()
                # 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)
code/test_on_video_noconf.py
@@ -1,4 +1,9 @@
import sys, os, argparse
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
@@ -8,10 +13,6 @@
import torchvision
import torch.nn.functional as F
from PIL import Image
import cv2
import matplotlib.pyplot as plt
import sys, os, argparse
import datasets, hopenet, utils
@@ -47,10 +48,8 @@
    if not os.path.exists(args.video_path):
        sys.exit('Video does not exist')
    # ResNet101 with 3 outputs.
    # model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 23, 3], 66)
    # ResNet50
    model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66, 0)
    # ResNet50 structure
    model = hopenet.Hopenet(torchvision.models.resnet.Bottleneck, [3, 4, 6, 3], 66)
    print 'Loading snapshot.'
    # Load snapshot
@@ -145,7 +144,7 @@
            y_min = max(y_min, 0)
            x_max = min(frame.shape[1], x_max)
            y_max = min(frame.shape[0], y_max)
            # Crop image
            # Crop face loosely
            img = frame[y_min:y_max,x_min:x_max]
            img = Image.fromarray(img)
@@ -154,15 +153,13 @@
            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 = 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
            yaw_predicted = angles[:,0].data[0].cpu()
            pitch_predicted = angles[:,1].data[0].cpu()
            roll_predicted = angles[:,2].data[0].cpu()
            # 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)