Skip to content

Instantly share code, notes, and snippets.

@duhaime
Created September 12, 2019 19:01
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 duhaime/70825f9bbc7454be680616799143533f to your computer and use it in GitHub Desktop.
Save duhaime/70825f9bbc7454be680616799143533f to your computer and use it in GitHub Desktop.
Keras Image to Vector
from keras.preprocessing.image import load_img, save_img, img_to_array, array_to_img
from keras.applications import Xception, VGG19, InceptionV3, imagenet_utils
import keras.backend as K
import numpy as np
model = Xception(weights='imagenet')
# VGG16, VGG19, and ResNet take 224×224 images; InceptionV3 and Xception take 299×299 inputs
img = load_img('l.jpg', target_size=(299,299))
arr = img_to_array(img)
arr = imagenet_utils.preprocess_input(arr)
# input shape must be n_images, w, h, colors
arr = np.expand_dims(arr, axis=0)
# complete forward pass through model
# preds = model.predict(arr)
# or extract values from the ith layer (e.g. the last layer == index position -1)
out = K.function([model.input], [model.layers[-1].output])([arr])[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment