torch初始化block

修改过的block没有预训练模型,初始化

res_stripe4_stage5= Bottleneck(256, 512, downsample=nn.Sequential(nn.Conv2d(256, 2048, 1, bias=False), nn.BatchNorm2d(2048))
# Bottleneck(256, 512,downsample=nn.Sequential(OrderedDict([('conv1', nn.Conv2d(256, 2048, 1, bias=False)),('bn1', nn.BatchNorm2d(2048))])))
self._initialize_Bottleneck_weights(res_stripe4_stage5)




    

def _initialize_Bottleneck_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0.0)
            if isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1.0)
                nn.init.constant_(m.bias, 0.0)

也可在网络的类中定义初始化函数

通过net._initialize_weights()进行初始化

你可能感兴趣的:(pytorch)