本文整理汇总了Python中torchvision.models.resnet152方法的典型用法代码示例。如果您正苦于以下问题:Python models.resnet152方法的具体用法?Python models.resnet152怎么用?Python models.resnet152使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models
的用法示例。
在下文中一共展示了models.resnet152方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_pretrained_resnet
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def get_pretrained_resnet(new_fc_dim=None):
"""
Fetches a pretrained resnet model (downloading if necessary) and chops off the top linear
layer. If new_fc_dim isn't None, then a new linear layer is added.
:param new_fc_dim:
:return:
"""
resnet152 = models.resnet152(pretrained=True)
del resnet152.fc
if new_fc_dim is not None:
resnet152.fc = nn.Linear(ENCODING_SIZE, new_fc_dim)
_init_fc(resnet152.fc)
else:
resnet152.fc = lambda x: x
return resnet152
示例2: test_untargeted_resnet152
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def test_untargeted_resnet152(image, label=None):
import torch
import torchvision.models as models
from perceptron.models.classification import PyTorchModel
mean = np.array([0.485, 0.456, 0.406]).reshape((3, 1, 1))
std = np.array([0.229, 0.224, 0.225]).reshape((3, 1, 1))
model_pyt = models.resnet152(pretrained=True).eval()
if torch.cuda.is_available():
model_pyt = model_pyt.cuda()
model = PyTorchModel(
model_pyt, bounds=(0, 1), num_classes=1000, preprocessing=(mean, std))
print(np.argmax(model.predictions(image)))
attack = Attack(model, criterion=Misclassification())
adversarial_obj = attack(image, label, unpack=False, epsilons=10000)
distance = adversarial_obj.distance
adversarial = adversarial_obj.image
return distance, adversarial
示例3: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, num_layers, pretrained, num_input_images=1):
super(ResnetEncoder, self).__init__()
self.num_ch_enc = np.array([64, 64, 128, 256, 512])
resnets = {18: models.resnet18,
34: models.resnet34,
50: models.resnet50,
101: models.resnet101,
152: models.resnet152}
if num_layers not in resnets:
raise ValueError("{} is not a valid number of resnet layers".format(num_layers))
if num_input_images > 1:
self.encoder = resnet_multiimage_input(num_layers, pretrained, num_input_images)
else:
self.encoder = resnets[num_layers](pretrained)
if num_layers > 34:
self.num_ch_enc[1:] *= 4
示例4: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self,option = 'resnet18',pret=True):
super(ResBase, self).__init__()
self.dim = 2048
if option == 'resnet18':
model_ft = models.resnet18(pretrained=pret)
self.dim = 512
if option == 'resnet50':
model_ft = models.resnet50(pretrained=pret)
if option == 'resnet101':
model_ft = models.resnet101(pretrained=pret)
if option == 'resnet152':
model_ft = models.resnet152(pretrained=pret)
if option == 'resnet200':
model_ft = Res200()
if option == 'resnetnext':
model_ft = ResNeXt(layer_num=101)
mod = list(model_ft.children())
mod.pop()
#self.model_ft =model_ft
self.features = nn.Sequential(*mod)
示例5: get_model
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def get_model(args):
assert args.type in ['2d', '3d']
if args.type == '2d':
print('Loading 2D-ResNet-152 ...')
model = models.resnet152(pretrained=True)
model = nn.Sequential(*list(model.children())[:-2], GlobalAvgPool())
model = model.cuda()
else:
print('Loading 3D-ResneXt-101 ...')
model = resnext.resnet101(
num_classes=400,
shortcut_type='B',
cardinality=32,
sample_size=112,
sample_duration=16,
last_fc=False)
model = model.cuda()
model_data = th.load(args.resnext101_model_path)
model.load_state_dict(model_data)
model.eval()
print('loaded')
return model
示例6: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, requires_grad=False, pretrained=True, num=18):
super(resnet, self).__init__()
if(num==18):
self.net = tv.resnet18(pretrained=pretrained)
elif(num==34):
self.net = tv.resnet34(pretrained=pretrained)
elif(num==50):
self.net = tv.resnet50(pretrained=pretrained)
elif(num==101):
self.net = tv.resnet101(pretrained=pretrained)
elif(num==152):
self.net = tv.resnet152(pretrained=pretrained)
self.N_slices = 5
self.conv1 = self.net.conv1
self.bn1 = self.net.bn1
self.relu = self.net.relu
self.maxpool = self.net.maxpool
self.layer1 = self.net.layer1
self.layer2 = self.net.layer2
self.layer3 = self.net.layer3
self.layer4 = self.net.layer4
示例7: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, requires_grad=False, pretrained=True, num=18):
super(resnet, self).__init__()
if(num==18):
self.net = models.resnet18(pretrained=pretrained)
elif(num==34):
self.net = models.resnet34(pretrained=pretrained)
elif(num==50):
self.net = models.resnet50(pretrained=pretrained)
elif(num==101):
self.net = models.resnet101(pretrained=pretrained)
elif(num==152):
self.net = models.resnet152(pretrained=pretrained)
self.N_slices = 5
self.conv1 = self.net.conv1
self.bn1 = self.net.bn1
self.relu = self.net.relu
self.maxpool = self.net.maxpool
self.layer1 = self.net.layer1
self.layer2 = self.net.layer2
self.layer3 = self.net.layer3
self.layer4 = self.net.layer4
示例8: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, requires_grad=False, pretrained=True, num=18):
super(resnet, self).__init__()
if (num == 18):
self.net = models.resnet18(pretrained=pretrained)
elif (num == 34):
self.net = models.resnet34(pretrained=pretrained)
elif (num == 50):
self.net = models.resnet50(pretrained=pretrained)
elif (num == 101):
self.net = models.resnet101(pretrained=pretrained)
elif (num == 152):
self.net = models.resnet152(pretrained=pretrained)
self.N_slices = 5
self.conv1 = self.net.conv1
self.bn1 = self.net.bn1
self.relu = self.net.relu
self.maxpool = self.net.maxpool
self.layer1 = self.net.layer1
self.layer2 = self.net.layer2
self.layer3 = self.net.layer3
self.layer4 = self.net.layer4
示例9: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, layers, atrous, pretrained=True):
super(ResNet, self).__init__()
self.inner_layer = []
if layers == 18:
self.backbone = models.resnet18(pretrained=pretrained)
elif layers == 34:
self.backbone = models.resnet34(pretrained=pretrained)
elif layers == 50:
self.backbone = models.resnet50(pretrained=pretrained)
elif layers == 101:
self.backbone = models.resnet101(pretrained=pretrained)
elif layers == 152:
self.backbone = models.resnet152(pretrained=pretrained)
else:
raise ValueError('resnet.py: network layers is no support yet')
def hook_func(module, input, output):
self.inner_layer.append(output)
self.backbone.layer1.register_forward_hook(hook_func)
self.backbone.layer2.register_forward_hook(hook_func)
self.backbone.layer3.register_forward_hook(hook_func)
self.backbone.layer4.register_forward_hook(hook_func)
示例10: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self):
super(ResNet152_bo,self).__init__()
# 设置网络名称,保存模型时命名使用
self.moduel_name=str("ResNet152_bo")
# 加载预训练好的网络权重
model = resnet152(pretrained=True)
# 固定权重 nn.Module有成员函数parameters()
if opt.fixed_weight:
for param in model.parameters():
param.requires_grad = False
# 结论:self.model_bo只有修改过的新层(即最后两层全连接层)的值为True
# 替换最后一层全连接层
# 新层默认requires_grad=True
# resnet152中有self.fc,作为前向过程的最后一层
# (修改输入图像大小,可通过报错信息来调整下面参数)
model.fc = nn.Linear(2048, 2) #420:131072 224:2048
# 此时self.model_bo的权重为预训练权重,修改的新层(全连接层)权重为自动初始化的
self.model_bo=model
#手动初始化fc层
self._initialize_weights()
示例11: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, args, pretrained=True, weldon_pretrained_path=None):
super(ResNet_weldon, self).__init__()
resnet = models.resnet152(pretrained=pretrained)
self.base_layer = nn.Sequential(*list(resnet.children())[:-2])
self.spaConv = nn.Conv2d(2048, 2400, 1,)
# add spatial aggregation layer
self.wldPool = WeldonPooling(15)
# Linear layer for imagenet classification
self.fc = nn.Linear(2400, 1000)
# Loading pretrained weights of resnet weldon on imagenet classification
if pretrained:
try:
state_di = torch.load(
weldon_pretrained_path, map_location=lambda storage, loc: storage)['state_dict']
self.load_state_dict(state_di)
except Exception:
print("Error when loading pretrained resnet weldon")
示例12: ImageEncoder
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def ImageEncoder(encoder, load):
encoder = ImageEncoderType.from_string(encoder)
if encoder is ImageEncoderType.RESNET152:
encoder = models.resnet152()
encoder.load_state_dict(torch.load(load))
encoder.fc = Identity()
encoder.cuda()
encoder.eval()
return encoder
示例13: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self):
super(ResNet152Fc, self).__init__()
model_resnet152 = models.resnet152(pretrained=True)
self.conv1 = model_resnet152.conv1
self.bn1 = model_resnet152.bn1
self.relu = model_resnet152.relu
self.maxpool = model_resnet152.maxpool
self.layer1 = model_resnet152.layer1
self.layer2 = model_resnet152.layer2
self.layer3 = model_resnet152.layer3
self.layer4 = model_resnet152.layer4
self.avgpool = model_resnet152.avgpool
self.__in_features = model_resnet152.fc.in_features
示例14: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self):
super(ResNet152Fc, self).__init__()
model_resnet152 = models.resnet152(pretrained=True)
self.conv1 = model_resnet152.conv1
self.bn1 = model_resnet152.bn1
self.relu = model_resnet152.relu
self.maxpool = model_resnet152.maxpool
self.layer1 = model_resnet152.layer1
self.layer2 = model_resnet152.layer2
self.layer3 = model_resnet152.layer3
self.layer4 = model_resnet152.layer4
self.avgpool = model_resnet152.avgpool
示例15: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import resnet152 [as 别名]
def __init__(self, zeroshot, embed_dim=None, att_domains=None, num_train_classes=None, l2_weight=None):
"""
:param zeroshot: Whether we're running in zeroshot mode (
can be true or False).
:param embed_dim: Dimension of embeddings (probably 300)
:param att_dims: List of domain sizes per attribute.
:param num_train_classes: If we're doing pretraining, number of classes to use
"""
super(ImsituModel, self).__init__()
self.l2_weight = l2_weight
if zeroshot:
if (embed_dim is not None) and (att_domains is not None):
print("Using embeddings and attributes for zeroshot")
elif embed_dim is not None:
print("Using embeddings for zeroshot")
elif att_domains is not None:
print("using attributes for zeroshot")
else:
raise ValueError("Must supply embeddings or attributes for zeroshot")
self.fc_dim = None
self.att_domains = att_domains if att_domains is not None else []
self.embed_dim = embed_dim
else:
if num_train_classes is None:
raise ValueError("Must supply a # of training classes")
self.fc_dim = num_train_classes
self.att_domains = []
self.embed_dim = None
self.resnet152 = get_pretrained_resnet(self.fc_dim)
if self.embed_dim is not None:
self.embed_linear = nn.Linear(ENCODING_SIZE, self.embed_dim)
_init_fc(self.embed_linear)
if self.att_dim is not None:
self.att_linear = nn.Linear(ENCODING_SIZE, self.att_dim)
_init_fc(self.att_linear)