|
|
@ -291,20 +291,22 @@ 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]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Read image shapes (wh)
|
|
|
|
|
|
|
|
sp = path.replace('.txt', '') + '.shapes' # shapefile path
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
with open(sp, 'r') as f: # read existing shapefile
|
|
|
|
|
|
|
|
s = [x.split() for x in f.read().splitlines()]
|
|
|
|
|
|
|
|
assert len(s) == n, 'Shapefile out of sync'
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.shapes = np.array(s, dtype=np.float64)
|
|
|
|
|
|
|
|
|
|
|
|
# Rectangular Training https://github.com/ultralytics/yolov3/issues/232
|
|
|
|
# Rectangular Training https://github.com/ultralytics/yolov3/issues/232
|
|
|
|
if self.rect:
|
|
|
|
if self.rect:
|
|
|
|
# Read image shapes (wh)
|
|
|
|
|
|
|
|
sp = path.replace('.txt', '') + '.shapes' # shapefile path
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
with open(sp, 'r') as f: # read existing shapefile
|
|
|
|
|
|
|
|
s = [x.split() for x in f.read().splitlines()]
|
|
|
|
|
|
|
|
assert len(s) == n, 'Shapefile out of sync'
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 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]
|
|
|
|