Skip to content

Commit

Permalink
Cache bug fix (ultralytics#1513)
Browse files Browse the repository at this point in the history
* Caching bug fix ultralytics#1508

* np.zeros((0,5)) x2
  • Loading branch information
glenn-jocher authored Nov 25, 2020
1 parent b3ceffb commit e9a0ae6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def test(data,
f = save_dir / f'test_batch{batch_i}_labels.jpg' # filename
plot_images(img, targets, paths, f, names) # labels
f = save_dir / f'test_batch{batch_i}_pred.jpg'
plot_images(img, output_to_target(output, width, height), paths, f, names) # predictions
plot_images(img, output_to_target(output), paths, f, names) # predictions

# Compute statistics
stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
Expand Down
4 changes: 2 additions & 2 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ def cache_labels(self, path=Path('./labels.cache')):
assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels'

# verify labels
l = []
if os.path.isfile(lb_file):
nf += 1 # label found
with open(lb_file, 'r') as f:
Expand All @@ -458,6 +457,7 @@ def cache_labels(self, path=Path('./labels.cache')):
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
Expand All @@ -470,7 +470,7 @@ def cache_labels(self, path=Path('./labels.cache')):
print(f'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]
x['results'] = [nf, nm, ne, nc, i + 1]
torch.save(x, path) # save for next time
logging.info(f"New cache created: {path}")
return x
Expand Down
21 changes: 3 additions & 18 deletions utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,12 @@ def plot_wh_methods(): # from utils.plots import *; plot_wh_methods()
fig.savefig('comparison.png', dpi=200)


def output_to_target(output, width, height):
def output_to_target(output):
# Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
if isinstance(output, torch.Tensor):
output = output.cpu().numpy()

targets = []
for i, o in enumerate(output):
if o is not None:
for pred in o:
box = pred[:4]
w = (box[2] - box[0]) / width
h = (box[3] - box[1]) / height
x = box[0] / width + w / 2
y = box[1] / height + h / 2
conf = pred[4]
cls = int(pred[5])

targets.append([i, cls, x, y, w, h, conf])

for *box, conf, cls in o.cpu().numpy():
targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf])
return np.array(targets)


Expand Down Expand Up @@ -153,9 +140,7 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
labels = image_targets.shape[1] == 6 # labels if no conf column
conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred)

boxes[[0, 2]] *= w
boxes[[0, 2]] += block_x
boxes[[1, 3]] *= h
boxes[[1, 3]] += block_y
for j, box in enumerate(boxes.T):
cls = int(classes[j])
Expand Down

0 comments on commit e9a0ae6

Please sign in to comment.