azeme1 / keras2ncnn

MIT License
24 stars 0 forks source link

keras2ncnn

Export Keras model to Tencent/NCNN.

Supported Keras layers and Features

Supported Keras Layer Optimization

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