我正在尝试使用此代码来可视化CNN的内核。在此之前,我已经训练了CNN(称为complete_model)。我还检查了论坛上提出的其他类似问题,但没有任何帮助。
def deprocess_image(x):
x -= x.mean()
x /= (x.std() + 1e-5)
x *= 0.1
x += 0.5
x = np.clip(x, 0, 1)
x *= 255
x = np.clip(x, 0, 255).astype('uint8')
return x
def generate_pattern(layer_name, filter_index, size=150):
layer_output = complete_model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
grads = K.gradients(loss, complete_model.input)[0]
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
iterate = K.function([complete_model.input], [loss, grads])
input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.
step = 1.
for i in range(80):
loss_value, grads_value = iterate([input_img_data])
input_img_data += grads_value * step
img = input_img_data[0]
return deprocess_image(img)
fig = plt.figure(figsize=(15, 12))
for img in range(30):
ax = fig.add_subplot(5, 6, img+1)
ax = plt.imshow(generate_pattern('conv2d_1', img))
plt.xticks([])
plt.yticks([])
fig.subplots_adjust(wspace=0.05, hspace=0.05)
我不断收到以下错误:
ValueError:Tensor(“ Const_13:0”,shape =(3,),dtype = int32)必须与Tensor(“ strided_slice_22:0”,shape =(None,147,147),dtype来自同一张图= float32)(图形为