pplonski / keras2cpp

This is a bunch of code to port Keras neural network model into pure C++.
MIT License
679 stars 153 forks source link

Issue dumping the network to .nnet file #46

Closed MaajidKhan closed 4 years ago

MaajidKhan commented 4 years ago

Hey, I have issue while dumping the network into dat file. Basically, 2nd step :

  1. Dump network to dat file python dump_to_cpp.py -a arch.json -w weights.h5 -o dumped_nn.dat.

while, I run this script dump_to_cpp.py , it throws an error:

Using TensorFlow backend. Read architecture from arch.json Read weights from weights.h5 Writing to converted_to_cpp.dat 0 name Traceback (most recent call last): File "dump_to_cpp.py", line 36, in fout.write('layer ' + str(ind) + ' ' + l['class_name'] + '\n') TypeError: string indices must be integers

The issue is, l should have been of data type 'list' but in our case it is a 'string'.

can you please rectify this issue and provide a working solution.

pplonski commented 4 years ago

Hey, from what I remember the code was working with Theano backend. So to use it with TensorFlow you need to provide your own code for transformation (it should be a matter of change in dimension order, so it should be very similar to current code). Sorry that it is not working out of the box.

Please check the following discussion https://stackoverflow.com/questions/36720498/convert-keras-model-to-c to find more similar solutions.

JosephPB commented 4 years ago

Hi @MaajidKhan for a TensorFlow conversion I think you'll need to change L30 in dump_to_cpp.py: to: for ind, l in enumerate(arch["config"]["layers"]): as the json file contains an extra key for "config".

You might then also need to make some edits to how the weights are read in too.

Hope this helps!

MaajidKhan commented 4 years ago

Hi @JosephPB , your fix worked for me for working around with Tensorflow backend. Thanks.