jonathanmarek1 / binarynet-tensorflow

https://jonathanmarek1.github.io/binarynet-tensorflow/
63 stars 18 forks source link

how to convert cifar_bnn trained model to C? #7

Closed wjtan99 closed 6 years ago

wjtan99 commented 6 years ago

@jonathanmarek1 Thanks a lot for sharing your excellent work.

In the example of cifar_bnn, you said

This example reimplements the CIFAR10 model from the BinaryNet paper. It also contains a basic example (`test_cifar10.c') to verify the exported weights and code. It has fully binary weights and activations and achieves an accuracy of around 88.6%.

But I could not find the test_cifar10.c.

I got my cifar_bnn.py running now. After the training is done, how do I convert it to a c code?

jonathanmarek1 commented 6 years ago

You need to call the following function after training is done:

tf_export.export(y, x0, name, quantize) 

(see xnornet.py, import tf_export is also needed)

It should work but the cifar example was used only as a training test and export never tested with it.

wjtan99 commented 6 years ago

Do you have any example which trains the model directly in Tensorflow then covert to C? The only example you have is loading a torch model then covert to Tensorflow. So I do not know which part of code is really needed if we train the binary net in Tensorflow.
Thanks.

jonathanmarek1 commented 6 years ago

You only have to call the export function. It should be used when the weights are loaded, either after training or after loading the checkpoint.

wjtan99 commented 6 years ago

Thanks for your quick reply. Before I saw your answer, I was reading your xnornet.py very carefully and realized that after the training is done, we can call the tf_export function and port the model into C. And that is what you said.
Do you have an example that you have verified working using this flow? Thanks a lot.

wjtan99 commented 6 years ago

@jonathanmarek1 I tried tf_export.export(y, x0, 'cifar_bnn', False) but ran into an error

Traceback (most recent call last): File "cifar_bnn.py", line 124, in tf_export.export(y, x0, 'cifar_bnn', False) File "/media/ubuntu/data/opensource/binarynet-tensorflow/tf_export.py", line 231, in export assert(False) AssertionError

I tracked into it, and found that the "Reshape" operator is not defined in tf_export.py.

Can you help me how to fix it? Thanks a lot.

wjtan99 commented 6 years ago

I changed the input and output format to use the shape exactly same as the ones in the networks, now this error is gone. Now I have error

Traceback (most recent call last): File "cifar_bnn.py", line 129, in tf_export.export(y, x0, 'cifar_bnn', False) File "/media/ubuntu/data/opensource/binarynet-tensorflow/tf_export.py", line 300, in export assert(layer.act != 'bin') # TODO AssertionError

The error happens at

        if layer.binary:
            print([binary, layer.binary, layer.act]) 
            assert(layer.act != 'bin') # TODO
            k = np.concatenate([layer.m, layer.b]).astype(np.float32)
            name =  'bin_float'

I am not sure if this is for the last layer, which is not binary. Can you take a look?

wjtan99 commented 6 years ago

I made it work finally. I will test the c code in a embedded device and let you know if everything is correct.
Thanks.