def main_test():
    xnet = HourglassNet(16, 8, (256, 256), (64, 64))

    xnet.load_model("../../trained_models/hg_s8_b1_sigma1/net_arch.json", "../../trained_models/hg_s8_b1_sigma1/weights_epoch22.h5")

    valdata = MPIIDataGen("../../data/mpii/mpii_annotations.json", "../../data/mpii/images",
                                inres=(256, 256), outres=(64, 64), is_train=False)

    total_good, total_fail = 0, 0
    threshold = 0.5

    print('val data size', valdata.get_dataset_size())

    count = 0
    batch_size = 8
    for _img, _gthmap, _meta in valdata.tt_generator(batch_size, 8, sigma=2, is_shuffle=False , with_meta=True):

        count += batch_size
        if count % (batch_size*100) == 0:
            print(count, 'processed', total_good, total_fail)

        if count > valdata.get_dataset_size():
            break

        out = xnet.model.predict(_img)

        good, bad = cal_heatmap_acc(out[-1], _meta, threshold)

        total_good += good
        total_fail += bad

    print(total_good, total_fail, threshold, total_good*1.0/(total_good + total_fail))
def main_eval(model_json, model_weights, num_stack, num_class, matfile):
    xnet = HourglassNet(num_class, num_stack, (256, 256), (64, 64))

    xnet.load_model(model_json, model_weights)

    valdata = MPIIDataGen("../../data/mpii/mpii_annotations.json", "../../data/mpii/images",
                          inres=(256, 256), outres=(64, 64), is_train=False)

    print 'val data size', valdata.get_dataset_size()

    valkps = np.zeros(shape=(valdata.get_dataset_size(), 16, 2), dtype=np.float)

    count = 0
    batch_size = 8
    for _img, _gthmap, _meta in valdata.generator(batch_size, num_stack, sigma=1, is_shuffle=False , with_meta=True):

        count += batch_size

        if count > valdata.get_dataset_size():
            break

        out = xnet.model.predict(_img)

        get_final_pred_kps(valkps, out[-1], _meta)

    scipy.io.savemat(matfile, mdict={'preds' : valkps})

    run_pckh(model_json, matfile)
Beispiel #3
0
def main_test():
    xnet = HourglassNet(16, 8, (256, 256), (64, 64))

    xnet.load_model("../../trained_models/hg_s8_b1_v1_adam/net_arch.json",
                    "../../trained_models/hg_s8_b1_v1_adam/weights_epoch22.h5")

    valdata = MPIIDataGen("../../data/mpii/mpii_annotations.json", "../../data/mpii/images",
                                inres=(256, 256), outres=(64, 64), is_train=False)

    print 'val data size', valdata.get_dataset_size()

    valkps = np.zeros(shape=(valdata.get_dataset_size(), 16, 2), dtype=np.float)

    count = 0
    batch_size = 8
    for _img, _gthmap, _meta in valdata.generator(batch_size, 8, sigma=2, is_shuffle=False , with_meta=True):

        count += batch_size

        if count > valdata.get_dataset_size():
            break

        out = xnet.model.predict(_img)

        get_final_pred_kps(valkps, out[-1], _meta)


    matfile = os.path.join( "../../trained_models/hg_s8_b1_v1_adam/", 'preds_e22.mat')
    scipy.io.savemat(matfile, mdict={'preds' : valkps})

    run_pckh('hg_s8_b1_epoch22', matfile)
def main_video(model_json, model_weights, num_stack, num_class, videofile,
               confth):

    xnet = HourglassNet(num_class, num_stack, (256, 256), (64, 64))
    xnet.load_model(model_json, model_weights)

    cap = cv2.VideoCapture(videofile)
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            rgb = frame[:, :, ::-1]  # bgr -> rgb
            out, scale = xnet.inference_rgb(rgb, frame.shape)

            kps = post_process_heatmap(out[0, :, :, :])

            ignore_kps = ['plevis', 'thorax', 'head_top']
            kp_keys = MPIIDataGen.get_kp_keys()
            mkps = list()
            for i, _kp in enumerate(kps):
                if kp_keys[i] in ignore_kps:
                    _conf = 0.0
                else:
                    _conf = _kp[2]
                mkps.append(
                    (_kp[0] * scale[1] * 4, _kp[1] * scale[0] * 4, _conf))

            framejoints = render_joints(frame, mkps, confth)

            cv2.imshow('frame', framejoints)
            cv2.waitKey(10)
Beispiel #5
0
def main_eval(model_json, model_weights, num_stack, num_class, matfile, tiny):
    inres = (192, 192) if tiny else (256, 256)
    outres = (48, 48) if tiny else (64, 64)
    num_channles = 128 if tiny else 256

    xnet = HourglassNet(num_classes=num_class,
                        num_stacks=num_stack,
                        num_channels=num_channles,
                        inres=inres,
                        outres=outres)

    xnet.load_model(model_json, model_weights)

    # dataset_path = '/home/tomas_bordac/nyu_croped'
    dataset_path = os.path.join('D:\\', 'nyu_croped')
    valdata = NYUHandDataGen('joint_data.mat',
                             dataset_path,
                             inres=inres,
                             outres=outres,
                             is_train=False)

    print('val data size', valdata.get_dataset_size())

    valkps = np.zeros(shape=(valdata.get_dataset_size(), 11, 2),
                      dtype=np.float)

    count = 0
    batch_size = 8
    for _img, _gthmap, _meta in valdata.generator(batch_size,
                                                  num_stack,
                                                  sigma=3,
                                                  is_shuffle=False,
                                                  with_meta=True):

        count += batch_size

        if count > valdata.get_dataset_size():
            break

        out = xnet.model.predict(_img)

        get_final_pred_kps(valkps, out[-1], _meta, outres)

    scipy.io.savemat(matfile, mdict={'preds': valkps})

    run_pckh(model_json, matfile)
Beispiel #6
0
def main_inference(model_json, model_weights, num_stack, num_class, imgfile, confth, tiny):
    if tiny:
        xnet = HourglassNet(num_classes=16, num_stacks=args.num_stack, num_channels=128, inres=(192, 192),
                            outres=(48, 48))
    else:
        xnet = HourglassNet(num_classes=16, num_stacks=args.num_stack, num_channels=256, inres=(256, 256),
                            outres=(64, 64))

    xnet.load_model(model_json, model_weights)

    out, scale = xnet.inference_file(imgfile)

    kps = post_process_heatmap(out[0, :, :, :])

    ignore_kps = ['plevis', 'thorax', 'head_top']
    kp_keys = MPIIDataGen.get_kp_keys()
    mkps = list()
    for i, _kp in enumerate(kps):
        if kp_keys[i] in ignore_kps:
            _conf = 0.0
        else:
            _conf = _kp[2]
        mkps.append((_kp[0] * scale[1] * 4, _kp[1] * scale[0] * 4, _conf))

    cvmat = render_joints(cv2.imread(imgfile), mkps, confth)

    cv2.imshow('frame', cvmat)
    cv2.waitKey()
Beispiel #7
0
def main_test():
    xnet = HourglassNet(16, 8, (256, 256), (64, 64))

    xnet.load_model("../../trained_models/hg_s8_b1/net_arch.json",
                    "../../trained_models/hg_s8_b1/weights_epoch29.h5")

    valdata = MPIIDataGen("../../data/mpii/mpii_annotations.json",
                          "../../data/mpii/images",
                          inres=(256, 256),
                          outres=(64, 64),
                          is_train=False)

    for _img, _gthmap in valdata.generator(1, 8, sigma=2, is_shuffle=True):
        out = xnet.model.predict(_img)

        scipy.misc.imshow(_img[0, :, :, :])
        #view_predict_hmap(_gthmap)
        view_predict_hmap(out, show_raw=False)
Beispiel #8
0
def main_eval(model_path, num_stack, num_class, matfile, tiny):
    inres = (192, 192) if tiny else (256, 256)
    outres = (48, 48) if tiny else (64, 64)
    num_channles = 128 if tiny else 256

    xnet = HourglassNet(num_classes=num_class,
                        num_stacks=num_stack,
                        num_channels=num_channles,
                        inres=inres,
                        outres=outres)

    xnet.load_model(model_path)

    valdata = MPIIDataGen("../../data/mpii/mpii_annotations.json",
                          "../../data/mpii/images",
                          inres=inres,
                          outres=outres,
                          is_train=False)

    print('val data size', valdata.get_dataset_size())

    valkps = np.zeros(shape=(valdata.get_dataset_size(), 16, 2),
                      dtype=np.float)

    count = 0
    batch_size = 8
    for _img, _gthmap, _meta in valdata.generator(batch_size,
                                                  num_stack,
                                                  sigma=1,
                                                  is_shuffle=False,
                                                  with_meta=True):

        count += batch_size

        if count > valdata.get_dataset_size():
            break

        out = xnet.model.predict(_img)

        get_final_pred_kps(valkps, out[-1], _meta, outres)

    scipy.io.savemat(matfile, mdict={'preds': valkps})

    run_pckh(model_path, matfile)
    def __init__(self,
                 stacks,
                 blocks,
                 depth_res,
                 nParts,
                 resume_p2v2c=None,
                 resume_p2v=None,
                 resume_v2c=None,
                 is_cuda=True):
        super(pvcNet, self).__init__()

        self.num_stacks = stacks
        self.num_blocks = blocks
        self.depth_res = depth_res
        self.nParts = nParts

        self.resume = dict()
        self.resume['p2v2c'] = resume_p2v2c
        self.resume['p2v'] = resume_p2v
        self.resume['v2c'] = resume_v2c
        self.is_cuda = is_cuda

        self.epoch = 0
        self.best_acc = 0
        self.current_acc = -0

        if len(set(self.depth_res)) == 1:
            self.c2f = False
        else:
            self.c2f = True

        print('coarse to fine mode: {}'.format(self.c2f))

        pix2vox = HourglassNet(Bottleneck, self.num_stacks, self.num_blocks,
                               self.depth_res)
        vox2coord = coordRegressor(self.nParts)

        if self.is_cuda:
            self.pix2vox = torch.nn.DataParallel(pix2vox).cuda()
            self.vox2coord = torch.nn.DataParallel(vox2coord).cuda()
        else:
            self.pix2vox = pix2vox
            self.vox2coord = vox2coord

        print('    p2v params: %.2fM' %
              (sum(p.numel() for p in self.pix2vox.parameters()) / 1000000.0))
        print('    v2c params: %.2fM' %
              (sum(p.numel()
                   for p in self.vox2coord.parameters()) / 1000000.0))
Beispiel #10
0
init_epoch = 0


# In[6]:


os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(0)
    
# TensorFlow wizardry
config = tf.ConfigProto()
    
# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True

# Only allow a total of half the GPU memory to be allocated
config.gpu_options.per_process_gpu_memory_fraction = 1.0
    
# Create a session with the above options specified.
k.tensorflow_backend.set_session(tf.Session(config=config))

xnet = HourglassNet(num_classes=16, num_stacks=num_stacks, num_channels=256, inres=(256, 256),outres=(64, 64))

if resume:
    xnet.resume_train(batch_size=batch_size,
                      model_path=model_path,
                      init_epoch=init_epoch, epochs=args.epochs)
else:
    xnet.build_model(show=True)
    xnet.train(epochs=epochs, model_path=model_path, batch_size=batch_size)
Beispiel #11
0
# %% Load Annotations into dataframes
import hourglass
import imp
imp.reload(hourglass)
from hourglass import HourglassNet
from constants import *
import matplotlib.pyplot as plt
import os
# %matplotlib inline

h = HourglassNet(NUM_COCO_KEYPOINTS, DEFAULT_NUM_HG, INPUT_CHANNELS, INPUT_DIM,
                 OUTPUT_DIM)
train_df, val_df = h.load_and_filter_annotations(DEFAULT_TRAIN_ANNOT_PATH,
                                                 DEFAULT_VAL_ANNOT_PATH, 0.1)

# %% Declare evaluation class instance
import pandas as pd
import evaluation
import HeatMap
imp.reload(evaluation)
imp.reload(HeatMap)

# %%
representative_set_df = pd.read_pickle(
    os.path.join(DEFAULT_PICKLE_PATH, 'representative_set.pkl'))
subdir = '2021-04-01-21h-59m_batchsize_16_hg_4_loss_weighted_mse_aug_light_sigma4_learningrate_5.0e-03_opt_rmsProp_gt-4kp_activ_sigmoid_subset_0.50_lrfix'
eval = evaluation.Evaluation(model_sub_dir=subdir, epoch=26)

# %% Save stacked evaluation heatmaps
import data_generator
imp.reload(data_generator)
Beispiel #12
0

if __name__ == "__main__":
    args = process_args()

    print("\n\nSetup start: {}\n".format(time.ctime()))
    setup_start = time.time()

    tensorflow_setup()

    hgnet = HourglassNet(num_classes=NUM_COCO_KEYPOINTS,
                         num_stacks=args.hourglass,
                         num_channels=NUM_CHANNELS,
                         inres=INPUT_DIM,
                         outres=OUTPUT_DIM,
                         loss_str=args.loss,
                         image_aug_str=args.augment,
                         pickle_name=args.pickle,
                         optimizer_str=args.optimizer,
                         learning_rate=args.learning_rate,
                         activation_str=args.activation)

    training_start = time.time()

    # TODO Save all model parameters in JSON for easy resuming and parsing later on
    if args.resume:
        print("\n\nResume training start: {}\n".format(time.ctime()))

        hgnet.resume_train(args.batch, args.model_save, args.resume_json, args.resume_weights, \
            args.resume_epoch, args.epochs, args.resume_subdir, args.subset, new_run=args.resume_with_new_run)
    else:
Beispiel #13
0
    # Only allow a total of half the GPU memory to be allocated
    # -config.gpu_options.per_process_gpu_memory_fraction = 1.0
    config.gpu_options.per_process_gpu_memory_fraction = 0.8

    # Create a session with the above options specified.
    # AttributeError: module 'keras.backend' has no attribute 'tensorflow_backend
    # -k.tensorflow_backend.set_session(tf.Session(config=config))
    # AttributeError: module 'keras.backend' has no attribute 'set_session'
    # -k.set_session(tf.Session(config=config))
    tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))"""

    if args.tiny:
        xnet = HourglassNet(num_classes=16,
                            num_stacks=args.num_stack,
                            num_channels=128,
                            inres=(192, 192),
                            outres=(48, 48))
    else:
        xnet = HourglassNet(num_classes=16,
                            num_stacks=args.num_stack,
                            num_channels=256,
                            inres=(256, 256),
                            outres=(64, 64))

    if args.resume:
        xnet.resume_train(batch_size=args.batch_size,
                          model_json=args.resume_model_json,
                          model_weights=args.resume_model,
                          init_epoch=args.init_epoch,
                          epochs=epochs)