blob: 2fd39079781f7a6e4221c9ab4b2f7e5552067a1c [file] [log] [blame]
steniu01bee466b2017-06-21 16:45:41 +01001/**
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
8One can find caffe <a href="https://github.com/BVLC/caffe/wiki/Model-Zoo">pre-trained models</a> on
9caffe's official github repository.
10
11The caffe_data_extractor.py provided in the @ref scripts folder is an example script that shows how to
SiCong Li86b53332017-08-23 11:02:43 +010012extract the parameter values from a trained model.
steniu01bee466b2017-06-21 16:45:41 +010013
SiCong Li86b53332017-08-23 11:02:43 +010014@note complex networks might require altering the script to properly work.
steniu01bee466b2017-06-21 16:45:41 +010015
16@subsection how_to How to use the script
17
18Install caffe following <a href="http://caffe.berkeleyvision.org/installation.html">caffe's document</a>.
19Make sure the pycaffe has been added into the PYTHONPATH.
20
21Download the pre-trained caffe model.
22
23Run the caffe_data_extractor.py script by
24
SiCong Li86b53332017-08-23 11:02:43 +010025 python caffe_data_extractor.py -m <caffe model> -n <caffe netlist>
steniu01bee466b2017-06-21 16:45:41 +010026
27For example, to extract the data from pre-trained caffe Alex model to binary file:
28
SiCong Li86b53332017-08-23 11:02:43 +010029 python caffe_data_extractor.py -m /path/to/bvlc_alexnet.caffemodel -n /path/to/caffe/models/bvlc_alexnet/deploy.prototxt
steniu01bee466b2017-06-21 16:45:41 +010030
31The script has been tested under Python2.7.
32
SiCong Li86b53332017-08-23 11:02:43 +010033@subsection result What is the expected output from the script
steniu01bee466b2017-06-21 16:45:41 +010034
SiCong Li86b53332017-08-23 11:02:43 +010035If the script runs successfully, it prints the names and shapes of each layer onto the standard
36output and generates *.npy files containing the weights and biases of each layer.
steniu01bee466b2017-06-21 16:45:41 +010037
38The @ref arm_compute::utils::load_trained_data shows how one could load
SiCong Li86b53332017-08-23 11:02:43 +010039the 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
43The script tensorflow_data_extractor.py extracts trainable parameters (e.g. values of weights and biases) from a
44trained 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
49network.
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}
53instead of:
54 {model_name}.ckpt
55When dealing with binary files with version >= 0.11, only pass {model_name} to -m option;
56when 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
60specified otherwise by the user. Thus should a user alter this default behavior and/or want to extract parameters from other
61collections, tf.GraphKeys.TRAINABLE_VARIABLES should be replaced accordingly.
62
63@subsection how_to How to use the script
64
65Install tensorflow and numpy.
66
67Download the pre-trained tensorflow model.
68
69Run tensorflow_data_extractor.py with
70
71 python tensorflow_data_extractor -m <path_to_binary_checkpoint_file> -n <path_to_metagraph_file>
72
73For 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
77Or 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
83The script has been tested with Tensorflow 1.2, 1.3 on Python 2.7.6 and Python 3.4.3.
84
85@subsection result What is the expected output from the script
86
87If 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
90The @ref arm_compute::utils::load_trained_data shows how one could load
91the weights and biases into tensor from the .npy file by the help of Accessor.
steniu01bee466b2017-06-21 16:45:41 +010092*/