Fix 6 Flake8 issues (#6541)
* F541 * F821 * F841 * E741 * E302 * E722 * Apply suggestions from code review * Update general.py * Update datasets.py * Update export.py * Update plots.py * Update plots.py Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
1 parent
e1a6a0b100
commit
cba4303d32
10 files changed
+55
-56
No files matched your search
+27
-26
@@ -59,7 +59,7 @@ def exif_size(img):
|
||||
s = (s[1], s[0])
|
||||
elif rotation == 8: # rotation 90
|
||||
s = (s[1], s[0])
|
||||
except:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return s
|
||||
@@ -420,7 +420,7 @@ class LoadImagesAndLabels(Dataset):
|
||||
cache, exists = np.load(cache_path, allow_pickle=True).item(), True # load dict
|
||||
assert cache['version'] == self.cache_version # same version
|
||||
assert cache['hash'] == get_hash(self.label_files + self.img_files) # same hash
|
||||
except:
|
||||
except Exception:
|
||||
cache, exists = self.cache_labels(cache_path, prefix), False # cache
|
||||
|
||||
# Display cache
|
||||
@@ -514,13 +514,13 @@ class LoadImagesAndLabels(Dataset):
|
||||
with Pool(NUM_THREADS) as pool:
|
||||
pbar = tqdm(pool.imap(verify_image_label, zip(self.img_files, self.label_files, repeat(prefix))),
|
||||
desc=desc, total=len(self.img_files))
|
||||
for im_file, l, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar:
|
||||
for im_file, lb, shape, segments, nm_f, nf_f, ne_f, nc_f, msg in pbar:
|
||||
nm += nm_f
|
||||
nf += nf_f
|
||||
ne += ne_f
|
||||
nc += nc_f
|
||||
if im_file:
|
||||
x[im_file] = [l, shape, segments]
|
||||
x[im_file] = [lb, shape, segments]
|
||||
if msg:
|
||||
msgs.append(msg)
|
||||
pbar.desc = f"{desc}{nf} found, {nm} missing, {ne} empty, {nc} corrupt"
|
||||
@@ -627,8 +627,8 @@ class LoadImagesAndLabels(Dataset):
|
||||
@staticmethod
|
||||
def collate_fn(batch):
|
||||
img, label, path, shapes = zip(*batch) # transposed
|
||||
for i, l in enumerate(label):
|
||||
l[:, 0] = i # add target image index for build_targets()
|
||||
for i, lb in enumerate(label):
|
||||
lb[:, 0] = i # add target image index for build_targets()
|
||||
return torch.stack(img, 0), torch.cat(label, 0), path, shapes
|
||||
|
||||
@staticmethod
|
||||
@@ -645,15 +645,15 @@ class LoadImagesAndLabels(Dataset):
|
||||
if random.random() < 0.5:
|
||||
im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2.0, mode='bilinear', align_corners=False)[
|
||||
0].type(img[i].type())
|
||||
l = label[i]
|
||||
lb = 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
|
||||
lb = 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)
|
||||
label4.append(lb)
|
||||
|
||||
for i, l in enumerate(label4):
|
||||
l[:, 0] = i # add target image index for build_targets()
|
||||
for i, lb in enumerate(label4):
|
||||
lb[:, 0] = i # add target image index for build_targets()
|
||||
|
||||
return torch.stack(img4, 0), torch.cat(label4, 0), path4, shapes4
|
||||
|
||||
@@ -743,6 +743,7 @@ def load_mosaic9(self, index):
|
||||
s = self.img_size
|
||||
indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices
|
||||
random.shuffle(indices)
|
||||
hp, wp = -1, -1 # height, width previous
|
||||
for i, index in enumerate(indices):
|
||||
# Load image
|
||||
img, _, (h, w) = load_image(self, index)
|
||||
@@ -906,30 +907,30 @@ def verify_image_label(args):
|
||||
if os.path.isfile(lb_file):
|
||||
nf = 1 # label found
|
||||
with open(lb_file) as f:
|
||||
l = [x.split() for x in f.read().strip().splitlines() if len(x)]
|
||||
if any([len(x) > 8 for x in l]): # is segment
|
||||
classes = np.array([x[0] for x in l], dtype=np.float32)
|
||||
segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in l] # (cls, xy1...)
|
||||
l = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
|
||||
l = np.array(l, dtype=np.float32)
|
||||
nl = len(l)
|
||||
lb = [x.split() for x in f.read().strip().splitlines() if len(x)]
|
||||
if any([len(x) > 8 for x in lb]): # is segment
|
||||
classes = np.array([x[0] for x in lb], dtype=np.float32)
|
||||
segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in lb] # (cls, xy1...)
|
||||
lb = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
|
||||
lb = np.array(lb, dtype=np.float32)
|
||||
nl = len(lb)
|
||||
if nl:
|
||||
assert l.shape[1] == 5, f'labels require 5 columns, {l.shape[1]} columns detected'
|
||||
assert (l >= 0).all(), f'negative label values {l[l < 0]}'
|
||||
assert (l[:, 1:] <= 1).all(), f'non-normalized or out of bounds coordinates {l[:, 1:][l[:, 1:] > 1]}'
|
||||
_, i = np.unique(l, axis=0, return_index=True)
|
||||
assert lb.shape[1] == 5, f'labels require 5 columns, {lb.shape[1]} columns detected'
|
||||
assert (lb >= 0).all(), f'negative label values {lb[lb < 0]}'
|
||||
assert (lb[:, 1:] <= 1).all(), f'non-normalized or out of bounds coordinates {lb[:, 1:][lb[:, 1:] > 1]}'
|
||||
_, i = np.unique(lb, axis=0, return_index=True)
|
||||
if len(i) < nl: # duplicate row check
|
||||
l = l[i] # remove duplicates
|
||||
lb = lb[i] # remove duplicates
|
||||
if segments:
|
||||
segments = segments[i]
|
||||
msg = f'{prefix}WARNING: {im_file}: {nl - len(i)} duplicate labels removed'
|
||||
else:
|
||||
ne = 1 # label empty
|
||||
l = np.zeros((0, 5), dtype=np.float32)
|
||||
lb = np.zeros((0, 5), dtype=np.float32)
|
||||
else:
|
||||
nm = 1 # label missing
|
||||
l = np.zeros((0, 5), dtype=np.float32)
|
||||
return im_file, l, shape, segments, nm, nf, ne, nc, msg
|
||||
lb = np.zeros((0, 5), dtype=np.float32)
|
||||
return im_file, lb, shape, segments, nm, nf, ne, nc, msg
|
||||
except Exception as e:
|
||||
nc = 1
|
||||
msg = f'{prefix}WARNING: {im_file}: ignoring corrupt image/label: {e}'
|
||||
|
||||
Reference in new issue
Block a user