timm库的安装和使用
(21条消息) timm库使用_粟悟饭&龟波功的博客-CSDN博客_timm
一、timm库简介
PyTorch Image Models,简称timm,是一个巨大的PyTorch代码集合,整合了常用的models、layers、utilities、optimizers、schedulers、data-loaders/augmentations和reference training/validation scripts。
二、安装
一般来说,conda install timm就可以了。但是我在安装时却出现这个错误:PackagesNotFoundError: The following packages are not available from current channels
解决方法:将conda-forge添加到搜索路径上
首先,当出现这种报错时,应该首先尝试使用以下命令将conda-forge channel添加到你的channel列表中:
conda config --append channels conda-forge
它告诉conda在搜索软件包时也要在conda-forge channel上查看。
然后你就可以尝试利用如下命令再次安装。
conda install timm
原因在于:channel可以看成是托管python包的服务器,当无法通过标准channel获得python包时,社区驱动的conda-forge通常是一个很好的地点。大部分问题都可以利用这条语句解决。
三、使用
1.查看所有模型
model_list = timm.list_models() print(model_list)
2.查看具有预训练参数的模型
model_pretrain_list = timm.list_models(pretrained=True) print(model_pretrain_list)
3.检索特定模型
model_resnet = timm.list_models('*resnet*') print(model_resnet)
4.创建模型
x = torch.randn((1, 3, 256, 512)) modle_mobilenetv2 = timm.create_model('mobilenetv2_100', pretrained=True) out = modle_mobilenetv2(x) # print(out.shape) # torch.Size([1, 1000]
5.创建模型–改变输出类别数
x = torch.randn((1, 3, 256, 512)) modle_mobilenetv2 = timm.create_model('mobilenetv2_100', pretrained=True, num_classes=100) out = modle_mobilenetv2(x) # print(out.shape) # torch.Size([1, 100])
6.创建模型–改变输入通道数
x = torch.randn((1, 10, 256, 512)) modle_mobilenetv2 = timm.create_model('mobilenetv2_100', pretrained=True, in_chans=10) out = modle_mobilenetv2(x) # print(out.shape) # torch.Size([1, 1000])
7.创建模型–只提取特征
x = torch.randn((1, 3, 256, 512)) modle_mobilenetv2 = timm.create_model('mobilenetv2_100', pretrained=True, features_only=True) out = modle_mobilenetv2(x) # for o in out: # print(o.shape) # torch.Size([1, 16, 128, 256]) # torch.Size([1, 24, 64, 128]) # torch.Size([1, 32, 32, 64]) # torch.Size([1, 96, 16, 32]) # torch.Size([1, 320, 8, 16])
浙公网安备 33010602011771号