greenhouse/utils/datasets.py

1035 lines
41 KiB
Python
Raw Normal View History

# Dataset utils and dataloaders
2018-08-26 10:51:39 +02:00
import glob
import logging
2018-08-26 10:51:39 +02:00
import math
import os
import random
import shutil
2019-09-10 01:34:23 +02:00
import time
from itertools import repeat
from multiprocessing.pool import ThreadPool
from pathlib import Path
2019-09-10 01:34:23 +02:00
from threading import Thread
2018-08-26 10:51:39 +02:00
import cv2
import numpy as np
import torch
2021-01-12 23:05:32 -08:00
import torch.nn.functional as F
2019-07-25 13:19:26 +02:00
from PIL import Image, ExifTags
2019-03-21 22:41:12 +02:00
from torch.utils.data import Dataset
from tqdm import tqdm
2018-08-26 10:51:39 +02:00
2021-01-12 23:05:32 -08:00
from utils.general import xyxy2xywh, xywh2xyxy, clean_str
from utils.torch_utils import torch_distributed_zero_first
2018-08-26 10:51:39 +02:00
# Parameters
2019-12-15 12:15:56 -08:00
help_url = 'https://github.com/ultralytics/yolov3/wiki/Train-Custom-Data'
img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng'] # acceptable image suffixes
vid_formats = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes
logger = logging.getLogger(__name__)
2019-07-04 14:03:13 +02:00
2019-07-09 20:56:58 +02:00
# Get orientation exif tag
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
def get_hash(files):
# Returns a single hash value of a list of files
return sum(os.path.getsize(f) for f in files if os.path.isfile(f))
2019-07-09 20:56:58 +02:00
def exif_size(img):
# Returns exif-corrected PIL size
s = img.size # (width, height)
try:
rotation = dict(img._getexif().items())[orientation]
if rotation == 6: # rotation 270
s = (s[1], s[0])
elif rotation == 8: # rotation 90
s = (s[1], s[0])
except:
2019-09-10 21:25:01 +02:00
pass
2019-07-09 20:56:58 +02:00
return s
2018-08-26 10:51:39 +02:00
def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False,
2021-01-12 23:05:32 -08:00
rank=-1, world_size=1, workers=8, image_weights=False, quad=False, prefix=''):
# Make sure only the first process in DDP process the dataset first, and the following others can use the cache
with torch_distributed_zero_first(rank):
dataset = LoadImagesAndLabels(path, imgsz, batch_size,
augment=augment, # augment images
hyp=hyp, # augmentation hyperparameters
rect=rect, # rectangular training
cache_images=cache,
single_cls=opt.single_cls,
stride=int(stride),
pad=pad,
2021-01-12 23:05:32 -08:00
image_weights=image_weights,
prefix=prefix)
batch_size = min(batch_size, len(dataset))
nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers
sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None
loader = torch.utils.data.DataLoader if image_weights else InfiniteDataLoader
# Use torch.utils.data.DataLoader() if dataset.properties will update during training else InfiniteDataLoader()
dataloader = loader(dataset,
batch_size=batch_size,
num_workers=nw,
sampler=sampler,
pin_memory=True,
2021-01-12 23:05:32 -08:00
collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn)
return dataloader, dataset
class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
""" Dataloader that reuses workers
Uses same syntax as vanilla DataLoader
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler))
self.iterator = super().__iter__()
def __len__(self):
return len(self.batch_sampler.sampler)
def __iter__(self):
for i in range(len(self)):
yield next(self.iterator)
class _RepeatSampler(object):
""" Sampler that repeats forever
Args:
sampler (Sampler)
"""
def __init__(self, sampler):
self.sampler = sampler
def __iter__(self):
while True:
yield from iter(self.sampler)
2019-02-11 12:44:12 +01:00
class LoadImages: # for inference
def __init__(self, path, img_size=640):
p = str(Path(path)) # os-agnostic
p = os.path.abspath(p) # absolute path
if '*' in p:
files = sorted(glob.glob(p, recursive=True)) # glob
elif os.path.isdir(p):
files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
elif os.path.isfile(p):
files = [p] # files
else:
2021-01-12 23:05:32 -08:00
raise Exception(f'ERROR: {p} does not exist')
images = [x for x in files if x.split('.')[-1].lower() in img_formats]
videos = [x for x in files if x.split('.')[-1].lower() in vid_formats]
ni, nv = len(images), len(videos)
2019-04-02 15:09:13 +02:00
2019-07-25 18:18:40 +02:00
self.img_size = img_size
2019-04-02 13:43:18 +02:00
self.files = images + videos
self.nf = ni + nv # number of files
self.video_flag = [False] * ni + [True] * nv
2021-01-12 23:05:32 -08:00
self.mode = 'image'
2019-04-02 13:43:18 +02:00
if any(videos):
self.new_video(videos[0]) # new video
else:
self.cap = None
2021-01-12 23:05:32 -08:00
assert self.nf > 0, f'No images or videos found in {p}. ' \
f'Supported formats are:\nimages: {img_formats}\nvideos: {vid_formats}'
2018-08-26 10:51:39 +02:00
def __iter__(self):
2019-04-02 13:43:18 +02:00
self.count = 0
2018-08-26 10:51:39 +02:00
return self
def __next__(self):
if self.count == self.nf:
2018-08-26 10:51:39 +02:00
raise StopIteration
2019-04-02 13:43:18 +02:00
path = self.files[self.count]
if self.video_flag[self.count]:
2019-04-02 15:09:13 +02:00
# Read video
2019-04-02 13:43:18 +02:00
self.mode = 'video'
ret_val, img0 = self.cap.read()
if not ret_val:
self.count += 1
self.cap.release()
if self.count == self.nf: # last video
2019-04-02 13:43:18 +02:00
raise StopIteration
else:
path = self.files[self.count]
self.new_video(path)
ret_val, img0 = self.cap.read()
self.frame += 1
2021-01-12 23:05:32 -08:00
print(f'video {self.count + 1}/{self.nf} ({self.frame}/{self.nframes}) {path}: ', end='')
2019-04-02 13:43:18 +02:00
else:
# Read image
self.count += 1
img0 = cv2.imread(path) # BGR
2019-08-06 01:27:27 +02:00
assert img0 is not None, 'Image Not Found ' + path
2021-01-12 23:05:32 -08:00
print(f'image {self.count}/{self.nf} {path}: ', end='')
2018-08-26 10:51:39 +02:00
# Padded resize
2019-10-08 13:25:50 +02:00
img = letterbox(img0, new_shape=self.img_size)[0]
2018-08-26 10:51:39 +02:00
2019-12-08 17:00:13 -08:00
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
2020-02-22 18:18:38 -08:00
img = np.ascontiguousarray(img)
2018-08-26 10:51:39 +02:00
2019-04-02 13:43:18 +02:00
return path, img, img0, self.cap
def new_video(self, path):
self.frame = 0
self.cap = cv2.VideoCapture(path)
self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
2018-08-26 10:51:39 +02:00
def __len__(self):
return self.nf # number of files
2018-08-26 10:51:39 +02:00
2019-02-11 13:45:04 +01:00
class LoadWebcam: # for inference
def __init__(self, pipe='0', img_size=640):
2019-07-25 18:18:40 +02:00
self.img_size = img_size
2019-08-11 17:47:44 +02:00
if pipe.isnumeric():
pipe = eval(pipe) # local camera
2019-08-11 17:47:44 +02:00
# pipe = 'rtsp://192.168.1.64/1' # IP camera
# pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login
2019-08-29 17:41:23 +02:00
# pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera
2019-08-11 19:16:54 +02:00
2019-08-29 17:41:23 +02:00
self.pipe = pipe
2019-08-11 17:47:44 +02:00
self.cap = cv2.VideoCapture(pipe) # video capture object
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size
2019-02-11 13:45:04 +01:00
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
2019-09-10 15:34:36 +02:00
if cv2.waitKey(1) == ord('q'): # q to quit
self.cap.release()
2019-02-11 13:45:04 +01:00
cv2.destroyAllWindows()
raise StopIteration
2019-08-29 17:41:23 +02:00
# Read frame
if self.pipe == 0: # local camera
ret_val, img0 = self.cap.read()
img0 = cv2.flip(img0, 1) # flip left-right
else: # IP camera
n = 0
while True:
n += 1
self.cap.grab()
if n % 30 == 0: # skip frames
ret_val, img0 = self.cap.retrieve()
if ret_val:
break
# Print
2021-01-12 23:05:32 -08:00
assert ret_val, f'Camera Error {self.pipe}'
2019-09-10 15:34:36 +02:00
img_path = 'webcam.jpg'
2021-01-12 23:05:32 -08:00
print(f'webcam {self.count}: ', end='')
2019-02-11 13:45:04 +01:00
# Padded resize
2019-10-08 13:25:50 +02:00
img = letterbox(img0, new_shape=self.img_size)[0]
2019-02-11 13:45:04 +01:00
2019-12-08 17:00:13 -08:00
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
2020-02-22 18:18:38 -08:00
img = np.ascontiguousarray(img)
2019-02-11 13:45:04 +01:00
2019-04-10 16:39:15 +02:00
return img_path, img, img0, None
2019-02-11 13:45:04 +01:00
def __len__(self):
2019-02-11 18:15:51 +01:00
return 0
2019-02-11 13:45:04 +01:00
2019-09-10 01:34:23 +02:00
class LoadStreams: # multiple IP or RTSP cameras
def __init__(self, sources='streams.txt', img_size=640):
2021-01-12 23:05:32 -08:00
self.mode = 'stream'
2019-09-10 01:34:23 +02:00
self.img_size = img_size
2019-09-19 18:43:29 +02:00
if os.path.isfile(sources):
with open(sources, 'r') as f:
2020-11-29 12:01:42 +01:00
sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
2019-09-19 19:09:59 +02:00
else:
sources = [sources]
2019-09-10 01:34:23 +02:00
n = len(sources)
self.imgs = [None] * n
2021-01-12 23:05:32 -08:00
self.sources = [clean_str(x) for x in sources] # clean source names for later
2019-09-10 01:34:23 +02:00
for i, s in enumerate(sources):
# Start the thread to read frames from the video stream
2021-01-12 23:05:32 -08:00
print(f'{i + 1}/{n}: {s}... ', end='')
cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
2021-01-12 23:05:32 -08:00
assert cap.isOpened(), f'Failed to open {s}'
2019-09-10 15:44:14 +02:00
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
2019-09-10 01:34:23 +02:00
fps = cap.get(cv2.CAP_PROP_FPS) % 100
2019-09-25 00:38:26 +02:00
_, self.imgs[i] = cap.read() # guarantee first frame
2019-09-10 15:44:14 +02:00
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
2021-01-12 23:05:32 -08:00
print(f' success ({w}x{h} at {fps:.2f} FPS).')
2019-09-10 01:34:23 +02:00
thread.start()
print('') # newline
2020-01-16 13:25:18 -08:00
# check for common shapes
s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
if not self.rect:
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
2019-09-10 01:34:23 +02:00
def update(self, index, cap):
# Read next stream frame in a daemon thread
2019-09-28 23:09:06 +02:00
n = 0
2019-09-10 01:34:23 +02:00
while cap.isOpened():
2019-09-28 23:09:06 +02:00
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n == 4: # read every 4th frame
_, self.imgs[index] = cap.retrieve()
n = 0
time.sleep(0.01) # wait time
2019-09-10 01:34:23 +02:00
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
img0 = self.imgs.copy()
2019-09-10 15:34:36 +02:00
if cv2.waitKey(1) == ord('q'): # q to quit
2019-09-10 01:34:23 +02:00
cv2.destroyAllWindows()
raise StopIteration
# Letterbox
img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0]
2019-09-10 01:34:23 +02:00
# Stack
img = np.stack(img, 0)
2019-12-08 17:00:13 -08:00
# Convert
2020-02-22 18:18:38 -08:00
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
img = np.ascontiguousarray(img)
2019-09-10 01:34:23 +02:00
return self.sources, img, img0, None
def __len__(self):
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
def img2label_paths(img_paths):
# Define label paths as a function of image paths
sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings
return [x.replace(sa, sb, 1).replace('.' + x.split('.')[-1], '.txt') for x in img_paths]
2019-05-21 17:37:34 +02:00
class LoadImagesAndLabels(Dataset): # for training/testing
def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
2021-01-12 23:05:32 -08:00
cache_images=False, single_cls=False, stride=32, pad=0.0, prefix=''):
2019-03-19 10:38:32 +02:00
self.img_size = img_size
2018-09-02 11:38:39 +02:00
self.augment = augment
2019-07-20 14:54:37 +02:00
self.hyp = hyp
2019-05-11 14:38:48 +02:00
self.image_weights = image_weights
self.rect = False if image_weights else rect
2020-03-10 13:33:14 -07:00
self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training)
self.mosaic_border = [-img_size // 2, -img_size // 2]
self.stride = stride
2019-07-04 14:03:13 +02:00
2020-06-20 09:58:48 -07:00
try:
f = [] # image files
for p in path if isinstance(path, list) else [path]:
p = Path(p) # os-agnostic
if p.is_dir(): # dir
f += glob.glob(str(p / '**' / '*.*'), recursive=True)
elif p.is_file(): # file
with open(p, 'r') as t:
2020-11-29 12:01:42 +01:00
t = t.read().strip().splitlines()
parent = str(p.parent) + os.sep
f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
else:
2021-01-12 23:05:32 -08:00
raise Exception(f'{prefix}{p} does not exist')
self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats])
2021-01-12 23:05:32 -08:00
assert self.img_files, f'{prefix}No images found'
except Exception as e:
2021-01-12 23:05:32 -08:00
raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}')
# Check cache
self.label_files = img2label_paths(self.img_files) # labels
cache_path = Path(self.label_files[0]).parent.with_suffix('.cache') # cached labels
if cache_path.is_file():
cache = torch.load(cache_path) # load
if cache['hash'] != get_hash(self.label_files + self.img_files) or 'results' not in cache: # changed
2021-01-12 23:05:32 -08:00
cache = self.cache_labels(cache_path, prefix) # re-cache
else:
2021-01-12 23:05:32 -08:00
cache = self.cache_labels(cache_path, prefix) # cache
# Display cache
[nf, nm, ne, nc, n] = cache.pop('results') # found, missing, empty, corrupted, total
desc = f"Scanning '{cache_path}' for images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupted"
2021-01-12 23:05:32 -08:00
tqdm(None, desc=prefix + desc, total=n, initial=n)
assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {help_url}'
# Read cache
cache.pop('hash') # remove hash
labels, shapes = zip(*cache.values())
self.labels = list(labels)
self.shapes = np.array(shapes, dtype=np.float64)
self.img_files = list(cache.keys()) # update
self.label_files = img2label_paths(cache.keys()) # update
if single_cls:
for x in self.labels:
x[:, 0] = 0
n = len(shapes) # number of images
bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index
nb = bi[-1] + 1 # number of batches
self.batch = bi # batch index of image
self.n = n
self.indices = range(n)
2020-06-20 09:58:48 -07:00
# Rectangular Training
2019-05-10 14:15:09 +02:00
if self.rect:
2019-04-24 21:23:54 +02:00
# Sort by aspect ratio
2020-06-20 09:58:48 -07:00
s = self.shapes # wh
2019-05-05 14:13:05 +02:00
ar = s[:, 1] / s[:, 0] # aspect ratio
2020-05-28 14:01:38 -07:00
irect = ar.argsort()
self.img_files = [self.img_files[i] for i in irect]
self.label_files = [self.label_files[i] for i in irect]
self.labels = [self.labels[i] for i in irect]
2020-05-28 14:01:38 -07:00
self.shapes = s[irect] # wh
ar = ar[irect]
2019-04-24 21:23:54 +02:00
# Set training image shapes
shapes = [[1, 1]] * nb
for i in range(nb):
ari = ar[bi == i]
mini, maxi = ari.min(), ari.max()
if maxi < 1:
shapes[i] = [maxi, 1]
elif mini > 1:
shapes[i] = [1, 1 / mini]
self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride
2019-04-27 17:44:26 +02:00
2020-01-10 11:45:51 -08:00
# Cache images into memory for faster training (WARNING: large datasets may exceed system RAM)
self.imgs = [None] * n
if cache_images:
2019-12-04 23:02:32 -08:00
gb = 0 # Gigabytes of cached images
2020-01-10 11:45:51 -08:00
self.img_hw0, self.img_hw = [None] * n, [None] * n
results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n))) # 8 threads
pbar = tqdm(enumerate(results), total=n)
for i, x in pbar:
self.imgs[i], self.img_hw0[i], self.img_hw[i] = x # img, hw_original, hw_resized = load_image(self, i)
2019-12-04 23:02:32 -08:00
gb += self.imgs[i].nbytes
2021-01-12 23:05:32 -08:00
pbar.desc = f'{prefix}Caching images ({gb / 1E9:.1f}GB)'
2019-08-06 17:24:30 +02:00
2021-01-12 23:05:32 -08:00
def cache_labels(self, path=Path('./labels.cache'), prefix=''):
# Cache dataset labels, check images and read shapes
x = {} # dict
nm, nf, ne, nc = 0, 0, 0, 0 # number missing, found, empty, duplicate
pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files))
for i, (im_file, lb_file) in enumerate(pbar):
try:
# verify images
im = Image.open(im_file)
im.verify() # PIL verify
shape = exif_size(im) # image size
assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels'
# verify labels
if os.path.isfile(lb_file):
nf += 1 # label found
with open(lb_file, 'r') as f:
2020-11-29 12:01:42 +01:00
l = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
if len(l):
assert l.shape[1] == 5, 'labels require 5 columns each'
assert (l >= 0).all(), 'negative labels'
assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels'
assert np.unique(l, axis=0).shape[0] == l.shape[0], 'duplicate labels'
else:
ne += 1 # label empty
l = np.zeros((0, 5), dtype=np.float32)
else:
nm += 1 # label missing
l = np.zeros((0, 5), dtype=np.float32)
x[im_file] = [l, shape]
except Exception as e:
nc += 1
2021-01-12 23:05:32 -08:00
print(f'{prefix}WARNING: Ignoring corrupted image and/or label {im_file}: {e}')
2021-01-12 23:05:32 -08:00
pbar.desc = f"{prefix}Scanning '{path.parent / path.stem}' for images and labels... " \
f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
if nf == 0:
2021-01-12 23:05:32 -08:00
print(f'{prefix}WARNING: No labels found in {path}. See {help_url}')
x['hash'] = get_hash(self.label_files + self.img_files)
x['results'] = [nf, nm, ne, nc, i + 1]
torch.save(x, path) # save for next time
2021-01-12 23:05:32 -08:00
logging.info(f'{prefix}New cache created: {path}')
return x
2019-07-09 14:18:19 +02:00
2019-03-21 22:41:12 +02:00
def __len__(self):
return len(self.img_files)
2018-08-26 10:51:39 +02:00
2019-05-21 17:37:34 +02:00
# def __iter__(self):
# self.count = -1
# print('ran dataset iter')
# #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF)
# return self
2019-03-21 01:57:16 +02:00
def __getitem__(self, index):
index = self.indices[index] # linear, shuffled, or image_weights
2019-05-10 14:15:09 +02:00
2019-12-02 15:26:36 -08:00
hyp = self.hyp
mosaic = self.mosaic and random.random() < hyp['mosaic']
if mosaic:
2019-10-08 13:25:50 +02:00
# Load mosaic
img, labels = load_mosaic(self, index)
2020-01-10 13:41:47 -08:00
shapes = None
2019-03-21 22:41:12 +02:00
# MixUp https://arxiv.org/pdf/1710.09412.pdf
if random.random() < hyp['mixup']:
img2, labels2 = load_mosaic(self, random.randint(0, self.n - 1))
r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0
img = (img * r + img2 * (1 - r)).astype(np.uint8)
labels = np.concatenate((labels, labels2), 0)
2019-04-24 21:23:54 +02:00
else:
2019-10-08 13:25:50 +02:00
# Load image
2020-01-10 11:45:51 -08:00
img, (h0, w0), (h, w) = load_image(self, index)
2019-10-08 13:25:50 +02:00
# Letterbox
2019-11-30 17:13:21 -08:00
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
2019-11-30 18:52:37 -08:00
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
2020-01-10 13:41:47 -08:00
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
2019-10-08 13:25:50 +02:00
# Load labels
labels = []
2020-04-07 16:57:22 -07:00
x = self.labels[index]
2020-04-28 11:07:26 -07:00
if x.size > 0:
2020-04-07 16:57:22 -07:00
# Normalized xywh to pixel xyxy format
labels = x.copy()
labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width
labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height
labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0]
labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1]
2019-03-21 22:41:12 +02:00
if self.augment:
2019-10-08 13:25:50 +02:00
# Augment imagespace
if not mosaic:
img, labels = random_perspective(img, labels,
degrees=hyp['degrees'],
translate=hyp['translate'],
scale=hyp['scale'],
shear=hyp['shear'],
perspective=hyp['perspective'])
2019-12-02 15:26:36 -08:00
# Augment colorspace
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
2019-03-21 22:41:12 +02:00
2019-10-08 13:25:50 +02:00
# Apply cutouts
# if random.random() < 0.9:
# labels = cutout(img, labels)
2019-09-20 14:58:57 +02:00
nL = len(labels) # number of labels
if nL:
labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh
labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1
labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1
2019-03-21 22:41:12 +02:00
if self.augment:
# flip up-down
if random.random() < hyp['flipud']:
2019-03-21 22:41:12 +02:00
img = np.flipud(img)
if nL:
2019-03-21 22:41:12 +02:00
labels[:, 2] = 1 - labels[:, 2]
# flip left-right
if random.random() < hyp['fliplr']:
img = np.fliplr(img)
if nL:
labels[:, 1] = 1 - labels[:, 1]
labels_out = torch.zeros((nL, 6))
if nL:
labels_out[:, 1:] = torch.from_numpy(labels)
2019-03-21 14:48:40 +02:00
2019-12-08 17:00:13 -08:00
# Convert
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
2019-12-08 17:52:44 -08:00
img = np.ascontiguousarray(img)
2018-08-26 10:51:39 +02:00
2020-04-07 16:57:22 -07:00
return torch.from_numpy(img), labels_out, self.img_files[index], shapes
@staticmethod
def collate_fn(batch):
2020-01-11 13:13:57 -08:00
img, label, path, shapes = zip(*batch) # transposed
for i, l in enumerate(label):
l[:, 0] = i # add target image index for build_targets()
2019-11-30 18:45:43 -08:00
return torch.stack(img, 0), torch.cat(label, 0), path, shapes
2018-08-26 10:51:39 +02:00
2021-01-12 23:05:32 -08:00
@staticmethod
def collate_fn4(batch):
img, label, path, shapes = zip(*batch) # transposed
n = len(shapes) // 4
img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
ho = torch.tensor([[0., 0, 0, 1, 0, 0]])
wo = torch.tensor([[0., 0, 1, 0, 0, 0]])
s = torch.tensor([[1, 1, .5, .5, .5, .5]]) # scale
for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
i *= 4
if random.random() < 0.5:
im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2., mode='bilinear', align_corners=False)[
0].type(img[i].type())
l = label[i]
else:
im = torch.cat((torch.cat((img[i], img[i + 1]), 1), torch.cat((img[i + 2], img[i + 3]), 1)), 2)
l = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s
img4.append(im)
label4.append(l)
for i, l in enumerate(label4):
l[:, 0] = i # add target image index for build_targets()
return torch.stack(img4, 0), torch.cat(label4, 0), path4, shapes4
2018-08-26 10:51:39 +02:00
# Ancillary functions --------------------------------------------------------------------------------------------------
2019-10-08 13:25:50 +02:00
def load_image(self, index):
2020-01-10 11:45:51 -08:00
# loads 1 image from dataset, returns img, original hw, resized hw
2020-01-10 11:55:54 -08:00
img = self.imgs[index]
if img is None: # not cached
2020-04-28 11:07:26 -07:00
path = self.img_files[index]
img = cv2.imread(path) # BGR
assert img is not None, 'Image Not Found ' + path
2020-01-10 11:45:51 -08:00
h0, w0 = img.shape[:2] # orig hw
r = self.img_size / max(h0, w0) # resize image to img_size
2020-06-20 09:58:48 -07:00
if r != 1: # always resize down, only resize up if training with augmentation
2020-04-10 18:58:34 -07:00
interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR
img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp)
2020-01-10 11:55:54 -08:00
return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized
else:
return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized
2019-10-08 13:25:50 +02:00
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
dtype = img.dtype # uint8
x = np.arange(0, 256, dtype=np.int16)
lut_hue = ((x * r[0]) % 180).astype(dtype)
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
2019-10-08 13:25:50 +02:00
cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
# Histogram equalization
2020-04-10 17:29:57 -07:00
# if random.random() < 0.2:
# for i in range(3):
# img[:, :, i] = cv2.equalizeHist(img[:, :, i])
2019-10-08 13:25:50 +02:00
def load_mosaic(self, index):
2021-01-12 23:05:32 -08:00
# loads images in a 4-mosaic
2019-10-08 13:25:50 +02:00
labels4 = []
s = self.img_size
yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
indices = [index] + [self.indices[random.randint(0, self.n - 1)] for _ in range(3)] # 3 additional image indices
2019-10-08 13:25:50 +02:00
for i, index in enumerate(indices):
# Load image
2020-01-10 11:45:51 -08:00
img, _, (h, w) = load_image(self, index)
2019-10-08 13:25:50 +02:00
2019-10-08 14:08:23 +02:00
# place img in img4
2019-10-08 13:25:50 +02:00
if i == 0: # top left
2020-04-10 16:28:59 -07:00
img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
2019-10-08 13:25:50 +02:00
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
elif i == 1: # top right
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
elif i == 2: # bottom left
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
2019-10-08 13:25:50 +02:00
elif i == 3: # bottom right
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
padw = x1a - x1b
padh = y1a - y1b
2020-04-28 11:07:26 -07:00
# Labels
x = self.labels[index]
labels = x.copy()
if x.size > 0: # Normalized xywh to pixel xyxy format
labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw
labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh
labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw
labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh
labels4.append(labels)
2019-11-07 14:46:16 -08:00
2019-12-02 15:26:36 -08:00
# Concat/clip labels
2019-10-13 18:39:32 +02:00
if len(labels4):
labels4 = np.concatenate(labels4, 0)
np.clip(labels4[:, 1:], 0, 2 * s, out=labels4[:, 1:]) # use with random_perspective
# img4, labels4 = replicate(img4, labels4) # replicate
2019-12-04 11:19:17 -08:00
# Augment
img4, labels4 = random_perspective(img4, labels4,
degrees=self.hyp['degrees'],
translate=self.hyp['translate'],
scale=self.hyp['scale'],
shear=self.hyp['shear'],
perspective=self.hyp['perspective'],
border=self.mosaic_border) # border to remove
2019-10-08 13:25:50 +02:00
return img4, labels4
2021-01-12 23:05:32 -08:00
def load_mosaic9(self, index):
# loads images in a 9-mosaic
labels9 = []
s = self.img_size
indices = [index] + [self.indices[random.randint(0, self.n - 1)] for _ in range(8)] # 8 additional image indices
for i, index in enumerate(indices):
# Load image
img, _, (h, w) = load_image(self, index)
# place img in img9
if i == 0: # center
img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
h0, w0 = h, w
c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates
elif i == 1: # top
c = s, s - h, s + w, s
elif i == 2: # top right
c = s + wp, s - h, s + wp + w, s
elif i == 3: # right
c = s + w0, s, s + w0 + w, s + h
elif i == 4: # bottom right
c = s + w0, s + hp, s + w0 + w, s + hp + h
elif i == 5: # bottom
c = s + w0 - w, s + h0, s + w0, s + h0 + h
elif i == 6: # bottom left
c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h
elif i == 7: # left
c = s - w, s + h0 - h, s, s + h0
elif i == 8: # top left
c = s - w, s + h0 - hp - h, s, s + h0 - hp
padx, pady = c[:2]
x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords
# Labels
x = self.labels[index]
labels = x.copy()
if x.size > 0: # Normalized xywh to pixel xyxy format
labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padx
labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + pady
labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padx
labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + pady
labels9.append(labels)
# Image
img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax]
hp, wp = h, w # height, width previous
# Offset
yc, xc = [int(random.uniform(0, s)) for x in self.mosaic_border] # mosaic center x, y
img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s]
# Concat/clip labels
if len(labels9):
labels9 = np.concatenate(labels9, 0)
labels9[:, [1, 3]] -= xc
labels9[:, [2, 4]] -= yc
np.clip(labels9[:, 1:], 0, 2 * s, out=labels9[:, 1:]) # use with random_perspective
# img9, labels9 = replicate(img9, labels9) # replicate
# Augment
img9, labels9 = random_perspective(img9, labels9,
degrees=self.hyp['degrees'],
translate=self.hyp['translate'],
scale=self.hyp['scale'],
shear=self.hyp['shear'],
perspective=self.hyp['perspective'],
border=self.mosaic_border) # border to remove
return img9, labels9
def replicate(img, labels):
# Replicate labels
h, w = img.shape[:2]
boxes = labels[:, 1:].astype(int)
x1, y1, x2, y2 = boxes.T
s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
x1b, y1b, x2b, y2b = boxes[i]
bh, bw = y2b - y1b, x2b - x1b
yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
img[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
return img, labels
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
2019-11-30 17:19:44 -08:00
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
2019-04-24 21:23:54 +02:00
shape = img.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
2019-11-30 16:58:56 -08:00
new_shape = (new_shape, new_shape)
2019-11-30 17:38:29 -08:00
# Scale ratio (new / old)
2020-04-18 12:07:44 -07:00
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
2019-11-30 18:52:37 -08:00
if not scaleup: # only scale down, do not scale up (for better test mAP)
2019-11-30 17:38:29 -08:00
r = min(r, 1.0)
2019-04-24 21:23:54 +02:00
2019-11-30 16:58:56 -08:00
# Compute padding
2019-11-30 17:38:29 -08:00
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
2019-11-30 16:58:56 -08:00
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
2019-11-30 17:13:21 -08:00
if auto: # minimum rectangle
dw, dh = np.mod(dw, 32), np.mod(dh, 32) # wh padding
2019-11-30 17:13:21 -08:00
elif scaleFill: # stretch
2019-06-21 21:27:50 +02:00
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
2019-11-30 16:58:56 -08:00
dw /= 2 # divide padding into 2 sides
dh /= 2
2019-04-21 20:35:11 +02:00
2019-08-01 01:47:05 +02:00
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
2019-04-21 20:35:11 +02:00
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
2019-07-23 13:47:30 +02:00
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
2019-11-30 16:58:56 -08:00
return img, ratio, (dw, dh)
2019-02-10 20:32:04 +01:00
2018-08-26 10:51:39 +02:00
def random_perspective(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0, border=(0, 0)):
2018-08-26 10:51:39 +02:00
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
2020-05-17 14:30:12 -07:00
# targets = [cls, xyxy]
2018-08-26 10:51:39 +02:00
height = img.shape[0] + border[0] * 2 # shape(h,w,c)
width = img.shape[1] + border[1] * 2
# Center
C = np.eye(3)
C[0, 2] = -img.shape[1] / 2 # x translation (pixels)
C[1, 2] = -img.shape[0] / 2 # y translation (pixels)
# Perspective
P = np.eye(3)
P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
2018-08-26 10:51:39 +02:00
# Rotation and Scale
R = np.eye(3)
2019-07-20 14:54:37 +02:00
a = random.uniform(-degrees, degrees)
2019-07-20 14:04:50 +02:00
# a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
2019-07-20 14:54:37 +02:00
s = random.uniform(1 - scale, 1 + scale)
2020-05-17 14:30:12 -07:00
# s = 2 ** random.uniform(-scale, scale)
R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
2018-08-26 10:51:39 +02:00
# Shear
S = np.eye(3)
2019-07-20 14:54:37 +02:00
S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
2018-08-26 10:51:39 +02:00
# Translation
T = np.eye(3)
T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
2019-10-13 17:40:45 +02:00
# Combined rotation matrix
M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
if perspective:
img = cv2.warpPerspective(img, M, dsize=(width, height), borderValue=(114, 114, 114))
else: # affine
img = cv2.warpAffine(img, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
# Visualize
# import matplotlib.pyplot as plt
# ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
# ax[0].imshow(img[:, :, ::-1]) # base
# ax[1].imshow(img2[:, :, ::-1]) # warped
2019-10-13 17:40:45 +02:00
# Transform label coordinates
n = len(targets)
if n:
# warp points
xy = np.ones((n * 4, 3))
2019-10-13 17:40:45 +02:00
xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
xy = xy @ M.T # transform
if perspective:
xy = (xy[:, :2] / xy[:, 2:3]).reshape(n, 8) # rescale
else: # affine
xy = xy[:, :2].reshape(n, 8)
# create new boxes
x = xy[:, [0, 2, 4, 6]]
y = xy[:, [1, 3, 5, 7]]
xy = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
2019-05-06 16:37:41 +02:00
# # apply angle-based reduction of bounding boxes
# radians = a * math.pi / 180
# reduction = max(abs(math.sin(radians)), abs(math.cos(radians))) ** 0.5
# x = (xy[:, 2] + xy[:, 0]) / 2
# y = (xy[:, 3] + xy[:, 1]) / 2
# w = (xy[:, 2] - xy[:, 0]) * reduction
# h = (xy[:, 3] - xy[:, 1]) * reduction
# xy = np.concatenate((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).reshape(4, n).T
# clip boxes
2019-12-08 13:04:40 -08:00
xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width)
xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height)
# filter candidates
i = box_candidates(box1=targets[:, 1:5].T * s, box2=xy.T)
targets = targets[i]
targets[:, 1:5] = xy[i]
2019-10-13 17:40:45 +02:00
return img, targets
2021-01-12 23:05:32 -08:00
def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
# Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
2021-01-12 23:05:32 -08:00
ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
2019-09-20 14:58:57 +02:00
def cutout(image, labels):
# Applies image cutout augmentation https://arxiv.org/abs/1708.04552
2019-09-20 14:58:57 +02:00
h, w = image.shape[:2]
2020-01-05 06:11:00 -08:00
def bbox_ioa(box1, box2):
2019-09-20 14:58:57 +02:00
# Returns the intersection over box2 area given box1, box2. box1 is 4, box2 is nx4. boxes are x1y1x2y2
box2 = box2.transpose()
# Get the coordinates of bounding boxes
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
# Intersection area
inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \
(np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0)
# box2 area
box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + 1e-16
# Intersection over box2 area
return inter_area / box2_area
2019-10-05 13:47:06 +02:00
# create random masks
2020-02-06 16:13:10 -08:00
scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
2019-10-05 13:47:06 +02:00
for s in scales:
mask_h = random.randint(1, int(h * s))
mask_w = random.randint(1, int(w * s))
# box
xmin = max(0, random.randint(0, w) - mask_w // 2)
ymin = max(0, random.randint(0, h) - mask_h // 2)
xmax = min(w, xmin + mask_w)
ymax = min(h, ymin + mask_h)
# apply random color mask
2020-02-06 16:13:10 -08:00
image[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
2019-10-05 13:47:06 +02:00
# return unobscured labels
2019-10-05 15:28:02 +02:00
if len(labels) and s > 0.03:
2019-10-05 13:47:06 +02:00
box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
2020-02-06 16:13:10 -08:00
labels = labels[ioa < 0.60] # remove >60% obscured labels
2019-10-05 13:47:06 +02:00
2019-09-21 23:55:20 +02:00
return labels
2019-09-20 14:58:57 +02:00
def create_folder(path='./new'):
2019-09-21 02:46:16 +02:00
# Create folder
if os.path.exists(path):
shutil.rmtree(path) # delete output folder
os.makedirs(path) # make new output folder
def flatten_recursive(path='../coco128'):
# Flatten a recursive directory by bringing all files to top level
new_path = Path(path + '_flat')
create_folder(new_path)
for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)):
shutil.copyfile(file, new_path / Path(file).name)
def extract_boxes(path='../coco128/'): # from utils.datasets import *; extract_boxes('../coco128')
# Convert detection dataset into classification dataset, with one directory per class
path = Path(path) # images dir
shutil.rmtree(path / 'classifier') if (path / 'classifier').is_dir() else None # remove existing
files = list(path.rglob('*.*'))
n = len(files) # number of files
for im_file in tqdm(files, total=n):
if im_file.suffix[1:] in img_formats:
# image
im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB
h, w = im.shape[:2]
# labels
lb_file = Path(img2label_paths([str(im_file)])[0])
if Path(lb_file).exists():
with open(lb_file, 'r') as f:
2020-11-29 12:01:42 +01:00
lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
for j, x in enumerate(lb):
c = int(x[0]) # class
f = (path / 'classifier') / f'{c}' / f'{path.stem}_{im_file.stem}_{j}.jpg' # new filename
if not f.parent.is_dir():
f.parent.mkdir(parents=True)
b = x[1:] * [w, h, w, h] # box
# b[2:] = b[2:].max() # rectangle to square
b[2:] = b[2:] * 1.2 + 3 # pad
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int)
b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}'
def autosplit(path='../coco128', weights=(0.9, 0.1, 0.0)): # from utils.datasets import *; autosplit('../coco128')
""" Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
# Arguments
path: Path to images directory
weights: Train, val, test weights (list)
"""
path = Path(path) # images dir
files = list(path.rglob('*.*'))
n = len(files) # number of files
indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split
txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files
[(path / x).unlink() for x in txt if (path / x).exists()] # remove existing
for i, img in tqdm(zip(indices, files), total=n):
if img.suffix[1:] in img_formats:
with open(path / txt[i], 'a') as f:
f.write(str(img) + '\n') # add image to txt file