Skip to content

Commit dcc033d

Browse files
fix: respect ratio sampling in MultiScaleDataSet
1 parent 2661c7c commit dcc033d

3 files changed

Lines changed: 250 additions & 3 deletions

File tree

‎ppocr/data/multi_scale_sampler.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,14 @@ def iter(self):
167167
def set_epoch(self, epoch: int):
168168
self.epoch = epoch
169169

170+
if self.ds_width:
171+
self.wh_ratio = self.data_source.wh_ratio
172+
self.wh_ratio_sort = self.data_source.wh_ratio_sort
173+
# Refresh cached batches after ratio_list resampling.
174+
self.batchs_in_one_epoch = self.iter()
175+
self.batchs_in_one_epoch_id = [
176+
i for i in range(len(self.batchs_in_one_epoch))
177+
]
178+
170179
def __len__(self):
171180
return self.length

‎ppocr/data/simple_dataset.py‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,32 @@ def wh_aware(self):
461461

462462
self.data_lines = data_line_new
463463
self.wh_ratio = np.array(wh_ratio)
464-
self.wh_ratio_sort = np.argsort(self.wh_ratio)
464+
self._refresh_wh_ratio_sort()
465465
self.data_idx_order_list = list(range(len(self.data_lines)))
466466

467+
def _refresh_wh_ratio_sort(self):
468+
if self._index_map is not None:
469+
sampled_indices = np.asarray(self._index_map, dtype=np.int64)
470+
sampled_order = np.argsort(self.wh_ratio[sampled_indices])
471+
self.wh_ratio_sort = sampled_indices[sampled_order]
472+
else:
473+
self.wh_ratio_sort = np.argsort(self.wh_ratio)
474+
475+
def _ensure_index_map(self):
476+
previous_epoch = self._cached_epoch
477+
super(MultiScaleDataSet, self)._ensure_index_map()
478+
if (
479+
self.ds_width
480+
and self._index_map is not None
481+
and self._cached_epoch != previous_epoch
482+
):
483+
self._refresh_wh_ratio_sort()
484+
485+
def reset_data_lines(self, seed=None, epoch=None):
486+
super(MultiScaleDataSet, self).reset_data_lines(seed=seed, epoch=epoch)
487+
if self.ds_width:
488+
self._refresh_wh_ratio_sort()
489+
467490
def resize_norm_img(self, data, imgW, imgH, padding=True):
468491
img = data["image"]
469492
h = img.shape[0]
@@ -498,18 +521,29 @@ def __getitem__(self, properties):
498521
# properties is a tuple, contains (width, height, index)
499522
img_height = properties[1]
500523
idx = properties[2]
524+
525+
if self._index_map is not None:
526+
self._ensure_index_map()
527+
501528
if self.ds_width and properties[3] is not None:
502529
wh_ratio = properties[3]
503530
img_width = img_height * (
504531
1 if int(round(wh_ratio)) == 0 else int(round(wh_ratio))
505532
)
506533
file_idx = self.wh_ratio_sort[idx]
507534
else:
508-
file_idx = self.data_idx_order_list[idx]
509535
img_width = properties[0]
510536
wh_ratio = None
537+
if self._index_map is not None:
538+
file_idx = self._index_map[idx]
539+
else:
540+
file_idx = self.data_idx_order_list[idx]
541+
542+
if self._index_map is not None:
543+
data_line = self._all_lines[file_idx]
544+
else:
545+
data_line = self.data_lines[file_idx]
511546

512-
data_line = self.data_lines[file_idx]
513547
try:
514548
data_line = data_line.decode("utf-8")
515549
substr = data_line.strip("\n").split(self.delimiter)

‎tests/ppocr/test_simple_dataset.py‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

Comments
 (0)