Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract features from pre-trained model #8

Open
dineshbvadhia opened this issue Mar 21, 2017 · 1 comment
Open

Extract features from pre-trained model #8

dineshbvadhia opened this issue Mar 21, 2017 · 1 comment

Comments

@dineshbvadhia
Copy link

To extract features from an arbitrary layer with say, VGG19, given an input image "elephant.jpg", the code in keras+TF is simply:

from keras.applications.vgg19 import VGG19
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
import numpy as np

base_model = VGG19(weights='imagenet')
model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').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)

block4_pool_features = model.predict(x)

This can be turned into a function to support multiple calls. How is this achieved in PyTorch?

@ATSEGYPT
Copy link

How to get the indices of the extracted "block4_pool_features"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants