glenn-jocher commited on
Commit
2da6444
·
unverified ·
1 Parent(s): 79af114

Fix for `python models/yolo.py --profile` (#4541)

Browse files

Profiling fix copies input to Detect layer to circumvent inplace changes to the feature maps.

Files changed (1) hide show
  1. models/yolo.py +3 -3
models/yolo.py CHANGED
@@ -48,7 +48,6 @@ class Detect(nn.Module):
48
  self.inplace = inplace # use in-place ops (e.g. slice assignment)
49
 
50
  def forward(self, x):
51
- # x = x.copy() # for profiling
52
  z = [] # inference output
53
  for i in range(self.nl):
54
  x[i] = self.m[i](x[i]) # conv
@@ -143,10 +142,11 @@ class Model(nn.Module):
143
  x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
144
 
145
  if profile:
146
- o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs
 
147
  t = time_sync()
148
  for _ in range(10):
149
- _ = m(x)
150
  dt.append((time_sync() - t) * 100)
151
  if m == self.model[0]:
152
  LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} {'module'}")
 
48
  self.inplace = inplace # use in-place ops (e.g. slice assignment)
49
 
50
  def forward(self, x):
 
51
  z = [] # inference output
52
  for i in range(self.nl):
53
  x[i] = self.m[i](x[i]) # conv
 
142
  x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
143
 
144
  if profile:
145
+ c = isinstance(m, Detect) # copy input as inplace fix
146
+ o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs
147
  t = time_sync()
148
  for _ in range(10):
149
+ m(x.copy() if c else x)
150
  dt.append((time_sync() - t) * 100)
151
  if m == self.model[0]:
152
  LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} {'module'}")