示例#1
0
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)

    required_flags = [
        'input_tfrecord_paths', 'output_tfrecord_path', 'inference_graph',
        'num_inter_threads', 'num_intra_threads'
    ]
    for flag_name in required_flags:
        if not getattr(FLAGS, flag_name):
            raise ValueError('Flag --{} is required'.format(flag_name))

    with tf.Session(config=tf.ConfigProto(
            inter_op_parallelism_threads=FLAGS.num_inter_threads,
            intra_op_parallelism_threads=FLAGS.num_intra_threads)) as sess:
        input_tfrecord_paths = [
            v for v in FLAGS.input_tfrecord_paths.split(',') if v
        ]
        tf.logging.info('Reading input from %d files',
                        len(input_tfrecord_paths))
        serialized_example_tensor, image_tensor = detection_inference.build_input(
            input_tfrecord_paths)
        tf.logging.info('Reading graph and building model...')
        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
             image_tensor, FLAGS.inference_graph)

        tf.logging.info('Running inference and writing output to {}'.format(
            FLAGS.output_tfrecord_path))
        sess.run(tf.local_variables_initializer())
        tf.train.start_queue_runners()

        latency = []
        with tf.python_io.TFRecordWriter(
                FLAGS.output_tfrecord_path) as tf_record_writer:
            try:
                for counter in itertools.count():
                    tf.logging.log_every_n(
                        tf.logging.INFO,
                        'Processed %d images... moving average latency %d ms',
                        200, counter + 1, np.mean(latency[-200:]))
                    start = time.time()
                    tf_example = detection_inference.\
                        infer_detections_and_add_to_example(
                            serialized_example_tensor, detected_boxes_tensor,
                            detected_scores_tensor, detected_labels_tensor,
                            FLAGS.discard_image_pixels)
                    duration = time.time() - start
                    latency.append(duration * 1000)
                    tf_record_writer.write(tf_example.SerializeToString())
            except tf.errors.OutOfRangeError:
                tf.logging.info('Finished processing records')
        latency = np.array(latency)
        print("Latency: min = {:.1f}, max = {:.1f}, mean= {:.1f}, median "
              "= {:.1f}".format(latency.min(), latency.max(), latency.mean(),
                                np.median(latency)))
    def test_simple(self):
        create_mock_graph()
        create_mock_tfrecord()

        serialized_example_tensor, image_tensor = detection_inference.build_input(
            [get_mock_tfrecord_path()])
        self.assertAllEqual(image_tensor.get_shape().as_list(),
                            [1, None, None, 3])

        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
             image_tensor, get_mock_graph_path())

        with self.test_session(use_gpu=False) as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            tf.train.start_queue_runners()

            tf_example = detection_inference.infer_detections_and_add_to_example(
                serialized_example_tensor, detected_boxes_tensor,
                detected_scores_tensor, detected_labels_tensor, False)

        self.assertProtoEquals(
            r"""
        features {
          feature {
            key: "image/detection/bbox/ymin"
            value { float_list { value: [0.0, 0.1] } } }
          feature {
            key: "image/detection/bbox/xmin"
            value { float_list { value: [0.8, 0.2] } } }
          feature {
            key: "image/detection/bbox/ymax"
            value { float_list { value: [0.7, 0.8] } } }
          feature {
            key: "image/detection/bbox/xmax"
            value { float_list { value: [1.0, 0.9] } } }
          feature {
            key: "image/detection/label"
            value { int64_list { value: [123, 246] } } }
          feature {
            key: "image/detection/score"
            value { float_list { value: [0.1, 0.2] } } }
          feature {
            key: "image/encoded"
            value { bytes_list { value:
              "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\001\000\000"
              "\000\001\010\002\000\000\000\220wS\336\000\000\000\022IDATx"
              "\234b\250f`\000\000\000\000\377\377\003\000\001u\000|gO\242"
              "\213\000\000\000\000IEND\256B`\202" } } }
          feature {
            key: "test_field"
            value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } }
    """, tf_example)
  def test_simple(self):
    create_mock_graph()
    create_mock_tfrecord()

    serialized_example_tensor, image_tensor = detection_inference.build_input(
        [get_mock_tfrecord_path()])
    self.assertAllEqual(image_tensor.get_shape().as_list(), [1, None, None, 3])

    (detected_boxes_tensor, detected_scores_tensor,
     detected_labels_tensor) = detection_inference.build_inference_graph(
         image_tensor, get_mock_graph_path())

    with self.test_session(use_gpu=False) as sess:
      sess.run(tf.global_variables_initializer())
      sess.run(tf.local_variables_initializer())
      tf.train.start_queue_runners()

      tf_example = detection_inference.infer_detections_and_add_to_example(
          serialized_example_tensor, detected_boxes_tensor,
          detected_scores_tensor, detected_labels_tensor, False)

    self.assertProtoEquals(r"""
        features {
          feature {
            key: "image/detection/bbox/ymin"
            value { float_list { value: [0.0, 0.1] } } }
          feature {
            key: "image/detection/bbox/xmin"
            value { float_list { value: [0.8, 0.2] } } }
          feature {
            key: "image/detection/bbox/ymax"
            value { float_list { value: [0.7, 0.8] } } }
          feature {
            key: "image/detection/bbox/xmax"
            value { float_list { value: [1.0, 0.9] } } }
          feature {
            key: "image/detection/label"
            value { int64_list { value: [123, 246] } } }
          feature {
            key: "image/detection/score"
            value { float_list { value: [0.1, 0.2] } } }
          feature {
            key: "image/encoded"
            value { bytes_list { value:
              "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\001\000\000"
              "\000\001\010\002\000\000\000\220wS\336\000\000\000\022IDATx"
              "\234b\250f`\000\000\000\000\377\377\003\000\001u\000|gO\242"
              "\213\000\000\000\000IEND\256B`\202" } } }
          feature {
            key: "test_field"
            value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } }
    """, tf_example)
示例#4
0
def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)

  sat=Satellite()

  sat.check_flags(required_flags = ['input_tfrecord_paths', 'output_tfrecord_path','inference_graph'])



  with tf.Session() as sess:
    input_tfrecord_paths = [
        v for v in FLAGS.input_tfrecord_paths.split(',') if v]

    number,image_tensor = detection_inference.build_input(
        input_tfrecord_paths)

    tf.logging.info('Reading Queue \t',number)

    tf.logging.info('Reading graph and building model...')

    with tf.gfile.Open(FLAGS.inference_graph, 'rb') as graph_def_file:
        graph_content = graph_def_file.read()

    (detected_boxes_tensor, detected_scores_tensor,
     detected_labels_tensor) = build_inference_graph(
         image_tensor, graph_content)

    tf.logging.info('Running inference and writing output to {}'.format(
        FLAGS.output_tfrecord_path))
    sess.run(tf.local_variables_initializer())
    tf.train.start_queue_runners()

    (serialized_example, detected_boxes, detected_scores,
     detected_classes) = sess.run([
         serialized_example_tensor, detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor
     ])
    detected_boxes = detected_boxes.T

    with tf.python_io.TFRecordWriter(
        FLAGS.output_tfrecord_path) as tf_record_writer:
      try:
        for counter in itertools.count():
          tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 10,
                                 counter)
          tf_example = detection_inference.infer_detections_and_add_to_example(
              serialized_example_tensor, detected_boxes_tensor,
              detected_scores_tensor, detected_labels_tensor,
              FLAGS.discard_image_pixels)
          tf_record_writer.write(tf_example.SerializeToString())
      except tf.errors.OutOfRangeError:
        tf.logging.info('Finished processing records')
示例#5
0
    def test_simple(self):
        create_mock_graph()
        encoded_image = create_mock_tfrecord()

        serialized_example_tensor, image_tensor = detection_inference.build_input(
            [get_mock_tfrecord_path()])
        self.assertAllEqual(image_tensor.get_shape().as_list(),
                            [1, None, None, 3])

        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
             image_tensor, get_mock_graph_path())

        with self.test_session(use_gpu=False) as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            tf.train.start_queue_runners()

            tf_example = detection_inference.infer_detections_and_add_to_example(
                serialized_example_tensor, detected_boxes_tensor,
                detected_scores_tensor, detected_labels_tensor, False)
        expected_example = tf.train.Example()
        text_format.Merge(
            r"""
        features {
          feature {
            key: "image/detection/bbox/ymin"
            value { float_list { value: [0.0, 0.1] } } }
          feature {
            key: "image/detection/bbox/xmin"
            value { float_list { value: [0.8, 0.2] } } }
          feature {
            key: "image/detection/bbox/ymax"
            value { float_list { value: [0.7, 0.8] } } }
          feature {
            key: "image/detection/bbox/xmax"
            value { float_list { value: [1.0, 0.9] } } }
          feature {
            key: "image/detection/label"
            value { int64_list { value: [123, 246] } } }
          feature {
            key: "image/detection/score"
            value { float_list { value: [0.1, 0.2] } } }
          feature {
            key: "test_field"
            value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } }""",
            expected_example)
        expected_example.features.feature[
            standard_fields.TfExampleFields.image_encoded].CopyFrom(
                dataset_util.bytes_feature(encoded_image))
        self.assertProtoEquals(expected_example, tf_example)
示例#6
0
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)

    required_flags = [
        'input_tfrecord_paths', 'output_tfrecord_path', 'inference_graph'
    ]
    for flag_name in required_flags:
        if not getattr(FLAGS, flag_name):
            raise ValueError('Flag --{} is required'.format(flag_name))

    with tf.Session() as sess:
        input_tfrecord_paths = [
            v for v in FLAGS.input_tfrecord_paths.split(',') if v
        ]
        tf.logging.info('Reading input from %d files',
                        len(input_tfrecord_paths))
        serialized_example_tensor, image_tensor = detection_inference.build_input(
            input_tfrecord_paths)
        tf.logging.info('Reading graph and building model...')
        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
             image_tensor, FLAGS.inference_graph)

        tf.logging.info('Running inference and writing output to {}'.format(
            FLAGS.output_tfrecord_path))
        sess.run(tf.local_variables_initializer())
        tf.train.start_queue_runners()

        print("entering into loop ")
        starttime = datetime.datetime.now()
        with tf.python_io.TFRecordWriter(
                FLAGS.output_tfrecord_path) as tf_record_writer:
            try:
                for counter in itertools.count():
                    tf.logging.log_every_n(tf.logging.INFO,
                                           'Processed %d images...', 10,
                                           counter)
                    t0 = datetime.datetime.now()
                    tf_example = detection_inference.infer_detections_and_add_to_example(
                        serialized_example_tensor, detected_boxes_tensor,
                        detected_scores_tensor, detected_labels_tensor,
                        FLAGS.discard_image_pixels)
                    t1 = datetime.datetime.now()
                    tf.logging.info('processed an image in %d ms',
                                    (t1 - t0).microseconds / 1000)
                    tf_record_writer.write(tf_example.SerializeToString())
            except tf.errors.OutOfRangeError:
                tf.logging.info('Finished processing records')
            endtime = datetime.datetime.now()
            print("running time is ")
            print((endtime - starttime).seconds)
def main(_):
    tf.enable_eager_execution()
    tf.logging.set_verbosity(tf.logging.INFO)

    required_flags = ['input_tfrecord_paths', 'output_tfrecord_path',
                      'inference_graph']
    for flag_name in required_flags:
        if not getattr(FLAGS, flag_name):
            raise ValueError('Flag --{} is required'.format(flag_name))

    if FLAGS.gpu_device:
        os.environ["CUDA_VISIBLE_DEVICES"] = str(FLAGS.gpu_device)

    output_folder = "/"
    output_folder = output_folder.join(FLAGS.output_tfrecord_path.split("/")[:-1])

    if not os.path.exists(output_folder):
        os.mkdir(output_folder)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        input_tfrecord_paths = [
            v for v in FLAGS.input_tfrecord_paths.split(',') if v]
        tf.logging.info('Reading input from %d files', len(input_tfrecord_paths))
        serialized_example_tensor, image_tensor = detection_inference.build_input(
            input_tfrecord_paths, num_additional_channels=FLAGS.num_additional_channels)
        tf.logging.info('Reading graph and building model...')
        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
            image_tensor, FLAGS.inference_graph)

        tf.logging.info('Running inference and writing output to {}'.format(
            FLAGS.output_tfrecord_path))
        sess.run(tf.local_variables_initializer())
        tf.train.start_queue_runners()
        with tf.python_io.TFRecordWriter(
                FLAGS.output_tfrecord_path) as tf_record_writer:
            try:
                for counter in itertools.count():
                    tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 10,
                                           counter)
                    tf_example = detection_inference.infer_detections_and_add_to_example(
                        serialized_example_tensor, detected_boxes_tensor,
                        detected_scores_tensor, detected_labels_tensor,
                        FLAGS.discard_image_pixels)

                    tf_record_writer.write(tf_example.SerializeToString())
            except tf.errors.OutOfRangeError:
                tf.logging.info('Finished processing records')
示例#8
0
def main(_):
    tf.logging.set_verbosity(tf.logging.INFO)

    required_flags = [
        'input_tfrecord_pattern', 'output_images_dir', 'inference_graph',
        'label_map'
    ]
    for flag_name in required_flags:
        if not getattr(FLAGS, flag_name):
            raise ValueError('Flag --{} is required'.format(flag_name))

    # load the categories
    category_index = label_map_util.create_category_index_from_labelmap(
        FLAGS.label_map, use_display_name=True)

    # create the outputdir if it doesn't exist already
    if not os.path.exists(FLAGS.output_images_dir):
        os.mkdir(FLAGS.output_images_dir)

    with tf.Session() as sess:
        input_tfrecord_paths = glob.glob(FLAGS.input_tfrecord_pattern)
        tf.logging.info('Reading input from %d files',
                        len(input_tfrecord_paths))
        serialized_example_tensor, image_tensor = detection_inference.build_input(
            input_tfrecord_paths)
        tf.logging.info('Reading graph and building model...')
        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
             image_tensor, FLAGS.inference_graph)

        tf.logging.info('Running inference and writing output to {}'.format(
            FLAGS.output_images_dir))
        sess.run(tf.local_variables_initializer())
        tf.train.start_queue_runners()
        try:
            for counter in itertools.count():
                tf.logging.log_every_n(tf.logging.INFO,
                                       'Processed %d images...', 10, counter)
                tf_example = detection_inference.infer_detections_and_add_to_example(
                    serialized_example_tensor, detected_boxes_tensor,
                    detected_scores_tensor, detected_labels_tensor, False)
                image_np = get_image_array_from_example(tf_example)
                draw_bounding_boxes_from_example(image_np, tf_example,
                                                 category_index)
                im = Image.fromarray(image_np)
                pid = tf_example.features.feature[
                    standard_fields.TfExampleFields.
                    source_id].bytes_list.value[0].decode()
                im.save(os.path.join(FLAGS.output_images_dir, pid + '.jpg'))
        except tf.errors.OutOfRangeError:
            tf.logging.info('Finished processing records')
    def test_discard_image(self):
        create_mock_graph()
        create_mock_tfrecord()

        serialized_example_tensor, image_tensor = detection_inference.build_input(
            [get_mock_tfrecord_path()])
        (detected_boxes_tensor, detected_scores_tensor,
         detected_labels_tensor) = detection_inference.build_inference_graph(
             image_tensor, get_mock_graph_path())

        with self.test_session(use_gpu=False) as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())
            tf.train.start_queue_runners()

            tf_example = detection_inference.infer_detections_and_add_to_example(
                serialized_example_tensor, detected_boxes_tensor,
                detected_scores_tensor, detected_labels_tensor, True)

        self.assertProtoEquals(
            r"""
        features {
          feature {
            key: "image/detection/bbox/ymin"
            value { float_list { value: [0.0, 0.1] } } }
          feature {
            key: "image/detection/bbox/xmin"
            value { float_list { value: [0.8, 0.2] } } }
          feature {
            key: "image/detection/bbox/ymax"
            value { float_list { value: [0.7, 0.8] } } }
          feature {
            key: "image/detection/bbox/xmax"
            value { float_list { value: [1.0, 0.9] } } }
          feature {
            key: "image/detection/label"
            value { int64_list { value: [123, 246] } } }
          feature {
            key: "image/detection/score"
            value { float_list { value: [0.1, 0.2] } } }
          feature {
            key: "test_field"
            value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } }
    """, tf_example)
  def test_discard_image(self):
    create_mock_graph()
    create_mock_tfrecord()

    serialized_example_tensor, image_tensor = detection_inference.build_input(
        [get_mock_tfrecord_path()])
    (detected_boxes_tensor, detected_scores_tensor,
     detected_labels_tensor) = detection_inference.build_inference_graph(
         image_tensor, get_mock_graph_path())

    with self.test_session(use_gpu=False) as sess:
      sess.run(tf.global_variables_initializer())
      sess.run(tf.local_variables_initializer())
      tf.train.start_queue_runners()

      tf_example = detection_inference.infer_detections_and_add_to_example(
          serialized_example_tensor, detected_boxes_tensor,
          detected_scores_tensor, detected_labels_tensor, True)

    self.assertProtoEquals(r"""
        features {
          feature {
            key: "image/detection/bbox/ymin"
            value { float_list { value: [0.0, 0.1] } } }
          feature {
            key: "image/detection/bbox/xmin"
            value { float_list { value: [0.8, 0.2] } } }
          feature {
            key: "image/detection/bbox/ymax"
            value { float_list { value: [0.7, 0.8] } } }
          feature {
            key: "image/detection/bbox/xmax"
            value { float_list { value: [1.0, 0.9] } } }
          feature {
            key: "image/detection/label"
            value { int64_list { value: [123, 246] } } }
          feature {
            key: "image/detection/score"
            value { float_list { value: [0.1, 0.2] } } }
          feature {
            key: "test_field"
            value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } }
    """, tf_example)
示例#11
0
def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)

  required_flags = ['input_tfrecord_paths', 'output_tfrecord_path',
                    'inference_graph']
  for flag_name in required_flags:
    if not getattr(FLAGS, flag_name):
      raise ValueError('Flag --{} is required'.format(flag_name))

  with tf.Session() as sess:
    input_tfrecord_paths = []
    for item in os.listdir(FLAGS.input_tfrecord_paths):
        if 'testdev' in item:
            input_tfrecord_paths.append('{}/{}'.format(FLAGS.input_tfrecord_paths,item))

    print(input_tfrecord_paths)
    # for v in FLAGS.input_tfrecord_paths.split(',') if v
    tf.logging.info('Reading input from %d files', len(input_tfrecord_paths))
    serialized_example_tensor, image_tensor = detection_inference.build_input(
        input_tfrecord_paths)
    tf.logging.info('Reading graph and building model...')
    (detected_boxes_tensor, detected_scores_tensor,
     detected_labels_tensor) = detection_inference.build_inference_graph(
         image_tensor, FLAGS.inference_graph)

    tf.logging.info('Running inference and writing output to {}'.format(
        FLAGS.output_tfrecord_path))
    sess.run(tf.local_variables_initializer())
    tf.train.start_queue_runners()
    with tf.python_io.TFRecordWriter(
        FLAGS.output_tfrecord_path) as tf_record_writer:
      try:
        for counter in itertools.count():
          tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 10,
                                 counter)
          tf_example = detection_inference.infer_detections_and_add_to_example(
              serialized_example_tensor, detected_boxes_tensor,
              detected_scores_tensor, detected_labels_tensor,
              FLAGS.discard_image_pixels)
          tf_record_writer.write(tf_example.SerializeToString())
      except tf.errors.OutOfRangeError:
        tf.logging.info('Finished processing records')
示例#12
0
def main(_):
  tf.logging.set_verbosity(tf.logging.INFO)

  required_flags = ['input_tfrecord_paths', 'output_tfrecord_path',
                    'inference_graph']
  for flag_name in required_flags:
    if not getattr(FLAGS, flag_name):
      raise ValueError('Flag --{} is required'.format(flag_name))

  with tf.Session() as sess:
    input_tfrecord_paths = [
        v for v in FLAGS.input_tfrecord_paths.split(',') if v]
    tf.logging.info('Reading input from %d files', len(input_tfrecord_paths))
    serialized_example_tensor, image_tensor = detection_inference.build_input(
        input_tfrecord_paths)
    tf.logging.info('Reading graph and building model...')
    (detected_boxes_tensor, detected_scores_tensor,
     detected_labels_tensor) = detection_inference.build_inference_graph(
         image_tensor, FLAGS.inference_graph)

    tf.logging.info('Running inference and writing output to {}'.format(
        FLAGS.output_tfrecord_path))
    sess.run(tf.local_variables_initializer())
    tf.train.start_queue_runners()
    with tf.python_io.TFRecordWriter(
        FLAGS.output_tfrecord_path) as tf_record_writer:
      try:
        for counter in itertools.count():
          tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 10,
                                 counter)
          tf_example = detection_inference.infer_detections_and_add_to_example(
              serialized_example_tensor, detected_boxes_tensor,
              detected_scores_tensor, detected_labels_tensor,
              FLAGS.discard_image_pixels)
          tf_record_writer.write(tf_example.SerializeToString())
      except tf.errors.OutOfRangeError:
        tf.logging.info('Finished processing records')
def extract_and_write_features(input_tfrecord_paths=[TEST_REC_PATH],
                               frozen_graph_path=PATH_TO_GRAPH,
                               write_dir=OUT_PATH,
                               cropped_features_tensor_names=CROPPED_FEATS_NAMES,
                               features_to_crop_tensor_names=FEATS_TO_CROP_NAMES):

    serialized_example_tensor, image_tensor = detection_inference.build_input(
        input_tfrecord_paths)

    g = load_frozen_graph(frozen_graph_path,
                          input_map={'image_tensor': image_tensor})
    # Get handle of tensors of interest in loaded graph.
    num_detections_tensor = tf.squeeze(
      g.get_tensor_by_name('num_detections:0'), 0)
    num_detections_tensor = tf.cast(num_detections_tensor, tf.int32)

    feats_to_crop_tensors_dict = {feature_tensor_name: g.get_tensor_by_name(feature_tensor_name)
                             for feature_tensor_name in features_to_crop_tensor_names}
    feats_to_crop_tensors_dict.update({'original_image': image_tensor})
    detected_scores_tensor = tf.squeeze(
      g.get_tensor_by_name('detection_scores:0'), 0)
    detected_scores_tensor = detected_scores_tensor[:num_detections_tensor]


    detected_boxes_tensor = tf.squeeze(
      g.get_tensor_by_name('detection_boxes:0'), 0)
    detected_boxes_tensor = detected_boxes_tensor[:num_detections_tensor]

    # Create condition to filter out tensors 
    # corresponding to objectness scores less than  0.50
    mask = detected_scores_tensor > 0.5
    mask.set_shape([None])

    # Apply the condition via boolean masks.
    cropped_feats_tensors_dict = {}
    for feature_tensor_name in cropped_features_tensor_names:
        cropped_feats_tensors_dict[feature_tensor_name] = tf.boolean_mask(tf.squeeze(
            g.get_tensor_by_name(feature_tensor_name), 0)[:num_detections_tensor], mask)

    # Use either ground truth or detected boxes for cropping features.
    # No need to apply boolean mask to ground truth boxes.
    if USE_GT_BOXES:
        boxes_tensor = get_boxes_from_serialized_example_tensor(serialized_example_tensor)
        num_boxes = tf.shape(boxes_tensor)[0]
    else:
        num_boxes = tf.reduce_sum(tf.cast(mask, tf.int32))
        # crop and resize 
        boxes_tensor = tf.boolean_mask(detected_boxes_tensor, mask)
    
    # Crop feature tensors using boxes.
    crop_tensors = {}
    for feature_to_crop_tensor_name, feature_to_crop_tensor in feats_to_crop_tensors_dict.items():
        crop_tensors[feature_to_crop_tensor_name] = tf.image.crop_and_resize(
                                     feature_to_crop_tensor,
                                     boxes_tensor,
                                     tf.zeros(num_boxes, dtype=tf.int32), 
                                     (16, 16)) 

    if AVG_POOL:
        global_pool_func = np.mean
    else:
        global_pool_func = np.max

    session_config = tf.ConfigProto(allow_soft_placement=True,
                                    log_device_placement=False)
    session_config.gpu_options.allow_growth = True

    with tf.Session(config=session_config) as sess:
        sess.run(tf.local_variables_initializer())
        tf.train.start_queue_runners()
        try:
            for counter in itertools.count():
                tf.logging.log_every_n(tf.logging.INFO,
                                       'Processed %d images...', 10,
                                       counter)
                (serialized_example, 
                n, boxes, *feat_list) = tf.get_default_session().run(
                                          [serialized_example_tensor,
                                           num_boxes, boxes_tensor] +
                                           list(cropped_feats_tensors_dict.values())+
                                           list(crop_tensors.values()))
                
                filename = get_file_name_from_serialized_example(serialized_example)
                labels = get_labels_from_serialized_example(serialized_example)
                per_image_w_dir = os.path.join(write_dir, filename[:-len('.png')])
                feat_dict = dict(zip(list(cropped_feats_tensors_dict.keys())+
                                     list(crop_tensors.keys()),
                                     feat_list))
                if n > 0:
                    _save_as_numpy(feat_dict, global_pool_func, per_image_w_dir, boxes=boxes, labels=labels)
                    #_save_numpy_crops_as_image(feat_dict['original_image'], os.path.join(per_image_w_dir, 'crops'))
                
        except tf.errors.OutOfRangeError:
            tf.logging.info('Finished processing records')