Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get output from a non final keras model layer

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?

like image 782
thebeancounter Avatar asked Aug 21 '18 12:08

thebeancounter


People also ask

How do you get the output of a model in Keras?

Just do a model. summary() . It will print all layers and their output shapes.

How do you make a layer non trainable?

Recursive setting of the trainable attributeIf you set trainable = False on a model or on any layer that has sublayers, all children layers become non-trainable as well.


1 Answers

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))
like image 74
Agost Biro Avatar answered Oct 04 '22 02:10

Agost Biro