Exemplo n.º 1
0
    def build_prediction(self, examples, **kwargs):
        """Builds tf graph for prediction.

    Args:
      examples: dict of input tensors keyed by name.
      prediction_task: the specific prediction task.

    Returns:
      predictions: dict of prediction results keyed by name.
    """
        options = self._model_proto
        is_training = self._is_training

        if is_training or len(options.eval_min_dimension) == 0:
            predictions = self._build_prediction(examples)
            predictions.update(self._postprocess(examples, predictions))
            return predictions

        inputs = examples[InputDataFields.image]
        assert inputs.get_shape()[0].value == 1

        proposal_scores_list = [[] for _ in range(1 + options.oicr_iterations)]

        # Get predictions from different resolutions.

        reuse = False
        for min_dimension in options.eval_min_dimension:
            inputs_resized = tf.expand_dims(
                imgproc.resize_image_to_min_dimension(inputs[0],
                                                      min_dimension)[0],
                axis=0)
            examples[InputDataFields.image] = inputs_resized

            with tf.variable_scope(tf.get_variable_scope(), reuse=reuse):
                predictions = self._build_prediction(examples)

            for i in range(1 + options.oicr_iterations):
                proposals_scores = predictions[
                    Cap2DetPredictions.oicr_proposal_scores +
                    '_at_{}'.format(i)]
                proposal_scores_list[i].append(proposals_scores)

            reuse = True

        # Aggregate predictions from different resolutions.

        predictions_aggregated = predictions
        for i in range(1 + options.oicr_iterations):
            proposal_scores = tf.stack(proposal_scores_list[i], axis=-1)
            proposal_scores = tf.reduce_mean(proposal_scores, axis=-1)
            predictions_aggregated[Cap2DetPredictions.oicr_proposal_scores +
                                   '_at_{}'.format(i)] = proposal_scores

        predictions_aggregated.update(
            self._postprocess(inputs, predictions_aggregated))

        return predictions_aggregated
Exemplo n.º 2
0
    def test_resize_image_to_min_dimension(self):
        tf.reset_default_graph()

        image = tf.placeholder(tf.float32, shape=[None, None, 3])

        resized_image = imgproc.resize_image_to_min_dimension(
            image, min_dimension=900)
        with self.test_session() as sess:
            img, img_shape = sess.run(resized_image,
                                      feed_dict={
                                          image: np.zeros([300, 400, 3]),
                                      })
            self.assertEqual(img.shape, (900, 1200, 3))
            self.assertAllEqual(img_shape, [900, 1200, 3])

            img, img_shape = sess.run(resized_image,
                                      feed_dict={
                                          image: np.zeros([400, 300, 3]),
                                      })
            self.assertEqual(img.shape, (1200, 900, 3))
            self.assertAllEqual(img_shape, [1200, 900, 3])
Exemplo n.º 3
0
 def _keep_aspect_ratio_resize_fn(image):
     return imgproc.resize_image_to_min_dimension(
         image, min_dimension=options.min_dimension)