Guest User

Untitled

a guest
Nov 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. from keras.preprocessing.image import load_img
  2. from keras.preprocessing.image import img_to_array
  3. from keras.applications.imagenet_utils import decode_predictions
  4. import matplotlib.pyplot as plt
  5.  
  6. filename = 'cat.jpg'
  7. # load an image in PIL format
  8. original = load_img(filename, target_size=(224, 224))
  9. print('PIL image size',original.size)
  10. plt.imshow(original)
  11. plt.show()
  12.  
  13. # convert the PIL image to a numpy array
  14. # IN PIL - image is in (width, height, channel)
  15. # In Numpy - image is in (height, width, channel)
  16. numpy_image = img_to_array(original)
  17. plt.imshow(np.uint8(numpy_image))
  18. plt.show()
  19. print('numpy array size',numpy_image.shape)
  20.  
  21. # Convert the image / images into batch format
  22. # expand_dims will add an extra dimension to the data at a particular axis
  23. # We want the input matrix to the network to be of the form (batchsize, height, width, channels)
  24. # Thus we add the extra dimension to the axis 0.
  25. image_batch = np.expand_dims(numpy_image, axis=0)
  26. print('image batch size', image_batch.shape)
  27. plt.imshow(np.uint8(image_batch[0]))
  28.  
  29.  
  30.  
  31. # prepare the image for the VGG model
  32. processed_image = vgg16.preprocess_input(image_batch.copy())
  33.  
  34. # get the predicted probabilities for each class
  35. predictions = vgg_model.predict(processed_image)
  36. print (predictions)
  37.  
  38. # convert the probabilities to class labels
  39. # We will get top 5 predictions which is the default
  40. #label = decode_predictions(predictions)
Add Comment
Please, Sign In to add comment