| 603 | return cache |
| 604 | |
| 605 | def cache_labels(self, path=Path('./labels.cache'), prefix=''): |
| 606 | # Cache dataset labels, check images and read shapes |
| 607 | x = {} # dict |
| 608 | nm, nf, ne, nc, msgs = 0, 0, 0, 0, [] # number missing, found, empty, corrupt, messages |
| 609 | desc = f"{prefix}Scanning {path.parent / path.stem}..." |
| 610 | with Pool(NUM_THREADS) as pool: |
| 611 | pbar = tqdm(pool.imap(verify_image_label, zip(self.im_files, self.label_files, repeat(prefix))), |
| 612 | desc=desc, |
| 613 | total=len(self.im_files), |
| 614 | bar_format=TQDM_BAR_FORMAT) |
| 615 | for im_file, lb, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar: |
| 616 | nm += nm_f |
| 617 | nf += nf_f |
| 618 | ne += ne_f |
| 619 | nc += nc_f |
| 620 | if im_file: |
| 621 | x[im_file] = [lb, shape, segments] |
| 622 | if msg: |
| 623 | msgs.append(msg) |
| 624 | pbar.desc = f"{desc} {nf} images, {nm + ne} backgrounds, {nc} corrupt" |
| 625 | |
| 626 | pbar.close() |
| 627 | if msgs: |
| 628 | LOGGER.info('\n'.join(msgs)) |
| 629 | if nf == 0: |
| 630 | LOGGER.warning(f'{prefix}WARNING ⚠️ No labels found in {path}. {HELP_URL}') |
| 631 | x['hash'] = get_hash(self.label_files + self.im_files) |
| 632 | x['results'] = nf, nm, ne, nc, len(self.im_files) |
| 633 | x['msgs'] = msgs # warnings |
| 634 | x['version'] = self.cache_version # cache version |
| 635 | try: |
| 636 | np.save(path, x) # save cache for next time |
| 637 | path.with_suffix('.cache.npy').rename(path) # remove .npy suffix |
| 638 | LOGGER.info(f'{prefix}New cache created: {path}') |
| 639 | except Exception as e: |
| 640 | LOGGER.warning(f'{prefix}WARNING ⚠️ Cache directory {path.parent} is not writeable: {e}') # not writeable |
| 641 | return x |
| 642 | |
| 643 | def __len__(self): |
| 644 | return len(self.im_files) |