在神经网络分别中,我们不仅想知道最终预测结果,还需要了解网络是凭借图像什么特征进行判断的。其中类激活热力图 (CAM,class activation map)就是一种很好的呈现方式。
目录标题
类激活热力图 (CAM,class activation map)
导入ImageNet VGG16网络
导入基础包
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
导入ImageNet VGG16网络
VGG16_model = tf.keras.applications.VGG16(include_top=True)
VGG16_model.summary()
加载任一图片
随便网上找一张图,比如使用率最高的猫星人
加载对应网络的图像处理方法,以及类别解码表
读取本地图片
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
def prepocess(x):
x = tf.io.read_file(x)
x = tf.image.decode_jpeg(x, channels=3)
x = tf.image.resize(x, [224,224])
x =tf.expand_dims(x, 0) # 扩维
x = preprocess_input(x)
return x
img_path=