利用pytorch实现Fooling Images(添加特定噪声到原始图像,使神经网络误识别)
2017-11-30 11:22
1046 查看
It suggests that it is the space, rather than the individual units, that contains the semantic information in the high layers of neural networks.
在深层的神经网络中,真正影响特征信息的,不是个体单元,而是空间信息。
we find that deep neural networks learn input-output mappings that are fairly discontinuous to a significant extent.
神经网络对输入图像的分析是不连续的,所以通过这个特点可以在原始图像上加上一种特定噪声来实现误导
These results suggest that the deep neural networks that are learned by backpropagation have nonintuitive characteristics and intrinsic blind spots, whose structure is connected to the data distribution in a non-obvious way.
这些分析中可以得到深度学习网络通过反向传播学习到的数据拥有非直接的特性,还有一些固有的盲点,这些数据的结构与数据的分布有着隐秘的关系。
下面程序定义了一个生成fooling image 的函数。
接下来运行函数来生成fooling image,具体信息通过上下文来观察。
展示fooling image与识别结果
可以看出,hay图像被误识别为stinggray,而这两个图片的区别通过放大可以观察出random噪声。
参考资料:
Szegedy et al, “Intriguing properties of neural networks”, ICLR 2014
cs231n
在深层的神经网络中,真正影响特征信息的,不是个体单元,而是空间信息。
we find that deep neural networks learn input-output mappings that are fairly discontinuous to a significant extent.
神经网络对输入图像的分析是不连续的,所以通过这个特点可以在原始图像上加上一种特定噪声来实现误导
These results suggest that the deep neural networks that are learned by backpropagation have nonintuitive characteristics and intrinsic blind spots, whose structure is connected to the data distribution in a non-obvious way.
这些分析中可以得到深度学习网络通过反向传播学习到的数据拥有非直接的特性,还有一些固有的盲点,这些数据的结构与数据的分布有着隐秘的关系。
Fooling Images
fooling images,顾名思义,就是指一张图片,虽然上面通过肉眼看到的是松鼠(举个例子),但是因为这张图片加了一些特定的噪声,所以神经网络会将它误识别为其他物体。理论基础
下面程序定义了一个生成fooling image 的函数。
def make_fooling_image(X, target_y, model): """ 生成一个与输入图像X类似的fooling image, 但是这个image会被分类成target_y. Inputs: - X: Input image; Tensor of shape (1, 3, 224, 224) - target_y: An integer in the range [0, 1000) - model: A pretrained CNN Returns: - X_fooling: An image that is close to X, but that is classifed as target_y by the model. """ # Initialize our fooling image to the input image, and wrap it in a Variable. X_fooling = X.clone() X_fooling_var = Variable(X_fooling, requires_grad=True) learning_rate = 0.8 ############################################################################## # TODO: Generate a fooling image X_fooling that the model will classify as # # the class target_y. You should perform gradient ascent on the score of the # # target class, stopping when the model is fooled. # 通过对目标分类的梯度更新直到该图像被模型误分为target_y # When computing an update step, first normalize the gradient: # # dX = learning_rate * g / ||g||_2 # # # # You should write a training loop. # # # # HINT: For most examples, you should be able to generate a fooling image # # in fewer than 100 iterations of gradient ascent. # # You can print your progress over iterations to check your algorithm. # ############################################################################## for i in range(100): # 前向操作 scores = model(X_fooling_var) # print(scores) # Current max index. _, index = scores.data.max(dim=1) print(index) # 当成功fool的时候break. if index[0] == target_y: break # Score for the target class. target_score = scores[0,target_y] # Backward. target_score.backward() # Gradient for image. im_grad = X_fooling_var.grad.data # 通过正则化的梯度对所输入图像进行更新. X_fooling_var.data += learning_rate * (im_grad / im_grad.norm()) # 梯度更新清零,否则梯度会进行累积. X_fooling_var.grad.data.zero_() return X_fooling
接下来运行函数来生成fooling image,具体信息通过上下文来观察。
idx = 0 target_y = 6 X_tensor = torch.cat([preprocess(Image.fromarray(x)) for x in X], dim=0) X_fooling = make_fooling_image(X_tensor[idx:idx+1], target_ c0ff y, model) scores = model(Variable(X_fooling)) # print(target_y) # print(scores.data.max(dim=1)[1]) assert target_y == scores.data.max(dim=1)[1][0], 'The model is not fooled!'
展示fooling image与识别结果
X_fooling_np = deprocess(X_fooling.clone()) X_fooling_np = np.asarray(X_fooling_np).astype(np.uint8) plt.subplot(1, 4, 1) plt.imshow(X[idx]) plt.title(class_names[y[idx]]) plt.axis('off') plt.subplot(1, 4, 2) plt.imshow(X_fooling_np) plt.title(class_names[target_y]) plt.axis('off') plt.subplot(1, 4, 3) X_pre = preprocess(Image.fromarray(X[idx])) diff = np.asarray(deprocess(X_fooling - X_pre, should_rescale=False)) plt.imshow(diff) plt.title('Difference') plt.axis('off') plt.subplot(1, 4, 4) diff = np.asarray(deprocess(10 * (X_fooling - X_pre), should_rescale=False)) plt.imshow(diff) plt.title('Magnified difference (10x)') plt.axis('off') plt.gcf().set_size_inches(12, 5) plt.show()
可以看出,hay图像被误识别为stinggray,而这两个图片的区别通过放大可以观察出random噪声。
参考资料:
Szegedy et al, “Intriguing properties of neural networks”, ICLR 2014
cs231n
相关文章推荐
- 在树莓派上利用神经网络字符识别实现智能送餐车
- 第三期 利用已经训练的神经网络进行图像识别 《显卡就是开发板》
- 利用pytorch实现神经网络风格迁移Neural Transfer
- 利用tensorflow一步一步实现基于MNIST 数据集进行手写数字识别的神经网络,逻辑回归
- 全卷机神经网络图像分割(U-net)-keras实现
- 手把手入门神经网络系列(2)_74行代码实现手写数字识别
- Opencv利用神经网络进行车牌识别(c++)
- 机器学习实验报告:利用3层神经网络对CIFAR-10图像数据库进行分类
- PyTorch上搭建简单神经网络实现回归和分类
- 基于神经网络和遗传算法的【手写数字识别】机器人的实现
- TensorFlow学习系列(四):利用神经网络实现泛逼近器(universal approximator)
- 全卷机神经网络图像分割(U-net)-keras实现
- Skoltech联合牛津发布「深度图像先验」:使用随机初始化神经网络实现图片去噪、超分辨率和修补
- 深度神经网络识别图形图像的基本原理(转)
- 个人认为从事图像识别和神经网络的朋友应该关注下CUDA
- 练习:利用颜色直方图匹配算法实现图像中目标的识别
- 修改一个像素,就能让神经网络识别图像出错
- 利用开源程序(ImageMagick+tesseract-ocr)实现图像验证码识别
- 图像处理方向上用神经网络(CNN)来实现
- 第一章: 利用神经网络识别手写数字