steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 1 | /** |
| 2 | @page data_import Importing data from existing models |
| 3 | |
| 4 | @tableofcontents |
| 5 | |
| 6 | @section caffe_data_extractor Extract data from pre-trained caffe model |
| 7 | |
| 8 | One can find caffe <a href="https://github.com/BVLC/caffe/wiki/Model-Zoo">pre-trained models</a> on |
| 9 | caffe's official github repository. |
| 10 | |
| 11 | The caffe_data_extractor.py provided in the @ref scripts folder is an example script that shows how to |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 12 | extract the parameter values from a trained model. |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 13 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 14 | @note complex networks might require altering the script to properly work. |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 15 | |
Anthony Barbier | 6a5627a | 2017-09-26 14:42:02 +0100 | [diff] [blame] | 16 | @subsection caffe_how_to How to use the script |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 17 | |
| 18 | Install caffe following <a href="http://caffe.berkeleyvision.org/installation.html">caffe's document</a>. |
| 19 | Make sure the pycaffe has been added into the PYTHONPATH. |
| 20 | |
| 21 | Download the pre-trained caffe model. |
| 22 | |
| 23 | Run the caffe_data_extractor.py script by |
| 24 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 25 | python caffe_data_extractor.py -m <caffe model> -n <caffe netlist> |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 26 | |
| 27 | For example, to extract the data from pre-trained caffe Alex model to binary file: |
| 28 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 29 | python caffe_data_extractor.py -m /path/to/bvlc_alexnet.caffemodel -n /path/to/caffe/models/bvlc_alexnet/deploy.prototxt |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 30 | |
| 31 | The script has been tested under Python2.7. |
| 32 | |
Anthony Barbier | 6a5627a | 2017-09-26 14:42:02 +0100 | [diff] [blame] | 33 | @subsection caffe_result What is the expected output from the script |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 34 | |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 35 | If the script runs successfully, it prints the names and shapes of each layer onto the standard |
| 36 | output and generates *.npy files containing the weights and biases of each layer. |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 37 | |
| 38 | The @ref arm_compute::utils::load_trained_data shows how one could load |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 39 | the weights and biases into tensor from the .npy file by the help of Accessor. |
| 40 | |
| 41 | @section tensorflow_data_extractor Extract data from pre-trained tensorflow model |
| 42 | |
| 43 | The script tensorflow_data_extractor.py extracts trainable parameters (e.g. values of weights and biases) from a |
| 44 | trained tensorflow model. A tensorflow model consists of the following two files: |
| 45 | |
| 46 | {model_name}.data-{step}-{global_step}: A binary file containing values of each variable. |
| 47 | |
| 48 | {model_name}.meta: A binary file containing a MetaGraph struct which defines the graph structure of the neural |
| 49 | network. |
| 50 | |
| 51 | @note Since Tensorflow version 0.11 the binary checkpoint file which contains the values for each parameter has the format of: |
| 52 | {model_name}.data-{step}-of-{max_step} |
| 53 | instead of: |
| 54 | {model_name}.ckpt |
| 55 | When dealing with binary files with version >= 0.11, only pass {model_name} to -m option; |
| 56 | when dealing with binary files with version < 0.11, pass the whole file name {model_name}.ckpt to -m option. |
| 57 | |
| 58 | @note This script relies on the parameters to be extracted being in the |
| 59 | 'trainable_variables' tensor collection. By default all variables are automatically added to this collection unless |
| 60 | specified otherwise by the user. Thus should a user alter this default behavior and/or want to extract parameters from other |
| 61 | collections, tf.GraphKeys.TRAINABLE_VARIABLES should be replaced accordingly. |
| 62 | |
Anthony Barbier | 6a5627a | 2017-09-26 14:42:02 +0100 | [diff] [blame] | 63 | @subsection tensorflow_how_to How to use the script |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 64 | |
| 65 | Install tensorflow and numpy. |
| 66 | |
| 67 | Download the pre-trained tensorflow model. |
| 68 | |
| 69 | Run tensorflow_data_extractor.py with |
| 70 | |
| 71 | python tensorflow_data_extractor -m <path_to_binary_checkpoint_file> -n <path_to_metagraph_file> |
| 72 | |
| 73 | For example, to extract the data from pre-trained tensorflow Alex model to binary files: |
| 74 | |
| 75 | python tensorflow_data_extractor -m /path/to/bvlc_alexnet -n /path/to/bvlc_alexnet.meta |
| 76 | |
| 77 | Or for binary checkpoint files before Tensorflow 0.11: |
| 78 | |
| 79 | python tensorflow_data_extractor -m /path/to/bvlc_alexnet.ckpt -n /path/to/bvlc_alexnet.meta |
| 80 | |
| 81 | @note with versions >= Tensorflow 0.11 only model name is passed to the -m option |
| 82 | |
| 83 | The script has been tested with Tensorflow 1.2, 1.3 on Python 2.7.6 and Python 3.4.3. |
| 84 | |
Anthony Barbier | 6a5627a | 2017-09-26 14:42:02 +0100 | [diff] [blame] | 85 | @subsection tensorflow_result What is the expected output from the script |
SiCong Li | 86b5333 | 2017-08-23 11:02:43 +0100 | [diff] [blame] | 86 | |
| 87 | If the script runs successfully, it prints the names and shapes of each parameter onto the standard output and generates |
| 88 | *.npy files containing the weights and biases of each layer. |
| 89 | |
| 90 | The @ref arm_compute::utils::load_trained_data shows how one could load |
| 91 | the weights and biases into tensor from the .npy file by the help of Accessor. |
steniu01 | bee466b | 2017-06-21 16:45:41 +0100 | [diff] [blame] | 92 | */ |