Dobiasd / frugally-deep

A lightweight header-only library for using Keras (TensorFlow) models in C++.
MIT License
1.07k stars 236 forks source link

Data required to create json #425

Open johnatan151 opened 1 week ago

johnatan151 commented 1 week ago

Hello,

I am trying to manually pass the values of my model to a JSON file so that it is compatible with fd. I am running into this issue not sure how to fix it. I wanted to ask if any knows what I can do to solve it or what the JSON file should contain exactly.

terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error' what(): [json.exception.type_error.302] type must be string, but is null

Dobiasd commented 1 week ago

Can you post the code/data required to re-create the problem?

johnatan151 commented 6 days ago

I should first state what my goal is since I am unsure if it's possible. My goal is to create a Pytorch model and write my own JSON file in the same format that Keras would produce. So I could run the Pytorch model as inference.

Dobiasd commented 5 days ago

Thanks for the context. The project sounds interesting, but also quite non-trivial. Frugally-deep's file format is very specific (and might change with newer versions). Especially the order of dimensions in the weights' matrices can become fiddly. Have you already looked into trying to convert your PyTorch model to Keras using whatever tool/lib, and then going to fdeep from there? If this would work, it would likely be simpler.

johnatan151 commented 2 days ago

I have considered that solution, but I want to be able to gather results only with pytorch to get a better comparison between the two.

Dobiasd commented 2 days ago

Which two would you like to compare?

To get back to your original question on what the JSON file should contain exactly: Frugally-deep's convert_model.py has the definition. For the architecture part, you'd have to look into the code of model.to_json in the Keras/TensorFlow source, or into the import_model.hpp of frugally-deep.

johnatan151 commented 2 days ago

Compare keras and pytorch

Dobiasd commented 1 day ago

Ah, ok. 👍

And why don't you compare them directly without frugally-deep?

johnatan151 commented 1 day ago

I'm hoping to create my own JSON file, as Keras saves models in .h5 format and PyTorch saves them in .pth. From my understanding, these formats differ because of the way each backend stores models based on their graph layout. I’d like to understand exactly what frugally-deep requires in its JSON file, since my Keras models work with frugally-deep, but my PyTorch models do not. Once this is complete I wont be using Fdeep and just pass my json files to C++ to run.

Dobiasd commented 1 day ago

I'm hoping to create my own JSON file, as Keras saves models in .h5 format and PyTorch saves them in .pth. From my understanding, these formats differ because of the way each backend stores models based on their graph layout.

Yes, Keras and PyTorch serialize their models differently.

I’d like to understand exactly what frugally-deep requires in its JSON file, since my Keras models work with frugally-deep, but my PyTorch models do not.

That's expected. Frugally-deep was built to allow running Keras models in C++ without using Keras/TensorFlow. It's not intended to be used with PyTorch models.

Once this is complete I wont be using Fdeep and just pass my json files to C++ to run.

How do you intend to achieve this? The JSON format you're targeting can only be used with frugally-deep.

Both, TensorFlow and PyTorch have C++ APIs:

Maybe this is more suitable for your use-case. If you want to compare Keras with PyTorch (performance, for example), frugally-deep would not help with this, because you would only compare frugally-deep with frugally-deep.

johnatan151 commented 1 day ago

I see thank you for the help I will take a look into these API's. Saved me time :)