コード例 #1
0
def load_net(attack_model, filename, path):
    if(attack_model == "CNN"):
        from deeprobust.image.netmodels.CNN import Net

        model = Net()
    if(attack_model == "ResNet18"):
        import deeprobust.image.netmodels.resnet as Net
        model = Net.ResNet18()

    model.load_state_dict(torch.load(path + filename))
    model.eval()
    return model
コード例 #2
0
    parser.add_argument(
        "--destination",
        default='./trained_models/',
        help="choose destination to load the pretrained models.")

    parser.add_argument("--filename", default="MNIST_CNN_epoch_20.pt")

    return parser.parse_args()


args = parameter_parser()  # read argument and creat an argparse object

model = Net()

model.load_state_dict(torch.load(args.destination + args.filename))
model.eval()
print("Finish loading network.")

xx = datasets.MNIST('./', download=False).data[999:1000].to('cuda')
xx = xx.unsqueeze_(1).float() / 255
#print(xx.size())

## Set Target
yy = datasets.MNIST('./', download=False).targets[999:1000].to('cuda')
"""
Generate adversarial examples
"""

F1 = FGSM(model, device="cuda")  ### or cuda
AdvExArray = F1.generate(xx, yy, **attack_params['FGSM_MNIST'])
コード例 #3
0
import numpy as np
import torch
import torch.nn as nn
from torchvision import datasets, models, transforms

from deeprobust.image.attack.Nattack import NATTACK
from deeprobust.image.netmodels.CNN import Net

#initialize model
model = Net()
model.load_state_dict(
    torch.load("defense_model/mnist_pgdtraining_0.3.pt",
               map_location=torch.device('cuda')))
model.eval()
print("----------model_parameters-----------")

for names, parameters in model.named_parameters():
    print(names, ',', parameters.type())
print("-------------------------------------")
data_loader = torch.utils.data.DataLoader(datasets.MNIST(
    'deeprobust/image/data',
    train=True,
    download=True,
    transform=transforms.Compose([transforms.ToTensor()])),
                                          batch_size=1,
                                          shuffle=True)

attack = NATTACK(model)
attack.generate(dataloader=data_loader, classnum=10)
コード例 #4
0
from deeprobust.image.attack.cw import CarliniWagner
from deeprobust.image.netmodels.CNN import Net
from deeprobust.image.config import attack_params

# print log
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.info("Start test cw attack")

# load model
model = Net()
model.load_state_dict(
    torch.load("./trained_models/MNIST_CNN_epoch_20.pt",
               map_location=torch.device('cuda')))
model.eval()

xx = datasets.MNIST('deeprobust/image/data', download=False).data[1234]
xx = xx.unsqueeze_(0).float() / 255
xx = xx.unsqueeze_(0).float().to('cuda')

## Set Targetå
yy = datasets.MNIST('deeprobust/image/data', download=False).targets[1234]
yy = yy.float()

attack = CarliniWagner(model, device='cuda')
AdvExArray = attack.generate(xx,
                             yy,
                             target_label=1,
コード例 #5
0
    parser.add_argument("--download destination",
                        default = '~/lyx/projects/models/download',
                        help = "choose destination to load the pretrained models.")

    parser.add_argument("--file name",
                        default = "MNIST_CNN")

    return parser.parse_args()

args = parameter_parser() # read argument and creat an argparse object

model = Net()
print("Download network from Google Drive.")

model.load_state_dict(torch.load(destination + filename))
model.eval()
print("Finish loading network.")

xx = datasets.MNIST('deeprobust/image/data', download = False).data[999:1000].to('cuda')
xx = xx.unsqueeze_(1).float()/255
print(xx.size())

## Set Targetå
yy = datasets.MNIST('deeprobust/image/data', download = False).targets[999:1000].to('cuda')


F1 = FGM(model, device = "cuda")       ### or cuda
AdvExArray = F1.generate(xx, yy, **attack_params['FGSM_MNIST'])

predict0 = model(xx)
コード例 #6
0
from deeprobust.image.attack.cw import CarliniWagner
from deeprobust.image.netmodels.CNN import Net
from deeprobust.image.config import attack_params

# print log
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
logger.info("Start test cw attack")

# load model
model = Net()
model.load_state_dict(
    torch.load("deeprobust/image/save_models/MNIST_CNN_epoch_20.pt",
               map_location=torch.device('cuda')))
model.eval()

xx = datasets.MNIST('deeprobust/image/data', download=False).data[1234]
xx = xx.unsqueeze_(0).float() / 255
xx = xx.unsqueeze_(0).float().to('cuda')

## Set Targetå
yy = datasets.MNIST('deeprobust/image/data', download=False).targets[1234]
yy = yy.float()

attack = CarliniWagner(model, device='cuda')
AdvExArray = attack.generate(xx,
                             yy,
                             target=1,
コード例 #7
0
import numpy as np
import torch
import torch.nn as nn
from torchvision import datasets, models, transforms

from deeprobust.image.attack.Nattack import NATTACK
from deeprobust.image.netmodels.CNN import Net

#initialize model
model = Net()
model.load_state_dict(
    torch.load("trained_models/mnist_fgsmtraining_0.2.pt",
               map_location=torch.device('cuda')))
model.eval()
print("----------model_parameters-----------")

for names, parameters in model.named_parameters():
    print(names, ',', parameters.type())
print("-------------------------------------")
data_loader = torch.utils.data.DataLoader(datasets.MNIST(
    'deeprobust/image/data',
    train=True,
    download=True,
    transform=transforms.Compose([transforms.ToTensor()])),
                                          batch_size=1,
                                          shuffle=True)

attack = NATTACK(model)
attack.generate(dataloader=data_loader, classnum=10)
コード例 #8
0
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F  #233
import torch.optim as optim
from torchvision import datasets, models, transforms
from PIL import Image

from lbfgs import LBFGS
from deeprobust.image.netmodels.CNN import Net
from deeprobust.image.config import attack_params

#load model
model = Net()
model.load_state_dict(
    torch.load(
        "/home/bizon/Desktop/liyaxin/deeprobust_trained_model/MNIST_CNN_epoch_20.pt",
        map_location=torch.device('cpu')))
model.eval()

import ipdb
ipdb.set_trace()

xx = datasets.MNIST('deeprobust/image/data', download=True).data[8888]
xx = xx.unsqueeze_(0).float() / 255
xx = xx.unsqueeze_(0).float()

## Set Targetå
yy = datasets.MNIST('deeprobust/image/data', download=False).targets[8888]
yy = yy.float()

predict0 = model(xx)