hendriawan

day2-04-vgg16

Nov 29th, 2022
981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | Source Code | 0 0
  1. from tensorflow.keras.preprocessing.image import load_img
  2. from tensorflow.keras.preprocessing.image import img_to_array
  3. from tensorflow.keras.applications.vgg16 import preprocess_input
  4. from tensorflow.keras.applications.vgg16 import decode_predictions
  5. from tensorflow.keras.applications.vgg16 import VGG16
  6.  
  7. model = VGG16()  # model = VGG16(weights=”imagenet”)
  8. image = load_img('mug.jpg', target_size=(224, 224))
  9. image = img_to_array(image)
  10.  
  11. # reshape data for the model -> (batchsize, height, width, channels)
  12. image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
  13.  
  14. # prepare the image for the VGG model
  15. image = preprocess_input(image)
  16.  
  17. # predict the probability across all output classes
  18. yhat = model.predict(image)
  19.  
  20. # convert the probabilities to class labels, top 5 default
  21. label = decode_predictions(yhat)
  22.  
  23. # retrieve the most likely result, e.g. highest probability
  24. label = label[0][0]
  25.  
  26. # print the classification
  27. print('%s (%.2f%%)' % (label[1], label[2]*100))
Advertisement
Add Comment
Please, Sign In to add comment