Ohnesorge
commited on
Commit
·
50183b3
1
Parent(s):
89bc801
inference engine
Browse files- inference.py +28 -0
inference.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.models import load_model
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
+
model = load_model('keras_model.h5')
|
| 7 |
+
|
| 8 |
+
# Create the array of the right shape to feed into the keras model
|
| 9 |
+
# The 'length' or number of images you can put into the array is
|
| 10 |
+
# determined by the first position in the shape tuple, in this case 1.
|
| 11 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 12 |
+
# Replace this with the path to your image
|
| 13 |
+
image = Image.open('<IMAGE_PATH>')
|
| 14 |
+
#resize the image to a 224x224 with the same strategy as in TM2:
|
| 15 |
+
#resizing the image to be at least 224x224 and then cropping from the center
|
| 16 |
+
size = (224, 224)
|
| 17 |
+
image = ImageOps.fit(image, size, Image.ANTIALIAS)
|
| 18 |
+
|
| 19 |
+
#turn the image into a numpy array
|
| 20 |
+
image_array = np.asarray(image)
|
| 21 |
+
# Normalize the image
|
| 22 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 23 |
+
# Load the image into the array
|
| 24 |
+
data[0] = normalized_image_array
|
| 25 |
+
|
| 26 |
+
# run the inference
|
| 27 |
+
prediction = model.predict(data)
|
| 28 |
+
print(prediction)
|