|
| 1 | +# Copyright 2024 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest import mock |
| 17 | +import numpy as np |
| 18 | +from official.projects.waste_identification_ml.docker_solution.prediction_pipeline import prediction_postprocessing |
| 19 | + |
| 20 | + |
| 21 | +class PostprocessingTest(unittest.TestCase): |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + super().setUp() |
| 25 | + self.results1 = { |
| 26 | + 'detection_boxes': [np.array([[0, 0, 100, 100], [100, 100, 200, 200]])], |
| 27 | + 'detection_masks': [ |
| 28 | + np.zeros((1, 512, 1024), dtype=np.uint8), |
| 29 | + np.ones((1, 512, 1024), dtype=np.uint8), |
| 30 | + ], |
| 31 | + 'detection_scores': [[0.9, 0.8]], |
| 32 | + 'detection_classes': [1, 2], |
| 33 | + 'detection_classes_names': ['class_1', 'class_2'], |
| 34 | + } |
| 35 | + |
| 36 | + self.results2 = { |
| 37 | + 'detection_boxes': [ |
| 38 | + np.array([[50, 50, 150, 150], [150, 150, 250, 250]]) |
| 39 | + ], |
| 40 | + 'detection_masks': [ |
| 41 | + np.full((1, 512, 1024), 0.5, dtype=np.uint8), |
| 42 | + np.full((1, 512, 1024), 0.5, dtype=np.uint8), |
| 43 | + ], |
| 44 | + 'detection_scores': [[0.9, 0.8]], |
| 45 | + 'detection_classes': [2, 1], |
| 46 | + 'detection_classes_names': ['class_2', 'class_1'], |
| 47 | + } |
| 48 | + |
| 49 | + self.category_indices = [[1, 2], [2, 1]] |
| 50 | + |
| 51 | + self.category_index = { |
| 52 | + 1: {'id': 1, 'name': 'class_1'}, |
| 53 | + 2: {'id': 2, 'name': 'class_2'}, |
| 54 | + } |
| 55 | + self.height = 512 |
| 56 | + self.width = 1024 |
| 57 | + |
| 58 | + def test_merge_predictions(self): |
| 59 | + results = prediction_postprocessing.merge_predictions( |
| 60 | + [self.results1, self.results2], |
| 61 | + 0.8, |
| 62 | + self.category_indices, |
| 63 | + self.category_index, |
| 64 | + 4, |
| 65 | + ) |
| 66 | + |
| 67 | + self.assertEqual(results['num_detections'], 4) |
| 68 | + self.assertEqual(results['detection_scores'].shape, (4,)) |
| 69 | + self.assertEqual(results['detection_boxes'].shape, (4, 4)) |
| 70 | + self.assertEqual(results['detection_classes'].shape, (4,)) |
| 71 | + self.assertEqual( |
| 72 | + results['detection_classes_names'], |
| 73 | + ['class_1', 'class_2', 'class_1', 'class_2'], |
| 74 | + ) |
| 75 | + self.assertEqual(results['detection_masks_reframed'].shape, (4, 512, 1024)) |
| 76 | + |
| 77 | + @mock.patch('postprocessing.find_similar_masks') |
| 78 | + def test_merge_predictions_calls_find_similar_masks( |
| 79 | + self, mock_find_similar_masks |
| 80 | + ): |
| 81 | + prediction_postprocessing.merge_predictions( |
| 82 | + [self.results1, self.results2], |
| 83 | + 0.8, |
| 84 | + self.category_indices, |
| 85 | + self.category_index, |
| 86 | + 4, |
| 87 | + ) |
| 88 | + |
| 89 | + mock_find_similar_masks.assert_called_once_with( |
| 90 | + self.results1, |
| 91 | + self.results2, |
| 92 | + 4, |
| 93 | + 0.8, |
| 94 | + self.category_indices, |
| 95 | + self.category_index, |
| 96 | + 0.3 * 512 * 1024, |
| 97 | + ) |
| 98 | + |
| 99 | + def test_merge_predictions_with_empty_results(self): |
| 100 | + results = prediction_postprocessing.merge_predictions( |
| 101 | + [{}, {}], |
| 102 | + 0.8, |
| 103 | + self.category_indices, |
| 104 | + self.category_index, |
| 105 | + 4, |
| 106 | + ) |
| 107 | + |
| 108 | + self.assertEqual(results['num_detections'], 0) |
| 109 | + self.assertEqual(results['detection_scores'].shape, (0,)) |
| 110 | + self.assertEqual(results['detection_boxes'].shape, (0, 4)) |
| 111 | + self.assertEqual(results['detection_classes'].shape, (0,)) |
| 112 | + self.assertEqual(results['detection_classes_names'], []) |
| 113 | + self.assertEqual(results['detection_masks_reframed'].shape, (0, 512, 1024)) |
| 114 | + |
| 115 | + def test_merge_predictions_with_invalid_category_indices(self): |
| 116 | + category_indices = [[1, 3], [2, 4]] |
| 117 | + |
| 118 | + with self.assertRaises(ValueError): |
| 119 | + prediction_postprocessing.merge_predictions( |
| 120 | + [self.results1, self.results2], |
| 121 | + 0.8, |
| 122 | + category_indices, |
| 123 | + self.category_index, |
| 124 | + 4, |
| 125 | + ) |
| 126 | + |
| 127 | + def test_transform_bounding_boxes(self): |
| 128 | + results = { |
| 129 | + 'detection_boxes': np.array([[ |
| 130 | + [0.1, 0.2, 0.4, 0.5], # Normalized coordinates |
| 131 | + [0.3, 0.3, 0.6, 0.7], |
| 132 | + ]]) |
| 133 | + } |
| 134 | + |
| 135 | + # Expected output for the adjusted height and width |
| 136 | + expected_transformed_boxes = [ |
| 137 | + [ |
| 138 | + int(0.1 * self.height), |
| 139 | + int(0.2 * self.width), |
| 140 | + int(0.4 * self.height), |
| 141 | + int(0.5 * self.width), |
| 142 | + ], |
| 143 | + [ |
| 144 | + int(0.3 * self.height), |
| 145 | + int(0.3 * self.width), |
| 146 | + int(0.6 * self.height), |
| 147 | + int(0.7 * self.width), |
| 148 | + ], |
| 149 | + ] |
| 150 | + |
| 151 | + transformed_boxes = prediction_postprocessing._transform_bounding_boxes( |
| 152 | + results |
| 153 | + ) |
| 154 | + |
| 155 | + self.assertEqual(transformed_boxes, expected_transformed_boxes) |
0 commit comments