Home | History | Annotate | only in /external/tensorflow/tensorflow/contrib/receptive_field
Up to higher level directory
NameDateSize
BUILD21-Aug-20182.9K
python/21-Aug-2018
README.md21-Aug-20185.6K
receptive_field_api.py21-Aug-20181.2K

README.md

      1 # Receptive field computation for convnets
      2 
      3 This library enables you to easily compute the receptive field parameters of
      4 your favorite convnet. You can use it to understand how big of an input image
      5 region your output features depend on. Better yet, using the parameters computed
      6 by the library, you can easily find the exact image region which is used to
      7 compute each convnet feature.
      8 
      9 ## Basic usage
     10 
     11 The main function to be called is `compute_receptive_field_from_graph_def`,
     12 which will return the receptive field, effective stride and effective padding
     13 for both horizontal and vertical directions.
     14 
     15 For example, if your model is constructed using the function
     16 `my_model_construction()`, you can use the library as follows:
     17 
     18 ```python
     19 import tensorflow as tf
     20 
     21 # Construct graph.
     22 g = tf.Graph()
     23 with g.as_default():
     24   images = tf.placeholder(tf.float32, shape=(1, None, None, 3), name='input_image')
     25   my_model_construction(images)
     26 
     27 # Compute receptive field parameters.
     28 rf_x, rf_y, eff_stride_x, eff_stride_y, eff_pad_x, eff_pad_y = \
     29   tf.contrib.receptive_field.compute_receptive_field_from_graph_def( \
     30     g.as_graph_def(), 'input_image', 'my_output_endpoint')
     31 ```
     32 
     33 Here's a simple example of computing the receptive field parameters for
     34 Inception-Resnet-v2. To get this to work, be sure to checkout
     35 [tensorflow/models](https://github.com/tensorflow/models), so that the Inception
     36 models are available to you. This can be done in three simple commands:
     37 
     38 ```sh
     39 git clone https://github.com/tensorflow/models
     40 cd models/research/slim
     41 sudo python setup.py install_lib
     42 ```
     43 
     44 You can then compute the receptive field parameters for Inception-Resnet-v2 as:
     45 
     46 ```python
     47 from nets import inception
     48 import tensorflow as tf
     49 
     50 # Construct graph.
     51 g = tf.Graph()
     52 with g.as_default():
     53   images = tf.placeholder(tf.float32, shape=(1, None, None, 3), name='input_image')
     54   inception.inception_resnet_v2_base(images)
     55 
     56 # Compute receptive field parameters.
     57 rf_x, rf_y, eff_stride_x, eff_stride_y, eff_pad_x, eff_pad_y = \
     58   tf.contrib.receptive_field.compute_receptive_field_from_graph_def( \
     59     g.as_graph_def(), 'input_image', 'InceptionResnetV2/Conv2d_7b_1x1/Relu')
     60 ```
     61 
     62 This will give you `rf_x = rf_y = 3039`, `eff_stride_x = eff_stride_y = 32`, and
     63 `eff_pad_x = eff_pad_y = 1482`. This means that each feature that is output at
     64 the node `'InceptionResnetV2/Conv2d_7b_1x1/Relu'` is computed from a region
     65 which is of size `3039x3039`. Further, by using the expressions
     66 
     67 ```python
     68 center_x = -eff_pad_x + feature_x*eff_stride_x + (rf_x - 1)/2
     69 center_y = -eff_pad_y + feature_y*eff_stride_y + (rf_y - 1)/2
     70 ```
     71 
     72 one can compute the center of the region in the input image that is used to
     73 compute the output feature at position `[feature_x, feature_y]`. For example,
     74 the feature at position `[0, 2]` at the output of the layer
     75 `'InceptionResnetV2/Conv2d_7b_1x1/Relu'` is centered in the original image in
     76 the position `[37, 101]`.
     77 
     78 TODO: include link to derivations and definitions of different parameters.
     79 
     80 ## Receptive field benchmark
     81 
     82 As you might expect, it is straightforward to run this library on the popular
     83 convnets, and gather their receptive fields. We provide a python script which
     84 does exactly that, available under `python/util/examples/rf_benchmark.py`.
     85 
     86 To get this to work, be sure to checkout
     87 [tensorflow/models](https://github.com/tensorflow/models) (see the 3-command
     88 instructions for this above). Then, simply:
     89 
     90 ```sh
     91 cd python/util/examples
     92 python rf_benchmark.py --csv_path /tmp/rf_benchmark_results.csv
     93 ```
     94 
     95 The script will write to stdout the receptive field parameters for many variants
     96 of several popular convnets: AlexNet, VGG, ResNet, Inception, Mobilenet. They
     97 are also written to the file `/tmp/rf_benchmark_results.csv`.
     98 
     99 TODO: include here a plot for receptive field sizes of different convnets.
    100 
    101 TODO: include table/link to pre-computed RF parameters.
    102 
    103 ## Compute RF parameters from a graph pbtxt
    104 
    105 We also provide a utility to compute the receptive field parameters directly
    106 from a graph protobuf file.
    107 
    108 Have a `graph.pbtxt` file and want to compute its receptive field parameters? We
    109 got you covered. The only prerequisite is to install
    110 [google/protobuf](https://github.com/google/protobuf), which you probably
    111 already have if you're using tensorflow (otherwise, follow installation
    112 instructions [here](https://github.com/google/protobuf/tree/master/python)).
    113 
    114 This should work:
    115 
    116 ```sh
    117 cd python/util/examples
    118 python compute_rf.py \
    119   --graph_path /path/to/graph.pbtxt \
    120   --output_path /path/to/output/rf_info.txt \
    121   --input_node my_input_node \
    122   --output_node my_output_node
    123 ```
    124 
    125 Don't know how to generate a graph protobuf file? Take a look at the
    126 `write_inception_resnet_v2_graph.py` script, which shows how to save it for the
    127 Inception-Resnet-v2 model:
    128 
    129 ```sh
    130 cd python/util/examples
    131 python write_inception_resnet_v2_graph.py --graph_dir /tmp --graph_filename graph.pbtxt
    132 ```
    133 
    134 This will write the Inception-Resnet-v2 graph protobuf to `/tmp/graph.pbtxt`.
    135 
    136 For completeness, here's how you would use this file to get the receptive field
    137 parameters of the Inception-Resnet-v2 model:
    138 
    139 ```sh
    140 cd python/util/examples
    141 python compute_rf.py \
    142   --graph_path /tmp/graph.pbtxt \
    143   --output_path /tmp/rf_info.txt \
    144   --input_node input_image \
    145   --output_node InceptionResnetV2/Conv2d_7b_1x1/Relu
    146 ```
    147 
    148 This will write the receptive field parameters of the model to
    149 `/tmp/rf_info.txt`, which will look like:
    150 
    151 ```sh
    152 Receptive field size (horizontal) = 3039
    153 Receptive field size (vertical) = 3039
    154 Effective stride (horizontal) = 32
    155 Effective stride (vertical) = 32
    156 Effective padding (horizontal) = 1482
    157 Effective padding (vertical) = 1482
    158 ```
    159 
    160 ## Authors
    161 
    162 André Araujo (github id: andrefaraujo) and Mark Sandler (github id:
    163 marksandler)
    164