如何把imagenet预训练的模型,输入层的通道数随心所欲的修改,从而来适应自己的任务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#增加一个通道
w = layers[ 0 ].weight
layers[ 0 ] = nn.Conv2d( 4 , 64 , kernel_size = ( 7 , 7 ), stride = ( 2 , 2 ), padding = ( 3 , 3 ), bias = False )
layers[ 0 ].weight = torch.nn.Parameter(torch.cat((w, w[:, : 1 , :, :]), dim = 1 ))
#方式2
w = layers[ 0 ].weight
layers[ 0 ] = nn.Conv2d( 4 , 64 , kernel_size = ( 7 , 7 ), stride = ( 2 , 2 ), padding = ( 3 , 3 ), bias = False )
layers[ 0 ].weight = torch.nn.Parameter(torch.cat((w, torch.zeros( 64 , 1 , 7 , 7 )), dim = 1 ))
#单通道输入
layers[ 0 ] = nn.Conv2d( 1 , 64 , kernel_size = ( 7 , 7 ), stride = ( 2 , 2 ), padding = ( 3 , 3 ), bias = False )
layers[ 0 ].weight = torch.nn.Parameter(w[:, : 1 , :, :])
|
以上这篇pytorch 实现在预训练模型的 input上增减通道就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/github_36923418/article/details/84567227