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: # [Warning] deprecated! for legacy print( 'initialize weights by filling (Fc:gaussian, Conv:kaiming_normal).' ) init_weights_by_filling( self, silent=False) # (self, gaussian_std=0.05, kaiming_normal=True) elif pretrained == 'caffemodel': model_path = pretrained_cfg.caffemodel.resnet101.model print("Loading caffe pretrained weights from %s" % (model_path)) state_dict = torch.load(model_path) # initialize weights by copying self.load_state_dict({ k: v for k, v in state_dict.items() if k in self.state_dict() }) elif pretrained == 'torchmodel': print("Loading pytorch pretrained weights from %s" % (model_urls[self.net_arch])) state_dict = model_zoo.load_url(model_urls[self.net_arch], progress=True) self.load_state_dict(state_dict, strict=False) else: raise NotImplementedError return self
def head_seq(in_size, reg_n_D, nr_cate=12, nr_fc8=334): # in_size=4096 seq = nn.Sequential( nn.Linear(in_size, nr_fc8), # Fc8_a nn.ReLU(inplace=True), #nn.Dropout(), nn.Linear(nr_fc8, nr_cate*reg_n_D), # Prob_a ) init_weights_by_filling(seq, gaussian_std=0.005) # fill weight with gaussian filler return seq
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': # We now have resnet50-caffe.pth resnet101_caffe.pth resnet152-caffe.pth available. from basic.common import env model_path = env.Home + '/WorkGallery/pytools/pytorch_util/pretrained_model.cache/{}-caffe.pth'.format( self.net_arch) print("Loading caffe pretrained weights from %s" % (model_path)) state_dict = torch.load(model_path) # initialize weights by copying self.load_state_dict({ k: v for k, v in state_dict.items() if k in self.state_dict() }) elif pretrained == 'torchmodel': print(''' [Warning] https://github.com/jwyang/faster-rcnn.pytorch If you want to use pytorch pre-trained models, please remember to transpose images from BGR to RGB, and also use the same data transformer (minus mean and normalize) as used in pretrained model. ''' ) raise NotImplementedError # # # from basic.common import env # if self.net_arch=='resnet101': # model_path = env.Home+'/WorkGallery/pytools/pytorch_util/pretrained_model.cache/resnet101-5d3b4d8f.pth' # elif self.net_arch=='resnet50': # model_path = env.Home+'/WorkGallery/pytools/pytorch_util/pretrained_model.cache/resnet50-19c8e357.pth' # else: # raise NotImplementedError # print("Loading pytorch pretrained weights from %s" %(model_path)) # state_dict = torch.load(model_path) # # initialize weights by copying # self.load_state_dict({k:v for k,v in state_dict.items() if k in self.state_dict()}) 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( '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__(self, nr_cate=3, _Trunk=ResNet101_Trunk): super(Test_Net, self).__init__() self.truck = _Trunk() # or _Trunk(end='pool5') self.nr_cate = nr_cate #-- Head architecture self.head_s2 = nn.Sequential( nn.Linear(2048, 84), nn.ReLU(inplace=True), #nn.Dropout(), nn.Linear(84, self.nr_cate * 3), # 252=3*3 ) self.head_s1 = nn.Sequential( nn.Linear(2048, 84), nn.ReLU(inplace=True), #nn.Dropout(), nn.Linear(84, self.nr_cate * 2), ) self.maskout = Maskout(nr_cate=nr_cate) init_weights_by_filling(self.head_s2) init_weights_by_filling(self.head_s1)
def head_seq(in_size, nr_cate=12, nr_fc8=1024): seq_fc8 = nn.Sequential( nn.Linear(in_size, nr_fc8), # Fc8_a nn.ReLU(inplace=True), nn.Dropout(), ) seq_ccss= nn.Linear(nr_fc8, nr_cate*2) # Prob_a seq_sgnc= nn.Linear(nr_fc8, nr_cate*4) # Prob_a # init_weights_by_filling(seq_fc8 , gaussian_std=0.005) # fill weight with gaussian filler init_weights_by_filling(seq_ccss, gaussian_std=0.005) # fill weight with gaussian filler init_weights_by_filling(seq_sgnc, gaussian_std=0.005) # fill weight with gaussian filler return seq_fc8, seq_ccss, seq_sgnc
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