Unverified Commit fe748d4a authored by pkulzc's avatar pkulzc Committed by GitHub
Browse files

Object detection changes: (#7208)

257914648  by lzc:

    Internal changes

--
257525973  by Zhichao Lu:

    Fixes bug that silently prevents checkpoints from loading when training w/ eager + functions. Also sets up scripts to run training.

--
257296614  by Zhichao Lu:

    Adding detection_features to model outputs

--
257234565  by Zhichao Lu:

    Fix wrong order of `classes_with_max_scores` in class-agnostic NMS caused by
    sorting in partitioned-NMS.

--
257232002  by ronnyvotel:

    Supporting `filter_nonoverlapping` option in np_box_list_ops.clip_to_window().

--
257198282  by Zhichao Lu:

    Adding the focal loss and l1 loss from the Objects as Points paper.

--
257089535  by Zhichao Lu:

    Create Keras based ssd + resnetv1 + fpn.

--
257087407  by Zhichao Lu:

    Make object_detection/data_decoders Python3-compatible.

--
257004582  by Zhichao Lu:

    Updates _decode_raw_data_into_masks_and_boxes to the latest binary masks-to-string encoding format.

--
257002124  by Zhichao Lu:

    Make object_detection/utils Python3-compatible, except json_utils.

    The patching trick used in json_utils is not going to work in Python 3.

--
256795056  by lzc:

    Add a detection_anchor_indices field to detection outputs.

--
256477542  by Zhichao Lu:

    Make object_detection/core Python3-compatible.

--
256387593  by Zhichao Lu:

    Edit class_id_function_approximations builder to skip class ids not present in label map.

--
256259039  by Zhichao Lu:

    Move NMS to TPU for FasterRCNN.

--
256071360  by rathodv:

    When multiclass_scores is empty, add one-hot encoding of groundtruth_classes as multiclass scores so that data_augmentation ops that expect the presence of multiclass_scores don't have to individually handle this case.

    Also copy input tensor_dict to out_tensor_dict first to avoid inplace modification.

--
256023645  by Zhichao Lu:

    Adds the first WIP iterations of TensorFlow v2 eager + functions style custom training & evaluation loops.

--
255980623  by Zhichao Lu:

    Adds a new data augmentation operation "remap_labels" which remaps a set of labels to a new label.

--
255753259  by Zhichao Lu:

    Announcement of the released evaluation tutorial for Open Images Challenge
    2019.

--
255698776  by lzc:

    Fix rewrite_nn_resize_op function which was broken by tf forward compatibility movement.

--
255623150  by Zhichao Lu:

    Add Keras-based ResnetV1 models.

--
255504992  by Zhichao Lu:

    Fixing the typo in specifying label expansion for ground truth segmentation
    file.

--
255470768  by Zhichao Lu:

    1. Fixing Python bug with parsed arguments.
    2. Adding capability to parse relevant columns from CSV header.
    3. Fixing bug with duplicated labels expansion.

--
255462432  by Zhichao Lu:

    Adds a new data augmentation operation "drop_label_probabilistically" which drops a given label with the given probability. This supports experiments on training in the presence of label noise.

--
255441632  by rathodv:

    Fallback on groundtruth classes when multiclass_scores tensor is empty.

--
255434899  by Zhichao Lu:

    Ensuring evaluation binary can run even with big files by synchronizing
    processing of ground truth and predictions: in this way, ground truth is not stored but immediatly
    used for evaluation. In case gt of object masks, this allows to run
    evaluations on relatively large sets.

--
255337855  by lzc:

    Internal change.

--
255308908  by Zhichao Lu:

    Add comment to clarify usage of calibration parameters proto.

--
255266371  by Zhichao Lu:

    Ensuring correct processing of the case, when no groundtruth masks are provided
    for an image.

--
255236648  by Zhichao Lu:

    Refactor model_builder in faster_rcnn.py to a util_map, so that it's possible to be overwritten.

--
255093285  by Zhichao Lu:

    Updating capability to subsample data during evaluation

--
255081222  by rathodv:

    Convert groundtruth masks to be of type float32 before its used in the loss function.

    When using mixed precision training, masks are represented using bfloat16 tensors in the input pipeline for performance reasons. We need to convert them to float32 before using it in the loss function.

--
254788436  by Zhichao Lu:

    Add forward_compatible to non_max_suppression_with_scores to make it is
    compatible with older tensorflow version.

--
254442362  by Zhichao Lu:

    Add num_layer field to ssd feature extractor proto.

--
253911582  by jonathanhuang:

    Plumbs Soft-NMS options (using the new tf.image.non_max_suppression_with_scores op) into the TF Object Detection API.  It adds a `soft_nms_sigma` field to the postprocessing proto file and plumbs this through to both the multiclass and class_agnostic versions of NMS. Note that there is no effect on behavior of NMS when soft_nms_sigma=0 (which it is set to by default).

    See also "Soft-NMS -- Improving Object Detection With One Line of Code" by Bodla et al (https://arxiv.org/abs/1704.04503)

--
253703949  by Zhichao Lu:

    Internal test fixes.

--
253151266  by Zhichao Lu:

    Fix the op type check for FusedBatchNorm, given that we introduced
    FusedBatchNormV3 in a previous change.

--
252718956  by Zhichao Lu:

    Customize activation function to enable relu6 instead of relu for saliency
    prediction model seastarization

--
252158593  by Zhichao Lu:

    Make object_detection/core Python3-compatible.

--
252150717  by Zhichao Lu:

    Make object_detection/core Python3-compatible.

--
251967048  by Zhichao Lu:

    Make GraphRewriter proto extensible.

--
251950039  by Zhichao Lu:

    Remove experimental_export_device_assignment from TPUEstimator.export_savedmodel(), so as to remove rewrite_for_inference().

    As a replacement, export_savedmodel() V2 API supports device_assignment where user call tpu.rewrite in model_fn and pass in device_assigment there.

--
251890697  by rathodv:

    Updated docstring to include new output nodes.

--
251662894  by Zhichao Lu:

    Add autoaugment augmentation option to objection detection api codebase. This
    is an available option in preprocessor.py.

    The intended usage of autoaugment is to be done along with random flipping and
    cropping for best results.

--
251532908  by Zhichao Lu:

    Add TrainingDataType enum to track whether class-specific or agnostic data was used to fit the calibration function.

    This is useful, since classes with few observations may require a calibration function fit on all classes.

--
251511339  by Zhichao Lu:

    Add multiclass isotonic regression to the calibration builder.

--
251317769  by pengchong:

    Internal Change.

--
250729989  by Zhichao Lu:

    Fixing bug in gt statistics count in case of mask and box annotations.

--
250729627  by Zhichao Lu:

    Label expansion for segmentation.

--
250724905  by Zhichao Lu:

    Fix use_depthwise in fpn and test it with fpnlite on ssd + mobilenet v2.

--
250670379  by Zhichao Lu:

    Internal change

250630364  by lzc:

    Fix detection_model_zoo footnotes

--
250560654  by Zhichao Lu:

    Fix static shape issue in matmul_crop_and_resize.

--
250534857  by Zhichao Lu:

    Edit class agnostic calibration function docstring to more accurately describe the function's outputs.

--
250533277  by Zhichao Lu:

    Edit the multiclass messages to use class ids instead of labels.

--

PiperOrigin-RevId: 257914648
parent 81123ebf
...@@ -14,7 +14,13 @@ ...@@ -14,7 +14,13 @@
# ============================================================================== # ==============================================================================
"""Contains functions which are convenient for unit testing.""" """Contains functions which are convenient for unit testing."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np import numpy as np
from six.moves import range
from six.moves import zip
import tensorflow as tf import tensorflow as tf
from object_detection.core import anchor_generator from object_detection.core import anchor_generator
......
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
"""Tests for object_detection.utils.test_utils.""" """Tests for object_detection.utils.test_utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
......
...@@ -15,6 +15,11 @@ ...@@ -15,6 +15,11 @@
"""Helper functions for manipulating collections of variables during training. """Helper functions for manipulating collections of variables during training.
""" """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging import logging
import re import re
...@@ -44,7 +49,7 @@ def filter_variables(variables, filter_regex_list, invert=False): ...@@ -44,7 +49,7 @@ def filter_variables(variables, filter_regex_list, invert=False):
a list of filtered variables. a list of filtered variables.
""" """
kept_vars = [] kept_vars = []
variables_to_ignore_patterns = list(filter(None, filter_regex_list)) variables_to_ignore_patterns = list([fre for fre in filter_regex_list if fre])
for var in variables: for var in variables:
add = True add = True
for pattern in variables_to_ignore_patterns: for pattern in variables_to_ignore_patterns:
...@@ -151,5 +156,24 @@ def get_variables_available_in_checkpoint(variables, ...@@ -151,5 +156,24 @@ def get_variables_available_in_checkpoint(variables,
logging.warning('Variable [%s] is not available in checkpoint', logging.warning('Variable [%s] is not available in checkpoint',
variable_name) variable_name)
if isinstance(variables, list): if isinstance(variables, list):
return vars_in_ckpt.values() return list(vars_in_ckpt.values())
return vars_in_ckpt return vars_in_ckpt
def get_global_variables_safely():
"""If not executing eagerly, returns tf.global_variables().
Raises a ValueError if eager execution is enabled,
because the variables are not tracked when executing eagerly.
If executing eagerly, use a Keras model's .variables property instead.
Returns:
The result of tf.global_variables()
"""
with tf.init_scope():
if tf.executing_eagerly():
raise ValueError("Global variables collection is not tracked when "
"executing eagerly. Use a Keras model's `.variables` "
"attribute instead.")
return tf.global_variables()
...@@ -14,6 +14,11 @@ ...@@ -14,6 +14,11 @@
# ============================================================================== # ==============================================================================
"""Tests for object_detection.utils.variables_helper.""" """Tests for object_detection.utils.variables_helper."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os import os
import tensorflow as tf import tensorflow as tf
...@@ -204,7 +209,7 @@ class GetVariablesAvailableInCheckpointTest(tf.test.TestCase): ...@@ -204,7 +209,7 @@ class GetVariablesAvailableInCheckpointTest(tf.test.TestCase):
graph2_variables_dict, checkpoint_path) graph2_variables_dict, checkpoint_path)
self.assertTrue(isinstance(out_variables, dict)) self.assertTrue(isinstance(out_variables, dict))
self.assertItemsEqual(out_variables.keys(), ['ckpt_weights']) self.assertItemsEqual(list(out_variables.keys()), ['ckpt_weights'])
self.assertTrue(out_variables['ckpt_weights'].op.name == 'weights') self.assertTrue(out_variables['ckpt_weights'].op.name == 'weights')
def test_return_variables_with_correct_sizes(self): def test_return_variables_with_correct_sizes(self):
......
...@@ -19,6 +19,10 @@ These functions often receive an image, perform some visualization on the image. ...@@ -19,6 +19,10 @@ These functions often receive an image, perform some visualization on the image.
The functions do not return a value, instead they modify the image itself. The functions do not return a value, instead they modify the image itself.
""" """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import abc import abc
import collections import collections
# Set headless-friendly backend. # Set headless-friendly backend.
...@@ -30,6 +34,8 @@ import PIL.ImageColor as ImageColor ...@@ -30,6 +34,8 @@ import PIL.ImageColor as ImageColor
import PIL.ImageDraw as ImageDraw import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont import PIL.ImageFont as ImageFont
import six import six
from six.moves import range
from six.moves import zip
import tensorflow as tf import tensorflow as tf
from object_detection.core import standard_fields as fields from object_detection.core import standard_fields as fields
...@@ -771,7 +777,7 @@ def visualize_boxes_and_labels_on_image_array( ...@@ -771,7 +777,7 @@ def visualize_boxes_and_labels_on_image_array(
display_str = '' display_str = ''
if not skip_labels: if not skip_labels:
if not agnostic_mode: if not agnostic_mode:
if classes[i] in category_index.keys(): if classes[i] in six.viewkeys(category_index):
class_name = category_index[classes[i]]['name'] class_name = category_index[classes[i]]['name']
else: else:
class_name = 'N/A' class_name = 'N/A'
...@@ -894,7 +900,7 @@ def add_hist_image_summary(values, bins, name): ...@@ -894,7 +900,7 @@ def add_hist_image_summary(values, bins, name):
tf.summary.image(name, hist_plot) tf.summary.image(name, hist_plot)
class EvalMetricOpsVisualization(object): class EvalMetricOpsVisualization(six.with_metaclass(abc.ABCMeta, object)):
"""Abstract base class responsible for visualizations during evaluation. """Abstract base class responsible for visualizations during evaluation.
Currently, summary images are not run during evaluation. One way to produce Currently, summary images are not run during evaluation. One way to produce
...@@ -903,7 +909,6 @@ class EvalMetricOpsVisualization(object): ...@@ -903,7 +909,6 @@ class EvalMetricOpsVisualization(object):
responsible for accruing images (with overlaid detections and groundtruth) responsible for accruing images (with overlaid detections and groundtruth)
and returning a dictionary that can be passed to `eval_metric_ops`. and returning a dictionary that can be passed to `eval_metric_ops`.
""" """
__metaclass__ = abc.ABCMeta
def __init__(self, def __init__(self,
category_index, category_index,
......
...@@ -14,11 +14,17 @@ ...@@ -14,11 +14,17 @@
# ============================================================================== # ==============================================================================
"""Tests for object_detection.utils.visualization_utils.""" """Tests for object_detection.utils.visualization_utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging import logging
import os import os
import numpy as np import numpy as np
import PIL.Image as Image import PIL.Image as Image
import six
from six.moves import range
import tensorflow as tf import tensorflow as tf
from object_detection.core import standard_fields as fields from object_detection.core import standard_fields as fields
...@@ -387,12 +393,12 @@ class VisualizationUtilsTest(tf.test.TestCase): ...@@ -387,12 +393,12 @@ class VisualizationUtilsTest(tf.test.TestCase):
groundtruth_classes groundtruth_classes
} }
metric_ops = eval_metric_ops.get_estimator_eval_metric_ops(eval_dict) metric_ops = eval_metric_ops.get_estimator_eval_metric_ops(eval_dict)
_, update_op = metric_ops[metric_ops.keys()[0]] _, update_op = metric_ops[next(six.iterkeys(metric_ops))]
with self.test_session() as sess: with self.test_session() as sess:
sess.run(tf.global_variables_initializer()) sess.run(tf.global_variables_initializer())
value_ops = {} value_ops = {}
for key, (value_op, _) in metric_ops.iteritems(): for key, (value_op, _) in six.iteritems(metric_ops):
value_ops[key] = value_op value_ops[key] = value_op
# First run enough update steps to surpass `max_examples_to_draw`. # First run enough update steps to surpass `max_examples_to_draw`.
...@@ -413,7 +419,7 @@ class VisualizationUtilsTest(tf.test.TestCase): ...@@ -413,7 +419,7 @@ class VisualizationUtilsTest(tf.test.TestCase):
[6 + i, 7 + i, 3], [6 + i, 7 + i, 3]] [6 + i, 7 + i, 3], [6 + i, 7 + i, 3]]
}) })
value_ops_out = sess.run(value_ops) value_ops_out = sess.run(value_ops)
for key, value_op in value_ops_out.iteritems(): for key, value_op in six.iteritems(value_ops_out):
self.assertNotEqual('', value_op) self.assertNotEqual('', value_op)
# Now run fewer update steps than `max_examples_to_draw`. A single value # Now run fewer update steps than `max_examples_to_draw`. A single value
...@@ -437,7 +443,7 @@ class VisualizationUtilsTest(tf.test.TestCase): ...@@ -437,7 +443,7 @@ class VisualizationUtilsTest(tf.test.TestCase):
}) })
value_ops_out = sess.run(value_ops) value_ops_out = sess.run(value_ops)
self.assertEqual( self.assertEqual(
'', six.b(''),
value_ops_out[metric_op_base + '/' + str(max_examples_to_draw - 1)]) value_ops_out[metric_op_base + '/' + str(max_examples_to_draw - 1)])
......
...@@ -27,10 +27,16 @@ Note1: groundtruth should be inserted before evaluation. ...@@ -27,10 +27,16 @@ Note1: groundtruth should be inserted before evaluation.
Note2: This module operates on numpy boxes and box lists. Note2: This module operates on numpy boxes and box lists.
""" """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from abc import abstractmethod from abc import abstractmethod
import collections import collections
import logging import logging
import numpy as np import numpy as np
import six
from six.moves import range
from object_detection.core import standard_fields from object_detection.core import standard_fields
from object_detection.utils import metrics from object_detection.utils import metrics
...@@ -179,7 +185,7 @@ class VRDDetectionEvaluator(object_detection_evaluation.DetectionEvaluator): ...@@ -179,7 +185,7 @@ class VRDDetectionEvaluator(object_detection_evaluation.DetectionEvaluator):
datatype label_data_type above). datatype label_data_type above).
""" """
if image_id not in self._image_ids: if image_id not in self._image_ids:
logging.warn('No groundtruth for the image with id %s.', image_id) logging.warning('No groundtruth for the image with id %s.', image_id)
# Since for the correct work of evaluator it is assumed that groundtruth # Since for the correct work of evaluator it is assumed that groundtruth
# is inserted first we make sure to break the code if is it not the case. # is inserted first we make sure to break the code if is it not the case.
self._image_ids.update([image_id]) self._image_ids.update([image_id])
...@@ -252,12 +258,12 @@ class VRDDetectionEvaluator(object_detection_evaluation.DetectionEvaluator): ...@@ -252,12 +258,12 @@ class VRDDetectionEvaluator(object_detection_evaluation.DetectionEvaluator):
recall_100, recall_100,
} }
if relationships: if relationships:
for key, average_precision in average_precisions.iteritems(): for key, average_precision in six.iteritems(average_precisions):
vrd_metrics[self._metric_prefix + 'AP@{}IOU/{}'.format( vrd_metrics[self._metric_prefix + 'AP@{}IOU/{}'.format(
self._matching_iou_threshold, self._matching_iou_threshold,
relationships[key])] = average_precision relationships[key])] = average_precision
else: else:
for key, average_precision in average_precisions.iteritems(): for key, average_precision in six.iteritems(average_precisions):
vrd_metrics[self._metric_prefix + 'AP@{}IOU/{}'.format( vrd_metrics[self._metric_prefix + 'AP@{}IOU/{}'.format(
self._matching_iou_threshold, key)] = average_precision self._matching_iou_threshold, key)] = average_precision
...@@ -352,7 +358,7 @@ class VRDPhraseDetectionEvaluator(VRDDetectionEvaluator): ...@@ -352,7 +358,7 @@ class VRDPhraseDetectionEvaluator(VRDDetectionEvaluator):
where the named bounding box is computed as an enclosing bounding box where the named bounding box is computed as an enclosing bounding box
of all bounding boxes of the i-th input structure. of all bounding boxes of the i-th input structure.
""" """
first_box_key = groundtruth_box_tuples.dtype.fields.keys()[0] first_box_key = next(six.iterkeys(groundtruth_box_tuples.dtype.fields))
miny = groundtruth_box_tuples[first_box_key][:, 0] miny = groundtruth_box_tuples[first_box_key][:, 0]
minx = groundtruth_box_tuples[first_box_key][:, 1] minx = groundtruth_box_tuples[first_box_key][:, 1]
maxy = groundtruth_box_tuples[first_box_key][:, 2] maxy = groundtruth_box_tuples[first_box_key][:, 2]
...@@ -388,7 +394,7 @@ class VRDPhraseDetectionEvaluator(VRDDetectionEvaluator): ...@@ -388,7 +394,7 @@ class VRDPhraseDetectionEvaluator(VRDDetectionEvaluator):
where the named bounding box is computed as an enclosing bounding box where the named bounding box is computed as an enclosing bounding box
of all bounding boxes of the i-th input structure. of all bounding boxes of the i-th input structure.
""" """
first_box_key = detections_box_tuples.dtype.fields.keys()[0] first_box_key = next(six.iterkeys(detections_box_tuples.dtype.fields))
miny = detections_box_tuples[first_box_key][:, 0] miny = detections_box_tuples[first_box_key][:, 0]
minx = detections_box_tuples[first_box_key][:, 1] minx = detections_box_tuples[first_box_key][:, 1]
maxy = detections_box_tuples[first_box_key][:, 2] maxy = detections_box_tuples[first_box_key][:, 2]
...@@ -459,7 +465,7 @@ class _VRDDetectionEvaluation(object): ...@@ -459,7 +465,7 @@ class _VRDDetectionEvaluation(object):
possibly additional classes. possibly additional classes.
""" """
if image_key in self._groundtruth_box_tuples: if image_key in self._groundtruth_box_tuples:
logging.warn( logging.warning(
'image %s has already been added to the ground truth database.', 'image %s has already been added to the ground truth database.',
image_key) image_key)
return return
...@@ -536,7 +542,7 @@ class _VRDDetectionEvaluation(object): ...@@ -536,7 +542,7 @@ class _VRDDetectionEvaluation(object):
median_rank@100: median rank computed on 100 top-scoring samples. median_rank@100: median rank computed on 100 top-scoring samples.
""" """
if self._num_gt_instances == 0: if self._num_gt_instances == 0:
logging.warn('No ground truth instances') logging.warning('No ground truth instances')
if not self._scores: if not self._scores:
scores = np.array([], dtype=float) scores = np.array([], dtype=float)
...@@ -546,8 +552,8 @@ class _VRDDetectionEvaluation(object): ...@@ -546,8 +552,8 @@ class _VRDDetectionEvaluation(object):
tp_fp_labels = np.concatenate(self._tp_fp_labels) tp_fp_labels = np.concatenate(self._tp_fp_labels)
relation_field_values = np.concatenate(self._relation_field_values) relation_field_values = np.concatenate(self._relation_field_values)
for relation_field_value, _ in ( for relation_field_value, _ in (six.iteritems(
self._num_gt_instances_per_relationship.iteritems()): self._num_gt_instances_per_relationship)):
precisions, recalls = metrics.compute_precision_recall( precisions, recalls = metrics.compute_precision_recall(
scores[relation_field_values == relation_field_value], scores[relation_field_values == relation_field_value],
tp_fp_labels[relation_field_values == relation_field_value], tp_fp_labels[relation_field_values == relation_field_value],
...@@ -556,7 +562,8 @@ class _VRDDetectionEvaluation(object): ...@@ -556,7 +562,8 @@ class _VRDDetectionEvaluation(object):
relation_field_value] = metrics.compute_average_precision( relation_field_value] = metrics.compute_average_precision(
precisions, recalls) precisions, recalls)
self._mean_average_precision = np.mean(self._average_precisions.values()) self._mean_average_precision = np.mean(
list(self._average_precisions.values()))
self._precisions, self._recalls = metrics.compute_precision_recall( self._precisions, self._recalls = metrics.compute_precision_recall(
scores, tp_fp_labels, self._num_gt_instances) scores, tp_fp_labels, self._num_gt_instances)
......
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
# ============================================================================== # ==============================================================================
"""Tests for tensorflow_models.object_detection.utils.vrd_evaluation.""" """Tests for tensorflow_models.object_detection.utils.vrd_evaluation."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment