def init_weights(self, pretrained='caffemodel'):
     """ Two ways to init weights:
         1) by copying pretrained weights.
         2) by filling empirical  weights. (e.g. gaussian, xavier, uniform, constant, bilinear).
     """
     if pretrained is None:
         print(
             'initialize weights by filling (Fc:gaussian, Conv:kaiming_normal).'
         )
         init_weights_by_filling(self)
     elif pretrained == 'caffemodel':
         print(
             "Initializing weights by copying (pretrained caffe weights).")
         src2dsts = dict(conv1='Convs.0',
                         conv2='Convs.4',
                         conv3='Convs.8',
                         conv4='Convs.10',
                         conv5='Convs.12',
                         fc6='Fcs.0',
                         fc7='Fcs.3')
         copy_weights(self.state_dict(),
                      'caffemodel.alexnet',
                      src2dsts=src2dsts)
     elif pretrained == 'torchmodel':
         raise NotImplementedError
     else:
         raise NotImplementedError
     return self
    def init_weights(self, pretrained='caffemodel'):
        """ Two ways to init weights:
            1) by copying pretrained weights.
            2) by filling empirical  weights. (e.g. gaussian, xavier, uniform, constant, bilinear).
        """

        if pretrained is None:
            print('Initializing weights by filling (with gaussian).')
            init_weights_by_filling(self)
        elif pretrained=='caffemodel':
            print("Initializing weights by copying (pretrained caffe weights).")
            src2dsts= odict(conv1_1='conv1.conv.0' , conv1_2='conv1.conv.2' ,
                            conv2_1='conv2.conv.0' , conv2_2='conv2.conv.2' ,
                            conv3_1='conv3.conv.0' , conv3_2='conv3.conv.2', conv3_3='conv3.conv.4',
                            conv4_1='conv4.conv.0' , conv4_2='conv4.conv.2', conv4_3='conv4.conv.4',
                            conv5_1='conv5.conv.0' , conv5_2='conv5.conv.2', conv5_3='conv5.conv.4',)
            copy_weights(self.state_dict(), 'caffemodel.%s'%(self.net_arch), src2dsts=src2dsts)
        elif pretrained=='torchmodel':
            src2dsts= odict([('features.0' ,'conv1.conv.0') , ('features.2' ,'conv1.conv.2'),
                             ('features.5' ,'conv2.conv.0') , ('features.7' ,'conv2.conv.2'),
                             ('features.10','conv3.conv.0') , ('features.12','conv3.conv.2'), ('features.14','conv3.conv.4'),
                             ('features.17','conv4.conv.0') , ('features.19','conv4.conv.2'), ('features.21','conv4.conv.4'),
                             ('features.24','conv5.conv.0') , ('features.26','conv5.conv.2'), ('features.28','conv5.conv.4'),])
            copy_weights(self.state_dict(), 'torchmodel.%s'%(self.net_arch), strict=False, src2dsts=src2dsts)
        else:
            raise NotImplementedError

        for name, m in self.named_modules():
            if isinstance(m, nn.ConvTranspose2d):
                print ('>>>>>>>>>>>', name)
                assert m.kernel_size[0] == m.kernel_size[1]
                try:
                    initial_weight = get_upsampling_weight(
                        m.in_channels, m.out_channels, m.kernel_size[0])
                    print (m.weight.shape, initial_weight.shape)
                    m.weight.data.copy_(initial_weight)
                except:
                    print ('pass')
                    pass
            elif isinstance(m, nn.BatchNorm2d): # classname.find('BatchNorm') != -1:
                print ('>>>>>>>>>>>', name)
                m.weight.data.normal_(1.0, 0.02)
                m.bias.data.fill_(0)
        return self
 def init_weights(self, pretrained='caffemodel'):
     """ Two ways to init weights:
         1) by copying pretrained weights.
         2) by filling empirical  weights. (e.g. gaussian, xavier, uniform, constant, bilinear).
     """
     if pretrained is None:
         print(
             'initialize weights by filling (Fc:gaussian, Conv:kaiming_normal).'
         )
         init_weights_by_filling(self)
     elif pretrained == 'caffemodel':
         print(
             "Initializing weights by copying (pretrained caffe weights).")
         src2dsts = get_caffeSrc2Dst(self.net_arch)
         copy_weights(self.state_dict(),
                      'caffemodel.%s' % (self.net_arch),
                      src2dsts=src2dsts)
     elif pretrained == 'torchmodel':
         raise NotImplementedError
     else:
         raise NotImplementedError
     return self
    def init_weights(self, pretrained='caffemodel'):
        """ Two ways to init weights:
            1) by copying pretrained weights.
            2) by filling empirical  weights. (e.g. gaussian, xavier, uniform, constant, bilinear).
        """
        # if   net_arch=='vgg16':
        #fc6='classifier.0', fc7='classifier.3')

        if pretrained is None:
            print('Initializing weights by filling (with gaussian).')
            init_weights_by_filling(self)
        elif pretrained == 'caffemodel':
            print(
                "Initializing weights by copying (pretrained caffe weights).")
            src2dsts = odict(
                conv1_1='conv1.conv.0',
                conv1_2='conv1.conv.2',
                conv2_1='conv2.conv.0',
                conv2_2='conv2.conv.2',
                conv3_1='conv3.conv.0',
                conv3_2='conv3.conv.2',
                conv3_3='conv3.conv.4',
                conv4_1='conv4.conv.0',
                conv4_2='conv4.conv.2',
                conv4_3='conv4.conv.4',
                conv5_1='conv5.conv.0',
                conv5_2='conv5.conv.2',
                conv5_3='conv5.conv.4',
            )
            copy_weights(self.state_dict(),
                         'caffemodel.%s' % (self.net_arch),
                         src2dsts=src2dsts)
        elif pretrained == 'torchmodel':
            src2dsts = odict([
                map(str.strip, x.split('=')) for x in map(
                    str.strip,
                    open(this_dir + '/src2dsts.vgg16_bn.txt').readlines())
                if not x.startswith('=')
            ])
            copy_weights(self.state_dict(),
                         'torchmodel.%s' % (self.net_arch),
                         strict=False,
                         src2dsts=src2dsts)
        else:
            print("[Error] unrecognized pretrained type: ", pretrained)
            raise NotImplementedError

        for name, m in self.named_modules():
            if isinstance(m, nn.ConvTranspose2d):
                print('>>>>>>>>>>>',
                      name)  #, m.in_channels, m.out_channels, m.kernel_size[0]
                assert m.kernel_size[0] == m.kernel_size[1]
                try:
                    initial_weight = get_upsampling_weight(
                        m.in_channels, m.out_channels, m.kernel_size[0])
                    print(m.weight.shape, initial_weight.shape)
                    m.weight.data.copy_(initial_weight)
                except:
                    print('pass')
                    pass
            elif isinstance(
                    m, nn.BatchNorm2d):  # classname.find('BatchNorm') != -1:
                print('>>>>>>>>>>>', name)
                m.weight.data.normal_(1.0, 0.02)
                m.bias.data.fill_(0)
        return self
Exemple #5
0
 def init_weights(self):
     copy_weights(self.state_dict(), 'torchmodel.alexnet', strict=False)
     return self