glenn-jocher commited on
Commit
e9a0ae6
1 Parent(s): b3ceffb

Cache bug fix (#1513)

Browse files

* Caching bug fix #1508

* np.zeros((0,5)) x2

Files changed (3) hide show
  1. test.py +1 -1
  2. utils/datasets.py +2 -2
  3. utils/plots.py +3 -18
test.py CHANGED
@@ -209,7 +209,7 @@ def test(data,
209
  f = save_dir / f'test_batch{batch_i}_labels.jpg' # filename
210
  plot_images(img, targets, paths, f, names) # labels
211
  f = save_dir / f'test_batch{batch_i}_pred.jpg'
212
- plot_images(img, output_to_target(output, width, height), paths, f, names) # predictions
213
 
214
  # Compute statistics
215
  stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
 
209
  f = save_dir / f'test_batch{batch_i}_labels.jpg' # filename
210
  plot_images(img, targets, paths, f, names) # labels
211
  f = save_dir / f'test_batch{batch_i}_pred.jpg'
212
+ plot_images(img, output_to_target(output), paths, f, names) # predictions
213
 
214
  # Compute statistics
215
  stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
utils/datasets.py CHANGED
@@ -443,7 +443,6 @@ class LoadImagesAndLabels(Dataset): # for training/testing
443
  assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels'
444
 
445
  # verify labels
446
- l = []
447
  if os.path.isfile(lb_file):
448
  nf += 1 # label found
449
  with open(lb_file, 'r') as f:
@@ -458,6 +457,7 @@ class LoadImagesAndLabels(Dataset): # for training/testing
458
  l = np.zeros((0, 5), dtype=np.float32)
459
  else:
460
  nm += 1 # label missing
 
461
  x[im_file] = [l, shape]
462
  except Exception as e:
463
  nc += 1
@@ -470,7 +470,7 @@ class LoadImagesAndLabels(Dataset): # for training/testing
470
  print(f'WARNING: No labels found in {path}. See {help_url}')
471
 
472
  x['hash'] = get_hash(self.label_files + self.img_files)
473
- x['results'] = [nf, nm, ne, nc, i]
474
  torch.save(x, path) # save for next time
475
  logging.info(f"New cache created: {path}")
476
  return x
 
443
  assert (shape[0] > 9) & (shape[1] > 9), 'image size <10 pixels'
444
 
445
  # verify labels
 
446
  if os.path.isfile(lb_file):
447
  nf += 1 # label found
448
  with open(lb_file, 'r') as f:
 
457
  l = np.zeros((0, 5), dtype=np.float32)
458
  else:
459
  nm += 1 # label missing
460
+ l = np.zeros((0, 5), dtype=np.float32)
461
  x[im_file] = [l, shape]
462
  except Exception as e:
463
  nc += 1
 
470
  print(f'WARNING: No labels found in {path}. See {help_url}')
471
 
472
  x['hash'] = get_hash(self.label_files + self.img_files)
473
+ x['results'] = [nf, nm, ne, nc, i + 1]
474
  torch.save(x, path) # save for next time
475
  logging.info(f"New cache created: {path}")
476
  return x
utils/plots.py CHANGED
@@ -86,25 +86,12 @@ def plot_wh_methods(): # from utils.plots import *; plot_wh_methods()
86
  fig.savefig('comparison.png', dpi=200)
87
 
88
 
89
- def output_to_target(output, width, height):
90
  # Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
91
- if isinstance(output, torch.Tensor):
92
- output = output.cpu().numpy()
93
-
94
  targets = []
95
  for i, o in enumerate(output):
96
- if o is not None:
97
- for pred in o:
98
- box = pred[:4]
99
- w = (box[2] - box[0]) / width
100
- h = (box[3] - box[1]) / height
101
- x = box[0] / width + w / 2
102
- y = box[1] / height + h / 2
103
- conf = pred[4]
104
- cls = int(pred[5])
105
-
106
- targets.append([i, cls, x, y, w, h, conf])
107
-
108
  return np.array(targets)
109
 
110
 
@@ -153,9 +140,7 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
153
  labels = image_targets.shape[1] == 6 # labels if no conf column
154
  conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred)
155
 
156
- boxes[[0, 2]] *= w
157
  boxes[[0, 2]] += block_x
158
- boxes[[1, 3]] *= h
159
  boxes[[1, 3]] += block_y
160
  for j, box in enumerate(boxes.T):
161
  cls = int(classes[j])
 
86
  fig.savefig('comparison.png', dpi=200)
87
 
88
 
89
+ def output_to_target(output):
90
  # Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
 
 
 
91
  targets = []
92
  for i, o in enumerate(output):
93
+ for *box, conf, cls in o.cpu().numpy():
94
+ targets.append([i, cls, *list(*xyxy2xywh(np.array(box)[None])), conf])
 
 
 
 
 
 
 
 
 
 
95
  return np.array(targets)
96
 
97
 
 
140
  labels = image_targets.shape[1] == 6 # labels if no conf column
141
  conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred)
142
 
 
143
  boxes[[0, 2]] += block_x
 
144
  boxes[[1, 3]] += block_y
145
  for j, box in enumerate(boxes.T):
146
  cls = int(classes[j])