Home > database >  How to get Layer Outputs of CNN
How to get Layer Outputs of CNN

Time:04-06

I built a VGG16 model and trained it. I would like to see output of softmax layer (prediction probabilities) of this model for test images. I searched for answers and tried below code. It gives this error 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. Here is the code snippet below, I tried test image (224,244,3) and array of that image for the "image" variable. Still gives the same error. Any help is highly appreciated.

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)

CodePudding user response:

you want to predict score of an image. model.predict passes your input image through the model and gives the score of your image.

model.predict(image)

CodePudding user response:

First save the model and then load the model like this

# # Save the model
filepath = './saved_model'
save_model(model, filepath)

# Load the model
model = load_model(filepath)

Then get the output for a test image like the following code

 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)

  • Related