glenn-jocher commited on
Commit
fa8f1fb
1 Parent(s): 84f9bb5

Simplify autoshape() post-process (#1653)

Browse files

* Simplify autoshape() post-process

* cleanup

* cleanup

Files changed (4) hide show
  1. hubconf.py +1 -1
  2. models/common.py +3 -4
  3. requirements.txt +4 -4
  4. utils/general.py +1 -1
hubconf.py CHANGED
@@ -108,7 +108,7 @@ def yolov5x(pretrained=False, channels=3, classes=80):
108
 
109
  if __name__ == '__main__':
110
  model = create(name='yolov5s', pretrained=True, channels=3, classes=80) # example
111
- model = model.fuse().autoshape() # for PIL/cv2/np inputs and NMS
112
 
113
  # Verify inference
114
  from PIL import Image
 
108
 
109
  if __name__ == '__main__':
110
  model = create(name='yolov5s', pretrained=True, channels=3, classes=80) # example
111
+ model = model.autoshape() # for PIL/cv2/np inputs and NMS
112
 
113
  # Verify inference
114
  from PIL import Image
models/common.py CHANGED
@@ -167,8 +167,7 @@ class autoShape(nn.Module):
167
 
168
  # Post-process
169
  for i in batch:
170
- if y[i] is not None:
171
- y[i][:, :4] = scale_coords(shape1, y[i][:, :4], shape0[i])
172
 
173
  return Detections(imgs, y, self.names)
174
 
@@ -177,13 +176,13 @@ class Detections:
177
  # detections class for YOLOv5 inference results
178
  def __init__(self, imgs, pred, names=None):
179
  super(Detections, self).__init__()
 
 
180
  self.imgs = imgs # list of images as numpy arrays
181
  self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
182
  self.names = names # class names
183
  self.xyxy = pred # xyxy pixels
184
  self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
185
- d = pred[0].device # device
186
- gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations
187
  self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
188
  self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
189
  self.n = len(self.pred)
 
167
 
168
  # Post-process
169
  for i in batch:
170
+ scale_coords(shape1, y[i][:, :4], shape0[i])
 
171
 
172
  return Detections(imgs, y, self.names)
173
 
 
176
  # detections class for YOLOv5 inference results
177
  def __init__(self, imgs, pred, names=None):
178
  super(Detections, self).__init__()
179
+ d = pred[0].device # device
180
+ gn = [torch.tensor([*[im.shape[i] for i in [1, 0, 1, 0]], 1., 1.], device=d) for im in imgs] # normalizations
181
  self.imgs = imgs # list of images as numpy arrays
182
  self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
183
  self.names = names # class names
184
  self.xyxy = pred # xyxy pixels
185
  self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
 
 
186
  self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
187
  self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
188
  self.n = len(self.pred)
requirements.txt CHANGED
@@ -9,8 +9,8 @@ Pillow
9
  PyYAML>=5.3
10
  scipy>=1.4.1
11
  tensorboard>=2.2
12
- torch>=1.6.0
13
- torchvision>=0.7.0
14
  tqdm>=4.41.0
15
 
16
  # logging -------------------------------------
@@ -26,5 +26,5 @@ pandas
26
  # scikit-learn==0.19.2 # for coreml quantization
27
 
28
  # extras --------------------------------------
29
- # thop # FLOPS computation
30
- # pycocotools>=2.0 # COCO mAP
 
9
  PyYAML>=5.3
10
  scipy>=1.4.1
11
  tensorboard>=2.2
12
+ torch>=1.7.0
13
+ torchvision>=0.8.1
14
  tqdm>=4.41.0
15
 
16
  # logging -------------------------------------
 
26
  # scikit-learn==0.19.2 # for coreml quantization
27
 
28
  # extras --------------------------------------
29
+ thop # FLOPS computation
30
+ pycocotools>=2.0 # COCO mAP
utils/general.py CHANGED
@@ -258,7 +258,7 @@ def wh_iou(wh1, wh2):
258
  return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter)
259
 
260
 
261
- def non_max_suppression(prediction, conf_thres=0.1, iou_thres=0.6, classes=None, agnostic=False, labels=()):
262
  """Performs Non-Maximum Suppression (NMS) on inference results
263
 
264
  Returns:
 
258
  return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter)
259
 
260
 
261
+ def non_max_suppression(prediction, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, labels=()):
262
  """Performs Non-Maximum Suppression (NMS) on inference results
263
 
264
  Returns: