This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TDA2: TI-DL: Instructions to import several tensorflow models

Part Number: TDA2

The instructions below show how to import two popular tensorflow networks (inception and mobilenet)
to TI-DL format and also how to import any custom network designed with Kera to TI-DL format.

Note that the steps for inception and mobilenet require tensorflow v1.1. The steps for Keras custom network require tensorflow 1.10.

Install tensorflow 1.1
----------------------------
o Download and install anaconda
o Open Anaconda Prompt
o type: conda create -n tf1.1_env python=3.5
o type: conda install –name tf1.1_env tensorflow=1.1.0 –channel conda-forge -y
o type: conda install keras
o In your System properties, add the environment variable TF_TOOLS_1.1="C:\Users\XXXX\AppData\Local\conda\conda\envs\tf1.1\Lib\site-packages\tensorflow\python\tools\optimize_for_inference.py"
o type: activate tf1.1_env

Install tensorflow 1.10
-------------------------------
Same instructions as above except replace 1.1 with 1.10

Download tensorflow model repo
----------------------------------------------
github.com/.../models
Create an environment variable TF_MODELS pointing to the installation directory, TF_MODELS="C:\..\tensorflow-models"

Environment variables settings
-------------------------------------------
Create system environment variables TI_DL and TI_DL_IMPORT . Both usually point to the same TI_DL installation directory {TIDL_install_path}\modules\ti_dl
There is one case when you need to have your TI_DL_IMPORT directory at a different location than TI_DL.
This is in case your TI_DL directory points to the original VISION_SDK location: C:\PROCESSOR_SDK_VISION_XX_XX_00_00\ti_components\algorithms\REL.TIDLSRC.01.01.01.00
This location results in a path that exceeds 260 characters, which is a windows limitation. This causes the import tools to fail by not being able to open
any input files specified in a configuration parameter file.
So the solution is to move the directories needed by the import tools: {TIDL_install_path}\modules\ti_dl\test and {TIDL_install_path}\modules\ti_dl\utils
to a different directory TI_DL_IMPORT that has a shorter path.
Linux users don't face this issue.

Inception v1
-----------------
•Download inception_v1.ckpt from :


•Reset Models repo to :5eab06018c51d0ec8bf85919bf50438e2c4056a3 - MobileNet V1 commit (#1551)
•Download attached script freeze_inception_net_v1.py and copy it to the directory pointed by the environment variable %TF_TOOLS_1.1%
•Open the script freeze_inception_net_v1.py and verify that the variable models_slim_dir is set to os.getenv('TF_MODELS')+"/slim" .
•Open an Anaconda Prompt
•Type: activate tf1.1_env
•Type: cd %TF_MODELS%\inception\inception
•Type: python "%TF_TOOLS_1.1%\freeze_inception_net_v1.py"
•Type: python "%TF_TOOLS_1.1%\optimize_for_inference.py" --input=inception_v1.pb --output=inceptionnet_v1.pb --input_names=input --output_name=softmax/Softmax
•Type: xcopy inceptionnet_v1.pb %TI_DL_IMPORT%\test\testvecs\config\tensorflow_models\inceptionnet_V1\inceptionnet_v1.pb
•Type: cd %TI_DL_IMPORT%\utils\tidlModelImport
•Type: tidl_model_import.out.exe ..\..\test\testvecs\config\import\tidl_import_inceptionNetv1.txt
•Verify the presence of files tidl_inception_v1_net.bin and tidl_inception_v1_param.bin and their timestamps in %TI_DL_IMPORT%\test\testvecs\config\tidl_models
•Copy these files into %TI_DL% directory: xcopy %TI_DL_IMPORT%\test\testvecs\config\tidl_models\tidl_inception_v1_* %TI_DL%\test\testvecs\config\tidl_models\
•Test the model by running the inference in host emulation, using the default configuration for inception net. For that edit the first 2 lines %TI_DL%\test\testvecs\config\config_list.txt:
1 ..\..\test\testvecs\config\infer\tidl_config_inceptionNetv1.txt
0
•Type: cd %TI_DL%\test\out
•Type: dsp_test_dl_algo.out.exe
Verify that the log finishes with: Layer 82 :-------Max Index 896 : 254 ------- TSC Cycles = 0.00

Mobilenet
-------------

•Download the attached script freeze_mobilenet.py and copy it to the directory pointed by the environment variable %TF_TOOLS_1.1%

•Reset your Models repo to the head.
•Open the script freeze_mobilenet.py and verify that the variable models_slim_dir is set to os.getenv('TF_MODELS')+"/research/slim" .
•Open an Anaconda Prompt
•Type: activate tf1.1_env
•Type: cd %TF_MODELS%\research\slim
•Type: python "%TF_TOOLS_1.1%\freeze_mobilenet.py"
•Type: cd MobileNet
•Type: python "%TF_TOOLS_1.1%\optimize_for_inference.py" --input=mobilenet_v1_1.0_224.pb --output=mobilenet_1_224.pb --input_names=input --output_names="softmax/Softmax"
•Type: xcopy mobilenet_1_224.pb %TI_DL_IMPORT%\test\testvecs\config\tensorflow_models\mobilenet\mobilenet_1_224.pb
•Type: cd %TI_DL_IMPORT%\utils\tidlModelImport
•Type: tidl_model_import.out.exe ..\..\test\testvecs\config\import\tidl_import_mobileNet1.txt
•Verify the presence of files tidl_net_mobilenet_1_224.bin and tidl_param_mobilenet_1_224.bin and their timestamps in %TI_DL_IMPORT%\test\testvecs\config\tidl_models
•Copy these files into %TI_DL% directory: xcopy %TI_DL_IMPORT%\test\testvecs\config\tidl_models\tidl_*_mobilenet_1_224.bin %TI_DL%\test\testvecs\config\tidl_models\
•Test the model by running the inference in host emulation, using the default configuration for mobile net. For that edit the first 2 lines %TI_DL%\test\testvecs\config\config_list.txt:
1 ..\..\test\testvecs\config\infer\tidl_config_mobileNet1.txt
0
•Type: cd %TI_DL%\test\out
•Type: dsp_test_dl_algo.out.exe
Verify that the log finishes with: Layer 30 :-------Max Index 717 : 255 ------- TSC Cycles = 0.00

Custom network designed using kera
--------------------------------------------------
•Download and unzip the example network package from e2e.ti.com/.../TensorflowExample.zip
Create a system environment variable %TF_KERAS_EXAMPLE% that points to this directory
•Type: copy %TF_KERAS_EXAMPLE%\tidl_import_tfCifar10.txt %TI_DL_IMPORT%\test\testvecs\config\import
•Type: activate tf1.10_env
•Type: cd %TF_KERAS_EXAMPLE%
•Type: python "cifar10_train.py"
•It might take a while to train the dataset so be patient.
•Type: cd tmp
•Type: python "%TF_TOOLS_1.10%\freeze_graph.py" --input_meta_graph="keras_cifar10_model.ckpt.meta" --input_checkpoint="keras_cifar10_model.ckpt" --output_graph=keras_frozen.pb --output_node_names="conv2d_5/BiasAdd" --input_binary=true
•Type: python "%TF_TOOLS_1.10%\optimize_for_inference.py" --input=keras_frozen.pb --output=keras_frozen_optimized.pb --input_names=conv2d_input --output_name=conv2d_5/BiasAdd
•Type: xcopy keras_frozen_optimized.pb %TI_DL_IMPORT%\test\testvecs\config\tensorflow_models\keras_frozen_optimized.pb
•Edit %TI_DL_IMPORT%\test\testvecs\config\import\tidl_import_tfCifar10.txt and correct the paths.
•Type: cd %TI_DL_IMPORT%\utils\tidlModelImport
•Type: tidl_model_import.out.exe ..\..\test\testvecs\config\import\tidl_import_tfCifar10.txt
•Verify the presence of files tidl_tfcifar10_net.bin and tidl_tfcifar10_param.bin and their timestamps in %TI_DL_IMPORT%\test\testvecs\config\tidl_models

python_freeze_graph.zip