Skip to content

Latest commit

 

History

History
27 lines (14 loc) · 2.33 KB

onnx2bnn.md

File metadata and controls

27 lines (14 loc) · 2.33 KB

About ONNX

ONNX (Open Neural Network Exchange) is an open format which is greatly supported or officially integrated by many frameworks and tools.

How onnx2bnn converts the models

  1. Recognizing binary convolutions, whose weights will be bit-packed. The developers of dabnn added several optimizer to ONNX in order to recognize binary convolutions. The details is in dabnn_*.h of https://github.com/daquexian/onnx/tree/optimizer_for_bnn/onnx/optimizer/passes. For bit-packing, please check out this documentation

  2. Update the weight and bias of BN layers following binary conv layers. Since -1 in binary convs is represented by a unset bit (i.e., 0), and bitcount returns the number of set bits (i.e., 1) in a N-bit operand, a correction is needed to get the correct result of binary convs. Specifically, denote a as an N-bit operand, b as the number of set bits in a, c as the number of unset bits in a, the result we want is

b - c = b - (N - b) = 2 * b - N = 2 * bitcount(a) - N

It is an affine transform of bitcount(a), so we accordingly update the weight and bias of the corresponding BN layers.

The details is in https://github.com/JDAI-CV/dabnn/blob/master/tools/onnx2bnn/OnnxConverter.cpp#L522.

  1. Other layers are converted as usual.

Notes (Need Attention)

There are some notes for model conversion.

  1. Binary convolutions are custom operations in training frameworks (e.g., TensorFlow, PyTorch), so the implementations are various. Unfortunately, the most existing implementations of binary convs are not correct. For example, they always pad 0 to their input, while the input should only be +1 or -1. The developers of dabnn provide a standard implementation of binary convs in PyTorch. We advise trainers of BNNs to use this implementation, or implement binary convs in their own training frameworks according to this implementation.

  2. onnx2bnn has multiple recognizing levels. It can even recognize the incorrect binary convs described above (the result will be incorrect though). Please check out this documentation for details.

  3. group is not supported for now.