如何获取CNN的层输出
How to get Layer Outputs of CNN
我构建了一个 VGG16 模型并对其进行了训练。我想查看此模型的测试图像的 softmax 层(预测概率)的输出。我搜索了答案并尝试了以下代码。它给出了这个错误 InvalidArgumentError: 2 root error(s) found. (0) INVALID_ARGUMENT: transpose expects a vector of size 3. But input(1) is a vector of size 4 [[{{node conv2d_26/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer}}]] [[conv2d_29/Relu/_311]] (1) INVALID_ARGUMENT: transpose expects a vector of size 3. But input(1) is a vector of size 4 [[{{node conv2d_26/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer}}]] 0 successful operations. 0 derived errors ignored.
这是下面的代码片段,我尝试了测试图像 (224,244,3) 和该图像的数组作为“图像”变量。仍然给出相同的错误。非常感谢任何帮助。
def get_all_outputs(model, input_data, learning_phase=1):
outputs = [layer.output for layer in model.layers[1:]] # exclude Input
layers_fn = K.function([model.input, K.learning_phase()], outputs)
return layers_fn([input_data, learning_phase])
outputs = get_all_outputs(model, image, 1)
您想预测图像的分数。
model.predict 通过模型传递您的输入图像并给出图像的分数。
model.predict(image)
先保存模型,然后像这样加载模型
# # Save the model
filepath = './saved_model'
save_model(model, filepath)
# Load the model
model = load_model(filepath)
然后像下面的代码一样获取测试图像的输出
# Generate predictions for samples
predictions = model.predict(samples_to_predict)
print(predictions)
# Generate arg maxes for predictions
classes = np.argmax(predictions, axis = 1)
print(classes)
我构建了一个 VGG16 模型并对其进行了训练。我想查看此模型的测试图像的 softmax 层(预测概率)的输出。我搜索了答案并尝试了以下代码。它给出了这个错误 InvalidArgumentError: 2 root error(s) found. (0) INVALID_ARGUMENT: transpose expects a vector of size 3. But input(1) is a vector of size 4 [[{{node conv2d_26/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer}}]] [[conv2d_29/Relu/_311]] (1) INVALID_ARGUMENT: transpose expects a vector of size 3. But input(1) is a vector of size 4 [[{{node conv2d_26/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer}}]] 0 successful operations. 0 derived errors ignored.
这是下面的代码片段,我尝试了测试图像 (224,244,3) 和该图像的数组作为“图像”变量。仍然给出相同的错误。非常感谢任何帮助。
def get_all_outputs(model, input_data, learning_phase=1):
outputs = [layer.output for layer in model.layers[1:]] # exclude Input
layers_fn = K.function([model.input, K.learning_phase()], outputs)
return layers_fn([input_data, learning_phase])
outputs = get_all_outputs(model, image, 1)
您想预测图像的分数。 model.predict 通过模型传递您的输入图像并给出图像的分数。
model.predict(image)
先保存模型,然后像这样加载模型
# # Save the model
filepath = './saved_model'
save_model(model, filepath)
# Load the model
model = load_model(filepath)
然后像下面的代码一样获取测试图像的输出
# Generate predictions for samples
predictions = model.predict(samples_to_predict)
print(predictions)
# Generate arg maxes for predictions
classes = np.argmax(predictions, axis = 1)
print(classes)