我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用torchvision.models.resnet34()。
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 resnet34_weldon(num_classes, pretrained=True, kmax=1, kmin=None): model = models.resnet34(pretrained) pooling = WeldonPool2d(kmax, kmin) return ResNetWSL(model, num_classes, pooling=pooling)
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 resnet34(num_classes=1000, pretrained='imagenet'): """Constructs a ResNet-34 model. """ model = models.resnet34(pretrained=False) if pretrained is not None: settings = pretrained_settings['resnet34'][pretrained] model = load_pretrained(model, num_classes, settings) model = modify_resnets(model) return model