def resnet18nodownsample(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], nodownsampling=True, **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18NoDownsampling' return model
def resnet18_ep0(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], disable_early_pooling=True, **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18_EP0' return model
def resnet18_dp00_dspl2(pretrained=False, **kwargs): """Constructs a ResNet-18 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(BasicBlock, [4, 4, None, None], disable_early_pooling=True, disable_early_downsampling=True, **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18_dp00_DSPL2' return model
def se_resnet50(num_classes=1000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict(model_zoo.load_url("https://www.dropbox.com/s/xpq8ne7rwa4kg4c/seresnet50-60a8950a85b2b.pkl")) return model
def resnet152(pretrained=False, **kwargs): """Constructs a ResNet-152 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet152'])) model.name = 'ResNet152' return model
def resnet18_dspl5(pretrained=False, **kwargs): """Constructs a ResNet-18 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(BasicBlock, [1, 1, 2, 2, 2, None, None, None], **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18_DSPL5' return model
def se_resnet50(num_classes=1000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict(load_state_dict_from_url( "https://github.com/moskomule/senet.pytorch/releases/download/archive/seresnet50-60a8950a85b2b.pkl")) return model
def resnet18_XXS(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], scale_factor=8, **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18_XXS' return model
def resnet18noskip_dspl3(pretrained=False, **kwargs): """Constructs a ResNet-18 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ kwargs["noskip"] = True model = ResNet(BasicBlock, [2, 3, 3, None], **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18NoSkip_DSPL3' return model
def resnet34(pretrained=False, **kwargs): """Constructs a ResNet-34 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet34'])) model.name = 'ResNet34' return model
def resnet18noskip(pretrained=False, **kwargs): """Constructs a ResNet-18 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ if "noskip" in kwargs: kwargs.pop("noskip") model = ResNet(BasicBlock, [2, 2, 2, 2], noskip=True, **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet18'])) model.name = 'ResNet18NoSkip' return model
def resnet50noskip(pretrained=False, **kwargs): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ if "noskip" in kwargs: kwargs.pop("noskip") model = ResNet(Bottleneck, [3, 4, 6, 3], noskip=True, **kwargs) if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['resnet50'])) model.name = 'ResNet50NoSkip' return model
def resnet152(pretrained=False, **kwargs): if pretrained: model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs) pretrained_state_dict = torch.load( './Authority/resnet152-b121ed2d.pth' ) # load_url函数根据model_urls字典下载或导入相应的预训练模型 now_state_dict = model.state_dict() # 返回model模块的字典 pretrained_state_dict.pop('fc.weight') # 排除全连接层的参数(全连接层返回分类个数) pretrained_state_dict.pop('fc.bias') now_state_dict.update(pretrained_state_dict) model.load_state_dict(now_state_dict) # 最后通过调用model的load_state_dict方法用预训练的模型参数来初始化你构建的网络结构, # 这个方法就是PyTorch中通用的用一个模型的参数初始化另一个模型的层的操作。load_state_dict方法还有一个重要的参数是strict, # 该参数默认是True,表示预训练模型的层和你的网络结构层严格对应相等(比如层名和维度) return model return ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
def se_resnet34(num_classes=1000, pretrained="/home/ibian/.torch/models/resnet34-333f7ec4.pth"): """Constructs a ResNet-34 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) # use pretrained model if pretrained is not None: print("=> loading pretrained model '{}'".format(pretrained)) model_dict = model.state_dict() checkpoint = torch.load(pretrained) checkpoint = {k: v for k, v in checkpoint.items() if k in model_dict} model_dict.update(checkpoint) model.load_state_dict(model_dict) return model
def resnet18_domain(classes=4, domains=30, url=None, **kwargs): """Constructs a ResNet-18 model. Args: num_classes (int): the number of classes of the classification model domains (int): the number of domains included in the model (#source + #auxuliary) """ # Instantiate original ResNet model_origin = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs) model_origin.load_state_dict(model_zoo.load_url(url)) # Instatiate Domain ResNet model = DomainResNet(DomainBlock, [2, 2, 2, 2], domains=domains, **kwargs) # Copy BN stats and params from the original to the domain-based c = 0 for m_orig in model_origin.named_modules(): if 'bn' in m_orig[0] or 'downsample.1' in m_orig[0]: for m_doms in model.named_modules(): if m_doms[0] == m_orig[0]: c += 1 for i in range(domains): m_doms[1].bns[i].running_var.data[:] = m_orig[ 1].running_var.data[:] m_doms[1].bns[i].running_mean.data[:] = m_orig[ 1].running_mean.data[:] m_doms[1].scale.data[i, :] = m_orig[1].weight.data[:] m_doms[1].bias.data[i, :] = m_orig[1].bias.data[:] elif 'conv' in m_orig[0] or 'downsample.0' in m_orig[0]: for m_doms in model.named_modules(): if m_doms[0] == m_orig[0]: m_doms[1].weight.data[:] = m_orig[1].weight.data[:] # Init classifier num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, classes) for m in model.modules(): if isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.0001) return model
class ClippedResNet(nn.Module): """ """ def __init__(self, pretrained=False, out_features=2): """ """ super().__init__() self.net = ResNet(BasicBlock, [2, 2, 2, 2]) if pretrained: self.net.load_state_dict( model_zoo.load_url(model_urls['resnet_18'])) # change last layer self.net.fc = nn.Linear(self.net.fc.in_features, out_features) self.out_features = out_features def forward(self, input): """ """ return self.net(input)
model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model def se_resnet50(num_classes=1_000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict( load_state_dict_from_url( "https://github.com/moskomule/senet.pytorch/releases/download/archive/seresnet50-60a8950a85b2b.pkl" )) return model def se_resnet101(num_classes=1_000): """Constructs a ResNet-101 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model
""" model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model def se_resnet50(num_classes=1_000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict(model_zoo.load_url("https://www.dropbox.com/s/xpq8ne7rwa4kg4c/seresnet50-60a8950a85b2b.pkl")) return model def se_resnet101(num_classes=1_000): """Constructs a ResNet-101 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model def se_resnet152(num_classes=1_000):
class UNetResNet(nn.Module): """PyTorch U-Net model using ResNet(34, 50, 101 or 152) encoder. UNet: https://arxiv.org/abs/1505.04597 ResNet: https://arxiv.org/abs/1512.03385 Proposed by Alexander Buslaev: https://www.linkedin.com/in/al-buslaev/ Args: encoder_depth (int): Depth of a ResNet encoder (34, 101 or 152). num_classes (int): Number of output classes. num_filters (int, optional): Number of filters in the last layer of decoder. Defaults to 32. dropout_2d (float, optional): Probability factor of dropout layer before output layer. Defaults to 0.2. pretrained (bool, optional): False - no pre-trained weights are being used. True - ResNet encoder is pre-trained on ImageNet. Defaults to False. is_deconv (bool, optional): False: bilinear interpolation is used in decoder. True: deconvolution is used in decoder. Defaults to False. """ def __init__(self, encoder_depth, num_classes, num_filters=32, dropout_2d=0.2, pretrained=False, is_deconv=False): super().__init__() self.num_classes = num_classes self.dropout_2d = dropout_2d if encoder_depth == 34: self.encoder = ResNet(SEBasicBlock, [3, 4, 6, 3]) if pretrained: self.encoder.load_state_dict(model_zoo.load_url( model_urls["resnet34"]), strict=False) bottom_channel_nr = 512 elif encoder_depth == 50: self.encoder = ResNet(SEBottleneck, [3, 4, 6, 3]) if pretrained: self.encoder.load_state_dict(model_zoo.load_url( model_urls["resnet50"]), strict=False) bottom_channel_nr = 2048 elif encoder_depth == 101: self.encoder = ResNet(SEBottleneck, [3, 4, 23, 3]) if pretrained: self.encoder.load_state_dict(model_zoo.load_url( model_urls["resnet101"]), strict=False) bottom_channel_nr = 2048 elif encoder_depth == 152: # self.encoder = torchvision.models.resnet152(pretrained=pretrained) self.encoder = ResNet(SEBottleneck, [3, 8, 36, 3]) if pretrained: self.encoder.load_state_dict(model_zoo.load_url( model_urls["resnet152"]), strict=False) bottom_channel_nr = 2048 else: raise NotImplementedError( 'only 34, 50, 101, 152 version of Resnet are implemented') self.input_adjust = nn.Sequential(self.encoder.conv1, self.encoder.bn1, self.encoder.relu) self.conv1 = self.encoder.layer1 self.conv2 = self.encoder.layer2 self.conv3 = self.encoder.layer3 self.conv4 = self.encoder.layer4 self.dec4 = DecoderBlockV2(bottom_channel_nr, num_filters * 8 * 2, num_filters * 8, is_deconv) self.dec3 = DecoderBlockV2(bottom_channel_nr // 2 + num_filters * 8, num_filters * 8 * 2, num_filters * 8, is_deconv) self.dec2 = DecoderBlockV2(bottom_channel_nr // 4 + num_filters * 8, num_filters * 4 * 2, num_filters * 2, is_deconv) self.dec1 = DecoderBlockV2(bottom_channel_nr // 8 + num_filters * 2, num_filters * 2 * 2, num_filters * 2 * 2, is_deconv) self.final = nn.Conv2d(num_filters * 2 * 2, num_classes, kernel_size=1) def forward(self, x): input_adjust = self.input_adjust(x) conv1 = self.conv1(input_adjust) conv2 = self.conv2(conv1) conv3 = self.conv3(conv2) center = self.conv4(conv3) dec4 = self.dec4(center) dec3 = self.dec3(torch.cat([dec4, conv3], 1)) dec2 = self.dec2(torch.cat([dec3, conv2], 1)) dec1 = F.dropout2d(self.dec1(torch.cat([dec2, conv1], 1)), p=self.dropout_2d) return self.final(dec1)
model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model def se_resnet50(num_classes=1_000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict( model_zoo.load_url( "https://www.dropbox.com/s/xpq8ne7rwa4kg4c/seresnet50-f6e7c9de8bf52b5.pkl" )) return model def se_resnet101(num_classes=1_000): """Constructs a ResNet-101 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model
return model def se_resnet50(num_classes=1_000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3]) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: # state_dict = load_state_dict_from_url(model_urls['resnet50'], # progress=progress) # model.load_state_dict(state_dict) model.load_state_dict(torch.load(PATH)) num_features = model.fc.in_features model.fc = nn.Linear(num_features, num_classes) return model def se_resnet101(num_classes=1_000): """Constructs a ResNet-101 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) return model
# model.avgpool = nn.AdaptiveAvgPool2d(1) # return model # def se_resnet50(num_classes=1_000, pretrained=False): """Constructs a ResNet-50 model. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet """ model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes) model.avgpool = nn.AdaptiveAvgPool2d(1) if pretrained: model.load_state_dict( model_zoo.load_url( "http://data.lip6.fr/cadene/pretrainedmodels/se_resnet50-ce0d4300.pth" )) return model # def se_resnet101(num_classes=1_000): # """Constructs a ResNet-101 model. # # Args: # pretrained (bool): If True, returns a model pre-trained on ImageNet # """ # model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes) # model.avgpool = nn.AdaptiveAvgPool2d(1) # return model # #