本文整理汇总了Python中torchvision.models.resnet.resnet50方法的典型用法代码示例。如果您正苦于以下问题:Python resnet.resnet50方法的具体用法?Python resnet.resnet50怎么用?Python resnet.resnet50使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models.resnet
的用法示例。
在下文中一共展示了resnet.resnet50方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _load_resnet_imagenet
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def _load_resnet_imagenet(pretrained=True):
# huge thx to https://github.com/ruotianluo/pytorch-faster-rcnn/blob/master/lib/nets/resnet_v1.py
backbone = resnet.resnet50(pretrained=pretrained)
for i in range(2, 4):
getattr(backbone, 'layer%d' % i)[0].conv1.stride = (2, 2)
getattr(backbone, 'layer%d' % i)[0].conv2.stride = (1, 1)
# use stride 1 for the last conv4 layer (same as tf-faster-rcnn)
backbone.layer4[0].conv2.stride = (1, 1)
backbone.layer4[0].downsample[0].stride = (1, 1)
# # Make batchnorm more sensible
# for submodule in backbone.modules():
# if isinstance(submodule, torch.nn.BatchNorm2d):
# submodule.momentum = 0.01
return backbone
示例2: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def __init__(self, num_classes=None, last_stride=1, pretrained=False):
super().__init__()
self.base = ResNet(last_stride)
if pretrained:
model_url = 'https://download.pytorch.org/models/resnet50-19c8e357.pth'
self.base.load_param(model_zoo.load_url(model_url))
self.num_classes = num_classes
if num_classes is not None:
self.bottleneck = nn.Sequential(
nn.Linear(self.in_planes, 512),
nn.BatchNorm1d(512),
nn.LeakyReLU(0.1),
nn.Dropout(p=0.5)
)
self.bottleneck.apply(weights_init_kaiming)
self.classifier = nn.Linear(512, self.num_classes)
self.classifier.apply(weights_init_classifier)
示例3: _load_resnet
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def _load_resnet(pretrained=True):
# huge thx to https://github.com/ruotianluo/pytorch-faster-rcnn/blob/master/lib/nets/resnet_v1.py
backbone = resnet.resnet50(pretrained=False)
if pretrained:
backbone.load_state_dict(model_zoo.load_url(
'https://s3.us-west-2.amazonaws.com/ai2-rowanz/resnet50-e13db6895d81.th'))
for i in range(2, 4):
getattr(backbone, 'layer%d' % i)[0].conv1.stride = (2, 2)
getattr(backbone, 'layer%d' % i)[0].conv2.stride = (1, 1)
return backbone
示例4: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def __init__(self, pretrained=True, input_channels=3):
model = resnet.resnet50(pretrained=pretrained)
super().__init__(
model=model,
input_channels=input_channels)
示例5: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def __init__(self):
super().__init__()
self.net = resnet.resnet50(pretrained=True)
self.stage1 = nn.Sequential(
self.net.conv1,
self.net.bn1,
self.net.relu,
self.net.maxpool
)
self.stage2 = self.net.layer1
self.stage3 = self.net.layer2
self.stage4 = self.net.layer3
self.stage5 = self.net.layer4
示例6: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def __init__(self, feature_channels=2048):
super().__init__(feature_channels)
self.res_net = resnet.resnet50()
self.res_net.avgpool = submodules.identity()
self.res_net.fc = submodules.identity()
weight_init.init(self.modules())
示例7: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def __init__(self, labelclassMap, numAnchors=9, backbone='resnet50', pretrained=True, out_planes=256, convertToInstanceNorm=False):
super(RetinaNet, self).__init__()
self.labelclassMap = labelclassMap
self.numClasses = len(labelclassMap.keys())
self.numAnchors = numAnchors
self.backbone = backbone
self.pretrained = pretrained
self.out_planes = out_planes
self.convertToInstanceNorm = convertToInstanceNorm
self.fpn = FPN(self.backbone, self.pretrained, self.out_planes, self.convertToInstanceNorm)
self.loc_head = self._make_head(self.numAnchors*4, dropout=0.2)
self.cls_head = self._make_head(self.numAnchors*self.numClasses, dropout=None)
示例8: loadFromStateDict
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def loadFromStateDict(stateDict):
# parse args
labelclassMap = stateDict['labelclassMap']
numAnchors = (stateDict['numAnchors'] if 'numAnchors' in stateDict else 9)
backbone = (stateDict['backbone'] if 'backbone' in stateDict else resnet.resnet50)
pretrained = (stateDict['pretrained'] if 'pretrained' in stateDict else True)
out_planes = (stateDict['out_planes'] if 'out_planes' in stateDict else 256)
convertToInstanceNorm = (stateDict['convertToInstanceNorm'] if 'convertToInstanceNorm' in stateDict else False)
state = (stateDict['model_state'] if 'model_state' in stateDict else None)
# return model
model = RetinaNet(labelclassMap, numAnchors, backbone, pretrained, out_planes, convertToInstanceNorm)
if state is not None:
model.load_state_dict(state)
return model
示例9: get
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def get(cls, args):
model = ResNet3DAutoencoder([3, 4, 6, 3]) # 50
if args.pretrained:
from torchvision.models.resnet import resnet50
model2d = resnet50(pretrained=True)
model.encoder.load_2d(model2d)
return model
示例10: get
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def get(cls, args):
model = ResNet3DNonLocal(Bottleneck3D, [3, 4, 6, 3]) # 50
if args.pretrained:
from torchvision.models.resnet import resnet50
model2d = resnet50(pretrained=True)
model.load_2d(model2d)
model.insert_nonlocal_blocks([0, 2, 3, 0])
return model
示例11: get
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def get(cls, args):
model = ResNet3DEncoder(Bottleneck3D, [3, 4, 6, 3]) # 50
if args.pretrained:
from torchvision.models.resnet import resnet50
model2d = resnet50(pretrained=True)
model.load_2d(model2d)
return model
示例12: get
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def get(cls, args):
model = ResNet3D(Bottleneck3D, [3, 4, 6, 3]) # 50
if args.pretrained:
from torchvision.models.resnet import resnet50
model2d = resnet50(pretrained=True)
model.load_2d(model2d)
return model
示例13: test_actor_observer_with_classifier_updates
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def test_actor_observer_with_classifier_updates(self):
torch.manual_seed(12345)
from torchvision.models.resnet import resnet50
from models.wrappers.actor_observer_with_classifier_wrapper import ActorObserverWithClassifierWrapper
from models.criteria.actor_observer_with_classifier_criterion import ActorObserverWithClassifierCriterion
args = Args()
args.nclass = 157
args.freeze_batchnorm = False
args.finaldecay = 0.9
args.decay = 0.9
args.margin = 0.0
args.classifier_weight = 1.0
args.share_selector = False
args.normalize_per_video = False
model = resnet50()
model = ActorObserverWithClassifierWrapper(model, args)
b, d = 10, 224
inputs = [torch.randn(b, 3, d, d), torch.randn(b, 3, d, d), torch.randn(b, 3, d, d)]
meta = {'thirdtime': torch.zeros(b),
'firsttime_pos': torch.zeros(b),
'firsttime_neg': torch.zeros(b),
'n': torch.zeros(b),
'n_ego': torch.zeros(b),
'id': ['asdf'] * b,
}
target = torch.ones(b, args.nclass)
target[b//2:, 0] = -1
args.balanceloss = False
args.window_smooth = 0
criterion = ActorObserverWithClassifierCriterion(args)
test_model_updates(inputs, model, target, criterion, meta)
示例14: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def __init__(self, arch, scene_classes=1055):
super(RGBBranch, self).__init__()
# --------------------------------#
# Base Network #
# ------------------------------- #
if arch == 'ResNet-18':
# ResNet-18 Network
base = resnet.resnet18(pretrained=True)
# Size parameters for ResNet-18
size_fc_RGB = 512
elif arch == 'ResNet-50':
# ResNet-50 Network
base = resnet.resnet50(pretrained=True)
# Size parameters for ResNet-50
size_fc_RGB = 2048
# --------------------------------#
# RGB Branch #
# ------------------------------- #
# First initial block
self.in_block = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1, return_indices=True)
)
# Encoder
self.encoder1 = base.layer1
self.encoder2 = base.layer2
self.encoder3 = base.layer3
self.encoder4 = base.layer4
# -------------------------------------#
# RGB Classifier #
# ------------------------------------ #
self.dropout = nn.Dropout(0.3)
self.avgpool = nn.AvgPool2d(7, stride=1)
self.fc = nn.Linear(size_fc_RGB, scene_classes)
# Loss
self.criterion = nn.CrossEntropyLoss()
示例15: create_model
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import resnet50 [as 别名]
def create_model(model_name, num_classes=1000, pretrained=False, **kwargs):
if 'test_time_pool' in kwargs:
test_time_pool = kwargs.pop('test_time_pool')
else:
test_time_pool = True
if model_name == 'dpn68':
model = dpn68(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn68b':
model = dpn68b(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn92':
model = dpn92(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn98':
model = dpn98(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn131':
model = dpn131(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'dpn107':
model = dpn107(
pretrained=pretrained, test_time_pool=test_time_pool, num_classes=num_classes)
elif model_name == 'resnet18':
model = resnet18(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet34':
model = resnet34(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet50':
model = resnet50(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet101':
model = resnet101(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'resnet152':
model = resnet152(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet121':
model = densenet121(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet161':
model = densenet161(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet169':
model = densenet169(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'densenet201':
model = densenet201(pretrained=pretrained, num_classes=num_classes, **kwargs)
elif model_name == 'inception_v3':
model = inception_v3(
pretrained=pretrained, num_classes=num_classes, transform_input=False, **kwargs)
else:
assert False, "Unknown model architecture (%s)" % model_name
return model