Keras: How to get feature maps with trained VGG16

Created on 6 Nov 2016  路  11Comments  路  Source: keras-team/keras

Please make sure that the boxes below are checked before you submit your issue. Thank you!

  • [x] Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/fchollet/keras.git --upgrade --no-deps

  • [x] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
    pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps

  • [x] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).

The example 'Extract features with VGG16' on https://keras.io/applications/
I use my picture try to extract features

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
from scipy.misc import imsave

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

img_path = '1.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)

features = model.predict(x)
print features
imsave('2.png', features)

print features--I get many matrices
but imsave('2.png', features) get error--ValueError: 'arr' does not have a suitable array shape for any mode.
I am a beginner about keras and python,I don't know how to get my feature maps by this example.Thank You!

stale

Most helpful comment

@barbaradarques
'''
from keras.applications import vgg16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
import numpy as np
import matplotlib.pyplot as plt

img_path = '1.png'
img = image.load_img(img_path, target_size=(224, 224))
model = vgg16.VGG16(weights='imagenet', include_top=True)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
model_extractfeatures = Model(input=model.input, output=model.get_layer('fc2').output)
fc2_features = model_extractfeatures.predict(x)
fc2_features = fc2_features.reshape((4096,1))
np.savetxt('fc2.txt',fc2_features)
plt.plot(fc2_features)
plt.show()
'''
It's my code to get feature matrix with VGG16 (keras version 1.2).

All 11 comments

As you have set include_top=False, it doesn't use the FC layer, thus you get the 7 x 7 x 512 matrix as output. See the VGG-16 macro-architecture here.
Also a 7 x 7 x 512 array cannot be saved as an image.

Get the 7 x 7 x 512 matrix as output? I was expecting a 3 x 3 x 512 matrix as output in the case of VGG16. In this case, this model extracts 25088 features from images resized to 50176 pixels, that does not seem in accordance with the first dense layer in the cited figure: https://www.cs.toronto.edu/~frossard/post/vgg16/vgg16.png

Did you manage to solve your problem? I'm struggling with the same issue.

@barbaradarques
'''
from keras.applications import vgg16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.models import Model
import numpy as np
import matplotlib.pyplot as plt

img_path = '1.png'
img = image.load_img(img_path, target_size=(224, 224))
model = vgg16.VGG16(weights='imagenet', include_top=True)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
model_extractfeatures = Model(input=model.input, output=model.get_layer('fc2').output)
fc2_features = model_extractfeatures.predict(x)
fc2_features = fc2_features.reshape((4096,1))
np.savetxt('fc2.txt',fc2_features)
plt.plot(fc2_features)
plt.show()
'''
It's my code to get feature matrix with VGG16 (keras version 1.2).

Oh, that was fast! Thanks!
But... isn't the feature map supposed to be a variation of the input image, like (filter * image)? Why are you flattening the output? How do I go from this long vector to such an image?

@barbaradarques
The VGG16's input size is restricted to 224*224 so the feature map is not variable.I flatten the output just for my following work.

Hmm, ok.

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
from keras.models import Sequential
from keras.layers import *
import pylab
model = VGG16(weights='imagenet', include_top=False)

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

features = model.predict(x)
pic=features[0,:,:,128]
pylab.imshow(pic)
pylab.gray()
pylab.show()

Here's my code to visualize one of the 512 feature map. Here I choose 128th feature map to visualize, U can set it at will.

@GloryDream That's exactly what I was looking for! Thank you very much!

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

Hi, I am new to programming in python. Can anyone please tell me the feature vector length for one image in (4096,1) if we use fc1 layer. What will be the length of feature vector for more then one image? Will it be for example (4096,10) or some other number?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NancyZxll picture NancyZxll  路  3Comments

somewacko picture somewacko  路  3Comments

LuCeHe picture LuCeHe  路  3Comments

harishkrishnav picture harishkrishnav  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments