本文整理汇总了Python中torchvision.models.alexnet方法的典型用法代码示例。如果您正苦于以下问题:Python models.alexnet方法的具体用法?Python models.alexnet怎么用?Python models.alexnet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models
的用法示例。
在下文中一共展示了models.alexnet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_untargeted_AlexNet
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def test_untargeted_AlexNet(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.alexnet(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
示例2: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self,feature,hidden_unit, D_in, D_out):
"""
In the constructor we instantiate two nn.Linear modules and assign them as
member variables.
"""
super(cnn_lstm, self).__init__()
self.model_ft = models.alexnet(pretrained=True)
# print (model_ft)
self.num_ftrs = self.model_ft.classifier[6].in_features
self.feature_model = list(self.model_ft.classifier.children())
self.feature_model.pop()
self.feature_model.pop()
# feature_model.append(nn.Linear(num_ftrs, 3))
self.feature_model.append(nn.Linear(self.num_ftrs, 1046))
self.feature_model.append(nn.Linear(1046, 100))
self.model_ft.classifier = nn.Sequential(*self.feature_model)
self.rnn = nn.LSTM(feature,hidden_unit,batch_first=True).cuda()
self.linear = torch.nn.Linear(D_in, D_out).cuda()
示例3: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self,feature,hidden_unit, D_in, D_out):
"""
In the constructor we instantiate two nn.Linear modules and assign them as
member variables.
"""
super(cnn_lstm, self).__init__()
self.model_ft = models.alexnet(pretrained=True)
# print (model_ft)
self.num_ftrs = self.model_ft.classifier[6].in_features
self.feature_model = list(self.model_ft.classifier.children())
self.feature_model.pop()
self.feature_model.pop()
# feature_model.append(nn.Linear(num_ftrs, 3))
self.feature_model.append(nn.Linear(self.num_ftrs, 1046))
# self.feature_model.append(nn.Linear(self.num_ftrs, 524))
self.feature_model.append(nn.Linear(1046, 100))
# self.feature_model.append(nn.Linear(524, 100))
self.model_ft.classifier = nn.Sequential(*self.feature_model)
self.rnn = nn.LSTM(feature,hidden_unit,batch_first=True).cuda()
self.linear = torch.nn.Linear(D_in, D_out).cuda()
示例4: getNetwork
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def getNetwork(args):
if (args.net_type == 'alexnet'):
net = models.alexnet(pretrained=args.finetune)
file_name = 'alexnet'
elif (args.net_type == 'vggnet'):
if(args.depth == 16):
net = models.vgg16(pretrained=args.finetune)
file_name = 'vgg-%s' %(args.depth)
elif (args.net_type == 'inception'):
net = models.inception(pretrained=args.finetune)
file_name = 'inceptino-v3'
elif (args.net_type == 'resnet'):
net = resnet(args.finetune, args.depth)
file_name = 'resnet-%s' %(args.depth)
else:
print('Error : Network should be either [VGGNet / ResNet]')
sys.exit(1)
return net, file_name
示例5: test_dissection
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def test_dissection():
verbose_progress(True)
from torchvision.models import alexnet
from torchvision import transforms
model = InstrumentedModel(alexnet(pretrained=True))
model.eval()
# Load an alexnet
model.retain_layers([
('features.0', 'conv1'),
('features.3', 'conv2'),
('features.6', 'conv3'),
('features.8', 'conv4'),
('features.10', 'conv5') ])
# load broden dataset
bds = BrodenDataset('dataset/broden',
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(IMAGE_MEAN, IMAGE_STDEV)]),
size=100)
# run dissect
dissect('dissect/test', model, bds,
examples_per_unit=10)
示例6: select
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def select(self, model_name=None):
"""select models to be run"""
logging.info("Run details")
logging.info("=" * 71)
models = [
self.alexnet,
self.resnet18,
self.resnet50,
self.vgg16,
self.squeezenet,
]
if model_name:
self.models = [
model for model in models for name in model_name if name == model.name
]
logging.info("Selected model(s) :: ")
for m in self.models:
logging.info("%s ------------- Batchsize :: %s " % (m.name, m.batch))
logging.info("=" * 71)
示例7: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self, requires_grad=False, pretrained=True):
super(alexnet, self).__init__()
alexnet_pretrained_features = tv.alexnet(pretrained=pretrained).features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
self.slice4 = torch.nn.Sequential()
self.slice5 = torch.nn.Sequential()
self.N_slices = 5
for x in range(2):
self.slice1.add_module(str(x), alexnet_pretrained_features[x])
for x in range(2, 5):
self.slice2.add_module(str(x), alexnet_pretrained_features[x])
for x in range(5, 8):
self.slice3.add_module(str(x), alexnet_pretrained_features[x])
for x in range(8, 10):
self.slice4.add_module(str(x), alexnet_pretrained_features[x])
for x in range(10, 12):
self.slice5.add_module(str(x), alexnet_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
示例8: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self, requires_grad=False, pretrained=True):
super(alexnet, self).__init__()
alexnet_pretrained_features = models.alexnet(pretrained=pretrained).features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
self.slice4 = torch.nn.Sequential()
self.slice5 = torch.nn.Sequential()
self.N_slices = 5
for x in range(2):
self.slice1.add_module(str(x), alexnet_pretrained_features[x])
for x in range(2, 5):
self.slice2.add_module(str(x), alexnet_pretrained_features[x])
for x in range(5, 8):
self.slice3.add_module(str(x), alexnet_pretrained_features[x])
for x in range(8, 10):
self.slice4.add_module(str(x), alexnet_pretrained_features[x])
for x in range(10, 12):
self.slice5.add_module(str(x), alexnet_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
示例9: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self, requires_grad=False, pretrained=True):
super(alexnet, self).__init__()
alexnet_pretrained_features = models.alexnet(
pretrained=pretrained).features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
self.slice4 = torch.nn.Sequential()
self.slice5 = torch.nn.Sequential()
self.N_slices = 5
for x in range(2):
self.slice1.add_module(str(x), alexnet_pretrained_features[x])
for x in range(2, 5):
self.slice2.add_module(str(x), alexnet_pretrained_features[x])
for x in range(5, 8):
self.slice3.add_module(str(x), alexnet_pretrained_features[x])
for x in range(8, 10):
self.slice4.add_module(str(x), alexnet_pretrained_features[x])
for x in range(10, 12):
self.slice5.add_module(str(x), alexnet_pretrained_features[x])
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
示例10: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self, images, model_key, layer, batch_size=256):
super().__init__(images, batch_size)
self.models = {
"alexnet": models.alexnet,
"squeezenet": models.squeezenet1_1,
"googlenet": models.googlenet
}
self.preprocessors = {
"alexnet": self.__preprocess_alexnet,
"squeezenet": self.__preprocess_squeezenet,
"googlenet": self.__preprocess_googlenet
}
self.batch_size = batch_size
self.layer = layer
self.model_key = model_key
self.model, self.feature_layer, self.output_size = self.__build_model(
layer)
示例11: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self):
super(AlexNetFc, self).__init__()
model_alexnet = models.alexnet(pretrained=True)
self.features = model_alexnet.features
self.classifier = nn.Sequential()
for i in range(6):
self.classifier.add_module(
"classifier"+str(i), model_alexnet.classifier[i])
self.__in_features = model_alexnet.classifier[6].in_features
示例12: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self):
super(AlexNetFc, self).__init__()
model_alexnet = models.alexnet(pretrained=True)
self.features = model_alexnet.features
self.classifier = nn.Sequential()
for i in range(6):
self.classifier.add_module("classifier"+str(i), model_alexnet.classifier[i])
示例13: get_example_params
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def get_example_params(example_index):
"""
Gets used variables for almost all visualizations, like the image, model etc.
Args:
example_index (int): Image id to use from examples
returns:
original_image (numpy arr): Original image read from the file
prep_img (numpy_arr): Processed image
target_class (int): Target class for the image
file_name_to_export (string): File name to export the visualizations
pretrained_model(Pytorch model): Model to use for the operations
"""
# Pick one of the examples
example_list = (('../input_images/snake.jpg', 56),
('../input_images/cat_dog.png', 243),
('../input_images/spider.png', 72))
img_path = example_list[example_index][0]
target_class = example_list[example_index][1]
file_name_to_export = img_path[img_path.rfind('/')+1:img_path.rfind('.')]
# Read image
original_image = Image.open(img_path).convert('RGB')
# Process image
prep_img = preprocess_image(original_image)
# Define model
pretrained_model = models.alexnet(pretrained=True)
return (original_image,
prep_img,
target_class,
file_name_to_export,
pretrained_model)
示例14: get_params
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def get_params(example_index):
"""
Gets used variables for almost all visualizations, like the image, model etc.
Args:
example_index (int): Image id to use from examples
returns:
original_image (numpy arr): Original image read from the file
prep_img (numpy_arr): Processed image
target_class (int): Target class for the image
file_name_to_export (string): File name to export the visualizations
pretrained_model(Pytorch model): Model to use for the operations
"""
# Pick one of the examples
example_list = [['../input_images/apple.JPEG', 948],
['../input_images/eel.JPEG', 390],
['../input_images/bird.JPEG', 13]]
selected_example = example_index
img_path = example_list[selected_example][0]
target_class = example_list[selected_example][1]
file_name_to_export = img_path[img_path.rfind('/')+1:img_path.rfind('.')]
# Read image
original_image = cv2.imread(img_path, 1)
# Process image
prep_img = preprocess_image(original_image)
# Define model
pretrained_model = models.alexnet(pretrained=True)
return (original_image,
prep_img,
target_class,
file_name_to_export,
pretrained_model)
示例15: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import alexnet [as 别名]
def __init__(self, opt):
super(AlexGaze, self).__init__()
self.features = nn.Sequential(
*list(models.alexnet(pretrained=True).features.children())
)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
self.fc1 = nn.Linear(9216, 500)
self.fc2 = nn.Linear(669, 400)
self.fc3 = nn.Linear(400, 200)
self.fc4 = nn.Linear(200, 169)
self.finalconv = nn.Conv2d(1, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))