How to use the models i have build

how can i use the models i’ve build?
thanks very much.

Hi @Justin

This page on Exporting explains how to save a trained model and it includes a link on Serving a TensorFlow Model so that it can be used outside PerceptiLabs.

That might get you started.

I don’t know whether it is currently possible to “use” a model in PerceptiLabs - as far as I know, the data provided is only used for training, validation and test and I haven’t seen a way to provide other input. @robertl might be able to comment further.

1 Like

Hi @Justin,

Julian provided some good links, here is also a small script I usually use (for image classification) that will run your model:

import tensorflow as tf
import os
from PIL import Image
import numpy as np
from tensorflow import keras

mapping = {0: "Normal", 1: "Defect"}
path_to_model = "Pills_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
defected = load_image("C:/Users/Robert/Documents/PerceptiLabs/Default/pills/defect/pic.6.571.0.png")
normal = load_image("C:/Users/Robert/Documents/PerceptiLabs/Default/pills/normal/pic.6.443.0.png")

#Loads the model
model = keras.models.load_model(path_to_model)

#Makes some predictions and catogirizes them
prediction1 = model(defected)
print(prediction1)
print(mapping[np.asarray(prediction1['labels']).argmax()])
prediction2 = model(normal)
print(prediction2)
print(mapping[np.asarray(prediction2['labels']).argmax()])

We don’t have any way to run inference on the model in the tool yet, but we are getting more and more requests around that so we likely will add something for that soon :slight_smile:

1 Like

@robertl That’s a very nice compact example of how to exploit a created model - probably worth referencing in the FAQ

3 Likes

Thanks very much :slightly_smiling_face:

Aaah hahah I was just looking for an answer to this the other day but managed to use my model to make a prediction after pulling my hairs a bit. Wish i would seen this first :rofl: Like @JulianSMoore said, it would be great to referencing this in the FAQ :slightly_smiling_face:
On the plus side, i learned some TensorFlow :v:

But, now I wonder, is there any possibility to have my (fully convolutional) model input accept another shape than it was trained on? Making it do that doesn’t seem (to me anyway) trivial in Tensorflow? I would want to have the input layer to be shape (None, None, None, 3) instead of fixed (None, 56, 56, 3) in my case… In pytorch, i could just feed the model any size and the output would change accordingly…

Haha, It got added to the FAQ yesterday :slight_smile:

As long as your model is fully convolutional it should be possible to use any shape you want. There is a chance that we export it in a way that locks the input to a certain shape though, but I have not tried myself.

Did you get a crash when trying with differently sized images?

1 Like

@robertl TF just throws an error trying to make an inference on anything thats not shape [(None, 56, 56, 3)]

Here’s the model.summary()

>>> model.summary()
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
low (InputLayer)             [(None, 56, 56, 3)]       0         
_________________________________________________________________
training_model (TrainingMode ({'high': (None, 224, 224 114307    
=================================================================
Total params: 114,307
Trainable params: 114,307
Non-trainable params: 0
_________________________________________________________________
>>> 

Hey @birdstream,

What exactly does the TF error say? :slight_smile:

Maybe I’m just doing things wrong here :sweat_smile: it says the input shape is other than expected (which really isn’t a mystery i guess)

full output:

>>> img = Image.open('/home/joakim/Bilder/250px-Limes_kiwis_berry_berries.jpg')
>>> img_array = tf.keras.preprocessing.image.img_to_array(img)
>>> img_array.shape
(188, 250, 3)
>>> img_array = img_array.reshape(1,188,250,3)
>>> img_array.shape
(1, 188, 250, 3)
>>> predict = model(img_array)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/joakim/anaconda3/envs/perc/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1013, in __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
  File "/home/joakim/anaconda3/envs/perc/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py", line 270, in assert_input_compatibility
    ', found shape=' + display_shape(x.shape))
ValueError: Input 0 is incompatible with layer model: expected shape=(None, 56, 56, 3), found shape=(1, 188, 250, 3)
>>>

Ah okay, yea then this is likely because the input shape in the model has been fixed.
I have added a ticket to look into how to make it accept any input size :slight_smile:

Just one last check on it, you only have Conv layers in the model right?
A single Dense layer would make it crash like this otherwise.

1 Like

Ah, okay :slight_smile:
Yes, it was only conv layers :slight_smile: