Skip to content

Commit

Permalink
googlenet-v1-tf: convert from original files by using a pre-convert s…
Browse files Browse the repository at this point in the history
…cript

Pretty much the same thing as ba2ddb1, but for googlenet-v1-tf.

This doesn't change the XML file in the resulting IR (apart from layer names),
but the BIN file does change, and I don't know why. However, I retested
the accuracy of the IR (on CPU with FP32 precision), and it exactly matches
what's listed in the documentation, so I assume the differences aren't
significant.
  • Loading branch information
Roman Donchenko committed Nov 16, 2020
1 parent 0093f21 commit 6f24c05
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 87 deletions.
56 changes: 0 additions & 56 deletions models/public/googlenet-v1-tf/freeze.py.patch

This file was deleted.

30 changes: 4 additions & 26 deletions models/public/googlenet-v1-tf/googlenet-v1-tf.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,8 @@

The `googlenet-v1-tf` model is one of the Inception family, designed to perform image classification.
Like the other Inception models, the `googlenet-v1-tf` model has been pretrained on the ImageNet image database.
Originally redistributed as a checkpoint file, was converted to frozen graph.
For details about this family of models, check out the [paper](https://arxiv.org/abs/1602.07261), [repository](https://github.com/tensorflow/models/tree/master/research/slim).

### Steps to Reproduce Conversion to Frozen Graph

1. Clone the original repository
```sh
git clone https://github.com/tensorflow/models.git
cd models/research/slim
```
2. Checkout the commit that the conversion was tested on:
```sh
git checkout 5d36f19
```
3. Apply `freeze.py.patch` patch
```sh
git apply path/to/freeze.py.patch
```
4. Download the [pretrained weights](http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz)
5. Install the dependencies:
```sh
pip install tensorflow==1.14.0
```
6. Run
```sh
python3 freeze.py --ckpt path/to/inception_v1.ckpt --name inception_v1 --num_classes 1001 --output InceptionV1/Logits/Predictions/Softmax
```

## Specification

| Metric | Value |
Expand Down Expand Up @@ -94,3 +68,7 @@ Object classifier according to ImageNet classes, name - `InceptionV1/Logits/Pred
The original model is distributed under the
[Apache License, Version 2.0](https://github.com/tensorflow/models/blob/master/LICENSE).
A copy of the license is provided in [APACHE-2.0-TensorFlow.txt](../licenses/APACHE-2.0-TensorFlow.txt).

The original model uses the TF-Slim library, which is distributed under the
[Apache License, Version 2.0](https://github.com/google-research/tf-slim/blob/master/LICENSE).
A copy of the license is provided in [APACHE-2.0-TFSlim.txt](../licenses/APACHE-2.0-TFSlim.txt).
58 changes: 53 additions & 5 deletions models/public/googlenet-v1-tf/model.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,65 @@ description: >-
For details about this family of models, check out the paper.
task_type: classification
files:
- name: inception_v1.frozen.pb
size: 26735789
sha256: 5098978b1c82717bb4d1702b200d715f6e1e37121fdf51795ff5d8a228a26485
source: https://download.01.org/opencv/public_models/012020/googlenet-v1-tf/inception_v1.frozen.pb
- name: inception_v1_2016_08_28.tar.gz
size: 24642554
sha256: 7a620c430fcaba8f8f716241f5148c4c47c035cce4e49ef02cfbe6cd1adf96a6
source: http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz
- name: models/research/slim/nets/inception.py
size: 1676
sha256: 626dc8ed0bd747d9f523062bdb0301b9740a4a31363c1601c9157834ac4ea3e9
source: https://github.com/tensorflow/models/raw/d4a6670ade6ebd007b7974f85d70c4c879ba99d6/research/slim/nets/inception.py
- name: models/research/slim/nets/inception_utils.py
size: 3079
sha256: 746d308f90dd3feae2b46298bcc64a836a999c617e10523310416ce6ec275659
source: https://github.com/tensorflow/models/raw/d4a6670ade6ebd007b7974f85d70c4c879ba99d6/research/slim/nets/inception_utils.py
- name: models/research/slim/nets/inception_v1.py
size: 16875
sha256: af1613056b50df10da13be66a859c35d72886facd07d175be47ccd5c9380a8ce
source: https://github.com/tensorflow/models/raw/d4a6670ade6ebd007b7974f85d70c4c879ba99d6/research/slim/nets/inception_v1.py
- name: models/research/slim/nets/nets_factory.py
size: 7253
sha256: ec4ea091ece9ad0f89fdc1da05397ae0a0be7c46ebefa02834d59a5c908d426a
source: https://github.com/tensorflow/models/raw/d4a6670ade6ebd007b7974f85d70c4c879ba99d6/research/slim/nets/nets_factory.py
- name: models/research/slim/tf_slim-1.1.0-py2.py3-none-any.whl
size: 352133
sha256: fa2bab63b3925bd42601102e7f178dce997f525742596bf404fa8a6918e146ff
source: https://files.pythonhosted.org/packages/02/97/b0f4a64df018ca018cc035d44f2ef08f91e2e8aa67271f6f19633a015ff7/tf_slim-1.1.0-py2.py3-none-any.whl
postprocessing:
- $type: unpack_archive
format: gztar
file: inception_v1_2016_08_28.tar.gz
- $type: unpack_archive
format: zip
file: models/research/slim/tf_slim-1.1.0-py2.py3-none-any.whl

# disable imports for models we don't need
- $type: regex_replace
pattern: 'from nets(\.[\w.]+)? import (?!inception)'
replacement: '# \g<0>'
file: models/research/slim/nets/nets_factory.py
- $type: regex_replace
pattern: '''(?!inception_v1)\w+'':'
replacement: '# \g<0>'
file: models/research/slim/nets/nets_factory.py
- $type: regex_replace
pattern: 'from nets\.inception_(?!v1)'
replacement: '# \g<0>'
file: models/research/slim/nets/inception.py

# inception_v3_arg_scope and inception_v1_arg_scope are the same object,
# but v3 would require us to download another file
- $type: regex_replace
pattern: '(?<=''inception_v1'': inception\.)inception_v3_arg_scope'
replacement: 'inception_v1_arg_scope'
file: models/research/slim/nets/nets_factory.py
model_optimizer_args:
- --input_shape=[1,224,224,3]
- --input=input
- --mean_values=input[127.5,127.5,127.5]
- --scale_values=input[127.5]
- --output=InceptionV1/Logits/Predictions/Softmax
- --input_model=$dl_dir/inception_v1.frozen.pb
- --input_model=$conv_dir/inception_v1.frozen.pb
- --reverse_input_channels
framework: tf
quantizable: yes
Expand Down
51 changes: 51 additions & 0 deletions models/public/googlenet-v1-tf/pre-convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3

# Copyright (c) 2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import importlib
import sys

from pathlib import Path

import tensorflow.compat.v1 as tf

NETWORK_NAME = 'inception_v1'

def main():
parser = argparse.ArgumentParser()
parser.add_argument('input_dir', type=Path)
parser.add_argument('output_dir', type=Path)
args = parser.parse_args()

sys.path.append(str(args.input_dir / 'models/research/slim'))
nets_factory = importlib.import_module('nets.nets_factory')

with tf.Session() as sess:
network_fn = nets_factory.get_network_fn(NETWORK_NAME, num_classes=1001)
size = network_fn.default_image_size

_, end_points = network_fn(tf.placeholder(
name='input', dtype=tf.float32, shape=(1, size, size, 3)))

tf.train.Saver().restore(sess, str(args.input_dir / (NETWORK_NAME + '.ckpt')))
frozen_graph_def = tf.graph_util.convert_variables_to_constants(
sess, tf.get_default_graph().as_graph_def(), [end_points['Predictions'].op.name])

tf.io.write_graph(frozen_graph_def, str(args.output_dir),
NETWORK_NAME + '.frozen.pb', as_text=False)

if __name__ == '__main__':
main()

0 comments on commit 6f24c05

Please sign in to comment.