Skip to content

Commit

Permalink
PyTorch Hub custom model to CUDA device fix (ultralytics#2636)
Browse files Browse the repository at this point in the history
Fix for ultralytics#2630 raised by @Pro100rus32

(cherry picked from commit ee16983)
  • Loading branch information
glenn-jocher authored and Lechtr committed Apr 1, 2021
1 parent 0b49a3c commit b7d28d3
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hubconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ def custom(path_or_model='path/to/model.pt', autoshape=True):
hub_model = Model(model.yaml).to(next(model.parameters()).device) # create
hub_model.load_state_dict(model.float().state_dict()) # load state_dict
hub_model.names = model.names # class names
return hub_model.autoshape() if autoshape else hub_model
if autoshape:
hub_model = hub_model.autoshape() # for file/URI/PIL/cv2/np inputs and NMS
device = select_device('0' if torch.cuda.is_available() else 'cpu') # default to GPU if available
return hub_model.to(device)


if __name__ == '__main__':
Expand Down

0 comments on commit b7d28d3

Please sign in to comment.