Unverified 提交 87ca35b9 authored 作者: Glenn Jocher's avatar Glenn Jocher 提交者: GitHub

Simplified PyTorch hub for custom models (#1677)

上级 54043a9f
...@@ -106,8 +106,31 @@ def yolov5x(pretrained=False, channels=3, classes=80): ...@@ -106,8 +106,31 @@ def yolov5x(pretrained=False, channels=3, classes=80):
return create('yolov5x', pretrained, channels, classes) return create('yolov5x', pretrained, channels, classes)
def custom(model='path/to/model.pt'):
"""YOLOv5-custom model from https://github.com/ultralytics/yolov5
Arguments (3 format options):
model (str): 'path/to/model.pt'
model (dict): torch.load('path/to/model.pt')
model (nn.Module): 'torch.load('path/to/model.pt')['model']
Returns:
pytorch model
"""
if isinstance(model, str):
model = torch.load(model) # load checkpoint
if isinstance(model, dict):
model = model['model'] # load model
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
if __name__ == '__main__': if __name__ == '__main__':
model = create(name='yolov5s', pretrained=True, channels=3, classes=80) # example model = create(name='yolov5s', pretrained=True, channels=3, classes=80) # pretrained example
# model = custom(model='path/to/model.pt') # custom example
model = model.autoshape() # for PIL/cv2/np inputs and NMS model = model.autoshape() # for PIL/cv2/np inputs and NMS
# Verify inference # Verify inference
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论