Skip to content

xinthink/flttf

Repository files navigation

flttf

A demonstration to use TensorFlow Lite in a Flutter project.

Handwritten digits recognition

Train the model

A simple neural network is trained for the handwritting recognizer, using the MNIST dataset, see mnist.py or mnist.ipynb for details.

Fits the model and save it to a tflite file:

# prepare the virtualenv
pipenv --python `which python3` install

# fits the model
pipenv run python mnist.py

You can also do further experiments using the Jupyter Notebook:

pipenv run jupyter notebook mnist.ipynb

Real-time object detection

Use the pre-trained Tiny YOLO v2 model.

Convert the saved model file

Prepare a virtualenv with TensorFlow 1.x installed, using Pipenv for example:

pipenv --python `which python3` install tensorflow==1.15 opencv-python keras cython

install darkflow:

pipenv shell
git clone https://github.com/thtrieu/darkflow.git
cd darkflow
pip3 install -e .

download the Tiny YOLO v2 model files:

curl https://pjreddie.com/media/files/yolov2-tiny.weights -o yolov2-tiny.weights
curl https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov2-tiny.cfg -o yolov2-tiny.cfg
curl https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names -o labels.txt

convert weights to pb, and then tflite:

# convert to pb
flow --model yolov2-tiny.cfg --load yolov2-tiny.weights --savepb

# convert to tflite
tflite_convert \
  --graph_def_file=built_graph/yolov2-tiny.pb \
  --output_file=yolov2-tiny.tflite \
  --input_format=TENSORFLOW_GRAPHDEF \
  --output_format=TFLITE \
  --input_shape=1,416,416,3 \
  --input_array=input \
  --output_array=output \
  --inference_type=FLOAT \
  --input_data_type=FLOAT

the output will be a yolov2-tiny.tflite file under the current directory.

Test the converted model

Enter the directory which built_graph located when you generate the .pb file using darkflow, put any test images into sample_img directory, and run the following command:

flow --metaLoad built_graph/yolov2-tiny.meta --pbLoad built_graph/yolov2-tiny.pb

labled images should be generated in sample_img/out directory, so that you can preview the result of object detection.