def _resnet(arch, block, layers, pretrained=False, progress=True, imagenet_pretrained=False, num_classes=1, lin_features=512, dropout_prob=0.5, bn_final=False, concat_pool=True, **kwargs): # Model creation base_model = ResNet(block, layers, num_classes=num_classes, **kwargs) # Imagenet pretraining if imagenet_pretrained: if pretrained: raise ValueError('imagenet_pretrained cannot be set to True if pretrained=True') state_dict = load_state_dict_from_url(imagenet_urls[arch], progress=progress) # Remove FC params from dict for key in ('fc.weight', 'fc.bias'): state_dict.pop(key, None) missing, unexpected = base_model.load_state_dict(state_dict, strict=False) if any(unexpected) or any(not elt.startswith('fc.') for elt in missing): raise KeyError(f"Missing parameters: {missing}\nUnexpected parameters: {unexpected}") # Cut at last conv layers model = cnn_model(base_model, model_cut, base_model.fc.in_features, num_classes, lin_features, dropout_prob, bn_final=bn_final, concat_pool=concat_pool) # Parameter loading if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def __init__(self, arch, norm, pre=False): super().__init__() if arch == 'ResX50': m = ResNet(Bottleneck, [3, 4, 6, 3], groups=32, width_per_group=4) print('Loaded Model RexNextssl') elif arch == 'ResS50': m = resnest50(pretrained=pre) print('Loaded model ResNest') blocks = [*m.children()] enc = blocks[:-2] self.enc = nn.Sequential(*enc) C = blocks[-1].in_features head = [ AdaptiveConcatPool2d(), Flatten(), #bs x 2*C nn.Linear(2 * C, 512), Mish() ] if norm == 'GN': head.append(nn.GroupNorm(32, 512)) print('Group Norm') elif norm == 'BN': head.append(nn.BatchNorm1d(512)) print('Batch Norm') else: print('No Norm') head.append(nn.Dropout(0.5)) head.append(nn.Linear(512, NUM_CLASSES - 1)) self.head = nn.Sequential(*head)
def fresnet50_v3(**kwargs): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ keep_dict = { 'big': [ 64, 20, 64, 26, 64, 32, 64, 116, 128, 52, 128, 39, 128, 52, 128, 231, 256, 77, 256, 231, 256, 205, 256, 103, 256, 180, 256, 128, 256, 205, 256, 205, 256, 103, 256, 103, 256, 154, 256, 77, 256, 103, 256, 410, 512, 359, 512, 154, 512 ], 'medium': [ 64, 13, 64, 13, 64, 13, 64, 52, 128, 26, 128, 26, 128, 26, 128, 103, 256, 52, 256, 77, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 154, 512, 103, 512, 103, 512 ], 'small': [ 64, 7, 64, 7, 64, 7, 64, 26, 128, 13, 128, 13, 128, 13, 128, 52, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 103, 512, 52, 512, 52, 512 ] } model = ResNet(BasicBlock_v3, [3, 4, 14, 3], keep_dict['small'], **kwargs) model.load_state_dict( torch.load( '/media/user1/Ubuntu 16.0/resnet50/model_resnet50_66.7M_0.1.pt')) return model
def __init__( self, block: nn.Module, layers: List[int], output_dims: List[int], groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: Optional[Tuple[bool, bool, bool]] = None, normalization: Normalization = None, blocks: int = 4, pretrained_settings: Optional[Dict[str, Union[str, int, float, List[Union[int, float]]]]] = None, pretrained: bool = False, progress: bool = False, ): ResNet.__init__( self, block, layers, groups=groups, width_per_group=width_per_group, norm_layer=normalization, replace_stride_with_dilation=replace_stride_with_dilation, ) Encoder.__init__(self, output_dims, pretrained_settings, pretrained, progress) self.blocks = blocks
def resnext_wsl(arch, pretrained, progress=True, **kwargs): """ models trained in weakly-supervised fashion on 940 million public images with 1.5K hashtags matching with 1000 ImageNet1K synsets, followed by fine-tuning on ImageNet1K dataset. https://github.com/facebookresearch/WSL-Images/ """ from torch.hub import load_state_dict_from_url from torchvision.models.resnet import ResNet, Bottleneck model_args = {'resnext101_32x8d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=8), 'resnext101_32x16d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=16), 'resnext101_32x32d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=32), 'resnext101_32x48d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=48)} args = model_args[arch] args.update(kwargs) model = ResNet(**args) if pretrained: model_urls = { 'resnext101_32x8d': 'https://download.pytorch.org/models/ig_resnext101_32x8-c38310e5.pth', 'resnext101_32x16d': 'https://download.pytorch.org/models/ig_resnext101_32x16-c6f796b0.pth', 'resnext101_32x32d': 'https://download.pytorch.org/models/ig_resnext101_32x32-e4b90b00.pth', 'resnext101_32x48d': 'https://download.pytorch.org/models/ig_resnext101_32x48-3e41cc8a.pth', } state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def _resnext(arch, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) return model
def __init__(self, stride=1, pretrained=False, **kwargs): super().__init__() #encoder m = ResNet(Bottleneck, [3, 4, 6, 3], groups=32, width_per_group=4) if pretrained: stt = torch.hub.load( 'facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_ssl').state_dict() m.load_state_dict(stt) self.enc0 = nn.Sequential(m.conv1, m.bn1, nn.ReLU(inplace=True)) self.enc1 = nn.Sequential( nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1), m.layer1) #256 self.enc2 = m.layer2 #512 self.enc3 = m.layer3 #1024 self.enc4 = m.layer4 #2048 #aspp with customized dilatations self.aspp = ASPP( 2048, 256, out_c=512, dilations=[stride * 1, stride * 2, stride * 3, stride * 4]) self.drop_aspp = nn.Dropout2d(0.5) #decoder self.dec4 = UnetBlock(512, 1024, 256) self.dec3 = UnetBlock(256, 512, 128) self.dec2 = UnetBlock(128, 256, 64) self.dec1 = UnetBlock(64, 64, 32) self.fpn = FPN([512, 256, 128, 64], [16] * 4) self.drop = nn.Dropout2d(0.1) self.final_conv = ConvLayer(32 + 16 * 4, 1, ks=1, norm_type=None, act_cls=None)
def resnet34_se(): model = ResNet(SEBasicBlock, [3, 4, 6, 3]) state_dict = load_state_dict_from_url(model_urls["resnet34"], progress=True) model.load_state_dict(state_dict, strict=False) return model
def __init__(self, pretrained=True, cuda=True): #super(ResNet, self).__init__( block=BasicBlock, layers=[2, 2, 2, 2] ) ResNet.__init__(self, block=BasicBlock, layers=[2, 2, 2, 2]) if pretrained: self.load_state_dict(model_zoo.load_url(resnet_urls['resnet18'])) self.filter = USM(in_channels=3, kernel_size=5, fixed_coeff=True, sigma=1.667, cuda=cuda, requires_grad=True) #self.filter.assign_weight(1.33) self.filter_conv1 = USM(in_channels=64, kernel_size=5, fixed_coeff=True, sigma=1.667, cuda=cuda)
def get_resnext(layers, pretrained, progress, **kwargs): from torchvision.models.resnet import ResNet, Bottleneck model = ResNet(Bottleneck, layers, **kwargs) model.load_state_dict( torch.load( '../input/resnext-50-ssl/semi_supervised_resnext50_32x4-ddb3e555.pth' )) return model
def resnet50_se(): model = ResNet(SEBottleneck, [3, 4, 6, 3]) state_dict = load_state_dict_from_url(model_urls["resnet50"], progress=True) model.load_state_dict(state_dict, strict=False) return model
def __init__(self, in_channels=1, n_classes=2, stride=1, inplanes=64, pre_ssl=True, **kwargs): super().__init__() store_attr('in_channels, n_classes, inplanes, pre_ssl') #encoder if pre_ssl: m = torch.hub.load( 'facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_ssl') else: m = ResNet(Bottleneck, [3, 4, 6, 3], groups=32, width_per_group=4) m.conv1.padding = (0, 0) if in_channels < 3: #print('Cutting input layer weights to', in_channels, 'channel(s).') with torch.no_grad(): m.conv1.weight = nn.Parameter(m.conv1.weight[:, :in_channels, ...]) elif in_channels > 3: m.conv1 = nn.Conv2d(in_channels, self.inplanes, kernel_size=7, stride=2, bias=False) #self.bn1 = m.bn1 if in_channels==3 else nn.BatchNorm2d(self.inplanes) self.enc0 = nn.Sequential(m.conv1, m.bn1, nn.ReLU(inplace=True)) self.enc1 = nn.Sequential( nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1), m.layer1) #256 self.enc2 = m.layer2 #512 self.enc3 = m.layer3 #1024 self.enc4 = m.layer4 #2048 #aspp with customized dilatations self.aspp = ASPP( 2048, 256, out_c=512, dilations=[stride * 1, stride * 2, stride * 3, stride * 4]) self.drop_aspp = nn.Dropout2d(0.5) #decoder self.dec4 = UnetBlock(512, 1024, 256, padding=0) self.dec3 = UnetBlock(256, 512, 128, padding=0) self.dec2 = UnetBlock(128, 256, 64, padding=0) self.dec1 = UnetBlock(64, 64, 32, padding=0) self.fpn = FPN([512, 256, 128, 64], [16] * 4) self.drop = nn.Dropout2d(0.1) self.final_conv = ConvLayer(32 + 16 * 4, n_classes, ks=1, norm_type=None, act_cls=None)
def resnext50_32x4d_se(**kwargs): kwargs["groups"] = 32 kwargs["width_per_group"] = 4 model = ResNet(SEBottleneck, [3, 4, 6, 3], **kwargs) state_dict = load_state_dict_from_url(model_urls["resnext50_32x4d"], progress=True) model.load_state_dict(state_dict, strict=False) return model
def resnet18(pretrained=False, model_dir=None, **kwargs): """Constructs a ResNet-18 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'], model_dir=model_dir)) return model
def __init__(self, pretrained=False, **kwargs): super(Mymodel, self).__init__(BasicBlock, [3, 4, 6, 3]) pre_model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs) if pretrained: pre_model.load_state_dict( model_zoo.load_url(model_urls['resnet34'])) # resnet34 fc = t.nn.Linear(in_features=1000, out_features=120) for param in pre_model.parameters(): # 预训练模型不需要学习参数 param.require_grad = False self.model = t.nn.Sequential(pre_model, fc) self.model_name = str(type(self)) # 默认名字
def _resnext(arch, num_classes, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model.load_state_dict(state_dict) in_features = model.fc.in_features model.fc = nn.Linear(in_features=in_features, out_features=num_classes, bias=True) return model
def get_resnet(resnet): if resnet == 'resnet18': return ResNet(BasicBlock, [2, 2, 2, 2]) elif resnet == 'resnet34': return ResNet(BasicBlock, [3, 4, 6, 3]) elif resnet == 'resnet50': return ResNet(Bottleneck, [3, 4, 6, 3]) elif resnet == 'resnet101': return ResNet(Bottleneck, [3, 4, 23, 3]) elif resnet == 'resnet152': return ResNet(Bottleneck, [3, 8, 36, 3])
def __init__(self, opt): super(ResNeXt50, self).__init__() self.model = ResNet(Bottleneck, [3, 4, 6, 3], groups=32, width_per_group=4) self.opt = opt # self.reduced_id_dim = opt.reduced_id_dim self.conv1x1 = nn.Conv2d(512 * Bottleneck.expansion, 512, kernel_size=1, padding=0) self.fc = nn.Linear(512 * Bottleneck.expansion, opt.num_classes)
def __init__(self, num_classes, pretrained=False, **kwargs): super(FaceModel, self).__init__() self.model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs) if pretrained: parameters = model_zoo.load_url(model_urls['resnet18']) self.model.load_state_dict(parameters) self.model.avgpool = None self.model.fc1 = nn.Linear(512 * 3 * 4, 512) self.model.fc2 = nn.Linear(512, 512) self.model.classifier = nn.Linear(512, num_classes) self.register_buffer('centers', torch.zeros(num_classes, 512)) self.num_classes = num_classes
def __init__(self, pretrained=False, **kwargs): super(Mymodel, self).__init__(BasicBlock, [3, 4, 6, 3]) pre_model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs) if pretrained: pre_model.load_state_dict(model_zoo.load_url(model_urls['resnet34'])) # resnet34 fc = t.nn.Linear(in_features=1000, out_features=120) for param in pre_model.parameters(): # 预训练模型不需要学习参数 param.require_grad = False self.model = t.nn.Sequential( pre_model, fc ) self.model_name = str(type(self)) # 默认名字
def resnet18(pretrained=False, **kwargs): """Constructs a ResNet-18 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs) # change your path model_path = '/home/jiayunpei/SSDG_github/pretrained_model/resnet18-5c106cde.pth' if pretrained: model.load_state_dict(torch.load(model_path)) print("loading model: ", model_path) # print(model) return model
def __init__(self, pretrained=False, **kwargs): super(MyNet, self).__init__() netname = args.arch.split('_')[0] if netname == 'resnet152': self.model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs) else: self.model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs) if pretrained: parameters = model_zoo.load_url(model_urls[netname]) self.model.load_state_dict(parameters) self.model.avgpool = nn.AvgPool2d(8) self.model.fc = nn.Linear(1024, 1) self.model.sig = nn.Sigmoid()
def __init__(self, n_classes=1, pretrained_resnet=True): super().__init__() self.n_classes = n_classes self.resnet = ResNet(BasicBlock, [3, 4, 6, 3], num_classes=n_classes) del self.resnet.fc if pretrained_resnet: self.resnet.load_state_dict(model_zoo.load_url( model_urls['resnet34']), strict=False) print('Loaded pretrained resnet weights') self.encoder1 = nn.Sequential( nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3, bias=False), nn.BatchNorm2d(64), nn.ReLU(inplace=True), ) self.encoder2 = nn.Sequential( nn.MaxPool2d(kernel_size=2, stride=2), self.resnet.layer1, ) # out: 64 self.encoder3 = self.resnet.layer2 # out: 128 self.encoder4 = self.resnet.layer3 # out: 256 self.encoder5 = self.resnet.layer4 # out: 512 self.center = nn.Sequential( ConvBn2d(512, 512, kernel_size=3, padding=1), nn.ReLU(inplace=True), ConvBn2d(512, 256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2) # Add ) self.decoder5 = Decoder(256, 512, 512, 64) self.decoder4 = Decoder(64, 256, 256, 64) self.decoder3 = Decoder(64, 128, 128, 64) self.decoder2 = Decoder(64, 64, 64, 64) self.decoder1 = Decoder(64, 64, 32, 64) self.logit_pixel = nn.Sequential( nn.Conv2d(320, 64, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(64, 1, kernel_size=1, padding=0), ) self.logit_feat = nn.Sequential( nn.Linear(512, 128), nn.ReLU(inplace=True), nn.Linear(128, 3), ) # 3 classes
def __init__(self, block, layers, num_classes=1000, kw=4, ka=4, fp_layers=None, align_zero=True, use_channel_quant=False, use_ckpt=False, use_multi_domain=False): ResNet.__init__(self, block, layers, num_classes) IDQ.__init__(self, ResNet.forward, kw, ka, fp_layers, align_zero, use_channel_quant, use_ckpt, use_multi_domain)
def _resnext(url, block, layers, pretrained, progress, **kwargs): # make pretrained compatible although it is not used in resnext. model = ResNet(block, layers, **kwargs) if pretrained == "imagenet" or pretrained is True: state_dict = load_state_dict_from_url(url, progress=progress) model.load_state_dict(state_dict) model.input_space = input_space model.input_range = input_range model.input_size = input_sizes model.mean = means model.std = stds return model
def resnet34(): class Dummy(): inplanes = n_features >> 3 r = Dummy() layer = lambda *a, **b: ResNet._make_layer(r, BasicBlock, *a, **b) return [ T('x', 'x', lambda x: x.permute(0, 3, 1, 2)), T( 'x', 'x', nn.Sequential( Conv2d(n_ch, n_features >> 3, kernel_size=(7, 7), padding=(3, 3), bias=False), layer(n_features >> 3, 3), layer(n_features >> 2, 4, stride=2), layer(n_features >> 1, 6, stride=2), layer(n_features, 3, stride=2), nn.AdaptiveAvgPool2d((None, 1)), # avgpool the freq. dim. )), T('x', 'x', lambda x: x.squeeze(3)), T('x', 'x', lambda x: x.transpose(1, 2)), ]
def __init__(self, feature_dim=128, group_norm=False): super(Model, self).__init__() self.f = [] for name, module in ResNet(BasicBlock, [1, 1, 1, 1], num_classes=10).named_children(): if name == 'conv1': module = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) if not isinstance(module, nn.Linear) and not isinstance( module, nn.MaxPool2d): self.f.append(module) # encoder self.f = nn.Sequential(*self.f) # projection head self.g = nn.Sequential(nn.Linear(512, 512, bias=False), nn.BatchNorm1d(512), nn.ReLU(inplace=True), nn.Linear(512, feature_dim, bias=True)) if group_norm: apply_gn(self)
def _resnet(arch, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) if pretrained: state_dict = load_state_dict_from_url(model_urls[arch], progress=progress) model = load_partial_weights(model, state_dict, verbose=progress) return model
def fresnet100_v3(**kwargs): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ keep_dict = {'180M': [58, 39, 45, 32, 45, 52, 45, 103, 116, 77, 116, 64, 116, 90, 116, 103, 116, 90, 116, 103, 116, 77, 116, 90, 116, 116, 116, 77, 116, 103, 116, 116, 116, 231, 231, 180, 231, 154, 231, 205, 231, 205, 231, 180, 231, 77, 231, 205, 231, 128, 231, 154, 231, 154, 231, 128, 231, 205, 231, 154, 231, 205, 231, 231, 231, 154, 231, 231, 231, 154, 231, 180, 231, 205, 231, 154, 231, 128, 231, 77, 231, 205, 231, 180, 231, 77, 231, 77, 231, 128, 231, 77, 231, 461, 461, 154, 461, 154, 461], '140M': [58, 13, 26, 20, 26, 20, 26, 77, 116, 39, 116, 39, 116, 52, 116, 77, 116, 64, 116, 52, 116, 39, 116, 64, 116, 64, 116, 39, 116, 64, 116, 52, 116, 231, 231, 128, 231, 103, 231, 77, 231, 154, 231, 103, 231, 77, 231, 128, 231, 77, 231, 77, 231, 103, 231, 77, 231, 128, 231, 77, 231, 128, 231, 128, 231, 103, 231, 77, 231, 103, 231, 128, 231, 128, 231, 77, 231, 77, 231, 77, 231, 128, 231, 77, 231, 52, 231, 77, 231, 77, 231, 52, 231, 410, 461, 103, 461, 103, 461]} model = ResNet(BasicBlock_v3, [3, 13, 30, 3], keep_dict['140M'], **kwargs) return model
def fresnet100_v3(**kwargs): """ Constructs a ResNet-100 model. :param kwargs: :return: """ model = ResNet(BasicBlock_v3, [3, 13, 30, 3], **kwargs) return model
def __init__(self, base_resnet: ResNet, n_classes: int): super().__init__() resnet_children = list(base_resnet.children()) self.activations = nn.Sequential(*resnet_children[:-1]) self.flat = Flatten() self.fc = nn.Linear(base_resnet.fc.in_features, n_classes)
def __init__(self,bits_len,pretrained=False,**kwargs): super(OriNet,self).__init__() self.bits_len=bits_len self.resnetmodel=ResNet(BasicBlock,[2,2,2,2],**kwargs) if pretrained: parameters=model_zoo.load_url(model_urls['resnet18']) self.model.load_state_dict(parameters) self.resnetmodel.avgpool=None self.resnetmodel.fc1=nn.Linear(512*3*4,512) self.resnetmodel.fc2=nn.Linear(512,512) self.resnetmodel.classifier=nn.Linear(512,bits_len) self.register_buffer('centers',torch.zeros(bits_len,512))
class OriNet(nn.Module): def __init__(self,bits_len,pretrained=False,**kwargs): super(OriNet,self).__init__() self.bits_len=bits_len self.resnetmodel=ResNet(BasicBlock,[2,2,2,2],**kwargs) if pretrained: parameters=model_zoo.load_url(model_urls['resnet18']) self.model.load_state_dict(parameters) self.resnetmodel.avgpool=None self.resnetmodel.fc1=nn.Linear(512*3*4,512) self.resnetmodel.fc2=nn.Linear(512,512) self.resnetmodel.classifier=nn.Linear(512,bits_len) self.register_buffer('centers',torch.zeros(bits_len,512)) def forward(self,input1): input1=self.resnetmodel.conv1(input1) input1=self.resnetmodel.bn1(input1) input1=self.resnetmodel.relu(input1) input1=self.resnetmodel.maxpool(input1) input1=self.resnetmodel.layer1(input1) input1=self.resnetmodel.layer2(input1) input1=self.resnetmodel.layer3(input1) input1=self.resnetmodel.layer4(input1) input1=input1.view(input1.size(),-1) input1=self.resnetmodel.fc1(input1) #feature for center loss input1=self.resnetmodel.fc2(input1) self.features=input1 input1=self.resnetmodel.classifier(input1) output1=F.log_softmax(input1) return output1