Skip to content

Instantly share code, notes, and snippets.

@twiddles
Last active May 26, 2017 14:40
Show Gist options
  • Save twiddles/be4026ebef87d7f8dc1ee38f3859def7 to your computer and use it in GitHub Desktop.
Save twiddles/be4026ebef87d7f8dc1ee38f3859def7 to your computer and use it in GitHub Desktop.
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
def save_tags(filename):
img = image.load_img(filename, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
tags = [x[1] for x in decode_predictions(preds, top=3)[0]]
with open(filename + '_tags.txt', 'w') as f:
f.write(','.join(tags))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment