Skip to content

Instantly share code, notes, and snippets.

@fredgido
Last active June 14, 2020 21:41
Show Gist options
  • Select an option

  • Save fredgido/81e65426ca2f948fb00dd352b15e3409 to your computer and use it in GitHub Desktop.

Select an option

Save fredgido/81e65426ca2f948fb00dd352b15e3409 to your computer and use it in GitHub Desktop.
import pprint
import tensorflow as tf
import keras
import tensorflow_hub as hub
import numpy as np
import PIL.Image as Image
model_url = "https://tfhub.dev/google/imagenet/nasnet_mobile/classification/4"
IMAGE_SHAPE = (224, 224)
classifier = tf.keras.Sequential([hub.KerasLayer(model_url, input_shape=IMAGE_SHAPE + (3,))])
url = 'https://danbooru.donmai.us/data/sample/sample-a50d476225caabea727d571759d2918e.jpg'
imagefile = keras.utils.get_file(url.split("/")[-1], url)
imagefile = Image.open(imagefile).resize(IMAGE_SHAPE)
imagefile = np.array(imagefile) / 255.0
print(imagefile.shape)
book_url = 'https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt'
labels_path = keras.utils.get_file(book_url.split("/")[-1], book_url)
imagenet_labels = open(labels_path).read().splitlines()
y = classifier.predict(imagefile[np.newaxis, ...]).tolist()[0]
rez = list(zip(list(y), list(range(0, len(list(y)) - 1))))
t = sorted(rez, key=lambda x: x[0], reverse=True)
cor = [(a, imagenet_labels[b]) for a, b in t]
pprint.pprint(cor[:20])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment