def accuracy_function(self, inputs, logits): """Returns the ops to measure the mean precision of the model.""" try: from cnn_quantization.tf_cnn_benchmarks import ssd_dataloader # pylint: disable=g-import-not-at-top from tensorflow_models.object_detection.box_coders import faster_rcnn_box_coder # pylint: disable=g-import-not-at-top from tensorflow_models.object_detection.core import box_coder # pylint: disable=g-import-not-at-top from tensorflow_models.object_detection.core import box_list # pylint: disable=g-import-not-at-top except ImportError: raise ImportError( 'To use the COCO dataset, you must clone the ' 'repo https://github.com/tensorflow/models and add ' 'tensorflow/models and tensorflow/models/research to ' 'the PYTHONPATH, and compile the protobufs; ' 'To evaluate using COCO' 'metric, download and install Python COCO API from' 'https://github.com/cocodataset/cocoapi') # Unpack model output back to locations and confidence scores of predictions # pred_locs: relative locations (coordiates) of objects in all SSD boxes # shape: [batch_size, NUM_SSD_BOXES, 4] # pred_labels: confidence scores of objects being of all categories # shape: [batch_size, NUM_SSD_BOXES, label_num] pred_locs, pred_labels = tf.split(logits, [4, self.label_num], 2) ssd_box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( scale_factors=ssd_constants.BOX_CODER_SCALES) anchors = box_list.BoxList( tf.convert_to_tensor(ssd_dataloader.DefaultBoxes()('ltrb'))) pred_boxes = box_coder.batch_decode(encoded_boxes=pred_locs, box_coder=ssd_box_coder, anchors=anchors) pred_scores = tf.nn.softmax(pred_labels, axis=2) # TODO(haoyuzhang): maybe use `gt_boxes` and `gt_classes` for visualization. _, gt_boxes, gt_classes, source_id, raw_shape = inputs # pylint: disable=unused-variable return { (constants.UNREDUCED_ACCURACY_OP_PREFIX + ssd_constants.PRED_BOXES): pred_boxes, (constants.UNREDUCED_ACCURACY_OP_PREFIX + ssd_constants.PRED_SCORES): pred_scores, # TODO(haoyuzhang): maybe use these values for visualization. # constants.UNREDUCED_ACCURACY_OP_PREFIX+'gt_boxes': gt_boxes, # constants.UNREDUCED_ACCURACY_OP_PREFIX+'gt_classes': gt_classes, (constants.UNREDUCED_ACCURACY_OP_PREFIX + ssd_constants.SOURCE_ID): source_id, (constants.UNREDUCED_ACCURACY_OP_PREFIX + ssd_constants.RAW_SHAPE): raw_shape }
def __init__(self): similarity_calc = region_similarity_calculator.IouSimilarity() matcher = argmax_matcher.ArgMaxMatcher( matched_threshold=ssd_constants.MATCH_THRESHOLD, unmatched_threshold=ssd_constants.MATCH_THRESHOLD, negatives_lower_than_unmatched=True, force_match_for_each_row=True) box_coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( scale_factors=ssd_constants.BOX_CODER_SCALES) self.default_boxes = DefaultBoxes()('ltrb') self.default_boxes = box_list.BoxList( tf.convert_to_tensor(self.default_boxes)) self.assigner = target_assigner.TargetAssigner( similarity_calc, matcher, box_coder)