Skip to content

Instantly share code, notes, and snippets.

@dansbecker
Last active April 28, 2017 20:55
Show Gist options
  • Save dansbecker/b1c7c893c3269cff84b6ccf59f82067b to your computer and use it in GitHub Desktop.
Save dansbecker/b1c7c893c3269cff84b6ccf59f82067b to your computer and use it in GitHub Desktop.
Use a pretrained network to featurize data for cats vs dogs
from keras.applications.resnet50 import ResNet50, preprocess_input
from keras.preprocessing.image import load_img, img_to_array
import numpy as np
from os import listdir
from os.path import join
import pandas as pd
def featurize(fpath, model, img_size):
'''Use trained model to convert image to vector describing content
Arguments
---------
fpath: string with path to file to featurize
model: Keras model object used to featurize image
img_size: Tuple with size image should be rescaled to during file loading. Must match model's input layer
'''
img = load_img(fpath, target_size=img_size)
raw_arr = img_to_array(img)
raw_arr = np.expand_dims(raw_arr, axis=0)
raw_arr = preprocess_input(raw_arr)
features = model.predict(raw_arr).flatten()
return features
if __name__ == "__main__":
data_dir = 'raw'
fnames = listdir(data_dir)
fpaths = (join(data_dir, f) for f in fnames)
model = ResNet50(weights='imagenet', include_top=False)
img_size = (224, 224) # image size expected by ResNet model
x = np.vstack([featurize(fpath, model, img_size) for fpath in image_paths])
y = np.array(['dog' in fname for fname in fnames])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment