models icon indicating copy to clipboard operation
models copied to clipboard

struct2depth inferencing changes depending on batch size

Open aj96 opened this issue 7 years ago • 3 comments

System information

  • What is the top-level directory of the model you are using: struct2depth
  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): yes, for the evaluation
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04
  • TensorFlow installed from (source or binary): pip install tensorflow-gpu
  • TensorFlow version (use command below): 1.13
  • Bazel version (if compiling from source): N/A
  • CUDA/cuDNN version: 10.0 / 7.4.1
  • GPU model and memory: Tesla P100 16Gb
  • Exact command to reproduce: python test_kitti_depth.py --dataset_dir=$kitti_dataset --output_dir=$path_to_save_numpy_file --ckpt_file=$path_to_saved_model && python eval_depth.py --kitti_dir=$kitti_dataset_dir --pred_file=$path_to_numpy_file

I have been trying to evaluate the results of my model, but the evaluation code was not provided, so I adapted the original evaluation code from the SfmLearner repo for struct2depth. All I've honestly changed is the model that is used for inferencing.

The issue is that when I change the batch size, the evaluation results change. I even tried with the researcher's provided trained model, and their model is also inconsistent with respect to the batch size.

Using the researchers' trained model:

Results with batch_size=1: abs_rel, sq_rel, rms, log_rms, d1_all, a1, a2, a3 0.1453, 1.1212, 5.3741, 0.2181, 0.0000, 0.8134, 0.9432, 0.9781

Results with batch_size=32: abs_rel, sq_rel, rms, log_rms, d1_all, a1, a2, a3 0.1436, 1.1007, 5.3271, 0.2163, 0.0000, 0.8168, 0.9450, 0.9782

I tried evaluating with the original SfMLearner repo, and their evaluation results are consistent when using different batch sizes.

Here is the meat of the code in test_kitti_depth.py for evaluating struct2depth. I didn't change eval_depth.py from the SfMLearner repo. Can someone please confirm they are getting similar results as me?

    sfm = Model(img_height=FLAGS.img_height, img_width=FLAGS.img_width, batch_size=FLAGS.batch_size,
                is_training=False)

    vars_to_restore = util.get_vars_to_save_and_restore(FLAGS.ckpt_file)
    saver = tf.train.Saver(vars_to_restore)
    sv = tf.train.Supervisor(saver=None)
    with sv.managed_session() as sess:
        saver.restore(sess, FLAGS.ckpt_file)
        pred_all = []
        for t in range(0, len(test_files), FLAGS.batch_size):
            if t % 100 == 0:
                print('processing %s: %d/%d' % (basename, t, len(test_files)))
            inputs = np.zeros(
                (FLAGS.batch_size, FLAGS.img_height, FLAGS.img_width, 3),
                dtype=np.uint8)
            for b in range(FLAGS.batch_size):
                idx = t + b
                if idx >= len(test_files):
                    break
                fh = open(test_files[idx], 'rb')
                raw_im = pil.open(fh)
                scaled_im = raw_im.resize((FLAGS.img_width, FLAGS.img_height), pil.ANTIALIAS)
                inputs[b] = np.array(scaled_im)
                # im = scipy.misc.imread(test_files[idx])
                # inputs[b] = scipy.misc.imresize(im, (FLAGS.img_height, FLAGS.img_width))
            #pred = sfm.inference(inputs, sess, mode='depth')
            pred = sfm.inference_depth(inputs, sess)
            for b in range(FLAGS.batch_size):
                idx = t + b
                if idx >= len(test_files):
                    break
                pred_all.append(pred[b,:,:,0])
        np.save(output_file, pred_all)

I'd also like to point out that the researchers' own model doesn't match the evaluation results on their paper. But I am assuming this is because I am not evaluating the model correctly?

aj96 avatar May 24 '19 19:05 aj96

Hi, for me, using the researchers' trained model: Results with batch_size=1: abs_rel, sq_rel, rms, log_rms, d1_all, a1, a2, a3 0.1452, 1.1166,5.3778, 0.2183,0.8127,0.9429, 0.9779 same as #6302

I actually found a bug in the inference.py, line 217 k = i - len(im_batch) + 1 + j However, even after fixing this bug, I still find that the raw output of the depth net est_depth = inference_model.inference_depth(im_batch, sess) is not consistent for different batch size.

liyingliu avatar Aug 13 '19 10:08 liyingliu

@liyingliu It is because they are using batch normalization in training mode during inference time. Their batch normalization was causing terrible effects when inferenced with it in inference mode. https://github.com/tensorflow/models/issues/6927

aj96 avatar Aug 13 '19 19:08 aj96

Hi There, We are checking to see if you still need help on this, as this seems to be an old issue. Please update this issue with the latest information, code snippet to reproduce your issue and error you are seeing. If we don't hear from you in the next 7 days, this issue will be closed automatically. If you don't need help on this issue any more, please consider closing this.

tensorflowbutler avatar Mar 18 '20 21:03 tensorflowbutler