本文整理汇总了Python中torchvision.models.inception_v3方法的典型用法代码示例。如果您正苦于以下问题:Python models.inception_v3方法的具体用法?Python models.inception_v3怎么用?Python models.inception_v3使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models
的用法示例。
在下文中一共展示了models.inception_v3方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def __init__(self, nef):
super(CNN_ENCODER, self).__init__()
if cfg.TRAIN.FLAG:
self.nef = nef
else:
self.nef = 256 # define a uniform ranker
model = models.inception_v3()
url = 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth'
model.load_state_dict(model_zoo.load_url(url))
for param in model.parameters():
param.requires_grad = False
print('Load pretrained model from ', url)
# print(model)
self.define_module(model)
self.init_trainable_weights()
示例2: get_image_format
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def get_image_format(framework_name, model_name):
"""Return the correct input range and shape for target framework and model"""
special_shape = {'pytorch':{'inception_v3': (299, 299)},
'keras': {'xception': (299, 299),
'inception_v3':(299, 299),
'yolo_v3': (416, 416),
'ssd300': (300, 300)}}
special_bound = {'keras':{'vgg16':(0, 255),
'vgg19':(0, 255),
'resnet50':(0, 255),
'ssd300': (0, 255)},
'cloud': {'aip_antiporn': (0, 255),
'google_safesearch': (0, 255),
'google_objectdetection': (0, 255)}}
default_shape = (224, 224)
default_bound = (0, 1)
if special_shape.get(framework_name, None):
if special_shape[framework_name].get(model_name, None):
default_shape = special_shape[framework_name][model_name]
if special_bound.get(framework_name, None):
if special_bound[framework_name].get(model_name, None):
default_bound = special_bound[framework_name][model_name]
return {'shape': default_shape, 'bounds': default_bound}
示例3: test_untargeted_inception_v3
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def test_untargeted_inception_v3(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.inception_v3(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
示例4: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def __init__(self, vocab_size, dec_hsz, rnn_layers, bsz, max_len, dropout, use_cuda):
super().__init__()
self.torch = torch.cuda if use_cuda else torch
self.dec_hsz = dec_hsz
self.rnn_layers = rnn_layers
self.bsz = bsz
self.max_len = max_len
self.vocab_size = vocab_size
self.dropout = dropout
self.enc = inception_v3(True)
self.enc_out = nn.Linear(1000, dec_hsz)
self.lookup_table = nn.Embedding(vocab_size, dec_hsz, padding_idx=PAD)
self.rnn = nn.LSTM(dec_hsz + dec_hsz, dec_hsz, rnn_layers,
batch_first=True,
dropout=dropout)
self.attn = Attention(dec_hsz)
self.out = nn.Linear(self.dec_hsz, vocab_size)
self._reset_parameters()
示例5: load_inception
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def load_inception(path='data/RafD/normal/inception_v3.pth'):
from torchvision.models import inception_v3
import torch
import torch.nn as nn
state_dict = torch.load(path)
net = inception_v3(pretrained=False, transform_input=True)
print("Loading inception_v3 from " + path)
net.aux_logits = False
num_ftrs = net.fc.in_features
net.fc = nn.Linear(num_ftrs, state_dict['fc.weight'].size(0))
net.load_state_dict(state_dict)
for param in net.parameters():
param.requires_grad = False
return net
# ==================================================================#
# ==================================================================#
示例6: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def __init__(self, device):
super(Model, self).__init__()
self.device = device
self.net = models.inception_v3(pretrained=True)
self.net.eval()
for param in self.net.parameters():
param.requires_grad = False
# Set up preprocessor.
self.preprocess = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
(self.width, self.height) = (299, 299)
示例7: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def __init__(self, true_class):
super(ImageClassifier, self).__init__()
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.net = models.inception_v3(pretrained=True).to(self.device)
self.net.eval()
for param in self.net.parameters():
param.requires_grad = False
self.preprocess = transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
)
self.label_map = self.load_imagenet_label_map()
self.true_class = true_class
self.true_label = self.label_map[self.true_class]
示例8: InceptionV3
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def InceptionV3(config):
return models.inception_v3(pretrained=True)
示例9: load_patched_inception_v3
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def load_patched_inception_v3():
inception = inception_v3(pretrained=True)
inception.eval()
inception.forward = forward.__get__(inception, Inception3)
return inception.to(device)
示例10: _load_keras_model
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def _load_keras_model(model_name, summary):
import keras.applications as models
switcher = {
'xception': lambda: models.xception.Xception(weights='imagenet'),
'vgg16': lambda: models.vgg16.VGG16(weights='imagenet'),
'vgg19': lambda: models.vgg19.VGG19(weights='imagenet'),
"resnet50": lambda: models.resnet50.ResNet50(weights='imagenet'),
"inception_v3": lambda: models.inception_v3.InceptionV3(weights='imagenet'),
"yolo_v3": lambda: _load_yolov3_model(),
"ssd300": lambda: _load_ssd300_model(),
"retina_resnet_50": lambda: _load_retinanet_resnet50_model()
}
_load_model = switcher.get(model_name, None)
_model = _load_model()
from perceptron.models.classification.keras import KerasModel as ClsKerasModel
from perceptron.models.detection.keras_ssd300 import KerasSSD300Model
from perceptron.models.detection.keras_yolov3 import KerasYOLOv3Model
from perceptron.models.detection.keras_retina_resnet50 import KerasResNet50RetinaNetModel
import numpy as np
format = get_image_format('keras', model_name)
if format['bounds'][1] == 1:
mean = np.array([0.485, 0.456, 0.406]).reshape((1, 1, 3))
std = np.array([0.229, 0.224, 0.225]).reshape((1, 1, 3))
preprocessing = (mean, std)
else:
preprocessing = (np.array([104, 116, 123]), 1)
switcher = {
'yolo_v3': lambda x: KerasYOLOv3Model(x, bounds=(0, 1)),
'ssd300': lambda x: KerasSSD300Model(x, bounds=(0, 255)),
'retina_resnet_50': lambda x: KerasResNet50RetinaNetModel(None, bounds=(0, 255)),
}
_wrap_model = switcher.get(
model_name,
lambda x: ClsKerasModel(x, bounds=format['bounds'], preprocessing=preprocessing))
kmodel = _wrap_model(_model)
return kmodel
示例11: _load_pytorch_model
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def _load_pytorch_model(model_name, summary):
import torchvision.models as models
switcher = {
'alexnet': lambda: models.alexnet(pretrained=True).eval(),
"vgg11": lambda: models.vgg11(pretrained=True).eval(),
"vgg11_bn": lambda: models.vgg11_bn(pretrained=True).eval(),
"vgg13": lambda: models.vgg13(pretrained=True).eval(),
"vgg13_bn": lambda: models.vgg13_bn(pretrained=True).eval(),
"vgg16": lambda: models.vgg16(pretrained=True).eval(),
"vgg16_bn": lambda: models.vgg16_bn(pretrained=True).eval(),
"vgg19": lambda: models.vgg19(pretrained=True).eval(),
"vgg19_bn": lambda: models.vgg19_bn(pretrained=True).eval(),
"resnet18": lambda: models.resnet18(pretrained=True).eval(),
"resnet34": lambda: models.resnet34(pretrained=True).eval(),
"resnet50": lambda: models.resnet50(pretrained=True).eval(),
"resnet101": lambda: models.resnet101(pretrained=True).eval(),
"resnet152": lambda: models.resnet152(pretrained=True).eval(),
"squeezenet1_0": lambda: models.squeezenet1_0(pretrained=True).eval(),
"squeezenet1_1": lambda: models.squeezenet1_1(pretrained=True).eval(),
"densenet121": lambda: models.densenet121(pretrained=True).eval(),
"densenet161": lambda: models.densenet161(pretrained=True).eval(),
"densenet201": lambda: models.densenet201(pretrained=True).eval(),
"inception_v3": lambda: models.inception_v3(pretrained=True).eval(),
}
_load_model = switcher.get(model_name, None)
_model = _load_model()
import torch
if torch.cuda.is_available():
_model = _model.cuda()
from perceptron.models.classification.pytorch import PyTorchModel as ClsPyTorchModel
import numpy as np
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))
pmodel = ClsPyTorchModel(
_model, bounds=(
0, 1), num_classes=1000, preprocessing=(
mean, std))
return pmodel
示例12: load_pytorch_model
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def load_pytorch_model(model_name):
import torchvision.models as models
switcher = {
'alexnet': lambda: models.alexnet(pretrained=True).eval(),
"vgg11": lambda: models.vgg11(pretrained=True).eval(),
"vgg11_bn": lambda: models.vgg11_bn(pretrained=True).eval(),
"vgg13": lambda: models.vgg13(pretrained=True).eval(),
"vgg13_bn": lambda: models.vgg13_bn(pretrained=True).eval(),
"vgg16": lambda: models.vgg16(pretrained=True).eval(),
"vgg16_bn": lambda: models.vgg16_bn(pretrained=True).eval(),
"vgg19": lambda: models.vgg19(pretrained=True).eval(),
"vgg19_bn": lambda: models.vgg19_bn(pretrained=True).eval(),
"resnet18": lambda: models.resnet18(pretrained=True).eval(),
"resnet34": lambda: models.resnet34(pretrained=True).eval(),
"resnet50": lambda: models.resnet50(pretrained=True).eval(),
"resnet101": lambda: models.resnet101(pretrained=True).eval(),
"resnet152": lambda: models.resnet152(pretrained=True).eval(),
"squeezenet1_0": lambda: models.squeezenet1_0(pretrained=True).eval(),
"squeezenet1_1": lambda: models.squeezenet1_1(pretrained=True).eval(),
"densenet121": lambda: models.densenet121(pretrained=True).eval(),
"densenet161": lambda: models.densenet161(pretrained=True).eval(),
"densenet201": lambda: models.densenet201(pretrained=True).eval(),
"inception_v3": lambda: models.inception_v3(pretrained=True).eval(),
}
_load_model = switcher.get(model_name, None)
_model = _load_model()
return _model
示例13: test_checks_input_size_for_inception_model
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def test_checks_input_size_for_inception_model(mocker):
with pytest.raises(ValueError) as error:
model = models.inception_v3()
backprop = Backprop(model)
target_class = 5
input_ = torch.zeros([1, 3, 224, 224])
backprop.calculate_gradients(input_, target_class)
assert 'Image must be 299x299 for Inception models.' in str(error.value)
示例14: load_inception
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def load_inception():
inception_model = inception_v3(pretrained=True, transform_input=False)
inception_model.cuda()
inception_model = torch.nn.DataParallel(inception_model, \
device_ids=range(opt.ngpu))
inception_model.eval()
return inception_model
示例15: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import inception_v3 [as 别名]
def __init__(self, transform_input=True):
super().__init__()
self.inception_network = inception_v3(pretrained=False, transform_input=False)
# Load state dict
state_dict = torch.utils.model_zoo.load_url("https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth", model_dir="metrics/inception")
self.inception_network.load_state_dict(state_dict)
self.inception_network.Mixed_7c.register_forward_hook(self.output_hook)
self.transform_input = transform_input