deep-learning-models icon indicating copy to clipboard operation
deep-learning-models copied to clipboard

How to get pool_3 layer from inception using Keras

Open RahulGupta-Reflektion opened this issue 7 years ago • 5 comments

I used the following code following example in Readme.md


    base_model = InceptionV3(include_top=False, weights='imagenet')
    model = Model(input=base_model.input, output=base_model.get_layer('pool_3').output)
    
    img_path = 'elephant.jpg'
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    pool_3_features = model.predict(x)


I get following error. What am I doing wrong.

Traceback (most recent call last):

  File "inception_v3.py", line 420, in <module>
    model = Model(input=base_model.input, output=base_model.get_layer('pool_3').output)
AttributeError: 'NoneType' object has no attribute 'output'

RahulGupta-Reflektion avatar Mar 31 '17 08:03 RahulGupta-Reflektion

I got this code to work


    model = InceptionV3(weights='imagenet', include_top=False)

    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    features = model.predict(x)

Is it getting pool_3 layer or some other layer. dimensions seems to match.

RahulGupta-Reflektion avatar Mar 31 '17 08:03 RahulGupta-Reflektion

model.summary() will print all the layers (along with the dimensions and parameters).

prashanthdumpuri avatar Nov 10 '17 23:11 prashanthdumpuri

@prashanthdumpuri Is there a way to look at specific layers from the summary? Inception has over 300 layers. Say I want to look at 35-50, is there a way to do this?

Moondra avatar Nov 26 '17 20:11 Moondra

base_model = InceptionV3(weights='imagenet')

for i, layer in enumerate(base_model.layers): print(i, layer.name)

This will print the name of every layer. So you can use a shortened for loop if you want to see specific layers.

And am pretty sure there is a layer summary as well. Check out https://github.com/fchollet/keras/blob/master/keras/utils/layer_utils.py

Hope that helps. Prashanth

On Nov 26, 2017, at 12:57 PM, Moondra [email protected] wrote:

@prashanthdumpuri Is there a way to look at specific layers from the summary? Inception has over 300 layers. Say I want to look at 35-50, is there a way to do this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

prashanthdumpuri avatar Nov 27 '17 02:11 prashanthdumpuri

@prashanthdumpuri Thank you very much.

Moondra avatar Nov 30 '17 20:11 Moondra