def main(_):
    new_files = pick_frame(FLAGS.dataset_dir)
    basename = os.path.basename(FLAGS.ckpt_file)

    im1_pl = tf.placeholder(dtype=tf.float32, shape=(1, FLAGS.img_height, FLAGS.img_width, 3))
    im2_pl = tf.placeholder(dtype=tf.float32, shape=(1, FLAGS.img_height, FLAGS.img_width, 3))
    pred_flows = flownet(im1_pl, im2_pl, flownet_spec='C')

    saver = tf.train.Saver([var for var in tf.all_variables() if 'flow' in var.name]) 
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    errs = np.zeros(NUM)

    if not FLAGS.output_dir is None and not os.path.exists(FLAGS.output_dir):
        os.makedirs(FLAGS.output_dir)

    with tf.Session(config=config) as sess:
        saver.restore(sess, FLAGS.ckpt_file)
        # For val set
        for t in range(0, len(new_files)):
            if t % 100 == 0:
                print('processing %s: %d/%d' % (basename, t, len(new_files)))
            raw_im0 = pil.open(new_files[t][0])
            raw_im1 = pil.open(new_files[t][1])
            scaled_im0 = raw_im0.resize((FLAGS.img_width, FLAGS.img_height), pil.ANTIALIAS)
            scaled_im1 = raw_im1.resize((FLAGS.img_width, FLAGS.img_height), pil.ANTIALIAS)
            # Minus ImageNet channel mean
            channel_mean = np.array([104.920005, 110.1753, 114.785955])
            scaled_im0 = (np.expand_dims(np.array(scaled_im0), axis=0).astype(np.float32)-channel_mean)/255.
            scaled_im1 = (np.expand_dims(np.array(scaled_im1), axis=0).astype(np.float32)-channel_mean)/255.
            feed_dict = {im1_pl: scaled_im0, im2_pl: scaled_im1}
            pred_flows_val = sess.run(pred_flows, feed_dict=feed_dict)           
            pred_flow_val = pred_flows_val[-1][0]

            # Only for training set
            if 'train' in FLAGS.dataset_dir:
                # no occlusion
                #gt_flow, mask = get_flow(new_files[t][0].replace('colored_0', 'flow_noc'))
                # all
                gt_flow, mask = get_flow(new_files[t][0].replace('colored_0', 'flow_occ'))
                errs[t], scaled_pred = compute_flow_error(gt_flow, pred_flow_val[0,:,:,:], mask)

            # Save for eval
            if 'test' in FLAGS.dataset_dir:
                errs[t], scaled_pred = compute_flow_error(np.array(raw_im0)[:,:,:2], pred_flow_val[0,:,:,:], np.array(raw_im0)[:,:,0])
                #png_name = os.path.join(FLAGS.output_dir, new_files[t][0].split('/')[-1])
                #write_flow_png(png_name, scaled_pred)

            # Save for visual colormap
            if not 'test' in FLAGS.dataset_dir and not FLAGS.output_dir is None:
                flow_im = flow_to_image(scaled_pred)
                #png_name = os.path.join(FLAGS.output_dir, new_files[t][0].split('/')[-1]).replace('png', 'jpg')
                #cv2.imwrite(png_name, flow_im[:,:,::-1])

        print('{:>10}'.format('(valid) endpoint error'))
        print('{:10.4f}'.format(errs.mean()))

        with open("test.log", 'w') as f:
            f.write(str(errs.mean()))
Beispiel #2
0
def main(_):
    os.environ["CUDA_VISIBLE_DEVICES"] = "2"
    new_files = pick_frame(FLAGS.dataset_dir)
    basename = os.path.basename(FLAGS.ckpt_file)

    im1_pl = tf.placeholder(dtype=tf.float32, shape=(1, FLAGS.img_height, FLAGS.img_width, 3))
    im2_pl = tf.placeholder(dtype=tf.float32, shape=(1, FLAGS.img_height, FLAGS.img_width, 3))
    # pred_flows = flownet(im1_pl, im2_pl, flownet_spec='C')
    hourglassFile = '/media/disk/Backup/02congzhang/git/flow/relative-depth-using-pytorch/hourglass.pickle'
    # hourglassdata = np.load(hourglassFile)
    with open(hourglassFile, 'rb') as file:
        hourglassdata = pickle.load(file)
    pred_flows = net(im1_pl, im2_pl, hourglassdata)[2]



    saver = tf.train.Saver([var for var in tf.all_variables()]) 
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    errs = np.zeros(NUM)
    f1   = np.zeros(NUM)

    if not FLAGS.output_dir is None and not os.path.exists(FLAGS.output_dir):
        os.makedirs(FLAGS.output_dir)

    with tf.Session(config=config) as sess:
        saver.restore(sess, '/media/disk/Backup/02congzhang/git/flow/DF-Net/ckpt/hourr1/model-40000')
        # For val set
        for t in range(0, len(new_files)):
            if t % 100 == 0:
                print('processing %s: %d/%d' % (basename, t, len(new_files)))
            raw_im0 = pil.open(new_files[t][0])
            raw_im1 = pil.open(new_files[t][1])
            scaled_im0 = raw_im0.resize((FLAGS.img_width, FLAGS.img_height), pil.ANTIALIAS)
            scaled_im1 = raw_im1.resize((FLAGS.img_width, FLAGS.img_height), pil.ANTIALIAS)
            # Minus ImageNet channel mean
            channel_mean = np.array([104.920005, 110.1753, 114.785955])
            scaled_im0 = (np.expand_dims(np.array(scaled_im0), axis=0).astype(np.float32)-channel_mean)/255.
            scaled_im1 = (np.expand_dims(np.array(scaled_im1), axis=0).astype(np.float32)-channel_mean)/255.
            feed_dict = {im1_pl: scaled_im0, im2_pl: scaled_im1}
            pred_flows_val = sess.run(pred_flows, feed_dict=feed_dict)           
            pred_flow_val = pred_flows_val[0]

            # Only for training set
            if 'train' in FLAGS.dataset_dir:
                # no occlusion
                #gt_flow, mask = get_flow(new_files[t][0].replace('image_2', 'flow_noc'))
                # all
                gt_flow, mask = get_flow(new_files[t][0].replace('image_2', 'flow_occ'))
                errs[t], scaled_pred, f1[t] = compute_flow_error(gt_flow, pred_flow_val[0,:,:,:], mask)

            # Save for eval
            if 'test' in FLAGS.dataset_dir:
                _, scaled_pred, _ = compute_flow_error(np.array(raw_im0)[:,:,:2], pred_flow_val[0,:,:,:], np.array(raw_im0)[:,:,0])
                png_name = os.path.join(FLAGS.output_dir, new_files[t][0].split('/')[-1])
                write_flow_png(png_name, scaled_pred)

            # Save for visual colormap
            if not 'test' in FLAGS.dataset_dir and not FLAGS.output_dir is None:
                flow_im = flow_to_image(scaled_pred)
                png_name = os.path.join(FLAGS.output_dir, new_files[t][0].split('/')[-1]).replace('png', 'jpg')
                cv2.imwrite(png_name, flow_im[:,:,::-1])

        print('{:>10}, {:>10}'.format('(valid) endpoint error', 'f1 score'))
        print('{:10.4f}, {:10.4f}'.format(errs.mean(), f1.mean()))