Skip to content

Instantly share code, notes, and snippets.

@Gabe-flomo
Created September 28, 2020 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gabe-flomo/54b8fb0c069f0de8353e9c51ecef29a1 to your computer and use it in GitHub Desktop.
Save Gabe-flomo/54b8fb0c069f0de8353e9c51ecef29a1 to your computer and use it in GitHub Desktop.
# load the model first and pass as an argument
model = VGG16()
model = Model(inputs = model.inputs, outputs = model.layers[-2].output)
def extract_features(file, model):
# load the image as a 224x224 array
img = load_img(file, target_size=(224,224))
# convert from 'PIL.Image.Image' to numpy array
img = np.array(img)
# reshape the data for the model reshape(num_of_samples, dim 1, dim 2, channels)
reshaped_img = img.reshape(1,224,224,3)
# prepare image for model
imgx = preprocess_input(reshaped_img)
# get the feature vector
features = model.predict(imgx, use_multiprocessing=True)
return features
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment