Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Created December 11, 2018 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dobiasd/14a3e233725a16bb7c86ca6f4d81a825 to your computer and use it in GitHub Desktop.
Save Dobiasd/14a3e233725a16bb7c86ca6f4d81a825 to your computer and use it in GitHub Desktop.

Minimal example of using a model with multiple input tensors.

# create_model.py

import numpy as np
from keras.layers import Input, Concatenate, Flatten, Dense
from keras.models import Model

input_1 = Input(shape=(240, 320, 3))
input_2 = Input(shape=(100, 1, 1))

output = Dense(4)(Concatenate()([Flatten()(input_1), Flatten()(input_2)]))

model = Model(inputs=[input_1, input_2], outputs=output)
model.compile(loss='categorical_crossentropy', optimizer='nadam')

model.save('keras_model.h5', include_optimizer=False)
// main.cpp

#include <fdeep/fdeep.hpp>

int main()
{
    const auto model = fdeep::load_model("fdeep_model.json");
    const fdeep::tensor5 image(fdeep::shape5(1, 1, 240, 320, 3), 0.0f);
    const fdeep::tensor5 values(fdeep::shape5(1, 1, 100, 1, 1), 0.0f);
    const auto result = model.predict({image, values});
    std::cout << fdeep::show_tensor5s(result) << std::endl;
}
python3 create_model.py
python3 convert_model.py keras_model.h5 fdeep_model.json
g++ -std=c++14 -O3 main.cpp
./a.out

output:

Loading json ... done. elapsed time: 0.089034 s
Running test 1 of 1 ... done. elapsed time: 0.002383 s
Loading, constructing, testing of fdeep_model.json took 0.134049 s overall.
[[[[0.0000]], [[0.0000]], [[0.0000]], [[0.0000]]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment