Skip to content

Commit df1a4db

Browse files
Ilya-KrylovAlexanderDokuchaev
authored andcommitted
Image retrieval (openvinotoolkit#130)
* Add Textile Detection and Recognition Demo * Add copyright to files in tensorflow_toolkit/textile * Add augmentation_config.json to tensorflow_toolkit/textile * Change structure * pylint * Add export to IR * Return param * Fix one import * Some changes to run the tf model * minor * updated requirements.txt * removed extra script * requirenments updated * minor * loss * triplet and margin as param * opencv * git * minor * minor * minor * minor * preload and pretile images * minor * minor * minor * amsoftmax revisited * removed dead code * changed some defaulat values for some parameters * minor reformat * remove commented out code * proper name * minor * fixed duplicating images * minor * minor * removed textile detector * pylint commont * minor * renamed * removed * changed headers * prev am_softmax * pylint tools/train.py * minor * minor * tools; * unified * readme updated * am softmax revisited * am softmax revisited again * minor * cofnig * refactored * refactored dataset.py * update config * update export.py * extracted crop_resize * change reshape for MO * add global average top1 * update param images list * add cpu_extensions param * added params to MO * fixes * update readme.md * minor * data * reinitialize lr after loading pretrained weights * update README.md * test with ie * final
1 parent de3c079 commit df1a4db

File tree

63 files changed

+1936
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1936
-0
lines changed

tensorflow_toolkit/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ them for predictions.
4646
* [SSD Object Detection](ssd_detector)
4747
* [Text recognition](text_recognition)
4848
* [Vehicle Attributes](vehicle_attributes)
49+
* [Image Retrieval](image_retrieval)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
model
2+
models
3+
venv*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
[MASTER]
2+
3+
# Specify a configuration file.
4+
#rcfile=
5+
6+
# Python code to execute, usually for sys.path manipulation such as
7+
# pygtk.require().
8+
#init-hook=
9+
10+
11+
# Add <file or directory> to the black list. It should be a base name, not a
12+
# path. You may set this option multiple times.
13+
ignore=
14+
15+
# Pickle collected data for later comparisons.
16+
persistent=yes
17+
18+
# List of plugins (as comma separated values of python modules names) to load,
19+
# usually to register additional checkers.
20+
load-plugins=
21+
22+
23+
[MESSAGES CONTROL]
24+
25+
# Enable the message, report, category or checker with the given id(s). You can
26+
# either give multiple identifier separated by comma (,) or put this option
27+
# multiple time.
28+
#enable=
29+
30+
# Disable the message, report, category or checker with the given id(s). You
31+
# can either give multiple identifier separated by comma (,) or put this option
32+
# multiple time.
33+
disable=import-error,
34+
missing-docstring,
35+
invalid-name,
36+
too-few-public-methods,
37+
redefined-builtin,
38+
duplicate-code,
39+
no-name-in-module,
40+
no-member,
41+
unsubscriptable-object,
42+
logging-format-interpolation
43+
44+
[REPORTS]
45+
46+
# Set the output format. Available formats are text, parseable, colorized, msvs
47+
# (visual studio) and html
48+
output-format=colorized
49+
50+
51+
# Put messages in a separate file for each module / package specified on the
52+
# command line instead of printing them on stdout. Reports (if any) will be
53+
# written in a file name "pylint_global.[txt|html]".
54+
files-output=no
55+
56+
# Tells whether to display a full report or only the messages
57+
reports=yes
58+
59+
# Python expression which should return a note less than 10 (10 is the highest
60+
# note). You have access to the variables errors warning, statement which
61+
# respectively contain the number of errors / warnings messages and the total
62+
# number of statements analyzed. This is used by the global evaluation report
63+
# (R0004).
64+
evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)
65+
66+
67+
[VARIABLES]
68+
69+
# Tells whether we should check for unused import in __init__ files.
70+
init-import=no
71+
72+
# A regular expression matching names used for dummy variables (i.e. not used).
73+
dummy-variables-rgx=_|dummy
74+
75+
# List of additional names supposed to be defined in builtins. Remember that
76+
# you should avoid to define new builtins when possible.
77+
additional-builtins=
78+
79+
80+
[BASIC]
81+
82+
# List of builtins function names that should not be used, separated by a comma
83+
bad-functions=map,filter,apply,input
84+
85+
# Regular expression which should only match correct module names
86+
module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
87+
88+
# Regular expression which should only match correct module level names
89+
const-rgx=(([a-zA-Z_][a-zA-Z0-9_]*)|(__.*__))$
90+
91+
# Regular expression which should only match correct class names
92+
class-rgx=[a-zA-Z_][a-zA-Z0-9_]+$
93+
94+
# Regular expression which should only match correct function names
95+
function-rgx=[a-z_][a-z0-9_]{2,30}$
96+
97+
# Regular expression which should only match correct method names
98+
method-rgx=[a-z_][a-z0-9_]{2,30}$
99+
100+
# Regular expression which should only match correct instance attribute names
101+
attr-rgx=[a-z_][a-z0-9_]{2,30}$
102+
103+
# Regular expression which should only match correct argument names
104+
argument-rgx=[a-z_][a-z0-9_]{2,30}$
105+
106+
# Regular expression which should only match correct variable names
107+
variable-rgx=[a-z_][a-z0-9_]{1,30}$
108+
109+
# Regular expression which should only match correct list comprehension /
110+
# generator expression variable names
111+
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$
112+
113+
# Good variable names which should always be accepted, separated by a comma
114+
good-names=i,j,k,ex,Run,_
115+
116+
# Bad variable names which should always be refused, separated by a comma
117+
bad-names=foo,bar,baz,toto,tutu,tata
118+
119+
# Regular expression which should only match functions or classes name which do
120+
# not require a docstring
121+
no-docstring-rgx=__.*__
122+
123+
124+
[MISCELLANEOUS]
125+
126+
# List of note tags to take in consideration, separated by a comma.
127+
notes=FIXME,XXX,TODO
128+
129+
130+
[FORMAT]
131+
132+
# Maximum number of characters on a single line.
133+
max-line-length=120
134+
135+
# Maximum number of lines in a module
136+
max-module-lines=1100
137+
138+
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
139+
# tab).
140+
indent-string=' '
141+
indent-after-paren=4
142+
143+
[SIMILARITIES]
144+
145+
# Minimum lines number of a similarity.
146+
min-similarity-lines=4
147+
148+
# Ignore comments when computing similarities.
149+
ignore-comments=yes
150+
151+
# Ignore docstrings when computing similarities.
152+
ignore-docstrings=yes
153+
154+
155+
[TYPECHECK]
156+
157+
# Tells whether missing members accessed in mixin class should be ignored. A
158+
# mixin class is detected if its name ends with "mixin" (case insensitive).
159+
ignore-mixin-members=yes
160+
161+
# List of classes names for which member attributes should not be checked
162+
# (useful for classes with attributes dynamically set).
163+
ignored-classes=SQLObject
164+
165+
# List of members which are set dynamically and missed by pylint inference
166+
# system, and so shouldn't trigger E0201 when accessed.
167+
generated-members=REQUEST,acl_users,aq_parent
168+
169+
[DESIGN]
170+
171+
# Maximum number of arguments for function / method
172+
max-args=8
173+
174+
# Argument names that match this expression will be ignored. Default to name
175+
# with leading underscore
176+
ignored-argument-names=_.*
177+
178+
# Maximum number of locals for function / method body
179+
max-locals=28
180+
181+
# Maximum number of return / yield for function / method body
182+
max-returns=6
183+
184+
# Maximum number of branch for function / method body
185+
max-branchs=12
186+
187+
# Maximum number of statements in function / method body
188+
max-statements=50
189+
190+
# Maximum number of parents for a class (see R0901).
191+
max-parents=7
192+
193+
# Maximum number of attributes for a class (see R0902).
194+
max-attributes=12
195+
196+
# Minimum number of public methods for a class (see R0903).
197+
min-public-methods=2
198+
199+
# Maximum number of public methods for a class (see R0904).
200+
max-public-methods=20
201+
202+
203+
[IMPORTS]
204+
205+
# Deprecated modules which should not be used, separated by a comma
206+
deprecated-modules=regsub,string,TERMIOS,Bastion,rexec
207+
208+
# Create a graph of every (i.e. internal and external) dependencies in the
209+
# given file (report RP0402 must not be disabled)
210+
import-graph=
211+
212+
# Create a graph of external dependencies in the given file (report RP0402 must
213+
# not be disabled)
214+
ext-import-graph=
215+
216+
# Create a graph of internal dependencies in the given file (report RP0402 must
217+
# not be disabled)
218+
int-import-graph=
219+
220+
# A comma-separated list of package or module names from where C extensions may
221+
# be loaded. Extensions are loading into the active Python interpreter and may
222+
# run arbitrary code.
223+
extension-pkg-whitelist=numpy,cv2,openvino,torch,tensorflow
224+
225+
226+
[CLASSES]
227+
228+
# List of method names used to declare (i.e. assign) instance attributes.
229+
defining-attr-methods=__init__,__new__,setUp
230+
231+
contextmanager-decorators=contextlib.contextmanager,tensorflow.python.util.tf_contextlib.contextmanager
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Image Retrieval
2+
3+
## Description
4+
5+
This code is intended to be used for image retrieval algorithm training when probe image is transformed by following transformation:
6+
* Random cropping
7+
* Rotation
8+
* Repetition
9+
* Color distortion
10+
11+
Trained model: [link](https://download.01.org/opencv/openvino_training_extensions/models/image_retrieval/image-retrieval-0001.tar.gz)
12+
13+
## Setup
14+
15+
### Prerequisites
16+
17+
* Ubuntu 16.04
18+
* Python 3.5 or 3.6
19+
* TensorFlow 2.0.0a0 (for training only)
20+
* OpenVINO 2019 R1 with Python API (to infer pre-trained model only)
21+
22+
### Installation
23+
24+
1. Create virtual environment
25+
```bash
26+
virtualenv venv -p python3 --prompt="(image_retrieval)"
27+
```
28+
29+
2. Activate virtual environment and setup OpenVINO variables
30+
```bash
31+
. venv/bin/activate
32+
```
33+
34+
3. Install the modules
35+
36+
```
37+
pip install -e .
38+
```
39+
40+
## Training
41+
42+
* To train the image retrieval model run the script `train.py` as follows:
43+
```
44+
python tools/train.py \
45+
--gallery data/gallery/gallery.txt \
46+
--test_images data/queries/quieries.txt \
47+
--test_gallery data/gallery/gallery.txt \
48+
--train_dir model \
49+
--model mobilenet_v2 \
50+
--augmentation_config configs/augmentation_config.json \
51+
--loss triplet_1.0 \
52+
--steps_per_epoch 500 \
53+
--batch_size 32 \
54+
--input_size 224 \
55+
--dump_hard_examples \
56+
--lr_drop_step 500
57+
```
58+
59+
* To fine-tune the image retrieval model run the script `train.py` as follows:
60+
```
61+
python tools/train.py \
62+
--gallery data/gallery/gallery.txt \
63+
--test_images data/queries/quieries.txt \
64+
--test_gallery data/gallery/gallery.txt \
65+
--train_dir model \
66+
--model mobilenet_v2 \
67+
--augmentation_config configs/augmentation_config.json \
68+
--loss triplet_1.0 \
69+
--steps_per_epoch 500 \
70+
--batch_size 32 \
71+
--input_size 224 \
72+
--dump_hard_examples \
73+
--lr_drop_step 500 \
74+
--model_weights pretrained_model/weights-251920
75+
```
76+
77+
The file `augmentation_config.json` contains the following parameters on gallery images augmentation.
78+
79+
Each line in the file with list of gallery images should have format
80+
```
81+
<path_to_image_folder>/<image_file_name> <id_of_gallery_group>
82+
```
83+
where `<id_of_gallery_group>` should be a number identifier to join similar (almost identical) gallery images
84+
into groups (but in the simplest case it may be different for each line).
85+
86+
## Evaluation
87+
88+
To test the image retrieval model using TensorFlow run the script `test.py` as follows:
89+
```
90+
python tools/test.py \
91+
--model_weights pretrained_model/weights-251920 \
92+
--gallery data/gallery/gallery.txt \
93+
--test_images data/queries/quieries.txt \
94+
--ie tf
95+
```
96+
97+
or using OpenVINO:
98+
99+
```
100+
python tools/test.py \
101+
--model_weights image-retrieval-0001.xml \
102+
--gallery data/gallery/gallery.txt \
103+
--test_images data/queries/quieries.txt \
104+
--ie ie \
105+
--cpu_extension /opt/intel/openvino/inference_engine/lib/intel64/libcpu_extension_avx512.so
106+
```
107+
108+
As result you should get:
109+
```
110+
9 1.00 1.00 1.00 0.00
111+
13 1.00 1.00 1.00 0.00
112+
16 1.00 1.00 1.00 0.00
113+
21 1.00 1.00 1.00 0.00
114+
AVERAGE: top1: 1.000 top5: 1.000 top10: 1.000 mean_index: 0.000
115+
AVERAGE top1 over all queries: 1.000
116+
```
117+
118+
## Export
119+
120+
* First step is to freeze your model:
121+
122+
```
123+
python tools/export.py \
124+
--model mobilenet_v2 \
125+
--model_weights pretrained_model/weights-251920
126+
```
127+
128+
* Second step is to run `mo.py`
129+
130+
**NOTE** You need to install TF1.12 to use model optimizer.
131+
132+
1. Create and activate new virtual environment
133+
```bash
134+
virtualenv venv_mo -p python3 --prompt="(ir-mo)"
135+
. venv_mo/bin/activate
136+
```
137+
138+
2. Install modules and activate environment for OpenVINO
139+
```bash
140+
pip3 install -r requirements-mo.txt
141+
source /opt/intel/openvino/bin/setupvars.sh
142+
```
143+
144+
3. Run model optimizer tool to export frozen graph to IR
145+
```bash
146+
mo.py --model_name image-retrieval \
147+
--input_model model/export/frozen_graph.pb \
148+
--mean_values [127.5,127.5,127.5] \
149+
--scale 127.5 \
150+
--data_type FP32 \
151+
--output_dir model/export/IR
152+
```

0 commit comments

Comments
 (0)