Skip to content

Instantly share code, notes, and snippets.

@sachinkale
Last active September 22, 2020 20:04
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 sachinkale/65fea1f40fba0ca5ebce482eba1ab88e to your computer and use it in GitHub Desktop.
Save sachinkale/65fea1f40fba0ca5ebce482eba1ab88e to your computer and use it in GitHub Desktop.
from tensorflow.keras.models import load_model
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
import json
model = load_model('vggK5-weights-best.h5')
img_path ='_DSC9102.png'
img = image.load_img(img_path, 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)
f = open('vggKFclassIdMap.json')
name_id_map = json.load(f)
score = tf.nn.softmax(preds[0])
result = str(np.argmax(score))
print("This image most likely belongs to \x1b[6;30;42m{}\x1b[0m".format(name_id_map[result]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment