预训练好的pth文件获取途径:
①Git开源项目里会有下载链接;
②翻墙去huggingface里搜索。Hugging Face – The AI community building the future.
情况一:如果该模型在timm中存在,则可以将下载好的pth文件放到XX/user/.cache/torch/hub/checkpoints目录下。以fasternet_s.pth文件为例,然后在代码中写:
import timm
model=timm.models.create_model('fasternet_s')
情况二:如果该模型不在timm中,则假设将internimage_s.pth文件放到本地的A目录下,则代码应写:
checkpoint = torch.hub.load_state_dict_from_url(url='A目录', map_location="cpu",check_hash=True,file_name="internimage_s.pth")
model.load_state_dict(checkpoint["model"])
tips:如何查看该模型是否在timm中,可以使用通用匹配符的方式,案例如下:
import timm
from pprint import pprint
model_names = timm.list_models('*resne*t*')
pprint(model_names)
>>> ['cspresnet50',
'cspresnet50d',
'cspresnet50w',
'cspresnext50',
...
]