serizba / cppflow

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

Difficulty with problem having multiple inputs and outputs #209

Open mohamedmkamra opened 1 year ago

mohamedmkamra commented 1 year ago

I am trying to load a model that has multiple inputs and outputs. I followed you example but I keep getting segmentation faults errors. The model is created using the following python code:

def build_model(X_train, d , n ):

    n_inputs = X_train.shape[1]
    n_outputs = Y_train.shape[1]
    model = Sequential()
    model.add(Dense(128, input_dim=n_inputs, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    model.add(Dropout(d))
    # model.add(Dense(32, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    # model.add(Dropout(0))
    model.add(Dense(n_outputs))
    optimizer  = tf.keras.optimizers.Adam( learning_rate=0.001  , beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, name='Adam') # These are the default values
    # Smaller learning rate will cause better accuracy, but with higher computaional power (larger training time)

    model.compile(loss ='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])

    return model

and I saved the model using the code:

# Save Best Model
BestModel.save(f"{Model_Dir}/Model", save_format='tf')

It should be straight forward using the example that you provided but I keep getting errors. Perhaps I should treat this as a single input and output but the data type would be a tuple or list? Am I missing something?

Thanks

serizba commented 1 year ago

Hi @mohamedmkamra

How are then you running the model with cppflow? What error you getting?

As far as I can see is a single-input, single-output model, so I don't think it's a multi-input problem.

mohamedmkamra commented 1 year ago

Actually the number of inputs is technically not 1. X_train is a numpy array with number of rows = ntrainData and number of columns = n_inputs Y_train is a numpy array with number of rows = ntrainData and number of columns = n_outputs This is why I am not sure about this.

serizba commented 1 year ago

ntrainData is the number of dimensions or features of your data?

mo7ammedmostafa commented 1 year ago

No, ntrainData is the size of training data ( i.e. number of training samples)

serizba commented 1 year ago

PyTorch models are prepared to take inputs of shape NxD (as yours). With N being number of samples, batch dimension, and D being the dimension of each data point.

Without seeing what are you trying is hard to make a guess.

mohamedmkamra commented 1 year ago

I am not using PyTorch, I am using tensor flow. I have used tensorflow::keras to train a neural network based on the model above.

def build_model(X_train, d , n ):

    n_inputs = X_train.shape[1]
    n_outputs = Y_train.shape[1]
    model = Sequential()
    model.add(Dense(128, input_dim=n_inputs, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    model.add(Dropout(d))
    # model.add(Dense(32, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    # model.add(Dropout(0))
    model.add(Dense(n_outputs))
    optimizer  = tf.keras.optimizers.Adam( learning_rate=0.001  , beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, name='Adam') # These are the default values
    # Smaller learning rate will cause better accuracy, but with higher computational power (larger training time)

    model.compile(loss ='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])

    return model

The NN model is simple: it has 5 inputs, and the size of training data is roughly 100K samples with every sample consisting of 5 values as inputs and 2 values as outputs.

NN

The number of hidden layers and number of neurons per layer shown here is just for illustration.

serizba commented 1 year ago

Yeah, I meant TensorFlow sorry.

But I don't see how you trying to use cppflow, you only posted Python code. If it's a TensorFlow related issue, it shouldn't be posted here.

mo7ammedmostafa commented 1 year ago

The tf model itself is good. It works fine for evaluation when the model files are read in Python.

But when I try to read the model files using cppflow, I get segmentation fault errors: Basically, I tried using the load_model example

include

include "cppflow/ops.h"

include "cppflow/model.h"

int main() {

auto input = cppflow::fill({10, 5}, 1.0f);
cppflow::model model(std::string(MODEL_PATH));
auto output = model(input);

std::cout << output << std::endl;

auto values = output.get_data<float>();

for (auto v : values) {
    std::cout << v << std::endl;
}
return 0;

}

yochananscharf commented 1 year ago

Try following this answer on StackOverflow