Skip to content

Commit

Permalink
Merge pull request #6319 from lgcy/lgcy-2.5-dev
Browse files Browse the repository at this point in the history
update lmdb_dateset for ppocrv3 rec
  • Loading branch information
LDOUBLEV authored May 19, 2022
2 parents f566f2a + 0dfb536 commit 7640fce
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ppocr/data/lmdb_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def __init__(self, config, mode, logger, seed=None):
if self.do_shuffle:
np.random.shuffle(self.data_idx_order_list)
self.ops = create_operators(dataset_config['transforms'], global_config)
self.ext_op_transform_idx = dataset_config.get("ext_op_transform_idx",
2)

ratio_list = dataset_config.get("ratio_list", [1.0])
self.need_reset = True in [x < 1 for x in ratio_list]
Expand Down Expand Up @@ -88,6 +90,29 @@ def get_img_data(self, value):
if imgori is None:
return None
return imgori

def get_ext_data(self):
ext_data_num = 0
for op in self.ops:
if hasattr(op, 'ext_data_num'):
ext_data_num = getattr(op, 'ext_data_num')
break
load_data_ops = self.ops[:self.ext_op_transform_idx]
ext_data = []

while len(ext_data) < ext_data_num:
lmdb_idx, file_idx = self.data_idx_order_list[np.random.randint(self.__len__())]
lmdb_idx = int(lmdb_idx)
file_idx = int(file_idx)
sample_info = self.get_lmdb_sample_info(self.lmdb_sets[lmdb_idx]['txn'],
file_idx)
if sample_info is None:
continue
img, label = sample_info
data = {'image': img, 'label': label}
outs = transform(data, load_data_ops)
ext_data.append(data)
return ext_data

def get_lmdb_sample_info(self, txn, index):
label_key = 'label-%09d'.encode() % index
Expand All @@ -109,6 +134,7 @@ def __getitem__(self, idx):
return self.__getitem__(np.random.randint(self.__len__()))
img, label = sample_info
data = {'image': img, 'label': label}
data['ext_data'] = self.get_ext_data()
outs = transform(data, self.ops)
if outs is None:
return self.__getitem__(np.random.randint(self.__len__()))
Expand Down

0 comments on commit 7640fce

Please sign in to comment.