|
| 1 | +# Copyright (c) 2026 PaddlePaddle 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 logging |
| 16 | + |
| 17 | +import cv2 |
| 18 | +import numpy as np |
| 19 | +import pytest |
| 20 | + |
| 21 | +from ppocr.data.multi_scale_sampler import MultiScaleSampler |
| 22 | +from ppocr.data.simple_dataset import MultiScaleDataSet |
| 23 | + |
| 24 | + |
| 25 | +def _create_dataset_file(tmp_path, prefix, num_samples, width, height): |
| 26 | + label_file = tmp_path / f"{prefix}.txt" |
| 27 | + |
| 28 | + with open(label_file, "w", encoding="utf-8") as f: |
| 29 | + for idx in range(num_samples): |
| 30 | + image_name = f"{prefix}_{idx}.jpg" |
| 31 | + image_path = tmp_path / image_name |
| 32 | + |
| 33 | + image = np.zeros((height, width, 3), dtype=np.uint8) |
| 34 | + cv2.imwrite(str(image_path), image) |
| 35 | + |
| 36 | + f.write(f"{image_name}\t{prefix}_{idx}\t{width}\t{height}\n") |
| 37 | + |
| 38 | + return str(label_file) |
| 39 | + |
| 40 | + |
| 41 | +def _build_config( |
| 42 | + tmp_path, |
| 43 | + label_file_list, |
| 44 | + ratio_list, |
| 45 | + ds_width, |
| 46 | + transforms, |
| 47 | + shuffle, |
| 48 | +): |
| 49 | + return { |
| 50 | + "Global": {}, |
| 51 | + "Train": { |
| 52 | + "dataset": { |
| 53 | + "name": "MultiScaleDataSet", |
| 54 | + "data_dir": str(tmp_path), |
| 55 | + "label_file_list": label_file_list, |
| 56 | + "ratio_list": ratio_list, |
| 57 | + "ds_width": ds_width, |
| 58 | + "transforms": transforms, |
| 59 | + }, |
| 60 | + "loader": { |
| 61 | + "shuffle": shuffle, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("ds_width", [False, True]) |
| 68 | +def test_multiscale_dataset_respects_ratio_sampling(tmp_path, ds_width): |
| 69 | + # Dataset A has a smaller aspect ratio than dataset B. When ds_width |
| 70 | + # is enabled, sorting the full dataset instead of the sampled subset |
| 71 | + # therefore exposes the indexing bug deterministically. |
| 72 | + a_file = _create_dataset_file( |
| 73 | + tmp_path, |
| 74 | + prefix="A", |
| 75 | + num_samples=2, |
| 76 | + width=10, |
| 77 | + height=10, |
| 78 | + ) |
| 79 | + b_file = _create_dataset_file( |
| 80 | + tmp_path, |
| 81 | + prefix="B", |
| 82 | + num_samples=2, |
| 83 | + width=20, |
| 84 | + height=10, |
| 85 | + ) |
| 86 | + |
| 87 | + config = _build_config( |
| 88 | + tmp_path=tmp_path, |
| 89 | + label_file_list=[a_file, b_file], |
| 90 | + ratio_list=[0.5, 0.5], |
| 91 | + ds_width=ds_width, |
| 92 | + transforms=[ |
| 93 | + { |
| 94 | + "DecodeImage": { |
| 95 | + "img_mode": "BGR", |
| 96 | + "channel_first": False, |
| 97 | + } |
| 98 | + }, |
| 99 | + { |
| 100 | + "KeepKeys": { |
| 101 | + "keep_keys": [ |
| 102 | + "image", |
| 103 | + "label", |
| 104 | + ] |
| 105 | + } |
| 106 | + }, |
| 107 | + ], |
| 108 | + shuffle=True, |
| 109 | + ) |
| 110 | + |
| 111 | + dataset = MultiScaleDataSet( |
| 112 | + config=config, |
| 113 | + mode="Train", |
| 114 | + logger=logging.getLogger(__name__), |
| 115 | + seed=0, |
| 116 | + ) |
| 117 | + |
| 118 | + assert len(dataset) == 2 |
| 119 | + |
| 120 | + # ratio_list should select one sample from each label file. |
| 121 | + sampled_indices = dataset._index_map |
| 122 | + assert sum(index < 2 for index in sampled_indices) == 1 |
| 123 | + assert sum(index >= 2 for index in sampled_indices) == 1 |
| 124 | + |
| 125 | + samples = [dataset.__getitem__([320, 48, idx, 1.0]) for idx in range(len(dataset))] |
| 126 | + labels = [sample[1] for sample in samples] |
| 127 | + |
| 128 | + assert sum(label.startswith("A_") for label in labels) == 1 |
| 129 | + assert sum(label.startswith("B_") for label in labels) == 1 |
| 130 | + |
| 131 | + |
| 132 | +def test_multiscale_sampler_refreshes_ratio_after_dataset_reset(tmp_path): |
| 133 | + label_file = tmp_path / "data.txt" |
| 134 | + |
| 135 | + sizes = [ |
| 136 | + (10, 10), |
| 137 | + (20, 10), |
| 138 | + (30, 10), |
| 139 | + (40, 10), |
| 140 | + ] |
| 141 | + |
| 142 | + with open(label_file, "w", encoding="utf-8") as f: |
| 143 | + for idx, (width, height) in enumerate(sizes): |
| 144 | + f.write(f"image_{idx}.jpg\tlabel_{idx}\t{width}\t{height}\n") |
| 145 | + |
| 146 | + config = _build_config( |
| 147 | + tmp_path=tmp_path, |
| 148 | + label_file_list=[str(label_file)], |
| 149 | + ratio_list=[0.5], |
| 150 | + ds_width=True, |
| 151 | + transforms=[], |
| 152 | + shuffle=False, |
| 153 | + ) |
| 154 | + |
| 155 | + dataset = MultiScaleDataSet( |
| 156 | + config=config, |
| 157 | + mode="Train", |
| 158 | + logger=logging.getLogger(__name__), |
| 159 | + seed=0, |
| 160 | + ) |
| 161 | + |
| 162 | + sampler = MultiScaleSampler( |
| 163 | + data_source=dataset, |
| 164 | + scales=[[320, 48]], |
| 165 | + first_bs=2, |
| 166 | + fix_bs=True, |
| 167 | + is_training=True, |
| 168 | + seed=0, |
| 169 | + ) |
| 170 | + |
| 171 | + first_expected_ratio = np.mean(dataset.wh_ratio[dataset.wh_ratio_sort]) |
| 172 | + |
| 173 | + first_epoch_batch = list(iter(sampler))[0] |
| 174 | + first_epoch_ratio = first_epoch_batch[0][3] |
| 175 | + |
| 176 | + assert np.isclose( |
| 177 | + first_epoch_ratio, |
| 178 | + first_expected_ratio, |
| 179 | + ) |
| 180 | + |
| 181 | + # Find another sampling seed that changes the expected aspect ratio. |
| 182 | + for seed in range(1, 100): |
| 183 | + dataset.reset_data_lines(seed=seed, epoch=seed) |
| 184 | + |
| 185 | + second_expected_ratio = np.mean(dataset.wh_ratio[dataset.wh_ratio_sort]) |
| 186 | + |
| 187 | + if not np.isclose( |
| 188 | + first_expected_ratio, |
| 189 | + second_expected_ratio, |
| 190 | + ): |
| 191 | + break |
| 192 | + else: |
| 193 | + pytest.fail("Could not generate a different sampled subset") |
| 194 | + |
| 195 | + # program.py calls set_epoch() after reset_data_lines(). |
| 196 | + sampler.set_epoch(0) |
| 197 | + |
| 198 | + second_epoch_batch = list(iter(sampler))[0] |
| 199 | + second_epoch_ratio = second_epoch_batch[0][3] |
| 200 | + |
| 201 | + assert np.isclose( |
| 202 | + second_epoch_ratio, |
| 203 | + second_expected_ratio, |
| 204 | + ) |
0 commit comments