Skip to content

Commit 443af8b

Browse files
authored
Cache v0.4 update (#3954)
1 parent a26e7de commit 443af8b

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

utils/datasets.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,11 @@ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, r
397397

398398
# Check cache
399399
self.label_files = img2label_paths(self.img_files) # labels
400-
cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache') # cached labels
401-
if cache_path.is_file():
402-
cache, exists = torch.load(cache_path), True # load
403-
if cache.get('version') != 0.3 or cache.get('hash') != get_hash(self.label_files + self.img_files):
404-
cache, exists = self.cache_labels(cache_path, prefix), False # re-cache
405-
else:
400+
cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache')
401+
try:
402+
cache, exists = np.load(cache_path, allow_pickle=True).item(), True # load dict
403+
assert cache['version'] == 0.4 and cache['hash'] == get_hash(self.label_files + self.img_files)
404+
except:
406405
cache, exists = self.cache_labels(cache_path, prefix), False # cache
407406

408407
# Display cache
@@ -496,9 +495,10 @@ def cache_labels(self, path=Path('./labels.cache'), prefix=''):
496495
x['hash'] = get_hash(self.label_files + self.img_files)
497496
x['results'] = nf, nm, ne, nc, len(self.img_files)
498497
x['msgs'] = msgs # warnings
499-
x['version'] = 0.3 # cache version
498+
x['version'] = 0.4 # cache version
500499
try:
501-
torch.save(x, path) # save cache for next time
500+
np.save(path, x) # save cache for next time
501+
path.with_suffix('.cache.npy').rename(path) # remove .npy suffix
502502
logging.info(f'{prefix}New cache created: {path}')
503503
except Exception as e:
504504
logging.info(f'{prefix}WARNING: Cache directory {path.parent} is not writeable: {e}') # path not writeable

0 commit comments

Comments
 (0)