【白菜】timm篇四:待写 | cabbage

cabbage

菜鸟写给小白的教程

0%

【白菜】timm篇四:待写

d待写

dddd

获取模型运行过程中的特征

+hook技术

获取特征 (一篇)

可以通过多种方式获得倒数第二个模型层的特征,而无需进行模型手术(尽管可以随意进行手术)。人们必须首先决定他们是想要池化的还是非池化的特征。

https://blog.csdn.net/qq_41917697/article/details/115026308
获取分类层前(倒数第二层)的特征

直接创建没有池化和分类层的模型,对于基于CNN的模型可以这样做
https://blog.csdn.net/qq_42003943/article/details/118382823?login=from_csdn
检查分类器,我们可以看到 timm 已经用一个新的、未经训练的、具有所需类别数量的线性层替换了最后一层;准备微调我们的数据集!
如果我们想避免完全创建最后一层,我们可以将类的数量设置为 0,这将创建一个具有恒等函数的模型作为最后一层;这对于检查倒数第二层的输出很有用。
identity()

直接调用forward_features()函数
x = torch.randn([1, 3, 224, 224])
Backbone1 = timm.create_model(‘vit_base_patch16_224’)
Backbone2 = timm.create_model(‘resnet50’)
feature1 = Backbone1.forward_features(x)
feature2 = Backbone2.forward_features(x)
print(‘vit_feature:’, feature1.shape, ‘resnet_feature:’, feature2.shape)# Results: vit_feature: torch.Size([1, 768]) resnet_feature: torch.Size([1, 2048, 7, 7])
————————————————
版权声明:本文为CSDN博主「ZhangChen@BJTU」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42003943/article/details/118382823

通过移除层来获得
https://blog.csdn.net/qq_42003943/article/details/118382823?login=from_csdn

获取中间层特征-注意并非所有model都有此选项
https://blog.csdn.net/qq_42003943/article/details/118382823?login=from_csdn
可通过out_indices参数指定从哪个level获取feature

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
x = torch.randn([1, 3, 224, 224])
feature_extractor = timm.create_model('resnet50', features_only=True) # 并非所有model都有此选项
feature_list = feature_extractor(x)
for a in feature_list:
print(a.shape)
# Results:
# torch.Size([1, 64, 112, 112])
# torch.Size([1, 256, 56, 56])
# torch.Size([1, 512, 28, 28])
# torch.Size([1, 1024, 14, 14])
# torch.Size([1, 2048, 7, 7])

##可通过out_indices参数指定从哪个level获取feature
feature_extractor = timm.create_model('resnet50', features_only=True, out_indices=[1, 3, 4])
feature_list = feature_extractor(x)
for a in feature_list:
print(a.shape)
# Results:
# torch.Size([1, 256, 56, 56])
# torch.Size([1, 1024, 14, 14])
# torch.Size([1, 2048, 7, 7])
————————————————
版权声明:本文为CSDN博主「ZhangChen@BJTU」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42003943/article/details/118382823

接下来我们来看一下这些特征提取器究竟是什么类型:

1
2
3
4
5
6
7
8
9
10
import timm
feature_extractor = timm.create_model('resnet34', features_only=True, out_indices=[3])

print('type:', type(feature_extractor))
print('len: ', len(feature_extractor))
for item in feature_extractor:
print(item)
————————————————
版权声明:本文为CSDN博主「Adenialzz」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44966641/article/details/121364784

输出:

type: <class ‘timm.models.features.FeatureListNet’>
len: 7
conv1
bn1
act1
maxpool
layer1
layer2
layer3

可以看到,feature_extractor 其实也是一个神经网络,在 timm 中称为 FeatureListNet,而我们通过 out_indices 参数来指定截取到哪一层特征。

需要注意的是,ViT 模型并不支持 features_only 选项(0.4.12版本)。

extractor = timm.create_model(‘vit_base_patch16_224’, features_only=True)
1
输出:

RuntimeError: features_only not implemented for Vision Transformer models.
1

————————————————
版权声明:本文为CSDN博主「Adenialzz」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44966641/article/details/121364784