[text] aaaaaa

Viewer

  1. import tensorflow.keras
  2. from PIL import Image, ImageOps
  3. import numpy as np
  4.  
  5. # Disable scientific notation for clarity
  6. np.set_printoptions(suppress=True)
  7.  
  8. # Load the model
  9. model = tensorflow.keras.models.load_model('keras_model.h5')
  10.  
  11. # Create the array of the right shape to feed into the keras model
  12. # The 'length' or number of images you can put into the array is
  13. # determined by the first position in the shape tuple, in this case 1.
  14. data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
  15.  
  16. # Replace this with the path to your image
  17. image = Image.open('test_photo.jpg')
  18.  
  19. #resize the image to a 224x224 with the same strategy as in TM2:
  20. #resizing the image to be at least 224x224 and then cropping from the center
  21. size = (224, 224)
  22. image = ImageOps.fit(image, size, Image.ANTIALIAS)
  23.  
  24. #turn the image into a numpy array
  25. image_array = np.asarray(image)
  26.  
  27. # display the resized image
  28. image.show()
  29.  
  30. # Normalize the image
  31. normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
  32.  
  33. # Load the image into the array
  34. data[0] = normalized_image_array
  35.  
  36. # run the inference
  37. prediction = model.predict(data)
  38. print(prediction)
  39.  

Editor

You can edit this paste and save as new:


File Description
  • aaaaaa
  • Paste Code
  • 22 Oct-2020
  • 1.14 Kb
You can Share it: