Skip to content

Commit 2da6444

Browse files
authored
Fix for python models/yolo.py --profile (#4541)
Profiling fix copies input to Detect layer to circumvent inplace changes to the feature maps.
1 parent 79af114 commit 2da6444

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

models/yolo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer
4848
self.inplace = inplace # use in-place ops (e.g. slice assignment)
4949

5050
def forward(self, x):
51-
# x = x.copy() # for profiling
5251
z = [] # inference output
5352
for i in range(self.nl):
5453
x[i] = self.m[i](x[i]) # conv
@@ -143,10 +142,11 @@ def forward_once(self, x, profile=False, visualize=False):
143142
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
144143

145144
if profile:
146-
o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs
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
147147
t = time_sync()
148148
for _ in range(10):
149-
_ = m(x)
149+
m(x.copy() if c else x)
150150
dt.append((time_sync() - t) * 100)
151151
if m == self.model[0]:
152152
LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} {'module'}")

0 commit comments

Comments
 (0)