政安晨:【Keras机器学习示例演绎】(十九)—— 可视化网络学习内容

目录

简介

设置

建立特征提取模型

设置梯度上升过程

设置端到端滤波器可视化回路

可视化目标层中的前 64 个滤波器


政安晨的个人主页政安晨

欢迎 👍点赞✍评论⭐收藏

收录专栏: TensorFlow与Keras机器学习实战

希望政安晨的博客能够对您有所裨益,如有不足之处,欢迎在评论区提出指正!

本文目标:显示 convnet 过滤器响应的视觉模式。

简介


在本示例中,我们将研究图像分类模型能学习到哪些视觉模式。我们将使用在 ImageNet 数据集上训练的 ResNet50V2 模型。

我们的过程很简单:我们将创建输入图像,最大限度地激活目标层

选在模型中间的某个位置:层 conv3_block4_out)中的特定滤波器。这些图像代表了过滤器响应模式的可视化。

设置

import osos.environ["KERAS_BACKEND"] = "tensorflow"import keras
import numpy as np
import tensorflow as tf# The dimensions of our input image
img_width = 180
img_height = 180
# Our target layer: we will visualize the filters from this layer.
# See `model.summary()` for list of layer names, if you want to change this.
layer_name = "conv3_block4_out"

建立特征提取模型

# Build a ResNet50V2 model loaded with pre-trained ImageNet weights
model = keras.applications.ResNet50V2(weights="imagenet", include_top=False)# Set up a model that returns the activation values for our target layer
layer = model.get_layer(name=layer_name)
feature_extractor = keras.Model(inputs=model.inputs, outputs=layer.output)

设置梯度上升过程


我们要最大化的 "损失 "只是目标层中特定滤波器激活的平均值。为避免边界效应,我们将边界像素排除在外。

def compute_loss(input_image, filter_index):activation = feature_extractor(input_image)# We avoid border artifacts by only involving non-border pixels in the loss.filter_activation = activation[:, 2:-2, 2:-2, filter_index]return tf.reduce_mean(filter_activation)

我们的梯度上升函数只需计算上述损失相对于输入图像的梯度,并更新更新图像,使其趋向于更强地激活目标滤波器的状态。

@tf.function
def gradient_ascent_step(img, filter_index, learning_rate):with tf.GradientTape() as tape:tape.watch(img)loss = compute_loss(img, filter_index)# Compute gradients.grads = tape.gradient(loss, img)# Normalize gradients.grads = tf.math.l2_normalize(grads)img += learning_rate * gradsreturn loss, img

设置端到端滤波器可视化回路

我们的流程如下

* 从接近 "全灰 "的随机图像(即视觉上的净图像)开始
* 重复应用上文定义的梯度上升阶跃函数
* 通过对输入图像进行归一化处理、居中裁剪并将其限制在 [0, 255] 范围内,将生成的输入图像转换为可显示的形式。

def initialize_image():# We start from a gray image with some random noiseimg = tf.random.uniform((1, img_width, img_height, 3))# ResNet50V2 expects inputs in the range [-1, +1].# Here we scale our random inputs to [-0.125, +0.125]return (img - 0.5) * 0.25def visualize_filter(filter_index):# We run gradient ascent for 20 stepsiterations = 30learning_rate = 10.0img = initialize_image()for iteration in range(iterations):loss, img = gradient_ascent_step(img, filter_index, learning_rate)# Decode the resulting input imageimg = deprocess_image(img[0].numpy())return loss, imgdef deprocess_image(img):# Normalize array: center on 0., ensure variance is 0.15img -= img.mean()img /= img.std() + 1e-5img *= 0.15# Center cropimg = img[25:-25, 25:-25, :]# Clip to [0, 1]img += 0.5img = np.clip(img, 0, 1)# Convert to RGB arrayimg *= 255img = np.clip(img, 0, 255).astype("uint8")return img

让我们在目标图层中使用滤镜 0 试试看:

from IPython.display import Image, displayloss, img = visualize_filter(0)
keras.utils.save_img("0.png", img)

这就是目标层 0 号滤波器响应最大化的输入结果:

display(Image("0.png"))

可视化目标层中的前 64 个滤波器

现在,让我们将目标层中的前 64 个滤波器做成一个 8x8 的网格,以了解模型学习到的不同视觉模式的范围。

# Compute image inputs that maximize per-filter activations
# for the first 64 filters of our target layer
all_imgs = []
for filter_index in range(64):print("Processing filter %d" % (filter_index,))loss, img = visualize_filter(filter_index)all_imgs.append(img)# Build a black picture with enough space for
# our 8 x 8 filters of size 128 x 128, with a 5px margin in between
margin = 5
n = 8
cropped_width = img_width - 25 * 2
cropped_height = img_height - 25 * 2
width = n * cropped_width + (n - 1) * margin
height = n * cropped_height + (n - 1) * margin
stitched_filters = np.zeros((width, height, 3))# Fill the picture with our saved filters
for i in range(n):for j in range(n):img = all_imgs[i * n + j]stitched_filters[(cropped_width + margin) * i : (cropped_width + margin) * i + cropped_width,(cropped_height + margin) * j : (cropped_height + margin) * j+ cropped_height,:,] = img
keras.utils.save_img("stiched_filters.png", stitched_filters)from IPython.display import Image, displaydisplay(Image("stiched_filters.png"))

演绎展示:

Processing filter 0
Processing filter 1
Processing filter 2
Processing filter 3
Processing filter 4
Processing filter 5
Processing filter 6
Processing filter 7
Processing filter 8
Processing filter 9
Processing filter 10
Processing filter 11
Processing filter 12
Processing filter 13
Processing filter 14
Processing filter 15
Processing filter 16
Processing filter 17
Processing filter 18
Processing filter 19
Processing filter 20
Processing filter 21
Processing filter 22
Processing filter 23
Processing filter 24
Processing filter 25
Processing filter 26
Processing filter 27
Processing filter 28
Processing filter 29
Processing filter 30
Processing filter 31
Processing filter 32
Processing filter 33
Processing filter 34
Processing filter 35
Processing filter 36
Processing filter 37
Processing filter 38
Processing filter 39
Processing filter 40
Processing filter 41
Processing filter 42
Processing filter 43
Processing filter 44
Processing filter 45
Processing filter 46
Processing filter 47
Processing filter 48
Processing filter 49
Processing filter 50
Processing filter 51
Processing filter 52
Processing filter 53
Processing filter 54
Processing filter 55
Processing filter 56
Processing filter 57
Processing filter 58
Processing filter 59
Processing filter 60
Processing filter 61
Processing filter 62
Processing filter 63

图像分类模型通过将其输入分解为 "向量基 "纹理过滤器来观察世界。


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/652064.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java集合相关的List、Set、Map基础知识

目录 一、集合介绍 二、List 三、Map HashMap的数据结构 如何理解红黑树 四、set 一、集合介绍 在Java中,集合是一种用于存储对象的数据结构,它提供了一种更加灵活和强大的方式来处理和操作数据。Java集合框架提供了一系列接口和类,用…

[极客大挑战 2019]Upload、[ACTF2020 新生赛]Upload、[MRCTF2020]你传你呢

[极客大挑战 2019]Upload 打开环境&#xff0c;是上传一句话木马的题 先上传1.php试试&#xff0c;发现不可以 试试改后缀为phtml&#xff0c;提示语句中不能包含<?&#xff0c;只能改木马&#xff1a; <script language"php">eval($_POST[line]);</sc…

牛客NC368 质数的计数【中等 基础数学,数论 C++/Java/Go/PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/190167d1990442da9adb133980259a27 思路 判断x是否是质数&#xff1a;这是判断质数最好的代码了public boolean isPrime(int x){if(x 2 || x3) return true;if(x%6!1 && x%6!5) return false; //不在6倍…

C语言:插入排序

插入排序 1.解释2.步骤3.举例分析示例结果分析 1.解释 插入排序是一种简单直观的排序算法&#xff0c;它的工作原理是通过构建有序序列&#xff0c;对于未排序数据&#xff0c;在已排序序列中从后向前扫描&#xff0c;找到相应位置并插入。插入排序在实现上&#xff0c;通常采…

【10-逻辑回归分类器:Scikit-learn中的二元分类实战】

文章目录 前言逻辑回归简介设置Scikit-learn环境选择数据集数据预处理训练逻辑回归模型模型评估优化模型结论前言 分类问题是机器学习中最常见的任务之一,而逻辑回归是处理二元分类问题的一种强大且直观的技术。本篇博文将深入探讨如何在Scikit-learn中实现逻辑回归分类器,并…

Hadoop之路

hadoop更适合在liunx环境下运行&#xff0c;会节省后期很多麻烦&#xff0c;而用虚拟器就太占主机内存了&#xff0c;因此后面我们将把hadoop安装到wsl后进行学习,后续学习的环境是Ubuntu-16.04 &#xff08;windows上如何安装wsl&#xff09; 千万强调&#xff0c;有的命令一…

深度学习运算:CUDA 编程简介

一、说明 如今&#xff0c;当我们谈论深度学习时&#xff0c;通常会将其实现与利用 GPU 来提高性能联系起来。GPU&#xff08;图形处理单元&#xff09;最初设计用于加速图像、2D 和 3D 图形的渲染。然而&#xff0c;由于它们能够执行许多并行操作&#xff0c;因此它们的实用性…

java-Spring-(MyBatis框架-xml管理)

目录 前置条件 xml与注解比较 1.1 xml定义 1.2 和SQL注解比较 建包准备 插入数据 ​编辑 更新数据 删除数据 查询数据 查看单字段查询 &#x1f3f7;&#x1f4a3;前置条件 创建一个spring boot 初始化的项目 &#x1f3f7;&#x1f4a3;xml与注解比较 1.1 xml定义 …

VBA技术资料MF145:清空回收站

我给VBA的定义&#xff1a;VBA是个人小型自动化处理的有效工具。利用好了&#xff0c;可以大大提高自己的工作效率&#xff0c;而且可以提高数据的准确度。“VBA语言専攻”提供的教程一共九套&#xff0c;分为初级、中级、高级三大部分&#xff0c;教程是对VBA的系统讲解&#…

ElasticSearch语句中must,must_not,should 组合关系

前言&#xff1a; 在实际应用中&#xff0c;发现当bool中同时使用must和should 没有达到想要的想过&#xff0c;而是只展示了must中的命中数据&#xff0c;所以打算探究一下bool中 三种逻辑关系的组合。 上述查询语句只展示了must的结果&#xff0c;没有should中的结果&#…

Faust勒索病毒:了解变种faust,以及如何保护您的数据

导言&#xff1a; 近年来&#xff0c;网络安全问题日益严峻&#xff0c;其中勒索病毒成为了一种日益猖獗的威胁。在众多勒索病毒中&#xff0c;.faust勒索病毒以其高度的隐秘性和破坏性引起了广泛关注。本文91数据恢复将深入剖析.faust勒索病毒的威胁特点&#xff0c;并提出相…

打包的意义 作用等前端概念集合 webpack基础配置等

基础网页是什么&#xff1f; 在学校最基础的三剑客 原生JS CSS H5就可以开发静态网页了 对于浏览器而言也能识别这些基础的文件和语法&#xff0c;真正的所见即所得&#xff0c;非常直接。 为什么要使用框架库&#xff1f; 对于常用的前端框架而言&#xff0c;无论是Vue Rea…