赞
踩
所填添加的注意机制只是一个演示,怎么添加还需要自己琢磨
class BasicBlock(nn.Module): expansion = 1 def __init__(self, inplanes, planes, stride=1, downsample=None, att=False): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride self.is_att = att self.att = Attention(planes) def forward(self, x): residual = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.is_att: scale = self.att(out) out = out * scale if self.downsample is not None: residual = self.downsample(x) out += residual out = self.relu(out) return out
在加载与训练时,
1.先加载预训练权重
2.筛选所需的预训练权重
3.再将预训练权重更新到model_dict中
4.load权重
if pretrained:
pretrained_dict=model_zoo.load_url(model_urls['resnet34'],model_dir='../pretrained')# Modify 'model_dir' according to your own path
print('Petrain Model Have been loaded!')
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
return model
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。