How to run an exported model in a python environment?

Hey, I’ve built and ran a model with the tutorial mnist dataset. Then i exported it and now I’m trying to load and predict new images in python. Later on trying on my Raspberry pi.

However, I’m not sure how to even run the exported model? Obviously I must be quite bad at googling since i can’t seem to find any answers.

from tensorflow import keras
from keras.preprocessing.image import load_img
from keras import *
model = keras.models.load_model("B:\Exports\MnistTest")
img = load_img('199.png')
model.predict(img)    

Getting lots of error doing it this way though. https://pastebin.com/70QD31VP

Hi @Toernblom,
Cool that you want to run it on a Raspberry pi!

Here is a small script to run an image classification model from PL I often use:

import tensorflow as tf
import os
from PIL import Image
import numpy as np
from tensorflow import keras
mapping = {0: "class0", 1: "class1", 2: "class2"}
path_to_model = "path/to/model"
def load_image(path):
    image = Image.open(path)
    image = np.array(image, dtype=np.float32)
    image = np.expand_dims(image, axis=0)
    return image
#Load some images
class_1_image = load_image("path/to/image1")
class_2_image = load_image("path/to/image2")
class_3_image = load_image("path/to/image3")
#Loads the model
model = keras.models.load_model(path_to_model)
#Makes some predictions and catogirizes them
prediction1 = model(class_1_image)
print(prediction1)
print(mapping[np.asarray(prediction1['labels']).argmax()])
prediction2 = model(class_2_image)
print(prediction2)
print(mapping[np.asarray(prediction2['labels']).argmax()])
prediction3 = model(class_3_image)
print(prediction3)
print(mapping[np.asarray(prediction3['labels']).argmax()])

By the looks of your error, it’s either because something went wrong in saving the variables, or the images sent into the model had a strange format.
If the script I just sent does not work for you, can you send both your original and exported model here and we can take a quick look? :slight_smile:

Hi @robertl, I also used the mnist dataset. I exported the model as a TensorFlow model, not compressed and not quantized.

When I try to run I receive the following error:

File “C:\Users\USER\anaconda3\lib\site-packages\keras\engine\input_spec.py”, line 266, in assert_input_compatibility
raise ValueError('Input ’ + str(input_index) +

ValueError: Input 0 is incompatible with layer model: expected shape=(None, 28, 28, 1), found shape=(1, 898, 885, 4)

AttributeError: type object ‘h5py.h5.H5PYConfig’ has no attribute ‘reduce_cython

I think it could be a problem with my Perceptilabs export because I received this warning:

WARNING:tensorflow: No training configuration found in save file, so the model was not compiled. Compile it manually.

Hi @JWalker,
Happy weekend!

Hmm, no immediate idea why you are getting a Cython error, are you using the same environment as you are using to run PerceptiLabs?

The warning you are getting should not cause any issues to my knowledge, you should be able to load it even though it has not been compiled.

I also noticed that you are having a shape issue. How does the data you are trying to send the model look like?

Thanks Robert,

How do I paste code in a way so that it is embedded in its own scrollable box?

Either paste the text, select it and then press this button:
Or upload a .txt file :slight_smile:

Thank you Robert.

Here is my exact code:

from PIL import Image
import numpy as np
from tensorflow import keras
#Loads the model
path_to_model = "d:/Perceptilabs/Exported Models/Number Recognition"
model = keras.models.load_model(path_to_model)
def load_image(path):
    image = Image.open(path)
    image = np.array(image, dtype=np.float32)
    image = np.expand_dims(image, axis=0)
    return image
#Load an image
image = load_image("C:/Users/USER/Desktop/Picture1.png")
#Makes a prediction
prediction1 = model.predict(image)
print(prediction1)

Variable explorer tells me that image is an Array of float 32. If you like, I can ask python to print exactly what is in that array. It is essentially full of zeroes.

I have attached the output of the console here. I am using Spyder 5.0.5.
Console Output.txt (9.3 KB)

I asked python to print image and it gave me this:

[[[[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   ...
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   ...
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   ...
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  ...
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   ...
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   ...
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]
  [[0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   ...
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]
   [0. 0. 0. 0.]]]]

Edit: I thought it might help if I attached a copy of the image that I was trying to use.

Thanks!

Can you try to run with an image from the training dataset and see if that works any better?
By the looks of it, the image you are trying to send into the model is a different size than what the model accepts (885x898x4 instead of 28x28x1), so it might just be that :slight_smile:

1 Like

Almost. Here is my current code.

from PIL import Image
import numpy as np
from tensorflow import keras
#Loads the model
path_to_model = "d:/Perceptilabs/Exported Models/Number Recognition"
model = keras.models.load_model(path_to_model)
size  = (28,28)
#Load an image and convert it to the correct size and luminescence 
image = Image.open("C:/Users/USER/Desktop/Picture1.png")
image = image.resize(size)
image = image.convert('L')
image.show() #show you image so you can compare the result
image = np.expand_dims(image, axis=0) #i don't know why i need this
#Just a check
print('image shape = \n', image.shape)
#Makes a prediction
prediction1 = model(image)
print(prediction1)

I am falling at the final hurdle.

It gives me a result of:

{‘Labels’: <tf.Tensor: shape=(1,), dtype=int32, numpy=array([1])>}

Which I am guessing is not quite the prediction.

Even when I add in the additional lines from your code, @robertl, it still gives me this:

File “D:\Perceptilabs\Run the model.py”, line 27, in
print(mapping[np.asarray(prediction1[‘labels’]).argmax()])

KeyError: ‘labels’

Awesome looks like we are getting closer!
Try this instead: print(np.asarray(prediction1['Labels']))

So Labels with a big L and remove the mapping() as that is now built into the prediction

@robertl

Yes, that works well, thank you.

Here is the working code. You have to have a white number on a black background for it to have any accuracy. Any change to that and you will receive gibberish, which makes sense because that is how it was trained.

from PIL import Image
import numpy as np
from tensorflow import keras
#Loads the model
path_to_model = "d:/Perceptilabs/Exported Models/Number Recognition"
model = keras.models.load_model(path_to_model)
size  = (28,28)
#Load an image and convert it to the correct size and luminescence 
image = Image.open("C:/Users/USER/Desktop/Picture.png")
image = image.resize(size)
image = image.convert('L')
image = np.expand_dims(image, axis=0)
#Makes a prediction
prediction1 = model(image)
print('The prediction is ',(np.asarray(prediction1['Labels'])))

Great to hear that it works now! :slight_smile:

1 Like

Thank you for your help. On to the next one :slight_smile: