hendriawan icon

day2-04-vgg16

hendriawan | PRO | 11/29/22 06:06:38 AM UTC | 0 ⭐ | 917 👁️ | Never ⏰ | []
Python |

990 B

|

Source Code

|

0 👍

/

0 👎

from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.applications.vgg16 import decode_predictions
from tensorflow.keras.applications.vgg16 import VGG16
 
model = VGG16()  # model = VGG16(weights=”imagenet”)
image = load_img('mug.jpg', target_size=(224, 224))
image = img_to_array(image)
 
# reshape data for the model -> (batchsize, height, width, channels)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
 
# prepare the image for the VGG model
image = preprocess_input(image)
 
# predict the probability across all output classes
yhat = model.predict(image)
 
# convert the probabilities to class labels, top 5 default
label = decode_predictions(yhat)
 
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
 
# print the classification
print('%s (%.2f%%)' % (label[1], label[2]*100))

Comments