import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
import keras
import numpy as np
import tensorflow as tf
print(keras.__version__,tf.__version__)
model = keras.applications.ResNet50V2(include_top=False)
layer = model.get_layer("conv3_block4_out")
feature_extractor = keras.Model(inputs=model.inputs, outputs=layer.output)
def compute_loss(input_image, filter_index,model):#参数:输入(四维张量),滤镜索引,特征提取模型
activation = model(input_image)#获取特征图
filter_activation = activation[:,:,:,filter_index]#(n,h,w)
# print(f'{filter_activation.shape=}')
return tf.reduce_mean(filter_activation)#返回的是单个滤镜图(矩阵)的均值,标量
@tf.function
def gradient_ascent_step(img, filter_index, learning_rate,model):#图函数
with tf.GradientTape() as tape:#对输入图片求梯度,梯度上升
tape.watch(img)#梯度带监视输入图片数据
loss = compute_loss(img, filter_index,model)#计算某个滤镜图的损失
# 用标量数据loss对输入数据求梯度
grads = tape.gradient(loss, img)
#梯度单位化(就是对其中的行或列向量单位化,向量长度变成1)
grads = tf.math.l2_normalize(grads)
img += learning_rate * grads#梯度上升,获取滤镜激活图
return loss, img
def initialize_image(w,h,c):#构造输入图像数据(-0.125,0.125)
img = tf.random.uniform((1,w,h,c))# 生成满足均匀分布的随机值
return (img - 0.5) * 0.25
def visualize