def main(_):
    data_dir = FLAGS.data_dir
    mode = FLAGS.mode
    assert mode in ["train", "val"]
    print "Generate data for model {}!".format(mode)
    label_map_dict = dataset_util.get_label_map_dict(FLAGS.label_map_path)

    image_dir = os.path.join(data_dir, 'JPEGImages')
    annotations_dir = os.path.join(data_dir, 'Annotations')

    # Test images are not included in the downloaded data set, so we shall perform
    # our own split.
    # random.seed(42)
    # random.shuffle(examples_list)
    # num_examples = len(examples_list)
    # num_train = int(num_examples)
    # train_examples = examples_list[:num_train]
    if not os.path.exists(FLAGS.output_dir):
        os.makedirs(FLAGS.output_dir)
    if mode == 'train':
        examples_path = os.path.join(data_dir, 'ImageSets/Main/trainval.txt')
        examples_list = dataset_util.read_examples_list(examples_path)
        print '{} training examples.', len(examples_list)
        train_output_path = os.path.join(FLAGS.output_dir, 'train.record')
        create_tf_record(train_output_path, label_map_dict, annotations_dir,
                         image_dir, examples_list)
    elif mode == 'val':
        examples_path = os.path.join(data_dir, 'ImageSets/Main/val.txt')
        examples_list = dataset_util.read_examples_list(examples_path)
        print '{} validation examples.', len(examples_list)
        val_output_path = os.path.join(FLAGS.output_dir, 'val.record')
        create_tf_record(val_output_path, label_map_dict, annotations_dir,
                         image_dir, examples_list)
def main(_):
    if FLAGS.set not in SETS:
        raise ValueError('set must be in : {}'.format(SETS))
    if FLAGS.year not in YEARS:
        raise ValueError('year must be in : {}'.format(YEARS))

    data_dir = FLAGS.data_dir
    years = ['VOC2012']
    if FLAGS.year != 'merged':
        years = [FLAGS.year]

    #writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    #label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path)

    for year in years:
        logging.info('Reading from PASCAL %s dataset.', year)
        panoptic_path = os.path.join(data_dir, year, 'panoptic_train.txt')
        #examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main',
        #                             'aeroplane_' + FLAGS.set + '.txt')
        annotations_dir = os.path.join(data_dir, year, FLAGS.annotations_dir)
        examples_list = dataset_util.read_examples_list(panoptic_path)
        #print(len(examples_list), examples_list[:10])

        for idx, example in enumerate(examples_list):
            if idx % 100 == 0:
                logging.info('On image %d of %d', idx, len(examples_list))
            path = os.path.join(annotations_dir, example + '.xml')
def main(_):
  if FLAGS.set not in SETS:
    raise ValueError('set must be in : {}'.format(SETS))

  data_dir = FLAGS.data_dir
  label_map_path = os.path.join(data_dir, "labels.pbtxt")
  examples_path = os.path.join(data_dir, "files.txt")
  annotations_dir = os.path.join(data_dir, "annotations")

  label_map_dict = label_map_util.get_label_map_dict(label_map_path)
  examples_list = dataset_util.read_examples_list(examples_path)

  writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
  for idx, example in enumerate(examples_list):
    if idx % 100 == 0:
      logging.info('On image %d of %d', idx, len(examples_list))
    path = os.path.join(annotations_dir, example + '.xml')
    with tf.gfile.GFile(path, 'r') as fid:
      xml_str = fid.read()
    xml = etree.fromstring(xml_str)
    data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
    tf_example = dict_to_tf_example(data, FLAGS.data_dir, label_map_dict, FLAGS.ignore_difficult_instances)
    writer.write(tf_example.SerializeToString())

  writer.close()
Beispiel #4
0
def main(_):
  data_dir = FLAGS.data_dir
  label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path)

  logging.info('Reading from dataset.')
  image_dir = os.path.join(data_dir, 'images')
  annotations_dir = os.path.join(data_dir, 'annotations')
  examples_path = os.path.join(annotations_dir, 'trainval.txt')
  examples_list = dataset_util.read_examples_list(examples_path)

  # Test images are not included in the downloaded data set, so we shall perform
  # our own split.
  random.seed(42)
  random.shuffle(examples_list)
  num_examples = len(examples_list)
  num_train = int(0.7 * num_examples)
  train_examples = examples_list[:num_train]
  val_examples = examples_list[num_train:]
  logging.info('%d training and %d validation examples.',
               len(train_examples), len(val_examples))

  train_output_path = os.path.join(FLAGS.output_dir, 'train.record')
  val_output_path = os.path.join(FLAGS.output_dir, 'val.record')
  create_tf_record(train_output_path, label_map_dict, annotations_dir,
                   image_dir, train_examples)
  create_tf_record(val_output_path, label_map_dict, annotations_dir,
                   image_dir, val_examples)
Beispiel #5
0
def main(_):
    if FLAGS.set not in SETS:
        raise ValueError('set must be in : {}'.format(SETS))

    data_dir = FLAGS.data_dir

    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    all_boxes = []
    all_small_boxes_count = 0

    logging.info('Reading from Ferrari dataset.')
    examples_path = os.path.join(data_dir, 'training_split_files',
                                 FLAGS.set + '.txt')
    annotations_dir = os.path.join(data_dir, 'SSD_Training_Data',
                                   FLAGS.annotations_dir)
    examples_list = dataset_util.read_examples_list(examples_path)
    for idx, example in enumerate(examples_list):
        if idx % 100 == 0:
            logging.info('On image %d of %d', idx, len(examples_list))
        path = os.path.join(annotations_dir, example + '.xml')
        try:
            path = path.replace('.mp4', '_mp4')
            with tf.gfile.GFile(path, 'rb') as fid:
                xml_str = fid.read()
        except:
            import ipdb
            ipdb.set_trace()
        try:
            xml = etree.fromstring(xml_str)
        except:
            import ipdb
            ipdb.set_trace()
        data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

        tf_example, boxes, small_boxes_count = dict_to_tf_example(
            data, FLAGS.data_dir, FLAGS.ignore_difficult_instances)
        writer.write(tf_example.SerializeToString())
        all_boxes += boxes
        all_small_boxes_count += small_boxes_count

    writer.close()

    import ipdb
    ipdb.set_trace()

    with open('{}_all_boxes.pkl'.format(FLAGS.set), 'wb') as f:
        pickle.dump(all_boxes, f)
Beispiel #6
0
def proccess(set_path, output_path):
    writer = tf.python_io.TFRecordWriter(output_path)
    label_map_dict = get_label_map_dict_from_json(label_map_path)

    examples_path = set_path
    examples_list = dataset_util.read_examples_list(examples_path)
    for idx, example in enumerate(examples_list):
        if idx % 100 == 0:
            logging.info('On image %d of %d', idx, len(examples_list))
        path = os.path.join(annotations_dir, example + '.xml')
        with tf.gfile.GFile(path, 'r') as fid:
            xml = ET.parse(fid).getroot()
        data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
        tf_example = dict_to_tf_example(path, data, images_dir, label_map_dict,
                                        FLAGS.ignore_difficult_instances)
        writer.write(tf_example.SerializeToString())

    writer.close()
Beispiel #7
0
def main(_):
    if FLAGS.year not in YEARS:
        raise ValueError('year must be in : {}'.format(YEARS))

    data_dir = FLAGS.data_dir
    years = ['VOC2007', 'VOC2012']
    if FLAGS.year != 'merged':
        years = [FLAGS.year]
    output_dir = FLAGS.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    for year in years:
        for split in SETS:
            filename = os.path.join(output_dir,
                                    '{}_{}.tfrecord'.format(year, split))
            if os.path.exists(filename):
                if FLAGS.overwrite or os.path.getsize(filename) == 0:
                    os.remove(filename)
                else:
                    print('skipped %s.' % filename)
                    continue
            writer = tf.python_io.TFRecordWriter(filename)
            print('Reading from PASCAL %s %s dataset.' % (year, split))
            examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main',
                                         split + '.txt')
            annotations_dir = os.path.join(data_dir, year, 'Annotations')
            examples_list = dataset_util.read_examples_list(examples_path)
            for example in tqdm(examples_list):
                try:
                    path = os.path.join(annotations_dir, example + '.xml')
                    with tf.gfile.GFile(path, 'r') as fid:
                        xml_str = fid.read()
                    xml = etree.fromstring(xml_str)
                    data = dataset_util.recursive_parse_xml_to_dict(
                        xml)['annotation']
                    tf_example = dict_to_tf_example(
                        data, FLAGS.data_dir, FLAGS.ignore_difficult_instances)
                    writer.write(tf_example.SerializeToString())
                except Exception as e:
                    print(str(e))
            writer.close()
def main(year, data_dir, annotations_dir, set, ignore_difficult_instances):
  if set not in SETS:
    raise ValueError('set must be in : {}'.format(SETS))
  if year not in YEARS:
    raise ValueError('year must be in : {}'.format(YEARS))

  data_dir = data_dir
  years = ['VOC2007', 'VOC2012']
  if year != 'merged':
    years = [year]

  writer = tf.python_io.TFRecordWriter(output_path)

  label_map_dict = label_map_util.get_label_map_dict(label_map_path)

  for year in years:
    logging.info('Reading from PASCAL %s dataset.', year)
    examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main', set + '.txt')
    # annotations_dir = os.path.join(data_dir, year, annotations_dir)
    annotations_dir = annotations_dir
    examples_list = dataset_util.read_examples_list(examples_path)
    for idx, example in enumerate(examples_list):
      if idx % 100 == 0:
        logging.info('On image %d of %d', idx, len(examples_list))
      path = os.path.join(annotations_dir, example + '.xml')
      with tf.gfile.GFile(path, 'r') as fid:
        xml_str = fid.read()
      # xml = etree.fromstring(xml_str)
      xml = etree.fromstring(xml_str.encode('utf-8'))
      data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

      tf_example = dict_to_tf_example(year, data, data_dir,
                                      label_map_dict, example,
                                      ignore_difficult_instances)
      writer.write(tf_example.SerializeToString())

  writer.close()
Beispiel #9
0
def main(_):
    if FLAGS.set not in SETS:
        raise ValueError('set must be in : {}'.format(SETS))
    # if FLAGS.year not in YEARS:
    # raise ValueError('year must be in : {}'.format(YEARS))

    data_dir = FLAGS.data_dir
    # years = ['VOC2007', 'VOC2012']
    # if FLAGS.year != 'merged':
    # years = [FLAGS.year]

    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    label_map_dict = get_label_map_dict(FLAGS.label_map_path)

    for year in range(1):
        logging.info('Reading from PASCAL-like customized dataset.')
        examples_path = os.path.join(data_dir, 'ImageSets', 'Main',
                                     FLAGS.set + '.txt')
        annotations_dir = os.path.join(data_dir, FLAGS.annotations_dir)
        examples_list = dataset_util.read_examples_list(examples_path)
        for idx, example in enumerate(examples_list):
            if idx % 100 == 0:
                logging.info('On image %d of %d', idx, len(examples_list))
            path = os.path.join(annotations_dir, example + '.xml')
            with tf.gfile.GFile(path, 'r') as fid:
                xml_str = fid.read()
            xml = etree.fromstring(xml_str)
            data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

            tf_example = dict_to_tf_example(data, FLAGS.data_dir,
                                            label_map_dict,
                                            FLAGS.ignore_difficult_instances)
            writer.write(tf_example.SerializeToString())

    writer.close()
Beispiel #10
0
def main(unused_argv):
    # Using the Winograd non-fused algorithms provides a small performance boost.
    os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'

    pred_hooks = None
    if FLAGS.debug:
        debug_hook = tf_debug.LocalCLIDebugHook()
        pred_hooks = [debug_hook]

    model = tf.estimator.Estimator(
        model_fn=mymodel.network_model_fn,
        model_dir=FLAGS.model_dir,
        params={
            'output_stride': FLAGS.output_stride,
            'batch_size':
            4,  # Batch size must be 1 because the images' size may differ
            'base_architecture': FLAGS.base_architecture,
            'pre_trained_model': None,
            'batch_norm_decay': None,
            'num_classes': _NUM_CLASSES,
        })

    examples = dataset_util.read_examples_list(FLAGS.infer_data_list)
    image_files = [
        os.path.join(FLAGS.data_dir, filename) + '.png'
        for filename in examples
    ]
    region_files = [
        os.path.join(FLAGS.region_data_dir, filename) + '.png'
        for filename in examples
    ]

    predictions = model.predict(input_fn=lambda: preprocessing.eval_input_fn(
        image_files, region_files),
                                hooks=pred_hooks)

    output_dir = FLAGS.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    for pred_dict, image_path in zip(predictions, image_files):
        image_basename = os.path.splitext(os.path.basename(image_path))[0]
        output_filename = image_basename + '.png'
        path_to_output = os.path.join(output_dir, output_filename)

        print("generating:", path_to_output)
        mask = pred_dict['decoded_labels']
        mask = Image.fromarray(mask)
        fig, ax = plt.subplots()
        #mask = mask[:, :, (2, 1, 0)]
        ax.imshow(mask, aspect='equal')
        plt.axis('off')
        # 去除图像周围的白边
        # 如果dpi=300,那么图像大小=height*width
        fig.set_size_inches(321 / 300.0, 321 / 300.0)
        plt.gca().xaxis.set_major_locator(plt.NullLocator())
        plt.gca().yaxis.set_major_locator(plt.NullLocator())
        plt.subplots_adjust(top=1,
                            bottom=0,
                            left=0,
                            right=1,
                            hspace=0,
                            wspace=0)
        plt.margins(0, 0)
        plt.savefig(path_to_output, dpi=300)
        """ plt.axis('off')
Beispiel #11
0
def main(unused_argv):
  # Using the Winograd non-fused algorithms provides a small performance boost.
  os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'
  os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
  os.environ["CUDA_VISIBLE_DEVICES"] = "3" 

  examples = dataset_util.read_examples_list(FLAGS.evaluation_data_list)
  image_files = [os.path.join(FLAGS.image_data_dir, filename) + '.png' for filename in examples]
  region_files = [os.path.join(FLAGS.region_data_dir, filename) + '.png' for filename in examples]
  label_files = [os.path.join(FLAGS.label_data_dir, filename) + '.png' for filename in examples]

  features,labels = preprocessing.eval_input_fn(image_files,region_files,label_files)

  predictions = mymodel.network_model_fn(
      features,
      labels,
      tf.estimator.ModeKeys.EVAL,
      params={
          'output_stride': FLAGS.output_stride,
          'batch_size': 8,  # Batch size must be 1 because the images' size may differ
          'base_architecture': FLAGS.base_architecture,
          'pre_trained_model': None,
          'batch_norm_decay': None,
          'num_classes': _NUM_CLASSES,
          'freeze_batch_norm': True
      }).predictions

  # Manually load the latest checkpoint
  saver = tf.train.Saver()
  with tf.Session() as sess:
    ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir)
    saver.restore(sess, ckpt.model_checkpoint_path)

    # Loop through the batches and store predictions and labels
    step = 1
    sum_cm = np.zeros((_NUM_CLASSES, _NUM_CLASSES), dtype=np.int32)
    start = timeit.default_timer()
    while True:
      try:
        preds = sess.run(predictions)
        sum_cm += preds['confusion_matrix']
        if not step % 100:
          stop = timeit.default_timer()
          tf.logging.info("current step = {} ({:.3f} sec)".format(step, stop-start))
          start = timeit.default_timer()
        step += 1
      except tf.errors.OutOfRangeError:
        break

    def compute_mean_iou(total_cm):
      """Compute the mean intersection-over-union via the confusion matrix."""
      sum_over_row = np.sum(total_cm, axis=0).astype(float)
      sum_over_col = np.sum(total_cm, axis=1).astype(float)
      cm_diag = np.diagonal(total_cm).astype(float)
      denominator = sum_over_row + sum_over_col - cm_diag

      # The mean is only computed over classes that appear in the
      # label or prediction tensor. If the denominator is 0, we need to
      # ignore the class.
      num_valid_entries = np.sum((denominator != 0).astype(float))

      # If the value of the denominator is 0, set it to 1 to avoid
      # zero division.
      denominator = np.where(
          denominator > 0,
          denominator,
          np.ones_like(denominator))

      ious = cm_diag / denominator

      print('Intersection over Union for each class:')
      for i, iou in enumerate(ious):
        print('    class {}: {:.4f}'.format(i, iou))

      # If the number of valid entries is 0 (no classes) we return 0.
      m_iou = np.where(
          num_valid_entries > 0,
          np.sum(ious) / num_valid_entries,
          0)
      m_iou = float(m_iou)
      print('mean Intersection over Union: {:.4f}'.format(float(m_iou)))

    def compute_accuracy(total_cm):
      """Compute the accuracy via the confusion matrix."""
      denominator = total_cm.sum().astype(float)
      cm_diag_sum = np.diagonal(total_cm).sum().astype(float)

      # If the number of valid entries is 0 (no classes) we return 0.
      accuracy = np.where(
          denominator > 0,
          cm_diag_sum / denominator,
          0)
      accuracy = float(accuracy)
      print('Pixel Accuracy: {:.4f}'.format(float(accuracy)))

    compute_mean_iou(sum_cm)
    compute_accuracy(sum_cm)
def main(unused_argv):
    # Using the Winograd non-fused algorithms provides a small performance boost.
    os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'

    pred_hooks = None
    if FLAGS.debug:
        debug_hook = tf_debug.LocalCLIDebugHook()
        pred_hooks = [debug_hook]

    model = tf.estimator.Estimator(
        model_fn=deeplabv3plus_hed_model.deeplabv3_plus_model_fn,
        model_dir=FLAGS.model_dir,
        params={
            'output_stride': FLAGS.output_stride,
            'batch_size':
            1,  # Batch size must be 1 because the images' size may differ
            'base_architecture': FLAGS.base_architecture,
            'pre_trained_model': None,
            'batch_norm_decay': None,
            'num_classes': _NUM_CLASSES,
        })

    examples = dataset_util.read_examples_list(FLAGS.infer_data_list)
    image_files = [
        os.path.join(FLAGS.data_dir, filename) for filename in examples
    ]

    predictions = model.predict(
        input_fn=lambda: preprocessing.eval_input_fn(image_files),
        hooks=pred_hooks)

    output_dir = FLAGS.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    for pred_dict, image_path in zip(predictions, image_files):
        image_basename = os.path.splitext(os.path.basename(image_path))[0]
        output_filename = image_basename + '.jpg'
        path_to_output = os.path.join(output_dir, output_filename)

        print("generating:", path_to_output)
        mask = pred_dict['decoded_labels']
        raw_mask = pred_dict['classes']
        cv2.imwrite(os.path.join(output_dir, image_basename + '.png'),
                    raw_mask)

        side1_mask = pred_dict['side_output1']
        cv2.imwrite(os.path.join(output_dir, image_basename + '_side1.png'),
                    side1_mask)
        side2_mask = pred_dict['side_output2']
        cv2.imwrite(os.path.join(output_dir, image_basename + '_side2.png'),
                    side2_mask)
        side3_mask = pred_dict['side_output3']
        cv2.imwrite(os.path.join(output_dir, image_basename + '_side3.png'),
                    side3_mask)
        side4_mask = pred_dict['side_output4']
        cv2.imwrite(os.path.join(output_dir, image_basename + '_side4.png'),
                    side4_mask)
        side5_mask = pred_dict['side_output5']
        cv2.imwrite(os.path.join(output_dir, image_basename + '_side5.png'),
                    side5_mask)
        side_fuse_mask = pred_dict['side_output_fuse']
        cv2.imwrite(
            os.path.join(output_dir, image_basename + '_side_fuse.png'),
            side_fuse_mask)
        #pdb.set_trace()
        #np.save(os.path.join(output_dir,image_basename+'.npy'),pred_dict['probabilities'])
        #pdb.set_trace()
        mask = Image.fromarray(mask)
        #pdb.set_trace()
        mask.save(path_to_output)