update datasets.py

This commit is contained in:
Glenn Jocher 2020-06-20 09:58:48 -07:00
parent dc06836968
commit 183e3833d2
1 changed files with 19 additions and 16 deletions

View File

@ -18,7 +18,7 @@ from utils.utils import xyxy2xywh, xywh2xyxy
help_url = 'https://github.com/ultralytics/yolov3/wiki/Train-Custom-Data' help_url = 'https://github.com/ultralytics/yolov3/wiki/Train-Custom-Data'
img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng'] img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng']
vid_formats = ['.mov', '.avi', '.mp4'] vid_formats = ['.mov', '.avi', '.mp4', '.mpg', '.mpeg', '.m4v', '.wmv', '.mkv']
# Get orientation exif tag # Get orientation exif tag
for orientation in ExifTags.TAGS.keys(): for orientation in ExifTags.TAGS.keys():
@ -63,7 +63,8 @@ class LoadImages: # for inference
self.new_video(videos[0]) # new video self.new_video(videos[0]) # new video
else: else:
self.cap = None self.cap = None
assert self.nF > 0, 'No images or videos found in ' + path assert self.nF > 0, 'No images or videos found in %s. Supported formats are:\nimages: %s\nvideos: %s' % \
(path, img_formats, vid_formats)
def __iter__(self): def __iter__(self):
self.count = 0 self.count = 0
@ -257,7 +258,7 @@ class LoadStreams: # multiple IP or RTSP cameras
class LoadImagesAndLabels(Dataset): # for training/testing class LoadImagesAndLabels(Dataset): # for training/testing
def __init__(self, path, img_size=416, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, def __init__(self, path, img_size=416, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
cache_images=False, single_cls=False): cache_images=False, single_cls=False, pad=0.0):
try: try:
path = str(Path(path)) # os-agnostic path = str(Path(path)) # os-agnostic
parent = str(Path(path).parent) + os.sep parent = str(Path(path).parent) + os.sep
@ -291,8 +292,6 @@ class LoadImagesAndLabels(Dataset): # for training/testing
self.label_files = [x.replace('images', 'labels').replace(os.path.splitext(x)[-1], '.txt') self.label_files = [x.replace('images', 'labels').replace(os.path.splitext(x)[-1], '.txt')
for x in self.img_files] for x in self.img_files]
# Rectangular Training https://github.com/ultralytics/yolov3/issues/232
if self.rect:
# Read image shapes (wh) # Read image shapes (wh)
sp = path.replace('.txt', '') + '.shapes' # shapefile path sp = path.replace('.txt', '') + '.shapes' # shapefile path
try: try:
@ -303,8 +302,12 @@ class LoadImagesAndLabels(Dataset): # for training/testing
s = [exif_size(Image.open(f)) for f in tqdm(self.img_files, desc='Reading image shapes')] s = [exif_size(Image.open(f)) for f in tqdm(self.img_files, desc='Reading image shapes')]
np.savetxt(sp, s, fmt='%g') # overwrites existing (if any) np.savetxt(sp, s, fmt='%g') # overwrites existing (if any)
self.shapes = np.array(s, dtype=np.float64)
# Rectangular Training https://github.com/ultralytics/yolov3/issues/232
if self.rect:
# Sort by aspect ratio # Sort by aspect ratio
s = np.array(s, dtype=np.float64) s = self.shapes # wh
ar = s[:, 1] / s[:, 0] # aspect ratio ar = s[:, 1] / s[:, 0] # aspect ratio
irect = ar.argsort() irect = ar.argsort()
self.img_files = [self.img_files[i] for i in irect] self.img_files = [self.img_files[i] for i in irect]
@ -322,7 +325,7 @@ class LoadImagesAndLabels(Dataset): # for training/testing
elif mini > 1: elif mini > 1:
shapes[i] = [1, 1 / mini] shapes[i] = [1, 1 / mini]
self.batch_shapes = np.ceil(np.array(shapes) * img_size / 64.).astype(np.int) * 64 self.batch_shapes = np.ceil(np.array(shapes) * img_size / 32. + pad).astype(np.int) * 32
# Cache labels # Cache labels
self.imgs = [None] * n self.imgs = [None] * n
@ -530,7 +533,7 @@ def load_image(self, index):
assert img is not None, 'Image Not Found ' + path assert img is not None, 'Image Not Found ' + path
h0, w0 = img.shape[:2] # orig hw h0, w0 = img.shape[:2] # orig hw
r = self.img_size / max(h0, w0) # resize image to img_size r = self.img_size / max(h0, w0) # resize image to img_size
if r < 1 or (self.augment and r != 1): # always resize down, only resize up if training with augmentation if r != 1: # always resize down, only resize up if training with augmentation
interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR 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) img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp)
return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized