(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
cache_images=False, single_cls=False, stride=32, pad=0.0, rank=-1)
| 327 | |
| 328 | class LoadImagesAndLabels(Dataset): # for training/testing |
| 329 | def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, |
| 330 | cache_images=False, single_cls=False, stride=32, pad=0.0, rank=-1): |
| 331 | self.img_size = img_size |
| 332 | self.augment = augment |
| 333 | self.hyp = hyp |
| 334 | self.image_weights = image_weights |
| 335 | self.rect = False if image_weights else rect |
| 336 | self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) |
| 337 | self.mosaic_border = [-img_size // 2, -img_size // 2] |
| 338 | self.stride = stride |
| 339 | |
| 340 | def img2label_paths(img_paths): |
| 341 | # Define label paths as a function of image paths |
| 342 | sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings |
| 343 | return [x.replace(sa, sb, 1).replace(os.path.splitext(x)[-1], '.txt') for x in img_paths] |
| 344 | |
| 345 | try: |
| 346 | f = [] # image files |
| 347 | for p in path if isinstance(path, list) else [path]: |
| 348 | p = str(Path(p)) # os-agnostic |
| 349 | parent = str(Path(p).parent) + os.sep |
| 350 | if os.path.isfile(p): # file |
| 351 | with open(p, 'r') as t: |
| 352 | t = t.read().splitlines() |
| 353 | f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path |
| 354 | elif os.path.isdir(p): # folder |
| 355 | f += glob.iglob(p + os.sep + '*.*') |
| 356 | else: |
| 357 | raise Exception('%s does not exist' % p) |
| 358 | self.img_files = sorted( |
| 359 | [x.replace('/', os.sep) for x in f if os.path.splitext(x)[-1].lower() in img_formats]) |
| 360 | assert len(self.img_files) > 0, 'No images found' |
| 361 | except Exception as e: |
| 362 | raise Exception('Error loading data from %s: %s\nSee %s' % (path, e, help_url)) |
| 363 | |
| 364 | # Check cache |
| 365 | self.label_files = img2label_paths(self.img_files) # labels |
| 366 | cache_path = str(Path(self.label_files[0]).parent) + '.cache' # cached labels |
| 367 | if os.path.isfile(cache_path): |
| 368 | cache = torch.load(cache_path) # load |
| 369 | if cache['hash'] != get_hash(self.label_files + self.img_files): # dataset changed |
| 370 | cache = self.cache_labels(cache_path) # re-cache |
| 371 | else: |
| 372 | cache = self.cache_labels(cache_path) # cache |
| 373 | |
| 374 | # Read cache |
| 375 | cache.pop('hash') # remove hash |
| 376 | labels, shapes = zip(*cache.values()) |
| 377 | self.labels = list(labels) |
| 378 | self.shapes = np.array(shapes, dtype=np.float64) |
| 379 | self.img_files = list(cache.keys()) # update |
| 380 | self.label_files = img2label_paths(cache.keys()) # update |
| 381 | |
| 382 | n = len(shapes) # number of images |
| 383 | bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index |
| 384 | nb = bi[-1] + 1 # number of batches |
| 385 | self.batch = bi # batch index of image |
| 386 | self.n = n |
nothing calls this directly
no test coverage detected