Skip to content

Commit d9c020e

Browse files
glenn-jocherClay Januhowski
authored andcommitted
Fix val.py Ensemble() (ultralytics#7490)
1 parent e962662 commit d9c020e

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

models/experimental.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def attempt_load(weights, map_location=None, inplace=True, fuse=True):
115115
return model[-1] # return model
116116
else:
117117
print(f'Ensemble created with {weights}\n')
118-
for k in ['names']:
119-
setattr(model, k, getattr(model[-1], k))
118+
for k in 'names', 'nc', 'yaml':
119+
setattr(model, k, getattr(model[0], k))
120120
model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride
121+
assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}'
121122
return model # return ensemble

val.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def run(
163163
# Dataloader
164164
if not training:
165165
if pt and not single_cls: # check --weights are trained on --data
166-
ncm = model.model.yaml['nc']
166+
ncm = model.model.nc
167167
assert ncm == nc, f'{weights[0]} ({ncm} classes) trained on different --data than what you passed ({nc} ' \
168168
f'classes). Pass correct combination of --weights and --data that are trained together.'
169169
model.warmup(imgsz=(1 if pt else batch_size, 3, imgsz, imgsz)) # warmup

0 commit comments

Comments
 (0)