当前位置:   article > 正文

3月24日毕设准备_torchattacks

torchattacks

3月24日毕设

Demo - White Box Attack (Imagenet)(https://nbviewer.org/github/Harry24k/adversarial-attacks-pytorch/blob/master/demos/White%20Box%20Attack%20%28ImageNet%29.ipynb)

import numpy as np
import json
import os
import sys
import time
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
%matplotlib inline
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.utils
from torchvision import models
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import torchattacks
from utils import imshow, image_folder_custom_label
print("PyTorch", torch.__version__)
print("Torchvision", torchvision.__version__)
print("Torchattacks", torchattacks.__version__)
print("Numpy", np.__version__)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

根据这里的输出是
PyTorch 1.9.1+cu102
Torchvision 0.10.1+cu102
Torchattacks 3.2.4
Numpy 1.19.5
我安装了Pytorch 和Torchvison 注意需直接安装pip install torch双等于1.9.1+cu102 显示找不到对应版本,需要后面加上pytorch官方的url才行。类似:pip3 install torch双等号1.9.0+cu111 torchvision==0.10.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
这里需要注意from utils import imshow, image_folder_custom_label中utils一般为用户自己定义的包,所以这里引用没有用。需要自己改代码。

imagnet_data = image_folder_custom_label(root='./data/imagenet', transform=transform, idx2label=idx2label)
  • 1

这里的image_folder_custom_label我用了from torchvision.datasets import ImageFolder替代,
imagnet_data=ImageFolder((root=’./data/imagenet’, transform=transform)这里需要注意,怎样使用ImageFolder,里面参数要了解,他是以文件夹名字作为label的。

imshow(torchvision.utils.make_grid(images, normalize=True), [imagnet_data.classes[i] for i in labels])
  • 1

这个imshow用plt代替。
但是结果不尽人意,鲁棒性都为0,可能是过程有误。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/365763?site
推荐阅读
相关标签
  

闽ICP备14008679号