keras2ncnn
Export Keras model to Tencent/NCNN.
Supported Keras layers and Features
- Functional Model API (the Sequential and Model as Layer features should be transformed in to flat Functional API Model)
- InputLayer
- ReLU/ReLU6/LeakyReLU/Softmax/Sigmoid
- Clip
- Reshape/Flatten(converted with NCNN::Reshape)
- MaxPooling2D/AveragePooling2D/MaxPool2D/AvgPool2D
- GlobalMaxPooling2D/GlobalAveragePooling2D/GlobalMaxPool2D/GlobalAvgPool2D
- ZeroPadding2D/ReflectPadding2D
- BatchNormalization/InstanceNormalization
- Conv2D/DepthwiseConv2D/SeparableConv2D(converted with split into NCNN::ConvolutionDepthWise->NCNN::Convolution)
- Concatenate/Add/Multiply
- UpSampling2D(nearest neighbour/bilinear)
- Conv2DTranspose(only for the even strides)
Supported Keras Layer Optimization
- Conv2D/DepthwiseConv2D/Conv2DTranspose with BatchNormalization
- BatchNormalization with Conv2D/DepthwiseConv2D/Conv2DTranspose(In Progress)
- ZeroPadding2D(In Progress :: Fusion with Convolution/Pooling)
Unit tests is written
Preconverted models
Some 'preconverted' models can be downloaded from
DropBox
Requirements installation
The code was tested with python3.7 with TensorFlow 1.x/2.x (CPU). The code should work with python3.x .
The behaviour with TensorFlow GPU/TPU.
git clone https://github.com/azeme1/keras2ncnn.git
cd keras2ncnn
pip3 install -r requirements.txt
Usage
The model zoo folder contains the sample model
(CelebA_PrismaNet_256_hair_seg_model_opt_001.hdf5)
as well as the result of the conversion
(graph: CelebA_PrismaNet_256_hair_seg_model_opt_001.param and
weights: CelebA_PrismaNet_256_hair_seg_model_opt_001.bin)
Load the model from the '.hdf5' file
python3 keras2ncnn.py --model_path=model_zoo/segmentation/hair/model_000/CelebA_PrismaNet_256_hair_seg_model_opt_001.hdf5
Load the model from the '.json' file (the weights should be located at the same folder in '.hdf5')
python3 keras2ncnn.py --model_path=model_zoo/segmentation/hair/model_000/CelebA_PrismaNet_256_hair_seg_model_opt_001.json
Useful Links
Tencent/NCNN documentation
TODO List
Code
Thanx
Important note
Sometimes good result can be achieved with Tensorflow conversion approach
import tensorflow as tf
model = tf.keras.models.load_model("model.h5")
model.save("saved_model")
after that convert 'model.ckpt' or 'model.pb' with following scripts