tensorflow/models

View on GitHub
official/projects/yolo/modeling/layers/detection_generator_test.py

Summary

Maintainability
A
2 hrs
Test Coverage
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for yolo detection generator."""
from absl.testing import parameterized
import tensorflow as tf, tf_keras

from official.projects.yolo.modeling.layers import detection_generator


class YoloDecoderTest(parameterized.TestCase, tf.test.TestCase):

  @parameterized.parameters(
      ('v1', None),
      ('v2', False),
      ('v2', True),
      ('greedy', None),
  )
  def test_network_creation(self, nms_version, use_class_agnostic_nms):
    """Test creation of ResNet family models."""
    tf_keras.backend.set_image_data_format('channels_last')
    input_shape = {
        '3': [1, 52, 52, 255],
        '4': [1, 26, 26, 255],
        '5': [1, 13, 13, 255]
    }
    classes = 80
    anchors = {
        '3': [[12.0, 19.0], [31.0, 46.0], [96.0, 54.0]],
        '4': [[46.0, 114.0], [133.0, 127.0], [79.0, 225.0]],
        '5': [[301.0, 150.0], [172.0, 286.0], [348.0, 340.0]]
    }

    box_type = {key: 'scaled' for key in anchors.keys()}

    layer = detection_generator.YoloLayer(
        anchors,
        classes,
        box_type=box_type,
        max_boxes=10,
        use_class_agnostic_nms=use_class_agnostic_nms,
        nms_version=nms_version,
    )

    inputs = {}
    for key in input_shape:
      inputs[key] = tf.ones(input_shape[key], dtype=tf.float32)

    endpoints = layer(inputs)

    boxes = endpoints['bbox']
    classes = endpoints['classes']

    self.assertAllEqual(boxes.shape.as_list(), [1, 10, 4])
    self.assertAllEqual(classes.shape.as_list(), [1, 10])


if __name__ == '__main__':
  tf.test.main()