我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用torchvision.models.resnet101()。
def __init__(self, args): nclass=args.nclass super(Net, self).__init__() self.backbone = args.backbone # copying modules from pretrained models if self.backbone == 'resnet50': self.pretrained = resnet.resnet50(pretrained=True) elif self.backbone == 'resnet101': self.pretrained = resnet.resnet101(pretrained=True) elif self.backbone == 'resnet152': self.pretrained = resnet.resnet152(pretrained=True) else: raise RuntimeError('unknown backbone: {}'.format(self.backbone)) n_codes = 32 self.head = nn.Sequential( nn.Conv2d(2048, 128, 1), nn.BatchNorm2d(128), nn.ReLU(inplace=True), encoding.nn.Encoding(D=128,K=n_codes), encoding.nn.View(-1, 128*n_codes), encoding.nn.Normalize(), nn.Linear(128*n_codes, nclass), )
def GetPretrainedModel(params, num_classes): if params['model'] == 'resnet18': model = models.resnet18(pretrained=True) elif params['model'] == 'resnet34': model = models.resnet34(pretrained=True) elif params['model'] == 'resnet50': model = models.resnet50(pretrained=True) elif params['model'] == 'resnet101': model = models.resnet101(pretrained=True) elif params['model'] == 'resnet152': model = models.resnet152(pretrained=True) else: raise ValueError('Unknown model type') num_features = model.fc.in_features model.fc = SigmoidLinear(num_features, num_classes) return model
def resnet101_wildcat(num_classes, pretrained=True, kmax=1, kmin=None, alpha=1, num_maps=1): model = models.resnet101(pretrained) pooling = nn.Sequential() pooling.add_module('class_wise', ClassWisePool(num_maps)) pooling.add_module('spatial', WildcatPool2d(kmax, kmin, alpha)) return ResNetWSL(model, num_classes * num_maps, pooling=pooling)
def __init__(self, num_classes, pretrained=True, use_aux=True): super(PSPNet, self).__init__() self.use_aux = use_aux resnet = models.resnet101() if pretrained: resnet.load_state_dict(torch.load(res101_path)) self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool) self.layer1, self.layer2, self.layer3, self.layer4 = resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4 for n, m in self.layer3.named_modules(): if 'conv2' in n: m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1) elif 'downsample.0' in n: m.stride = (1, 1) for n, m in self.layer4.named_modules(): if 'conv2' in n: m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1) elif 'downsample.0' in n: m.stride = (1, 1) self.ppm = _PyramidPoolingModule(2048, 512, (1, 2, 3, 6)) self.final = nn.Sequential( nn.Conv2d(4096, 512, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(512, momentum=.95), nn.ReLU(inplace=True), nn.Dropout(0.1), nn.Conv2d(512, num_classes, kernel_size=1) ) if use_aux: self.aux_logits = nn.Conv2d(1024, num_classes, kernel_size=1) initialize_weights(self.aux_logits) initialize_weights(self.ppm, self.final)
def resnet101_weldon(num_classes, pretrained=True, kmax=1, kmin=None): model = models.resnet101(pretrained) pooling = WeldonPool2d(kmax, kmin) return ResNetWSL(model, num_classes, pooling=pooling)
def __init__(self): super(ResNet,self).__init__() self.features = nn.Sequential(*list(resnet101.children())[:-3])
def __init__(self, opt): super().__init__() self.opt = opt if opt.netSpec == 'resnet101': resnet = models.resnet101(pretrained=opt.pretrain) elif opt.netSpec == 'resnet50': resnet = models.resnet50(pretrained=opt.pretrain) elif opt.netSpec == 'resnet34': resnet = models.resnet34(pretrained=opt.pretrain) self.conv1 = resnet.conv1 self.layer1 = resnet.layer1 self.layer2 = resnet.layer2 self.layer3 = resnet.layer3 self.layer4 = resnet.layer4 for m in self.modules(): if isinstance(m, nn.Conv2d): # m.stride = 1 m.requires_grad = False if isinstance(m, nn.BatchNorm2d): m.requires_grad = False self.layer5a = PSPDec(512, 128, (1,1)) self.layer5b = PSPDec(512, 128, (2,2)) self.layer5c = PSPDec(512, 128, (3,3)) self.layer5d = PSPDec(512, 128, (6,6)) self.final = nn.Sequential( nn.Conv2d(512*2, 512, 3, padding=1, bias=False), nn.BatchNorm2d(512, momentum=.95), nn.ReLU(inplace=True), nn.Dropout(.1), nn.Conv2d(512, opt.numClasses, 1), )
def resnet101(num_classes=1000, pretrained='imagenet'): """Constructs a ResNet-101 model. """ model = models.resnet101(pretrained=False) if pretrained is not None: settings = pretrained_settings['resnet101'][pretrained] model = load_pretrained(model, num_classes, settings) model = modify_resnets(model) return model
def load_pretrained_model(faster_rcnn_model, model_name='vgg16'): if model_name == 'vgg16': model = models.vgg16(pretrained=True) faster_rcnn_model.rpn.features = model.features mod = list(model.classifier.children())[:-1] faster_rcnn_model.fcs = nn.Sequential(*mod) elif model_name == 'resnet101': model = models.resnet101(pretrained=True) faster_rcnn_model.rpn.features = nn.Sequential(model.conv1, model.bn1, model.relu, model.maxpool, model.layer1, model.layer2, model.layer3, model.layer4, model.avgpool) faster_rcnn_model.fcs = model.fc
def __init__(self, num_classes, input_size, pretrained=True, use_aux=True): super(PSPNetDeform, self).__init__() self.input_size = input_size self.use_aux = use_aux resnet = models.resnet101() if pretrained: resnet.load_state_dict(torch.load(res101_path)) self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool) self.layer1 = resnet.layer1 self.layer2 = resnet.layer2 self.layer3 = resnet.layer3 self.layer4 = resnet.layer4 for n, m in self.layer3.named_modules(): if 'conv2' in n: m.padding = (1, 1) m.stride = (1, 1) elif 'downsample.0' in n: m.stride = (1, 1) for n, m in self.layer4.named_modules(): if 'conv2' in n: m.padding = (1, 1) m.stride = (1, 1) elif 'downsample.0' in n: m.stride = (1, 1) for idx in range(len(self.layer3)): self.layer3[idx].conv2 = Conv2dDeformable(self.layer3[idx].conv2) for idx in range(len(self.layer4)): self.layer4[idx].conv2 = Conv2dDeformable(self.layer4[idx].conv2) self.ppm = _PyramidPoolingModule(2048, 512, (1, 2, 3, 6)) self.final = nn.Sequential( nn.Conv2d(4096, 512, kernel_size=3, padding=1, bias=False), nn.BatchNorm2d(512, momentum=.95), nn.ReLU(inplace=True), nn.Dropout(0.1), nn.Conv2d(512, num_classes, kernel_size=1) ) if use_aux: self.aux_logits = nn.Conv2d(1024, num_classes, kernel_size=1) initialize_weights(self.aux_logits) initialize_weights(self.ppm, self.final)