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 fo...
parent 81123ebf
......@@ -14,7 +14,13 @@
# ==============================================================================
"""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
from six.moves import range
from six.moves import zip
import tensorflow as tf
from object_detection.core import anchor_generator
......
......@@ -15,6 +15,10 @@
"""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 tensorflow as tf
......
......@@ -15,6 +15,11 @@
"""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 re
......@@ -44,7 +49,7 @@ def filter_variables(variables, filter_regex_list, invert=False):
a list of filtered variables.
"""
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:
add = True
for pattern in variables_to_ignore_patterns:
......@@ -151,5 +156,24 @@ def get_variables_available_in_checkpoint(variables,
logging.warning('Variable [%s] is not available in checkpoint',
variable_name)
if isinstance(variables, list):
return vars_in_ckpt.values()
return list(vars_in_ckpt.values())
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 @@
# ==============================================================================
"""Tests for object_detection.utils.variables_helper."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import tensorflow as tf
......@@ -204,7 +209,7 @@ class GetVariablesAvailableInCheckpointTest(tf.test.TestCase):
graph2_variables_dict, checkpoint_path)
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')
def test_return_variables_with_correct_sizes(self):
......
......@@ -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.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import abc
import collections
# Set headless-friendly backend.
......@@ -30,6 +34,8 @@ import PIL.ImageColor as ImageColor
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
import six
from six.moves import range
from six.moves import zip
import tensorflow as tf
from object_detection.core import standard_fields as fields
......@@ -771,7 +777,7 @@ def visualize_boxes_and_labels_on_image_array(
display_str = ''
if not skip_labels:
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']
else:
class_name = 'N/A'
......@@ -894,7 +900,7 @@ def add_hist_image_summary(values, bins, name):
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.
Currently, summary images are not run during evaluation. One way to produce
......@@ -903,7 +909,6 @@ class EvalMetricOpsVisualization(object):
responsible for accruing images (with overlaid detections and groundtruth)
and returning a dictionary that can be passed to `eval_metric_ops`.
"""
__metaclass__ = abc.ABCMeta
def __init__(self,
category_index,
......
......@@ -14,11 +14,17 @@
# ==============================================================================
"""Tests for object_detection.utils.visualization_utils."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import os
import numpy as np
import PIL.Image as Image
import six
from six.moves import range
import tensorflow as tf
from object_detection.core import standard_fields as fields
......@@ -387,12 +393,12 @@ class VisualizationUtilsTest(tf.test.TestCase):
groundtruth_classes
}
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:
sess.run(tf.global_variables_initializer())
value_ops = {}
for key, (value_op, _) in metric_ops.iteritems():
for key, (value_op, _) in six.iteritems(metric_ops):
value_ops[key] = value_op
# First run enough update steps to surpass `max_examples_to_draw`.
......@@ -413,7 +419,7 @@ class VisualizationUtilsTest(tf.test.TestCase):
[6 + i, 7 + i, 3], [6 + i, 7 + i, 3]]
})
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)
# Now run fewer update steps than `max_examples_to_draw`. A single value
......@@ -437,7 +443,7 @@ class VisualizationUtilsTest(tf.test.TestCase):
})
value_ops_out = sess.run(value_ops)
self.assertEqual(
'',
six.b(''),
value_ops_out[metric_op_base + '/' + str(max_examples_to_draw - 1)])
......
......@@ -27,10 +27,16 @@ Note1: groundtruth should be inserted before evaluation.
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
import collections
import logging
import numpy as np
import six
from six.moves import range
from object_detection.core import standard_fields
from object_detection.utils import metrics
......@@ -179,7 +185,7 @@ class VRDDetectionEvaluator(object_detection_evaluation.DetectionEvaluator):
datatype label_data_type above).
"""
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
# is inserted first we make sure to break the code if is it not the case.
self._image_ids.update([image_id])
......@@ -252,12 +258,12 @@ class VRDDetectionEvaluator(object_detection_evaluation.DetectionEvaluator):
recall_100,
}
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(
self._matching_iou_threshold,
relationships[key])] = average_precision
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(
self._matching_iou_threshold, key)] = average_precision
......@@ -352,7 +358,7 @@ class VRDPhraseDetectionEvaluator(VRDDetectionEvaluator):
where the named bounding box is computed as an enclosing bounding box
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]
minx = groundtruth_box_tuples[first_box_key][:, 1]
maxy = groundtruth_box_tuples[first_box_key][:, 2]
......@@ -388,7 +394,7 @@ class VRDPhraseDetectionEvaluator(VRDDetectionEvaluator):
where the named bounding box is computed as an enclosing bounding box
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]
minx = detections_box_tuples[first_box_key][:, 1]
maxy = detections_box_tuples[first_box_key][:, 2]
......@@ -459,7 +465,7 @@ class _VRDDetectionEvaluation(object):
possibly additional classes.
"""
if image_key in self._groundtruth_box_tuples:
logging.warn(
logging.warning(
'image %s has already been added to the ground truth database.',
image_key)
return
......@@ -536,7 +542,7 @@ class _VRDDetectionEvaluation(object):
median_rank@100: median rank computed on 100 top-scoring samples.
"""
if self._num_gt_instances == 0:
logging.warn('No ground truth instances')
logging.warning('No ground truth instances')
if not self._scores:
scores = np.array([], dtype=float)
......@@ -546,8 +552,8 @@ class _VRDDetectionEvaluation(object):
tp_fp_labels = np.concatenate(self._tp_fp_labels)
relation_field_values = np.concatenate(self._relation_field_values)
for relation_field_value, _ in (
self._num_gt_instances_per_relationship.iteritems()):
for relation_field_value, _ in (six.iteritems(
self._num_gt_instances_per_relationship)):
precisions, recalls = metrics.compute_precision_recall(
scores[relation_field_values == relation_field_value],
tp_fp_labels[relation_field_values == relation_field_value],
......@@ -556,7 +562,8 @@ class _VRDDetectionEvaluation(object):
relation_field_value] = metrics.compute_average_precision(
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(
scores, tp_fp_labels, self._num_gt_instances)
......
......@@ -14,6 +14,10 @@
# ==============================================================================
"""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 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