You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

977 lines
41KB

  1. # YOLOv5 dataset utils and dataloaders
  2. import glob
  3. import hashlib
  4. import json
  5. import logging
  6. import os
  7. import random
  8. import shutil
  9. import time
  10. from itertools import repeat
  11. from multiprocessing.pool import ThreadPool, Pool
  12. from pathlib import Path
  13. from threading import Thread
  14. import cv2
  15. import numpy as np
  16. import torch
  17. import torch.nn.functional as F
  18. import yaml
  19. from PIL import Image, ExifTags
  20. from torch.utils.data import Dataset
  21. from tqdm import tqdm
  22. from utils.augmentations import Albumentations, augment_hsv, copy_paste, letterbox, mixup, random_perspective
  23. from utils.general import check_requirements, check_file, check_dataset, xywh2xyxy, xywhn2xyxy, xyxy2xywhn, \
  24. xyn2xy, segments2boxes, clean_str
  25. from utils.torch_utils import torch_distributed_zero_first
  26. # Parameters
  27. HELP_URL = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
  28. IMG_FORMATS = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng', 'webp', 'mpo'] # acceptable image suffixes
  29. VID_FORMATS = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes
  30. NUM_THREADS = min(8, os.cpu_count()) # number of multiprocessing threads
  31. # Get orientation exif tag
  32. for orientation in ExifTags.TAGS.keys():
  33. if ExifTags.TAGS[orientation] == 'Orientation':
  34. break
  35. def get_hash(paths):
  36. # Returns a single hash value of a list of paths (files or dirs)
  37. size = sum(os.path.getsize(p) for p in paths if os.path.exists(p)) # sizes
  38. h = hashlib.md5(str(size).encode()) # hash sizes
  39. h.update(''.join(paths).encode()) # hash paths
  40. return h.hexdigest() # return hash
  41. def exif_size(img):
  42. # Returns exif-corrected PIL size
  43. s = img.size # (width, height)
  44. try:
  45. rotation = dict(img._getexif().items())[orientation]
  46. if rotation == 6: # rotation 270
  47. s = (s[1], s[0])
  48. elif rotation == 8: # rotation 90
  49. s = (s[1], s[0])
  50. except:
  51. pass
  52. return s
  53. def exif_transpose(image):
  54. """
  55. Transpose a PIL image accordingly if it has an EXIF Orientation tag.
  56. From https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageOps.py
  57. :param image: The image to transpose.
  58. :return: An image.
  59. """
  60. exif = image.getexif()
  61. orientation = exif.get(0x0112, 1) # default 1
  62. if orientation > 1:
  63. method = {2: Image.FLIP_LEFT_RIGHT,
  64. 3: Image.ROTATE_180,
  65. 4: Image.FLIP_TOP_BOTTOM,
  66. 5: Image.TRANSPOSE,
  67. 6: Image.ROTATE_270,
  68. 7: Image.TRANSVERSE,
  69. 8: Image.ROTATE_90,
  70. }.get(orientation)
  71. if method is not None:
  72. image = image.transpose(method)
  73. del exif[0x0112]
  74. image.info["exif"] = exif.tobytes()
  75. return image
  76. def create_dataloader(path, imgsz, batch_size, stride, single_cls=False, hyp=None, augment=False, cache=False, pad=0.0,
  77. rect=False, rank=-1, workers=8, image_weights=False, quad=False, prefix=''):
  78. # Make sure only the first process in DDP process the dataset first, and the following others can use the cache
  79. with torch_distributed_zero_first(rank):
  80. dataset = LoadImagesAndLabels(path, imgsz, batch_size,
  81. augment=augment, # augment images
  82. hyp=hyp, # augmentation hyperparameters
  83. rect=rect, # rectangular training
  84. cache_images=cache,
  85. single_cls=single_cls,
  86. stride=int(stride),
  87. pad=pad,
  88. image_weights=image_weights,
  89. prefix=prefix)
  90. batch_size = min(batch_size, len(dataset))
  91. nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, workers]) # number of workers
  92. sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None
  93. loader = torch.utils.data.DataLoader if image_weights else InfiniteDataLoader
  94. # Use torch.utils.data.DataLoader() if dataset.properties will update during training else InfiniteDataLoader()
  95. dataloader = loader(dataset,
  96. batch_size=batch_size,
  97. num_workers=nw,
  98. sampler=sampler,
  99. pin_memory=True,
  100. collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn)
  101. return dataloader, dataset
  102. class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
  103. """ Dataloader that reuses workers
  104. Uses same syntax as vanilla DataLoader
  105. """
  106. def __init__(self, *args, **kwargs):
  107. super().__init__(*args, **kwargs)
  108. object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler))
  109. self.iterator = super().__iter__()
  110. def __len__(self):
  111. return len(self.batch_sampler.sampler)
  112. def __iter__(self):
  113. for i in range(len(self)):
  114. yield next(self.iterator)
  115. class _RepeatSampler(object):
  116. """ Sampler that repeats forever
  117. Args:
  118. sampler (Sampler)
  119. """
  120. def __init__(self, sampler):
  121. self.sampler = sampler
  122. def __iter__(self):
  123. while True:
  124. yield from iter(self.sampler)
  125. class LoadImages: # for inference
  126. def __init__(self, path, img_size=640, stride=32):
  127. p = str(Path(path).absolute()) # os-agnostic absolute path
  128. if '*' in p:
  129. files = sorted(glob.glob(p, recursive=True)) # glob
  130. elif os.path.isdir(p):
  131. files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
  132. elif os.path.isfile(p):
  133. files = [p] # files
  134. else:
  135. raise Exception(f'ERROR: {p} does not exist')
  136. images = [x for x in files if x.split('.')[-1].lower() in IMG_FORMATS]
  137. videos = [x for x in files if x.split('.')[-1].lower() in VID_FORMATS]
  138. ni, nv = len(images), len(videos)
  139. self.img_size = img_size
  140. self.stride = stride
  141. self.files = images + videos
  142. self.nf = ni + nv # number of files
  143. self.video_flag = [False] * ni + [True] * nv
  144. self.mode = 'image'
  145. if any(videos):
  146. self.new_video(videos[0]) # new video
  147. else:
  148. self.cap = None
  149. assert self.nf > 0, f'No images or videos found in {p}. ' \
  150. f'Supported formats are:\nimages: {IMG_FORMATS}\nvideos: {VID_FORMATS}'
  151. def __iter__(self):
  152. self.count = 0
  153. return self
  154. def __next__(self):
  155. if self.count == self.nf:
  156. raise StopIteration
  157. path = self.files[self.count]
  158. if self.video_flag[self.count]:
  159. # Read video
  160. self.mode = 'video'
  161. ret_val, img0 = self.cap.read()
  162. if not ret_val:
  163. self.count += 1
  164. self.cap.release()
  165. if self.count == self.nf: # last video
  166. raise StopIteration
  167. else:
  168. path = self.files[self.count]
  169. self.new_video(path)
  170. ret_val, img0 = self.cap.read()
  171. self.frame += 1
  172. print(f'video {self.count + 1}/{self.nf} ({self.frame}/{self.frames}) {path}: ', end='')
  173. else:
  174. # Read image
  175. self.count += 1
  176. img0 = cv2.imread(path) # BGR
  177. assert img0 is not None, 'Image Not Found ' + path
  178. print(f'image {self.count}/{self.nf} {path}: ', end='')
  179. # Padded resize
  180. img = letterbox(img0, self.img_size, stride=self.stride)[0]
  181. # Convert
  182. img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
  183. img = np.ascontiguousarray(img)
  184. return path, img, img0, self.cap
  185. def new_video(self, path):
  186. self.frame = 0
  187. self.cap = cv2.VideoCapture(path)
  188. self.frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
  189. def __len__(self):
  190. return self.nf # number of files
  191. class LoadWebcam: # for inference
  192. def __init__(self, pipe='0', img_size=640, stride=32):
  193. self.img_size = img_size
  194. self.stride = stride
  195. self.pipe = eval(pipe) if pipe.isnumeric() else pipe
  196. self.cap = cv2.VideoCapture(self.pipe) # video capture object
  197. self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size
  198. def __iter__(self):
  199. self.count = -1
  200. return self
  201. def __next__(self):
  202. self.count += 1
  203. if cv2.waitKey(1) == ord('q'): # q to quit
  204. self.cap.release()
  205. cv2.destroyAllWindows()
  206. raise StopIteration
  207. # Read frame
  208. ret_val, img0 = self.cap.read()
  209. img0 = cv2.flip(img0, 1) # flip left-right
  210. # Print
  211. assert ret_val, f'Camera Error {self.pipe}'
  212. img_path = 'webcam.jpg'
  213. print(f'webcam {self.count}: ', end='')
  214. # Padded resize
  215. img = letterbox(img0, self.img_size, stride=self.stride)[0]
  216. # Convert
  217. img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
  218. img = np.ascontiguousarray(img)
  219. return img_path, img, img0, None
  220. def __len__(self):
  221. return 0
  222. class LoadStreams: # multiple IP or RTSP cameras
  223. def __init__(self, sources='streams.txt', img_size=640, stride=32):
  224. self.mode = 'stream'
  225. self.img_size = img_size
  226. self.stride = stride
  227. if os.path.isfile(sources):
  228. with open(sources, 'r') as f:
  229. sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
  230. else:
  231. sources = [sources]
  232. n = len(sources)
  233. self.imgs, self.fps, self.frames, self.threads = [None] * n, [0] * n, [0] * n, [None] * n
  234. self.sources = [clean_str(x) for x in sources] # clean source names for later
  235. for i, s in enumerate(sources): # index, source
  236. # Start thread to read frames from video stream
  237. print(f'{i + 1}/{n}: {s}... ', end='')
  238. if 'youtube.com/' in s or 'youtu.be/' in s: # if source is YouTube video
  239. check_requirements(('pafy', 'youtube_dl'))
  240. import pafy
  241. s = pafy.new(s).getbest(preftype="mp4").url # YouTube URL
  242. s = eval(s) if s.isnumeric() else s # i.e. s = '0' local webcam
  243. cap = cv2.VideoCapture(s)
  244. assert cap.isOpened(), f'Failed to open {s}'
  245. w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  246. h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  247. self.fps[i] = max(cap.get(cv2.CAP_PROP_FPS) % 100, 0) or 30.0 # 30 FPS fallback
  248. self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
  249. _, self.imgs[i] = cap.read() # guarantee first frame
  250. self.threads[i] = Thread(target=self.update, args=([i, cap]), daemon=True)
  251. print(f" success ({self.frames[i]} frames {w}x{h} at {self.fps[i]:.2f} FPS)")
  252. self.threads[i].start()
  253. print('') # newline
  254. # check for common shapes
  255. s = np.stack([letterbox(x, self.img_size, stride=self.stride)[0].shape for x in self.imgs], 0) # shapes
  256. self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
  257. if not self.rect:
  258. print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
  259. def update(self, i, cap):
  260. # Read stream `i` frames in daemon thread
  261. n, f, read = 0, self.frames[i], 1 # frame number, frame array, inference every 'read' frame
  262. while cap.isOpened() and n < f:
  263. n += 1
  264. # _, self.imgs[index] = cap.read()
  265. cap.grab()
  266. if n % read == 0:
  267. success, im = cap.retrieve()
  268. self.imgs[i] = im if success else self.imgs[i] * 0
  269. time.sleep(1 / self.fps[i]) # wait time
  270. def __iter__(self):
  271. self.count = -1
  272. return self
  273. def __next__(self):
  274. self.count += 1
  275. if not all(x.is_alive() for x in self.threads) or cv2.waitKey(1) == ord('q'): # q to quit
  276. cv2.destroyAllWindows()
  277. raise StopIteration
  278. # Letterbox
  279. img0 = self.imgs.copy()
  280. img = [letterbox(x, self.img_size, auto=self.rect, stride=self.stride)[0] for x in img0]
  281. # Stack
  282. img = np.stack(img, 0)
  283. # Convert
  284. img = img[..., ::-1].transpose((0, 3, 1, 2)) # BGR to RGB, BHWC to BCHW
  285. img = np.ascontiguousarray(img)
  286. return self.sources, img, img0, None
  287. def __len__(self):
  288. return len(self.sources) # 1E12 frames = 32 streams at 30 FPS for 30 years
  289. def img2label_paths(img_paths):
  290. # Define label paths as a function of image paths
  291. sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings
  292. return [sb.join(x.rsplit(sa, 1)).rsplit('.', 1)[0] + '.txt' for x in img_paths]
  293. class LoadImagesAndLabels(Dataset): # for training/testing
  294. def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
  295. cache_images=False, single_cls=False, stride=32, pad=0.0, prefix=''):
  296. self.img_size = img_size
  297. self.augment = augment
  298. self.hyp = hyp
  299. self.image_weights = image_weights
  300. self.rect = False if image_weights else rect
  301. self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training)
  302. self.mosaic_border = [-img_size // 2, -img_size // 2]
  303. self.stride = stride
  304. self.path = path
  305. self.albumentations = Albumentations() if augment else None
  306. try:
  307. f = [] # image files
  308. for p in path if isinstance(path, list) else [path]:
  309. p = Path(p) # os-agnostic
  310. if p.is_dir(): # dir
  311. f += glob.glob(str(p / '**' / '*.*'), recursive=True)
  312. # f = list(p.rglob('**/*.*')) # pathlib
  313. elif p.is_file(): # file
  314. with open(p, 'r') as t:
  315. t = t.read().strip().splitlines()
  316. parent = str(p.parent) + os.sep
  317. f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
  318. # f += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib)
  319. else:
  320. raise Exception(f'{prefix}{p} does not exist')
  321. self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in IMG_FORMATS])
  322. # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in img_formats]) # pathlib
  323. assert self.img_files, f'{prefix}No images found'
  324. except Exception as e:
  325. raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {HELP_URL}')
  326. # Check cache
  327. self.label_files = img2label_paths(self.img_files) # labels
  328. cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache')
  329. try:
  330. cache, exists = np.load(cache_path, allow_pickle=True).item(), True # load dict
  331. assert cache['version'] == 0.4 and cache['hash'] == get_hash(self.label_files + self.img_files)
  332. except:
  333. cache, exists = self.cache_labels(cache_path, prefix), False # cache
  334. # Display cache
  335. nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupted, total
  336. if exists:
  337. d = f"Scanning '{cache_path}' images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupted"
  338. tqdm(None, desc=prefix + d, total=n, initial=n) # display cache results
  339. if cache['msgs']:
  340. logging.info('\n'.join(cache['msgs'])) # display warnings
  341. assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {HELP_URL}'
  342. # Read cache
  343. [cache.pop(k) for k in ('hash', 'version', 'msgs')] # remove items
  344. labels, shapes, self.segments = zip(*cache.values())
  345. self.labels = list(labels)
  346. self.shapes = np.array(shapes, dtype=np.float64)
  347. self.img_files = list(cache.keys()) # update
  348. self.label_files = img2label_paths(cache.keys()) # update
  349. if single_cls:
  350. for x in self.labels:
  351. x[:, 0] = 0
  352. n = len(shapes) # number of images
  353. bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index
  354. nb = bi[-1] + 1 # number of batches
  355. self.batch = bi # batch index of image
  356. self.n = n
  357. self.indices = range(n)
  358. # Rectangular Training
  359. if self.rect:
  360. # Sort by aspect ratio
  361. s = self.shapes # wh
  362. ar = s[:, 1] / s[:, 0] # aspect ratio
  363. irect = ar.argsort()
  364. self.img_files = [self.img_files[i] for i in irect]
  365. self.label_files = [self.label_files[i] for i in irect]
  366. self.labels = [self.labels[i] for i in irect]
  367. self.shapes = s[irect] # wh
  368. ar = ar[irect]
  369. # Set training image shapes
  370. shapes = [[1, 1]] * nb
  371. for i in range(nb):
  372. ari = ar[bi == i]
  373. mini, maxi = ari.min(), ari.max()
  374. if maxi < 1:
  375. shapes[i] = [maxi, 1]
  376. elif mini > 1:
  377. shapes[i] = [1, 1 / mini]
  378. self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride
  379. # Cache images into memory for faster training (WARNING: large datasets may exceed system RAM)
  380. self.imgs = [None] * n
  381. if cache_images:
  382. gb = 0 # Gigabytes of cached images
  383. self.img_hw0, self.img_hw = [None] * n, [None] * n
  384. results = ThreadPool(NUM_THREADS).imap(lambda x: load_image(*x), zip(repeat(self), range(n)))
  385. pbar = tqdm(enumerate(results), total=n)
  386. for i, x in pbar:
  387. self.imgs[i], self.img_hw0[i], self.img_hw[i] = x # img, hw_original, hw_resized = load_image(self, i)
  388. gb += self.imgs[i].nbytes
  389. pbar.desc = f'{prefix}Caching images ({gb / 1E9:.1f}GB)'
  390. pbar.close()
  391. def cache_labels(self, path=Path('./labels.cache'), prefix=''):
  392. # Cache dataset labels, check images and read shapes
  393. x = {} # dict
  394. nm, nf, ne, nc, msgs = 0, 0, 0, 0, [] # number missing, found, empty, corrupt, messages
  395. desc = f"{prefix}Scanning '{path.parent / path.stem}' images and labels..."
  396. with Pool(NUM_THREADS) as pool:
  397. pbar = tqdm(pool.imap_unordered(verify_image_label, zip(self.img_files, self.label_files, repeat(prefix))),
  398. desc=desc, total=len(self.img_files))
  399. for im_file, l, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar:
  400. nm += nm_f
  401. nf += nf_f
  402. ne += ne_f
  403. nc += nc_f
  404. if im_file:
  405. x[im_file] = [l, shape, segments]
  406. if msg:
  407. msgs.append(msg)
  408. pbar.desc = f"{desc}{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
  409. pbar.close()
  410. if msgs:
  411. logging.info('\n'.join(msgs))
  412. if nf == 0:
  413. logging.info(f'{prefix}WARNING: No labels found in {path}. See {HELP_URL}')
  414. x['hash'] = get_hash(self.label_files + self.img_files)
  415. x['results'] = nf, nm, ne, nc, len(self.img_files)
  416. x['msgs'] = msgs # warnings
  417. x['version'] = 0.4 # cache version
  418. try:
  419. np.save(path, x) # save cache for next time
  420. path.with_suffix('.cache.npy').rename(path) # remove .npy suffix
  421. logging.info(f'{prefix}New cache created: {path}')
  422. except Exception as e:
  423. logging.info(f'{prefix}WARNING: Cache directory {path.parent} is not writeable: {e}') # path not writeable
  424. return x
  425. def __len__(self):
  426. return len(self.img_files)
  427. # def __iter__(self):
  428. # self.count = -1
  429. # print('ran dataset iter')
  430. # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF)
  431. # return self
  432. def __getitem__(self, index):
  433. index = self.indices[index] # linear, shuffled, or image_weights
  434. hyp = self.hyp
  435. mosaic = self.mosaic and random.random() < hyp['mosaic']
  436. if mosaic:
  437. # Load mosaic
  438. img, labels = load_mosaic(self, index)
  439. shapes = None
  440. # MixUp augmentation
  441. if random.random() < hyp['mixup']:
  442. img, labels = mixup(img, labels, *load_mosaic(self, random.randint(0, self.n - 1)))
  443. else:
  444. # Load image
  445. img, (h0, w0), (h, w) = load_image(self, index)
  446. # Letterbox
  447. shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
  448. img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
  449. shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
  450. labels = self.labels[index].copy()
  451. if labels.size: # normalized xywh to pixel xyxy format
  452. labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
  453. if self.augment:
  454. img, labels = random_perspective(img, labels,
  455. degrees=hyp['degrees'],
  456. translate=hyp['translate'],
  457. scale=hyp['scale'],
  458. shear=hyp['shear'],
  459. perspective=hyp['perspective'])
  460. nl = len(labels) # number of labels
  461. if nl:
  462. labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0], clip=True, eps=1E-3)
  463. if self.augment:
  464. # Albumentations
  465. img, labels = self.albumentations(img, labels)
  466. # HSV color-space
  467. augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
  468. # Flip up-down
  469. if random.random() < hyp['flipud']:
  470. img = np.flipud(img)
  471. if nl:
  472. labels[:, 2] = 1 - labels[:, 2]
  473. # Flip left-right
  474. if random.random() < hyp['fliplr']:
  475. img = np.fliplr(img)
  476. if nl:
  477. labels[:, 1] = 1 - labels[:, 1]
  478. # Cutouts
  479. # labels = cutout(img, labels, p=0.5)
  480. labels_out = torch.zeros((nl, 6))
  481. if nl:
  482. labels_out[:, 1:] = torch.from_numpy(labels)
  483. # Convert
  484. img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
  485. img = np.ascontiguousarray(img)
  486. return torch.from_numpy(img), labels_out, self.img_files[index], shapes
  487. @staticmethod
  488. def collate_fn(batch):
  489. img, label, path, shapes = zip(*batch) # transposed
  490. for i, l in enumerate(label):
  491. l[:, 0] = i # add target image index for build_targets()
  492. return torch.stack(img, 0), torch.cat(label, 0), path, shapes
  493. @staticmethod
  494. def collate_fn4(batch):
  495. img, label, path, shapes = zip(*batch) # transposed
  496. n = len(shapes) // 4
  497. img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
  498. ho = torch.tensor([[0., 0, 0, 1, 0, 0]])
  499. wo = torch.tensor([[0., 0, 1, 0, 0, 0]])
  500. s = torch.tensor([[1, 1, .5, .5, .5, .5]]) # scale
  501. for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
  502. i *= 4
  503. if random.random() < 0.5:
  504. im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2., mode='bilinear', align_corners=False)[
  505. 0].type(img[i].type())
  506. l = label[i]
  507. else:
  508. im = torch.cat((torch.cat((img[i], img[i + 1]), 1), torch.cat((img[i + 2], img[i + 3]), 1)), 2)
  509. l = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s
  510. img4.append(im)
  511. label4.append(l)
  512. for i, l in enumerate(label4):
  513. l[:, 0] = i # add target image index for build_targets()
  514. return torch.stack(img4, 0), torch.cat(label4, 0), path4, shapes4
  515. # Ancillary functions --------------------------------------------------------------------------------------------------
  516. def load_image(self, index):
  517. # loads 1 image from dataset, returns img, original hw, resized hw
  518. img = self.imgs[index]
  519. if img is None: # not cached
  520. path = self.img_files[index]
  521. img = cv2.imread(path) # BGR
  522. assert img is not None, 'Image Not Found ' + path
  523. h0, w0 = img.shape[:2] # orig hw
  524. r = self.img_size / max(h0, w0) # ratio
  525. if r != 1: # if sizes are not equal
  526. img = cv2.resize(img, (int(w0 * r), int(h0 * r)),
  527. interpolation=cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR)
  528. return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized
  529. else:
  530. return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized
  531. def load_mosaic(self, index):
  532. # loads images in a 4-mosaic
  533. labels4, segments4 = [], []
  534. s = self.img_size
  535. yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
  536. indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices
  537. for i, index in enumerate(indices):
  538. # Load image
  539. img, _, (h, w) = load_image(self, index)
  540. # place img in img4
  541. if i == 0: # top left
  542. img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
  543. x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
  544. x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
  545. elif i == 1: # top right
  546. x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
  547. x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
  548. elif i == 2: # bottom left
  549. x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
  550. x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
  551. elif i == 3: # bottom right
  552. x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
  553. x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
  554. img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
  555. padw = x1a - x1b
  556. padh = y1a - y1b
  557. # Labels
  558. labels, segments = self.labels[index].copy(), self.segments[index].copy()
  559. if labels.size:
  560. labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
  561. segments = [xyn2xy(x, w, h, padw, padh) for x in segments]
  562. labels4.append(labels)
  563. segments4.extend(segments)
  564. # Concat/clip labels
  565. labels4 = np.concatenate(labels4, 0)
  566. for x in (labels4[:, 1:], *segments4):
  567. np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
  568. # img4, labels4 = replicate(img4, labels4) # replicate
  569. # Augment
  570. img4, labels4, segments4 = copy_paste(img4, labels4, segments4, p=self.hyp['copy_paste'])
  571. img4, labels4 = random_perspective(img4, labels4, segments4,
  572. degrees=self.hyp['degrees'],
  573. translate=self.hyp['translate'],
  574. scale=self.hyp['scale'],
  575. shear=self.hyp['shear'],
  576. perspective=self.hyp['perspective'],
  577. border=self.mosaic_border) # border to remove
  578. return img4, labels4
  579. def load_mosaic9(self, index):
  580. # loads images in a 9-mosaic
  581. labels9, segments9 = [], []
  582. s = self.img_size
  583. indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices
  584. for i, index in enumerate(indices):
  585. # Load image
  586. img, _, (h, w) = load_image(self, index)
  587. # place img in img9
  588. if i == 0: # center
  589. img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
  590. h0, w0 = h, w
  591. c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates
  592. elif i == 1: # top
  593. c = s, s - h, s + w, s
  594. elif i == 2: # top right
  595. c = s + wp, s - h, s + wp + w, s
  596. elif i == 3: # right
  597. c = s + w0, s, s + w0 + w, s + h
  598. elif i == 4: # bottom right
  599. c = s + w0, s + hp, s + w0 + w, s + hp + h
  600. elif i == 5: # bottom
  601. c = s + w0 - w, s + h0, s + w0, s + h0 + h
  602. elif i == 6: # bottom left
  603. c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h
  604. elif i == 7: # left
  605. c = s - w, s + h0 - h, s, s + h0
  606. elif i == 8: # top left
  607. c = s - w, s + h0 - hp - h, s, s + h0 - hp
  608. padx, pady = c[:2]
  609. x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords
  610. # Labels
  611. labels, segments = self.labels[index].copy(), self.segments[index].copy()
  612. if labels.size:
  613. labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format
  614. segments = [xyn2xy(x, w, h, padx, pady) for x in segments]
  615. labels9.append(labels)
  616. segments9.extend(segments)
  617. # Image
  618. img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax]
  619. hp, wp = h, w # height, width previous
  620. # Offset
  621. yc, xc = [int(random.uniform(0, s)) for _ in self.mosaic_border] # mosaic center x, y
  622. img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s]
  623. # Concat/clip labels
  624. labels9 = np.concatenate(labels9, 0)
  625. labels9[:, [1, 3]] -= xc
  626. labels9[:, [2, 4]] -= yc
  627. c = np.array([xc, yc]) # centers
  628. segments9 = [x - c for x in segments9]
  629. for x in (labels9[:, 1:], *segments9):
  630. np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
  631. # img9, labels9 = replicate(img9, labels9) # replicate
  632. # Augment
  633. img9, labels9 = random_perspective(img9, labels9, segments9,
  634. degrees=self.hyp['degrees'],
  635. translate=self.hyp['translate'],
  636. scale=self.hyp['scale'],
  637. shear=self.hyp['shear'],
  638. perspective=self.hyp['perspective'],
  639. border=self.mosaic_border) # border to remove
  640. return img9, labels9
  641. def create_folder(path='./new'):
  642. # Create folder
  643. if os.path.exists(path):
  644. shutil.rmtree(path) # delete output folder
  645. os.makedirs(path) # make new output folder
  646. def flatten_recursive(path='../datasets/coco128'):
  647. # Flatten a recursive directory by bringing all files to top level
  648. new_path = Path(path + '_flat')
  649. create_folder(new_path)
  650. for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)):
  651. shutil.copyfile(file, new_path / Path(file).name)
  652. def extract_boxes(path='../datasets/coco128'): # from utils.datasets import *; extract_boxes()
  653. # Convert detection dataset into classification dataset, with one directory per class
  654. path = Path(path) # images dir
  655. shutil.rmtree(path / 'classifier') if (path / 'classifier').is_dir() else None # remove existing
  656. files = list(path.rglob('*.*'))
  657. n = len(files) # number of files
  658. for im_file in tqdm(files, total=n):
  659. if im_file.suffix[1:] in IMG_FORMATS:
  660. # image
  661. im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB
  662. h, w = im.shape[:2]
  663. # labels
  664. lb_file = Path(img2label_paths([str(im_file)])[0])
  665. if Path(lb_file).exists():
  666. with open(lb_file, 'r') as f:
  667. lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
  668. for j, x in enumerate(lb):
  669. c = int(x[0]) # class
  670. f = (path / 'classifier') / f'{c}' / f'{path.stem}_{im_file.stem}_{j}.jpg' # new filename
  671. if not f.parent.is_dir():
  672. f.parent.mkdir(parents=True)
  673. b = x[1:] * [w, h, w, h] # box
  674. # b[2:] = b[2:].max() # rectangle to square
  675. b[2:] = b[2:] * 1.2 + 3 # pad
  676. b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int)
  677. b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
  678. b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
  679. assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}'
  680. def autosplit(path='../datasets/coco128/images', weights=(0.9, 0.1, 0.0), annotated_only=False):
  681. """ Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
  682. Usage: from utils.datasets import *; autosplit()
  683. Arguments
  684. path: Path to images directory
  685. weights: Train, val, test weights (list, tuple)
  686. annotated_only: Only use images with an annotated txt file
  687. """
  688. path = Path(path) # images dir
  689. files = sum([list(path.rglob(f"*.{img_ext}")) for img_ext in IMG_FORMATS], []) # image files only
  690. n = len(files) # number of files
  691. random.seed(0) # for reproducibility
  692. indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split
  693. txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files
  694. [(path.parent / x).unlink(missing_ok=True) for x in txt] # remove existing
  695. print(f'Autosplitting images from {path}' + ', using *.txt labeled images only' * annotated_only)
  696. for i, img in tqdm(zip(indices, files), total=n):
  697. if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
  698. with open(path.parent / txt[i], 'a') as f:
  699. f.write('./' + img.relative_to(path.parent).as_posix() + '\n') # add image to txt file
  700. def verify_image_label(args):
  701. # Verify one image-label pair
  702. im_file, lb_file, prefix = args
  703. nm, nf, ne, nc = 0, 0, 0, 0 # number missing, found, empty, corrupt
  704. try:
  705. # verify images
  706. im = Image.open(im_file)
  707. im.verify() # PIL verify
  708. shape = exif_size(im) # image size
  709. assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels'
  710. assert im.format.lower() in IMG_FORMATS, f'invalid image format {im.format}'
  711. if im.format.lower() in ('jpg', 'jpeg'):
  712. with open(im_file, 'rb') as f:
  713. f.seek(-2, 2)
  714. assert f.read() == b'\xff\xd9', 'corrupted JPEG'
  715. # verify labels
  716. segments = [] # instance segments
  717. if os.path.isfile(lb_file):
  718. nf = 1 # label found
  719. with open(lb_file, 'r') as f:
  720. l = [x.split() for x in f.read().strip().splitlines() if len(x)]
  721. if any([len(x) > 8 for x in l]): # is segment
  722. classes = np.array([x[0] for x in l], dtype=np.float32)
  723. segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in l] # (cls, xy1...)
  724. l = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
  725. l = np.array(l, dtype=np.float32)
  726. if len(l):
  727. assert l.shape[1] == 5, 'labels require 5 columns each'
  728. assert (l >= 0).all(), 'negative labels'
  729. assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels'
  730. assert np.unique(l, axis=0).shape[0] == l.shape[0], 'duplicate labels'
  731. else:
  732. ne = 1 # label empty
  733. l = np.zeros((0, 5), dtype=np.float32)
  734. else:
  735. nm = 1 # label missing
  736. l = np.zeros((0, 5), dtype=np.float32)
  737. return im_file, l, shape, segments, nm, nf, ne, nc, ''
  738. except Exception as e:
  739. nc = 1
  740. msg = f'{prefix}WARNING: Ignoring corrupted image and/or label {im_file}: {e}'
  741. return [None, None, None, None, nm, nf, ne, nc, msg]
  742. def dataset_stats(path='coco128.yaml', autodownload=False, verbose=False, profile=False, hub=False):
  743. """ Return dataset statistics dictionary with images and instances counts per split per class
  744. To run in parent directory: export PYTHONPATH="$PWD/yolov5"
  745. Usage1: from utils.datasets import *; dataset_stats('coco128.yaml', autodownload=True)
  746. Usage2: from utils.datasets import *; dataset_stats('../datasets/coco128_with_yaml.zip')
  747. Arguments
  748. path: Path to data.yaml or data.zip (with data.yaml inside data.zip)
  749. autodownload: Attempt to download dataset if not found locally
  750. verbose: Print stats dictionary
  751. """
  752. def round_labels(labels):
  753. # Update labels to integer class and 6 decimal place floats
  754. return [[int(c), *[round(x, 4) for x in points]] for c, *points in labels]
  755. def unzip(path):
  756. # Unzip data.zip TODO: CONSTRAINT: path/to/abc.zip MUST unzip to 'path/to/abc/'
  757. if str(path).endswith('.zip'): # path is data.zip
  758. assert Path(path).is_file(), f'Error unzipping {path}, file not found'
  759. assert os.system(f'unzip -q {path} -d {path.parent}') == 0, f'Error unzipping {path}'
  760. dir = path.with_suffix('') # dataset directory
  761. return True, str(dir), next(dir.rglob('*.yaml')) # zipped, data_dir, yaml_path
  762. else: # path is data.yaml
  763. return False, None, path
  764. def hub_ops(f, max_dim=1920):
  765. # HUB ops for 1 image 'f'
  766. im = Image.open(f)
  767. r = max_dim / max(im.height, im.width) # ratio
  768. if r < 1.0: # image too large
  769. im = im.resize((int(im.width * r), int(im.height * r)))
  770. im.save(im_dir / Path(f).name, quality=75) # save
  771. zipped, data_dir, yaml_path = unzip(Path(path))
  772. with open(check_file(yaml_path), encoding='ascii', errors='ignore') as f:
  773. data = yaml.safe_load(f) # data dict
  774. if zipped:
  775. data['path'] = data_dir # TODO: should this be dir.resolve()?
  776. check_dataset(data, autodownload) # download dataset if missing
  777. hub_dir = Path(data['path'] + ('-hub' if hub else ''))
  778. stats = {'nc': data['nc'], 'names': data['names']} # statistics dictionary
  779. for split in 'train', 'val', 'test':
  780. if data.get(split) is None:
  781. stats[split] = None # i.e. no test set
  782. continue
  783. x = []
  784. dataset = LoadImagesAndLabels(data[split]) # load dataset
  785. for label in tqdm(dataset.labels, total=dataset.n, desc='Statistics'):
  786. x.append(np.bincount(label[:, 0].astype(int), minlength=data['nc']))
  787. x = np.array(x) # shape(128x80)
  788. stats[split] = {'instance_stats': {'total': int(x.sum()), 'per_class': x.sum(0).tolist()},
  789. 'image_stats': {'total': dataset.n, 'unlabelled': int(np.all(x == 0, 1).sum()),
  790. 'per_class': (x > 0).sum(0).tolist()},
  791. 'labels': [{str(Path(k).name): round_labels(v.tolist())} for k, v in
  792. zip(dataset.img_files, dataset.labels)]}
  793. if hub:
  794. im_dir = hub_dir / 'images'
  795. im_dir.mkdir(parents=True, exist_ok=True)
  796. for _ in tqdm(ThreadPool(NUM_THREADS).imap(hub_ops, dataset.img_files), total=dataset.n, desc='HUB Ops'):
  797. pass
  798. # Profile
  799. stats_path = hub_dir / 'stats.json'
  800. if profile:
  801. for _ in range(1):
  802. file = stats_path.with_suffix('.npy')
  803. t1 = time.time()
  804. np.save(file, stats)
  805. t2 = time.time()
  806. x = np.load(file, allow_pickle=True)
  807. print(f'stats.npy times: {time.time() - t2:.3f}s read, {t2 - t1:.3f}s write')
  808. file = stats_path.with_suffix('.json')
  809. t1 = time.time()
  810. with open(file, 'w') as f:
  811. json.dump(stats, f) # save stats *.json
  812. t2 = time.time()
  813. with open(file, 'r') as f:
  814. x = json.load(f) # load hyps dict
  815. print(f'stats.json times: {time.time() - t2:.3f}s read, {t2 - t1:.3f}s write')
  816. # Save, print and return
  817. if hub:
  818. print(f'Saving {stats_path.resolve()}...')
  819. with open(stats_path, 'w') as f:
  820. json.dump(stats, f) # save stats.json
  821. if verbose:
  822. print(json.dumps(stats, indent=2, sort_keys=False))
  823. return stats