예제 #1
0
def getData():
    Dpath = '/home/denitome/PythonProjects/sfm_new/'
    json_file = ut.getCaffeCpm(
    ) + '/jsonDatasets/H36M_annotations_testSet.json'
    (data, num_elem) = ut.loadJsonFile(json_file)
    # 2d predictions
    test = ut.sio.loadmat(Dpath + 'Data/testSet_with_predictions.mat')
    mask_camera = test['mask_camera'][0]
    mask_action = test['mask_action'][0]
    preds = test['pred']
    # 3d predictions
    p_res_dict = ut.sio.loadmat(Dpath + 'predicted_3d_poses_testset.mat')
    preds_3d = p_res_dict['pred_models']
    # mask person
    idx = np.where(mask_action == 2)[0]
    t = np.where((idx[:-1] + 1) != idx[1:])[0][0] + 1
    fno_person = idx[t]
    mask_person = np.ones(num_elem) * 9
    mask_person[fno_person:] = 11
    # all masks
    masks = {}
    masks['camera'] = mask_camera
    masks['action'] = mask_action
    masks['person'] = mask_person
    return (data, preds, preds_3d, masks)
예제 #2
0
def getLossOnValidationSet(NN, models_dir, json_file, masks, prototxt, output,
                           samplingRate):
    # get models
    files = [f for f in ut.os.listdir(models_dir) if f.endswith('.caffemodel')]
    files = sorted(files, key=getIter)

    print 'Loading json file with annotations...'
    (data, num_elem) = ut.loadJsonFile(json_file)
    print 'Loading mask file...'
    masks = ut.sio.loadmat(masks)
    print 'Done.'

    results = [dict() for x in range(len(files))]
    for i in range(len(files)):
        model_dir = '%s/%s' % (models_dir, files[i])
        iterNumber = getIter(model_dir)
        print '-------------------------------'
        print '  Evaluating iteration: %d' % iterNumber
        print '-------------------------------'
        net = ut.loadNetFromPath(model_dir, prototxt)
        val = runCaffeOnModel(NN, net, data[0:num_elem:samplingRate], masks,
                              iterNumber)
        results[i] = val
        # save json
        with open(output, 'w+') as out:
            json.dump(results, out)
    return results
예제 #3
0
def getLossOnValidationSet(NN, models_dir, json_file, masks, prototxt, output, samplingRate):
    # get models
    files = [f for f in ut.os.listdir(models_dir) if f.endswith('.caffemodel')]
    files = sorted(files, key=getIter)
    
    print 'Loading json file with annotations...'
    (data, num_elem) = ut.loadJsonFile(json_file)
    print 'Loading mask file...'
    masks = ut.sio.loadmat(masks)
    print 'Done.'
    
    results = [dict() for x in range(len(files))]
    for i in range(len(files)):
        model_dir = '%s/%s' % (models_dir, files[i])
        iterNumber = getIter(model_dir)
        print '-------------------------------'
        print '  Evaluating iteration: %d' % iterNumber
        print '-------------------------------'
        net = ut.loadNetFromPath(model_dir, prototxt)
        val = runCaffeOnModel(NN, net, data[0:num_elem:samplingRate], masks, iterNumber)
        results[i] = val
        # save json
        with open(output, 'w+') as out:
            json.dump(results, out)
    return results
예제 #4
0
def produceInput(NN):
    json_file = ut.getCaffeCpm() + '/jsonDatasets/H36M_annotations_testSet.json'
    (data, num_elem) = ut.loadJsonFile(json_file)
    curr_data = data[0]
    
    img = ut.cv2.imread(curr_data['img_paths'])
    joints = np.array(curr_data['joint_self'])
    
    center = ut.getCenterJoint(joints)
    img_width = img.shape[1]
    img_height = img.shape[0]
    box_points = ut.getBoundingBox(joints, center, NN['offset'], img_width, img_height)
    
    # manipulate image and joints for caffe
    (img_croppad, joints) = ut.cropImage(img, box_points, joints)
    (resizedImage, joints) = ut.resizeImage(img_croppad, NN['inputSize'], joints)
    resizedImage = np.divide(resizedImage, float(256))
    resizedImage = np.subtract(resizedImage, 0.5)
    
    # generate labels and center channel
    input_size = NN['inputSize']
    center_channel = ut.generateGaussian(NN['sigma_center'], input_size, [input_size/2, input_size/2])
    
    fno = int(curr_data['annolist_index'])
    camera = curr_data['camera']
    action = curr_data['action']
    person = curr_data['person']
    metadata_ch = ut.generateMaskChannel(NN['inputSize'], fno, camera, action, person)
    imgch = np.concatenate((resizedImage, center_channel[:,:,np.newaxis], metadata_ch), axis=2)
    
    imgch = np.transpose(imgch, (2, 0, 1))
    return imgch
예제 #5
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('prototxt', metavar='prototxt', type=str, help='Path to the prototxt file')
    parser.add_argument('caffemodel', metavar='caffemodel', type=str, help='Path to the caffemodel')
    parser.add_argument('-o', dest='out_directory', help='Output directory where to store the predictions')
    parser.add_argument('--json_file', dest='json_file', type=str, help='Path to the json file containing the test-set data')
    parser.add_argument('---with_cpu', action='store_true', dest='with_cpu', help='Caffe uses CPU for feature extraction')
    parser.add_argument('--num_parts', default=1, type=int, dest='num_parts', help='Split the test set into num_parts. Default set to 1')
    parser.add_argument('--run_part', default=1, type=int, dest='run_part', help='Run part of the splitted test set. Default set to 1')
    parser.add_argument('--merge_parts_dir', dest='merge_parts_dir', type=str, help='Merge part contained in the directory')
    
    args = parser.parse_args()
    checkFilesExistance(args.prototxt, args.caffemodel)
    
    # set environment
    NN = ut.load_configuration(gpu=(not args.with_cpu))
    ut.setCaffeMode(NN['GPU'])
    
    # get json file path
    json_file = ut.getCaffeCpm() + '/jsonDatasets/H36M_annotations_testSet.json'
    if args.json_file is not None:
        json_file = args.json_file
    
    # load caffe model
    net = ut.loadNetFromPath(args.caffemodel, args.prototxt)
    (data, num_elem) = ut.loadJsonFile(json_file)
    
    # Execution in multiple machines
    elems_per_part = int(np.floor(num_elem/int(args.num_parts)))
    offset_data = 0                                             # for not perfect divisions
    offset = elems_per_part*(int(args.run_part)-1)              # depending the part we are running
    if (args.run_part == args.num_parts):
        offset_data = num_elem - elems_per_part*int(args.num_parts)
    idx_part = range(offset, offset + elems_per_part + offset_data)
    
    # Set output file path
    output_file = getNameOutputFile(args.caffemodel)
    if args.out_directory is not None:
        output_file = args.out_directory
        output_file += '/predictions.mat'

    if args.merge_parts_dir is not None:
        (preds, errors, covs, frame_num) = mergeParts(NN, args.merge_parts_dir, num_elem, elems_per_part)
        ut.sio.savemat(output_file, {'preds':preds,'errors':errors,'covs':covs,'frame_num':frame_num})
        print 'mean error: %r' % errors.mean()
        return
    
    # run model on test set
    (preds, errors, covs, frame_num) = executeOnTestSet(NN, net, data[idx_part], elems_per_part)
    print 'Mean error on the test set: %r' % errors.mean()
    
    # save results
    if (args.num_parts > 1):
        output_file = re.sub('\.caffemodel', '_predictions_part', args.caffemodel)
        output_file += '%d.mat' % (int(args.run_part))
    ut.sio.savemat(output_file, {'preds':preds,'errors':errors,'covs':covs,'frame_num':frame_num})
예제 #6
0
def produceInput(NN):
    json_file = ut.getCaffeCpm(
    ) + '/jsonDatasets/H36M_annotations_testSet.json'
    (data, num_elem) = ut.loadJsonFile(json_file)
    curr_data = data[0]

    img = ut.cv2.imread(curr_data['img_paths'])
    joints = np.array(curr_data['joint_self'])

    center = ut.getCenterJoint(joints)
    img_width = img.shape[1]
    img_height = img.shape[0]
    box_points = ut.getBoundingBox(joints, center, NN['offset'], img_width,
                                   img_height)

    # manipulate image and joints for caffe
    (img_croppad, joints) = ut.cropImage(img, box_points, joints)
    (resizedImage, joints) = ut.resizeImage(img_croppad, NN['inputSize'],
                                            joints)
    resizedImage = np.divide(resizedImage, float(256))
    resizedImage = np.subtract(resizedImage, 0.5)

    # generate labels and center channel
    input_size = NN['inputSize']
    center_channel = ut.generateGaussian(NN['sigma_center'], input_size,
                                         [input_size / 2, input_size / 2])

    fno = int(curr_data['annolist_index'])
    camera = curr_data['camera']
    action = curr_data['action']
    person = curr_data['person']
    metadata_ch = ut.generateMaskChannel(NN['inputSize'], fno, camera, action,
                                         person)
    imgch = np.concatenate(
        (resizedImage, center_channel[:, :, np.newaxis], metadata_ch), axis=2)

    imgch = np.transpose(imgch, (2, 0, 1))
    return imgch
예제 #7
0
def getData():
    Dpath = '/home/denitome/PythonProjects/sfm_new/'
    json_file = ut.getCaffeCpm() + '/jsonDatasets/H36M_annotations_testSet.json'
    (data, num_elem) = ut.loadJsonFile(json_file)
    # 2d predictions    
    test = ut.sio.loadmat(Dpath+'Data/testSet_with_predictions.mat')
    mask_camera = test['mask_camera'][0]
    mask_action = test['mask_action'][0]
    preds = test['pred']
    # 3d predictions
    p_res_dict = ut.sio.loadmat(Dpath+'predicted_3d_poses_testset.mat')
    preds_3d = p_res_dict['pred_models']
    # mask person
    idx = np.where(mask_action == 2)[0]
    t = np.where((idx[:-1]+1) != idx[1:])[0][0] + 1
    fno_person = idx[t]
    mask_person = np.ones(num_elem)*9
    mask_person[fno_person:] = 11
    # all masks
    masks = {}
    masks['camera'] = mask_camera
    masks['action'] = mask_action
    masks['person'] = mask_person
    return (data, preds, preds_3d, masks)
예제 #8
0
파일: demo_img.py 프로젝트: DenisTome/caffe
            return i+fno
    return -1

## SET ENVIRONMENT
caffemodel = ut.getCaffeCpm() + '/prototxt/caffemodel/manifold_diffarch3/pose_iter_22000.caffemodel'
prototxt = ut.getCaffeCpm() + '/prototxt/pose_deploy_singleimg.prototxt'
output_dir = '/home/denitome/Desktop/imgs/tmp/'
checkFilesExistance(prototxt, caffemodel)

NN = ut.load_configuration(gpu=True)
ut.setCaffeMode(NN['GPU'])

# load caffe model
json_file = ut.getCaffeCpm() + '/jsonDatasets/H36M_annotations_testSet.json'
net = ut.loadNetFromPath(caffemodel, prototxt)
(data, num_elem) = ut.loadJsonFile(json_file)
    
idx = getIndex(data, camera=1, person=9, action=2, fno=10)
#idx = getIndex(data, camera=1, person=11, action=14, fno=950)
executeOnFrame(NN, net, data[idx], output_dir, proj=True, astr='_tmp', show=True, stage_3d_err=True, hm_joint=6)

#tmp_data = data
#data=data[idx]

# TODO: remove

##differr = errors_orig-errors
##idx = np.argmax(differr[differr < 31.76])
#phi = np.log(1+(errors_orig -errors)/errors3d)
#idx = np.where(phi == phi[phi < 0.84].max())[0][0]
#executeOnFrame(NN, net, data[idx], output_dir, proj=True, astr=('_%d' % idx), show=True, stage_3d_err=False)
예제 #9
0

## SET ENVIRONMENT
caffemodel = ut.getCaffeCpm(
) + '/prototxt/caffemodel/manifold_diffarch3/pose_iter_22000.caffemodel'
prototxt = ut.getCaffeCpm() + '/prototxt/pose_deploy_singleimg.prototxt'
output_dir = '/home/denitome/Desktop/imgs/tmp/'
checkFilesExistance(prototxt, caffemodel)

NN = ut.load_configuration(gpu=True)
ut.setCaffeMode(NN['GPU'])

# load caffe model
json_file = ut.getCaffeCpm() + '/jsonDatasets/H36M_annotations_testSet.json'
net = ut.loadNetFromPath(caffemodel, prototxt)
(data, num_elem) = ut.loadJsonFile(json_file)

idx = getIndex(data, camera=1, person=9, action=2, fno=10)
#idx = getIndex(data, camera=1, person=11, action=14, fno=950)
executeOnFrame(NN,
               net,
               data[idx],
               output_dir,
               proj=True,
               astr='_tmp',
               show=True,
               stage_3d_err=True,
               hm_joint=6)

#tmp_data = data
#data=data[idx]