serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel
https://serizba.github.io/cppflow/
MIT License
785 stars 178 forks source link

Run time error with input dimensions #210

Closed davidissamattos closed 2 years ago

davidissamattos commented 2 years ago

Hi, I am trying to run ccpflow for a simple regression example and I keep getting the following error runtime error:

2022-08-29 16:00:11.005482: I tensorflow/cc/saved_model/loader.cc:228] Restoring SavedModel bundle.
2022-08-29 16:00:11.063842: I tensorflow/cc/saved_model/loader.cc:212] Running initialization op on SavedModel bundle at path: ../tfmodel/
2022-08-29 16:00:11.080403: I tensorflow/cc/saved_model/loader.cc:301] SavedModel load for tags { serve }; Status: success: OK. Took 102661 microseconds.

terminate called after throwing an instance of 'std::runtime_error'
  what():  In[0] and In[1] has different ndims: [1] vs. [1,64]
    [[{{function_node __inference__wrapped_model_51816}}{{node sequential_9/dense_16/Relu}}]]

My model is very simple (from https://www.tensorflow.org/tutorials/keras/regression) and saved with regular save.

horsepower = np.array(train_features['Horsepower'])
horsepower_normalizer = layers.Normalization(input_shape=[1,], axis=None, name='hp')
horsepower_normalizer.adapt(horsepower)

horsepower_model = tf.keras.Sequential([
    horsepower_normalizer,
    layers.Dense(64, activation='relu'),
    layers.Dense(64, activation='relu'),
    layers.Dense(1, name='mpg')
])

My cpp code also very simple

#include <iostream>
#include "cppflow/cppflow.h"

int main() {

    // Load the model
    cppflow::model model("../tfmodel/");

    // Set the input
    float input = 100.0;

    // Run

    // Get the input and output graph nodes from saved_model_cli 
    auto output = model({{"serving_default_hp_input:0", input}}, {"StatefulPartitionedCall:0"});

    // Show the predicted value
    std::cout << output << std::endl;

   return 0;
}

The code compiles fine and the libraries are linked with CMake on Windows. Other cppflow functions work, but I have this problem with the dimensions

Does anyone knows how to proceed? I have not seen any regression or numeric examples, only image classification. Thanks for any pointer, Best David

serizba commented 2 years ago

Hi @davidissamattos

Probably the issue is that you are feeding a scalar instead of a tensor. Try feeding a tensor of shape [1, 1].

auto input_vector = std::vector<float>({1.0}); 
auto input_tensor = cppflow::tensor(input_vector, {1, 1});
davidissamattos commented 2 years ago

Awesome! Thanks @serizba! I did try before setting the input as a tensor but (as I realize now) I was also having runtime errors with the output (printing output instead of output[0].

For future reference, this is the working code:

#include <iostream>
#include "cppflow/cppflow.h"

int main() {

    // Load the model
    cppflow::model model("../tfmodel/");

    // Set the input (should be a tensor)
    auto input_vector = std::vector<float>({100.0}); 
    auto input_tensor = cppflow::tensor(input_vector, {1, 1});

    // Run

    // Get the input and output graph nodes from saved_model_cli 
    auto output = model({{"serving_default_hp_input:0", input_tensor}}, {"StatefulPartitionedCall:0"});

    // Show the predicted class
    std::cout << output[0] << std::endl;

    return 0;
}