gpt4 book ai didi

python - 如何在预测阶段获得所有输出 keras 层(特征图)?

转载 作者:行者123 更新时间:2023-12-04 10:16:38 26 4
gpt4 key购买 nike

我尝试使用 cfiar 数据集并尝试获取每个输出的特征图,将其中一个测试图像作为输入。

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))

model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

layer_input = test_images[0]

for i in range(len(model.layers)):
get_layer_output = K.function(inputs = model.layers[i].input, outputs = model.layers[i].output)
print(get_layer_output(layer_input))
layer_input = model.layers[i].output

我的感觉是我误解了如何设置输入以及如何在预测期间获取输出。

最佳答案

您应该设置 输入 model.layers[0].input如果您正在使用 Sequential模型。

第一 ,展开维数测试输入到 包括 Batch_Size:

layer_input = test_images[0]    
plt.imshow(layer_input) # Plot Test Image
layer_input = tf.expand_dims(layer_input,0) # Add prefix of Batch Size
print(layer_input.shape) # Prints : (1, 32, 32, 3)

输出 :

original_image

绘图修改代码:
for i in range(len(model.layers)):
get_layer_output = K.function(inputs = model.layers[0].input, outputs = model.layers[i].output)

get_1_output = get_layer_output(layer_input)
# print(get_1_output.shape) << Use this to check if the Output shape matches the shape of Model.summary()

if get_1_output.ndim == 4: # Check for Dimensionality to plot ONE feature map (Batch size, Length, Width
plt.imshow(get_1_output[0][:,:,:3]) # Plots the output of Conv2D and MaxPooling
else:
print(get_1_output) # If not Image, ie. Array, print the Values

plt.show()


输出 :

feature_maps

我希望我回答了你的问题。

关于python - 如何在预测阶段获得所有输出 keras 层(特征图)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61031470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com