Example #1
0
def predict_homography(inputs, network_id='cvgghmg', reuse=None,
                       is_training=True, scope='hier_hmg'):
  """Estimates homography using a selected deep neural network.

  Args:
    inputs: batch of input image pairs of data type float32 and of shape
      [batch_size, height, width, None]
    network_id: deep neural network method
    reuse: whether to reuse this network weights
    is_training: whether used for training or testing
    scope: the scope of variables in this function
  Raises:
    ValueError: The nework_id was not good.
  Returns:
    a list of homographies at each level and a list of images warped by
    the list of corresponding homographies
  """
  with slim.arg_scope(models.homography_arg_scope(
      weight_decay=FLAGS.weight_decay)):
    if network_id == 'hier':
      return models.hier_homography_estimator(
          inputs, num_param=8, num_layer=FLAGS.num_layer,
          num_level=FLAGS.num_level,
          dropout_keep_prob=FLAGS.dropout_keep,
          is_training=is_training, reuse=reuse, scope=scope)
    elif network_id == 'fmask_sem':
      return models.hier_homography_fmask_estimator(
          inputs, num_param=8, num_layer=FLAGS.num_layer,
          num_level=FLAGS.num_level,
          dropout_keep_prob=FLAGS.dropout_keep,
          is_training=is_training, reuse=reuse, scope=scope)
    else:
      raise ValueError('Unknown network_id: %s' % network_id)
Example #2
0
def run_test():
  """Estimates the homography between two input images.
  """
  image1 = cv2.imread(FLAGS.image1)
  image2 = cv2.imread(FLAGS.image2)
  image_list = [image1, image2]
  image_norm_list = []
  for i in range(2):
    if FLAGS.network_id == 'fmask_sem':
      image_scale = cv2.resize(image_list[i],
                               (FLAGS.train_width, FLAGS.train_height),
                               cv2.INTER_LANCZOS4)
    else:
      image_gray = cv2.cvtColor(image_list[i], cv2.COLOR_BGR2GRAY)
      image_scale = cv2.resize(image_gray,
                               (FLAGS.train_width, FLAGS.train_height),
                               cv2.INTER_LANCZOS4)

    image_norm = image_scale / 256.0 - 0.5
    image_norm_list.append(image_norm)
  if FLAGS.network_id == 'fmask_sem':
    norm_image_pair = np.expand_dims(np.concatenate(image_norm_list, 2), axis=0)
    num_channel = 3
  else:
    norm_image_pair = np.expand_dims(np.stack(image_norm_list, -1), axis=0)
    num_channel = 1

  batch_pairs = tf.placeholder(tf.float32,
                               [1, FLAGS.train_height, FLAGS.train_width,
                                2 * num_channel])
  with slim.arg_scope(models.homography_arg_scope()):
    if FLAGS.network_id == 'fmask_sem':
      batch_hmg_prediction, _ = models.hier_homography_fmask_estimator(
          batch_pairs, num_param=8, num_layer=FLAGS.num_layer,
          num_level=FLAGS.num_level, is_training=False)
    else:
      batch_hmg_prediction, _ = models.hier_homography_estimator(
          batch_pairs, num_param=8, num_layer=FLAGS.num_layer,
          num_level=FLAGS.num_level, is_training=False)

  batch_warped_result, _ = hmg_util.homography_warp_per_batch(
      batch_pairs[Ellipsis, 0 : num_channel],
      batch_hmg_prediction[FLAGS.num_level - 1])

  saver = tf.Saver()
  with tf.Session() as sess:
    saver.restore(sess, FLAGS.model_path)
    image_warp, homography_list = sess.run(
        [batch_warped_result, batch_hmg_prediction],
        feed_dict={batch_pairs: norm_image_pair})
    for i in range(8):
      logging.info('%f ', homography_list[FLAGS.num_level - 1][0][i])
    cv2.imwrite('%s/input0.jpg' % FLAGS.out_dir,
                (image_norm_list[0] + 0.5) * 256)
    cv2.imwrite('%s/input1.jpg' % FLAGS.out_dir,
                (image_norm_list[1] + 0.5) * 256)
    cv2.imwrite('%s/result.jpg' % FLAGS.out_dir, (image_warp[0] + 0.5) * 256)