glenn-jocher commited on
Commit
ecc2c7b
1 Parent(s): 6134ec5

Remove named arguments where possible (#7105)

Browse files

* Remove named arguments where possible

Speed improvements.

* Update yolo.py

* Update yolo.py

* Update yolo.py

Files changed (2) hide show
  1. models/common.py +7 -7
  2. models/yolo.py +5 -5
models/common.py CHANGED
@@ -121,7 +121,7 @@ class BottleneckCSP(nn.Module):
121
  def forward(self, x):
122
  y1 = self.cv3(self.m(self.cv1(x)))
123
  y2 = self.cv2(x)
124
- return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))
125
 
126
 
127
  class C3(nn.Module):
@@ -136,7 +136,7 @@ class C3(nn.Module):
136
  # self.m = nn.Sequential(*(CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)))
137
 
138
  def forward(self, x):
139
- return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))
140
 
141
 
142
  class C3TR(C3):
@@ -527,7 +527,7 @@ class AutoShape(nn.Module):
527
  p = next(self.model.parameters()) if self.pt else torch.zeros(1) # for device and type
528
  autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference
529
  if isinstance(imgs, torch.Tensor): # torch
530
- with amp.autocast(enabled=autocast):
531
  return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference
532
 
533
  # Pre-process
@@ -550,19 +550,19 @@ class AutoShape(nn.Module):
550
  shape1.append([y * g for y in s])
551
  imgs[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update
552
  shape1 = [make_divisible(x, self.stride) if self.pt else size for x in np.array(shape1).max(0)] # inf shape
553
- x = [letterbox(im, new_shape=shape1, auto=False)[0] for im in imgs] # pad
554
  x = np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2))) # stack and BHWC to BCHW
555
  x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32
556
  t.append(time_sync())
557
 
558
- with amp.autocast(enabled=autocast):
559
  # Inference
560
  y = self.model(x, augment, profile) # forward
561
  t.append(time_sync())
562
 
563
  # Post-process
564
- y = non_max_suppression(y if self.dmb else y[0], self.conf, iou_thres=self.iou, classes=self.classes,
565
- agnostic=self.agnostic, multi_label=self.multi_label, max_det=self.max_det) # NMS
566
  for i in range(n):
567
  scale_coords(shape1, y[i][:, :4], shape0[i])
568
 
 
121
  def forward(self, x):
122
  y1 = self.cv3(self.m(self.cv1(x)))
123
  y2 = self.cv2(x)
124
+ return self.cv4(self.act(self.bn(torch.cat((y1, y2), 1))))
125
 
126
 
127
  class C3(nn.Module):
 
136
  # self.m = nn.Sequential(*(CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)))
137
 
138
  def forward(self, x):
139
+ return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
140
 
141
 
142
  class C3TR(C3):
 
527
  p = next(self.model.parameters()) if self.pt else torch.zeros(1) # for device and type
528
  autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference
529
  if isinstance(imgs, torch.Tensor): # torch
530
+ with amp.autocast(autocast):
531
  return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference
532
 
533
  # Pre-process
 
550
  shape1.append([y * g for y in s])
551
  imgs[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update
552
  shape1 = [make_divisible(x, self.stride) if self.pt else size for x in np.array(shape1).max(0)] # inf shape
553
+ x = [letterbox(im, shape1, auto=False)[0] for im in imgs] # pad
554
  x = np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2))) # stack and BHWC to BCHW
555
  x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32
556
  t.append(time_sync())
557
 
558
+ with amp.autocast(autocast):
559
  # Inference
560
  y = self.model(x, augment, profile) # forward
561
  t.append(time_sync())
562
 
563
  # Post-process
564
+ y = non_max_suppression(y if self.dmb else y[0], self.conf, self.iou, self.classes, self.agnostic,
565
+ self.multi_label, max_det=self.max_det) # NMS
566
  for i in range(n):
567
  scale_coords(shape1, y[i][:, :4], shape0[i])
568
 
models/yolo.py CHANGED
@@ -71,13 +71,13 @@ class Detect(nn.Module):
71
 
72
  def _make_grid(self, nx=20, ny=20, i=0):
73
  d = self.anchors[i].device
 
74
  if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
75
- yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)], indexing='ij')
76
  else:
77
- yv, xv = torch.meshgrid([torch.arange(ny, device=d), torch.arange(nx, device=d)])
78
- grid = torch.stack((xv, yv), 2).expand((1, self.na, ny, nx, 2)).float()
79
- anchor_grid = (self.anchors[i].clone() * self.stride[i]) \
80
- .view((1, self.na, 1, 1, 2)).expand((1, self.na, ny, nx, 2)).float()
81
  return grid, anchor_grid
82
 
83
 
 
71
 
72
  def _make_grid(self, nx=20, ny=20, i=0):
73
  d = self.anchors[i].device
74
+ shape = 1, self.na, ny, nx, 2 # grid shape
75
  if check_version(torch.__version__, '1.10.0'): # torch>=1.10.0 meshgrid workaround for torch>=0.7 compatibility
76
+ yv, xv = torch.meshgrid(torch.arange(ny, device=d), torch.arange(nx, device=d), indexing='ij')
77
  else:
78
+ yv, xv = torch.meshgrid(torch.arange(ny, device=d), torch.arange(nx, device=d))
79
+ grid = torch.stack((xv, yv), 2).expand(shape).float()
80
+ anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape).float()
 
81
  return grid, anchor_grid
82
 
83