问题
I am using ubuntu with python 3 and keras over tensorflow, I am trying to create a model using transfer learning from a pre trained keras model as explained here:
I am using the following code
import numpy as np
from keras.applications import vgg16, inception_v3, resnet50, mobilenet
from keras import Model
a = np.random.rand(1, 224, 224, 3) + 0.001
a = mobilenet.preprocess_input(a)
mobilenet_model = mobilenet.MobileNet(weights='imagenet')
mobilenet_model.summary()
inputLayer = mobilenet_model.input
m = Model(input=inputLayer, outputs=mobilenet_model.get_layer("conv_pw_13_relu")(inputLayer))
m.set_weights(mobilenet_model.get_weights()[:len(m.get_weights())])
p = m.predict(a)
print(np.std(p), np.mean(p))
print(p.shape)
The output of the layer I am using is always an array of zeros, Should I load the weight to p that i am creating in order for the pre trained model to actually work?
回答1:
There is a difference between layers and the outputs of those layers in Keras. You can think of layers as representing a computation and the outputs as the results of those computation. When you instantiate a Model
object, it expects the results of a computation as it's output, instead of the computation itself, hence the error. To fix it, you can pass the output of the layer to the Model
constructor:
import numpy as np
from keras.applications import vgg16, inception_v3, resnet50, mobilenet
from keras import Model
a = np.random.rand(24, 224, 224, 3)
a = mobilenet.preprocess_input(a)
mobilenet_model = mobilenet.MobileNet(weights='imagenet')
mobilenet_model.summary()
model_output = mobilenet_model.get_layer("conv_pw_13_relu").output
m = Model(inputs=mobilenet_model.input, outputs=model_output)
print(m.predict(a))
回答2:
In order to acess the output of an intermediate layer in a Keras model, Keras Provides different ways.
In your case You can take output of the layer you want like this
model_out = mobilenet_model.get_layer("layer_you_want").output
m = Model(input=inputLayer, outputs=model_out)
For more details regarding this and other methods available take a look at this documentation
来源:https://stackoverflow.com/questions/51949208/get-output-from-a-non-final-keras-model-layer