Esempio n. 1
0
def _run_inference():
    """Runs all images through depth model and saves depth maps."""
    ckpt_basename = os.path.basename(FLAGS.model_ckpt)
    ckpt_modelname = os.path.basename(os.path.dirname(FLAGS.model_ckpt))
    output_dir = os.path.join(
        FLAGS.output_dir,
        FLAGS.kitti_video.replace('/', '_') + '_' + ckpt_modelname + '_' +
        ckpt_basename)
    if not gfile.exists(output_dir):
        gfile.makedirs(output_dir)
    inference_model = model.Model(is_training=False,
                                  seq_length=FLAGS.seq_length,
                                  batch_size=FLAGS.batch_size,
                                  img_height=FLAGS.img_height,
                                  img_width=FLAGS.img_width)
    vars_to_restore = util.get_vars_to_restore(FLAGS.model_ckpt)
    saver = tf.compat.v1.train.Saver(vars_to_restore)
    sv = tf.compat.v1.train.Supervisor(logdir='/tmp/', saver=None)
    with sv.managed_session() as sess:
        saver.restore(sess, FLAGS.model_ckpt)
        if FLAGS.kitti_video == 'test_files_eigen':
            im_files = util.read_text_lines(
                util.get_resource_path('dataset/kitti/test_files_eigen.txt'))
            im_files = [os.path.join(FLAGS.kitti_dir, f) for f in im_files]
        else:
            video_path = os.path.join(FLAGS.kitti_dir, FLAGS.kitti_video)
            im_files = gfile.glob(
                os.path.join(video_path, 'image_02/data', '*.png'))
            im_files = [f for f in im_files if 'disp' not in f]
            im_files = sorted(im_files)
        for i in range(0, len(im_files), FLAGS.batch_size):
            if i % 100 == 0:
                logging.info('Generating from %s: %d/%d', ckpt_basename, i,
                             len(im_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 = i + b
                if idx >= len(im_files):
                    break
                im = imageio.imread(im_files[idx])
                inputs[b] = np.array(
                    Image.fromarray(im).resize(
                        (FLAGS.img_width, FLAGS.img_height)))
            results = inference_model.inference(inputs, sess, mode='depth')
            for b in range(FLAGS.batch_size):
                idx = i + b
                if idx >= len(im_files):
                    break
                if FLAGS.kitti_video == 'test_files_eigen':
                    depth_path = os.path.join(output_dir, '%03d.png' % idx)
                else:
                    depth_path = os.path.join(output_dir, '%04d.png' % idx)

                depth_map = results['depth'][b]
                depth_map = np.squeeze(depth_map)
                colored_map = _normalize_depth_for_display(depth_map,
                                                           cmap=CMAP)
                imageio.imsave(depth_path, colored_map)
Esempio n. 2
0
def _run_inference():
  """Runs all images through depth model and saves depth maps."""
  ckpt_basename = os.path.basename(FLAGS.model_ckpt)
  ckpt_modelname = os.path.basename(os.path.dirname(FLAGS.model_ckpt))
  output_dir = os.path.join(FLAGS.output_dir,
                            FLAGS.kitti_video.replace('/', '_') + '_' +
                            ckpt_modelname + '_' + ckpt_basename)
  if not gfile.Exists(output_dir):
    gfile.MakeDirs(output_dir)
  inference_model = model.Model(is_training=False,
                                seq_length=FLAGS.seq_length,
                                batch_size=FLAGS.batch_size,
                                img_height=FLAGS.img_height,
                                img_width=FLAGS.img_width)
  vars_to_restore = util.get_vars_to_restore(FLAGS.model_ckpt)
  saver = tf.train.Saver(vars_to_restore)
  sv = tf.train.Supervisor(logdir='/tmp/', saver=None)
  with sv.managed_session() as sess:
    saver.restore(sess, FLAGS.model_ckpt)
    if FLAGS.kitti_video == 'test_files_eigen':
      im_files = util.read_text_lines(
          util.get_resource_path('dataset/kitti/test_files_eigen.txt'))
      im_files = [os.path.join(FLAGS.kitti_dir, f) for f in im_files]
    else:
      video_path = os.path.join(FLAGS.kitti_dir, FLAGS.kitti_video)
      im_files = gfile.Glob(os.path.join(video_path, 'image_02/data', '*.png'))
      im_files = [f for f in im_files if 'disp' not in f]
      im_files = sorted(im_files)
    for i in range(0, len(im_files), FLAGS.batch_size):
      if i % 100 == 0:
        logging.info('Generating from %s: %d/%d', ckpt_basename, i,
                     len(im_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 = i + b
        if idx >= len(im_files):
          break
        im = scipy.misc.imread(im_files[idx])
        inputs[b] = scipy.misc.imresize(im, (FLAGS.img_height, FLAGS.img_width))
      results = inference_model.inference(inputs, sess, mode='depth')
      for b in range(FLAGS.batch_size):
        idx = i + b
        if idx >= len(im_files):
          break
        if FLAGS.kitti_video == 'test_files_eigen':
          depth_path = os.path.join(output_dir, '%03d.png' % idx)
        else:
          depth_path = os.path.join(output_dir, '%04d.png' % idx)
        depth_map = results['depth'][b]
        depth_map = np.squeeze(depth_map)
        colored_map = _normalize_depth_for_display(depth_map, cmap=CMAP)
        input_float = inputs[b].astype(np.float32) / 255.0
        vertical_stack = np.concatenate((input_float, colored_map), axis=0)
        scipy.misc.imsave(depth_path, vertical_stack)
Esempio n. 3
0
def train(train_model, pretrained_ckpt, checkpoint_dir, train_steps,
          summary_freq):
    """Train model."""
    if pretrained_ckpt is not None:
        vars_to_restore = util.get_vars_to_restore(pretrained_ckpt)
        pretrain_restorer = tf.train.Saver(vars_to_restore)
    vars_to_save = util.get_vars_to_restore()
    saver = tf.train.Saver(vars_to_save + [train_model.global_step],
                           max_to_keep=MAX_TO_KEEP)
    sv = tf.train.Supervisor(logdir=checkpoint_dir,
                             save_summaries_secs=0,
                             saver=None)
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with sv.managed_session(config=config) as sess:
        if pretrained_ckpt is not None:
            logging.info('Restoring pretrained weights from %s',
                         pretrained_ckpt)
            pretrain_restorer.restore(sess, pretrained_ckpt)
        logging.info('Attempting to resume training from %s...',
                     checkpoint_dir)
        checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
        logging.info('Last checkpoint found: %s', checkpoint)
        if checkpoint:
            saver.restore(sess, checkpoint)

        logging.info('Training...')
        start_time = time.time()
        last_summary_time = time.time()
        steps_per_epoch = train_model.reader.steps_per_epoch
        step = 1
        while step <= train_steps:
            fetches = {
                'train': train_model.train_op,
                'global_step': train_model.global_step,
                'incr_global_step': train_model.incr_global_step
            }

            if step % summary_freq == 0:
                fetches['loss'] = train_model.total_loss
                fetches['summary'] = sv.summary_op

            results = sess.run(fetches)
            global_step = results['global_step']

            if step % summary_freq == 0:
                sv.summary_writer.add_summary(results['summary'], global_step)
                train_epoch = math.ceil(global_step / steps_per_epoch)
                train_step = global_step - (train_epoch - 1) * steps_per_epoch
                this_cycle = time.time() - last_summary_time
                last_summary_time += this_cycle
                logging.info(
                    'Epoch: [%2d] [%5d/%5d] time: %4.2fs (%ds total) loss: %.3f',
                    train_epoch, train_step, steps_per_epoch, this_cycle,
                    time.time() - start_time, results['loss'])

            if step % steps_per_epoch == 0:
                logging.info('[*] Saving checkpoint to %s...', checkpoint_dir)
                saver.save(sess,
                           os.path.join(checkpoint_dir, 'model'),
                           global_step=global_step)

            # Setting step to global_step allows for training for a total of
            # train_steps even if the program is restarted during training.
            step = global_step + 1
Esempio n. 4
0
def train(train_model, pretrained_ckpt, checkpoint_dir, train_steps,
          summary_freq):
  """Train model."""
  if pretrained_ckpt is not None:
    vars_to_restore = util.get_vars_to_restore(pretrained_ckpt)
    pretrain_restorer = tf.train.Saver(vars_to_restore)
  vars_to_save = util.get_vars_to_restore()
  saver = tf.train.Saver(vars_to_save + [train_model.global_step],
                         max_to_keep=MAX_TO_KEEP)
  sv = tf.train.Supervisor(logdir=checkpoint_dir, save_summaries_secs=0,
                           saver=None)
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True
  with sv.managed_session(config=config) as sess:
    if pretrained_ckpt is not None:
      logging.info('Restoring pretrained weights from %s', pretrained_ckpt)
      pretrain_restorer.restore(sess, pretrained_ckpt)
    logging.info('Attempting to resume training from %s...', checkpoint_dir)
    checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
    logging.info('Last checkpoint found: %s', checkpoint)
    if checkpoint:
      saver.restore(sess, checkpoint)

    logging.info('Training...')
    start_time = time.time()
    last_summary_time = time.time()
    steps_per_epoch = train_model.reader.steps_per_epoch
    step = 1
    while step <= train_steps:
      fetches = {
          'train': train_model.train_op,
          'global_step': train_model.global_step,
          'incr_global_step': train_model.incr_global_step
      }

      if step % summary_freq == 0:
        fetches['loss'] = train_model.total_loss
        fetches['summary'] = sv.summary_op

      results = sess.run(fetches)
      global_step = results['global_step']

      if step % summary_freq == 0:
        sv.summary_writer.add_summary(results['summary'], global_step)
        train_epoch = math.ceil(global_step / steps_per_epoch)
        train_step = global_step - (train_epoch - 1) * steps_per_epoch
        this_cycle = time.time() - last_summary_time
        last_summary_time += this_cycle
        logging.info(
            'Epoch: [%2d] [%5d/%5d] time: %4.2fs (%ds total) loss: %.3f',
            train_epoch, train_step, steps_per_epoch, this_cycle,
            time.time() - start_time, results['loss'])

      if step % steps_per_epoch == 0:
        logging.info('[*] Saving checkpoint to %s...', checkpoint_dir)
        saver.save(sess, os.path.join(checkpoint_dir, 'model'),
                   global_step=global_step)

      # Setting step to global_step allows for training for a total of
      # train_steps even if the program is restarted during training.
      step = global_step + 1
Esempio n. 5
0
def _run_inference():
    """Runs all images through depth model and saves depth maps."""
    ckpt_basename = os.path.basename(FLAGS.model_ckpt)
    ckpt_modelname = os.path.basename(os.path.dirname(FLAGS.model_ckpt))
    og_dir = FLAGS.output_dir
    output_dir = os.path.join(
        FLAGS.output_dir,
        FLAGS.kitti_video.replace('/', '_') + '_' + ckpt_modelname + '_' +
        ckpt_basename)
    if not gfile.Exists(output_dir):
        gfile.MakeDirs(output_dir)
    inference_model = model.Model(is_training=False,
                                  seq_length=FLAGS.seq_length,
                                  batch_size=FLAGS.batch_size,
                                  img_height=FLAGS.img_height,
                                  img_width=FLAGS.img_width)
    vars_to_restore = util.get_vars_to_restore(FLAGS.model_ckpt)
    saver = tf.train.Saver(vars_to_restore)
    sv = tf.train.Supervisor(logdir='/tmp/', saver=None)
    with sv.managed_session() as sess:
        saver.restore(sess, FLAGS.model_ckpt)
        if FLAGS.kitti_video == 'test_files_eigen':
            im_files = util.read_text_lines(
                util.get_resource_path('dataset/kitti/test_files_eigen.txt'))
            im_files = [os.path.join(FLAGS.kitti_dir, f) for f in im_files]
        else:
            video_path = os.path.join(FLAGS.kitti_dir, FLAGS.kitti_video)
            #edit
            #im_files = gfile.Glob(os.path.join(video_path, 'image_02/data', '*.png'))
            im_files = gfile.Glob(os.path.join(video_path, '*.png'))  #delete
            #end edit
            im_files = [f for f in im_files if 'disp' not in f]
            im_files = sorted(im_files)
        # regularPictures(im_files, output_dir, inference_model)
        for i in range(0, len(im_files), FLAGS.batch_size):
            if i % 100 == 0:
                logging.info('Generating from %s: %d/%d', ckpt_basename, i,
                             len(im_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 = i + b
                if idx >= len(im_files):
                    break
                im = scipy.misc.imread(im_files[idx],
                                       mode='RGB')  #added 2nd arg
                inputs[b] = scipy.misc.imresize(
                    im, (FLAGS.img_height, FLAGS.img_width))
            results = inference_model.inference(inputs, sess, mode='depth')
            for b in range(FLAGS.batch_size):
                idx = i + b
                if idx >= len(im_files):
                    break
                if FLAGS.kitti_video == 'test_files_eigen':
                    depth_path = os.path.join(output_dir, '%03d.png' % idx)
                else:
                    depth_path = os.path.join(output_dir, '%04d.png' % idx)
                depth_map = results['depth'][b]
                depth_map = np.squeeze(depth_map)
                colored_map = _normalize_depth_for_display(depth_map,
                                                           cmap=CMAP)
                input_float = inputs[b].astype(np.float32) / 255.0
                vertical_stack = np.concatenate((input_float, colored_map),
                                                axis=0)
                scipy.misc.imsave(depth_path, vertical_stack)
                #edit
                if FLAGS.kitti_video == 'test_files_eigen':
                    outPath = os.path.join(og_dir, 'reg_pics',
                                           '%03d.png' % idx)
                else:
                    outPath = os.path.join(og_dir, 'reg_pics',
                                           '%04d.png' % idx)
                tempImg = scipy.misc.imread(im_files[0], mode='RGB')
                resized_colored_map = scipy.misc.imresize(
                    colored_map, (len(tempImg), len(tempImg[0])))
                scipy.misc.imsave(outPath, resized_colored_map)