我试着获取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
我的感觉是我误解了一些关于如何设置输入以及如何在预测过程中获取输出的东西。
2条答案
按热度按时间5fjcxozz1#
如果使用
Sequential
型号,则应将输入设置为model.layers[0].input
。希望我回答了你的问题。
4nkexdtk2#
你可以使用下面的python脚本来绘制神经网络的特征图。
参考:https://www.analyticsvidhya.com/blog/2020/11/tutorial-how-to-visualize-feature-maps-directly-from-cnn-layers/