Search code examples
python-3.xtensorflowtensorflow2.0structured-data

Tensorflow2 ValueError: Could not find matching function to call loaded from the SavedModel


I'm trying to take data from a json string convert it into a format that tensorflow can use and make a prediction.

import tensorflow as tf
import pandas as pd
import json
import matplotlib.pyplot as ply
%matplotlib inline
from sklearn.model_selection import train_test_split
import numpy as np

new_model = tf.keras.models.load_model('demo_2020_05/demo_1')
json_string = '[{"S8":"String Data 8" , "S9":"String Data 9" , "S4":"String Data 4" , "S3":"String Data 3" , "S7":"String Data 7" , "S5":"String Data 5" , "S2":"String Data 2" , "S6":"String Data 6" , "I2": 400 , "I3": 0 , "I1": 0  , "S1":"String Data 1"  ,"S10":"String Data 10"}]'
a_json = json.loads(json_string)


predict = {}
for i in a_json: 
    for key, val in i.items():      
         predict[key] = []

for i in a_json:
    for key, val in i.items():
        temp = []
        temp = predict[key]
        temp.append(val)
        predict[key] = temp

for i in a_json:
    for key, val in i.items():
        temp = []
        temp = predict[key]
        temp_np = np.array(temp)
        predict[key] = temp_np

print(predict)
    {'S1': array(['String Data 1'], dtype='<U42'), 'S2': array(['String Data 2'], dtype='<U23'), 'S3': array([String Data 3'], dtype='<U6'), 'S4': array(['String Data 4'], dtype='<U5'), 'S5': array(['String Data 5'], dtype='<U5'), 'S6': array(['String Data 6'], dtype='<U6'), 'S7': array(['String Data 7'], dtype='<U2'), 'S8': array(['String Data 8'], dtype='<U12'), 'I1': array([400]), 'I2': array([0]), 'I3': array([0]), 'S9': array(['String Data 9'], dtype='<U11'), 'S10': array(['String Data 10'], dtype='<U7')}






new_model.predict(predict)

This is the exact structure and data I passed to the model when the model was built. I've copy/pasted the json, the for loops and the prediction call and it works and makes a prediction in the jupyter notebook that created the model.

This is where I get the following error :

ValueError: Could not find matching function to call loaded from the SavedModel. Got:

* {
      'S1': <tf.Tensor 'features:0' shape=(None, 1) dtype=string>
    , 'S2': <tf.Tensor 'features_1:0' shape=(None, 1) dtype=string>
    , 'S3': <tf.Tensor 'features_2:0' shape=(None, 1) dtype=string>
    , 'S4': <tf.Tensor 'features_3:0' shape=(None, 1) dtype=string>
    , 'S5': <tf.Tensor 'features_4:0' shape=(None, 1) dtype=string>
    , 'S6': <tf.Tensor 'features_5:0' shape=(None, 1) dtype=string>
    , 'S7': <tf.Tensor 'features_6:0' shape=(None, 1) dtype=string>
    , 'I1': <tf.Tensor 'features_7:0' shape=(None, 1) dtype=int64>
    , 'S8': <tf.Tensor 'features_8:0' shape=(None, 1) dtype=string>
    , 'S9': <tf.Tensor 'features_9:0' shape=(None, 1) dtype=string>
    , 'S10': <tf.Tensor 'features_10:0' shape=(None, 1) dtype=string>
    , 'I2': <tf.Tensor 'features_11:0' shape=(None, 1) dtype=int64>
    , 'I3': <tf.Tensor 'features_12:0' shape=(None, 1) dtype=int64>
    }
* None
  Keyword arguments: {}

Expected these arguments to match one of the following 1 option(s):

Option 1:
Positional arguments (2 total):
 * {
  'I3': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I3')
, 'S7': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S7')
, 'S8': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S8')
, 'I2': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I2')
, 'S3': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S3')
, 'S6': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S6')
, 'S9': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S9')
, 'S2': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S2')
, 'I1': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I1')
, 'S1': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S1')
, 'S4': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S4')
, 'S5': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S5')
, 'S10': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S10')
}
* None
  Keyword arguments: {}

It seems that predict() is looking for a different datatype/structure. However I've tried to send it what I think it's asking for doing the following.

ta_S4 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S4 = ta_S4.write(0,'String Data 4')

ta_S3 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S3 = ta_S3.write(0,'String Data 3')

ta_S7 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S7 = ta_S7.write(0,'String Data 7')

ta_S5 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S5 = ta_S5.write(0,'String Data 5')

ta_S2 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S2 = ta_S2.write(0,'String Data 2')

ta_S6 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S6 = ta_S6.write(0,'String Data 6')

ta_I2= tf.TensorArray(tf.int32, size=1, dynamic_size=True)
ta_I2  = ta_I2.write(0, 400)

ta_I3 = tf.TensorArray(tf.int32, size=1, dynamic_size=True)
ta_I3 = ta_I3.write(0, 0)

ta_I1 = tf.TensorArray(tf.int32, size=1, dynamic_size=True)
ta_I1 = ta_I1.write(0, 0)

ta_S1 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S1 = ta_S1.write(0,'String Data 1')

ta_S10 = tf.TensorArray(tf.string, size=1, dynamic_size=True)
ta_S10 = ta_S10 .write(0,'String Data 10')




predict = {'S8': ta_S8,
 'S9': ta_S9,
 'S4': ta_S4,
 'S3': ta_S3,
 'S7': ta_S7,
 'S5': ta_S5,
 'S2': ta_S2,
 'S6': ta_S6,
 'I2': ta_I2,
 'I3': ta_I3,
 'I1': ta_I1,
 'S1': ta_S1,
 'S10': ta_S10
          }

new_model.predict(predict)

This errors with :

ValueError: Failed to find data adapter that can handle input: (<class 'dict'> containing {"<class 'str'>"} keys and {"<class 'tensorflow.python.ops.tensor_array_ops.TensorArray'>"} values), <class 'NoneType'>

I'm not sure what to pass to new_model.predict().

Edit 1:

To ask the question more simply. How can I create a data structure that resembles this (shown just below) to pass to new_model.predict() to get a prediction?

{
      'I3': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I3')
    , 'S7': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S7')
    , 'S8': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S8')
    , 'I2': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I2')
    , 'S3': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S3')
    , 'S6': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S6')
    , 'S9': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S9')
    , 'S2': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S2')
    , 'I1': TensorSpec(shape=(None, 1), dtype=tf.int32, name='features/I1')
    , 'S1': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S1')
    , 'S4': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S4')
    , 'S5': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S5')
    , 'S10': TensorSpec(shape=(None, 1), dtype=tf.string, name='features/S10')
    }
    * None
      Keyword arguments: {} 

Solution

  • This was solved with

    pip uninstall tensorflow
    pip uninstall tensorboard
    pip install -q tf-nightly
    pip install --ignore-installed tf-nightly