Skip to content

Commit caf04f5

Browse files
committed
Updates to Training section
1 parent fee5f84 commit caf04f5

File tree

1 file changed

+78
-47
lines changed

1 file changed

+78
-47
lines changed

docs/source/training.rst

+78-47
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ Preparing the Workspace
4848
└─ workspace/
4949
└─ training_demo/
5050
51-
3. The ``training_demo`` folder shall be our `training folder`, which will contain all files related to our model training. It is advisable to create a separate training folder each time we wish to train a different model. The typical structure for training folders is shown below.
51+
3. The ``training_demo`` folder shall be our `training folder`, which will contain all files related to our model training. It is advisable to create a separate training folder each time we wish to train on a different dataset. The typical structure for training folders is shown below.
5252

5353
.. code-block:: default
5454
5555
training_demo/
5656
├─ annotations/
57+
├─ exported-models/
5758
├─ images/
5859
│ ├─ test/
5960
│ └─ train/
@@ -64,6 +65,7 @@ Preparing the Workspace
6465
Here's an explanation for each of the folders/filer shown in the above tree:
6566

6667
- ``annotations``: This folder will be used to store all ``*.csv`` files and the respective TensorFlow ``*.record`` files, which contain the list of annotations for our dataset images.
68+
- ``exported-models``: This folder will be used to store exported versions of our trained model(s).
6769
- ``images``: This folder contains a copy of all the images in our dataset, as well as the respective ``*.xml`` files produced for each one, once ``labelImg`` is used to annotate objects.
6870

6971
* ``images/train``: This folder contains a copy of all images, and the respective ``*.xml`` files, which will be used to train our model.
@@ -79,15 +81,20 @@ If you do not understand most of the things mentioned above, no need to worry, a
7981
Preparing the Dataset
8082
---------------------
8183

84+
Annotate the Dataset
85+
********************
86+
8287
.. _labelImg_install:
8388

8489
Install LabelImg
85-
****************
90+
~~~~~~~~~~~~~~~~
8691

8792
There exist several ways to install ``labelImg``. Below are 3 of the most common.
8893

89-
Get from PyPI (Recommended)
90-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
.. _labelImg_install_pip:
95+
96+
Using PIP (Recommended)
97+
#######################
9198
1. Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
9299
2. Run the following command to install ``labelImg``:
93100

@@ -104,7 +111,7 @@ Get from PyPI (Recommended)
104111
labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
105112
106113
Use precompiled binaries (Easy)
107-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114+
###############################
108115
Precompiled binaries for both Windows and Linux can be found `here <http://tzutalin.github.io/labelImg/>`__ .
109116

110117
Installation is the done in three simple steps:
@@ -137,7 +144,7 @@ Installation is the done in three simple steps:
137144
labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
138145
139146
Build from source (Hard)
140-
~~~~~~~~~~~~~~~~~~~~~~~~
147+
########################
141148
The steps for installing from source follow below.
142149

143150
**1. Download labelImg**
@@ -193,25 +200,27 @@ The steps for installing from source follow below.
193200
# From within Tensorflow/addons/labelImg
194201
python labelImg.py
195202
# or
196-
python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
203+
python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
197204
198-
Annotate Images
199-
***************
200205
201-
To annotate images we will be using the `labelImg <https://github.com/tzutalin/labelImg>`_ package. If you haven't installed the package yet, then have a look at :ref:`labelImg_install`.
206+
Annotate Images
207+
~~~~~~~~~~~~~~~
202208

203209
- Once you have collected all the images to be used to test your model (ideally more than 100 per class), place them inside the folder ``training_demo/images``.
204-
- Open a new `Anaconda/Command Prompt` window and ``cd`` into ``Tensorflow/addons/labelImg``.
205-
- If (as suggested in :ref:`labelImg_install`) you created a separate Conda environment for ``labelImg`` then go ahead and activate it by running:
210+
- Open a new `Terminal` window.
211+
- Next go ahead and start ``labelImg``, pointing it to your ``training_demo/images`` folder.
212+
213+
- If you installed ``labelImg`` :ref:`labelImg_install_pip`:
206214

207215
.. code-block:: default
208216
209-
activate labelImg
217+
labelImg <PATH_TO_TF>/TensorFlow/workspace/training_demo/images
210218
211-
- Next go ahead and start ``labelImg``, pointing it to your ``training_demo/images`` folder.
219+
- Othewise, ``cd`` into ``Tensorflow/addons/labelImg`` and run:
212220

213221
.. code-block:: default
214222
223+
# From within Tensorflow/addons/labelImg
215224
python labelImg.py ../../workspace/training_demo/images
216225
217226
- A File Explorer Dialog windows should open, which points to the ``training_demo/images`` folder.
@@ -232,16 +241,42 @@ I won't be covering a tutorial on how to use ``labelImg``, but you can have a lo
232241
Partition the Dataset
233242
*********************
234243

235-
Once you have finished annotating your image dataset, it is a general convention to use only part of it for training, and the rest is used for evaluation purposes (e.g. as discussed in :ref:`evaluation_sec`).
244+
Once you have finished annotating your image dataset, it is a general convention to use only part
245+
of it for training, and the rest is used for evaluation purposes (e.g. as discussed in
246+
:ref:`evaluation_sec`).
236247

237-
Typically, the ratio is 90%/10%, i.e. 90% of the images are used for training and the rest 10% is maintained for testing, but you can chose whatever ratio suits your needs.
248+
Typically, the ratio is 90%/10%, i.e. 90% of the images are used for training and the rest 10% is
249+
maintained for testing, but you can chose whatever ratio suits your needs.
238250

239-
Once you have decided how you will be splitting your dataset, copy all training images, together with their corresponding ``*.xml`` files, and place them inside the ``training_demo/images/train`` folder. Similarly, copy all testing images, with their ``*.xml`` files, and paste them inside ``training_demo/images/test``.
251+
Once you have decided how you will be splitting your dataset, copy all training images, together
252+
with their corresponding ``*.xml`` files, and place them inside the ``training_demo/images/train``
253+
folder. Similarly, copy all testing images, with their ``*.xml`` files, and paste them inside
254+
``training_demo/images/test``.
240255

241-
For lazy people like myself, who cannot be bothered to do the above, I have put tugether a simple script that automates the above process:
256+
For lazy people like myself, who cannot be bothered to do the above, I have put together a simple
257+
script that automates the above process:
242258

243259
.. literalinclude:: scripts/partition_dataset.py
244260

261+
- Under the ``TensorFlow`` folder, create a new folder ``TensorFlow/scripts``, which we can use to store some useful scripts.
262+
- To make things even tidier, let's create a new folder ``TensorFlow/scripts/preprocessing``, where we shall store scripts that we can use to preprocess our training inputs. Below is out ``TensorFlow`` directory tree structure, up to now:
263+
264+
.. code-block:: default
265+
266+
TensorFlow/
267+
├─ addons/ (Optional)
268+
│ └─ labelImg/
269+
├─ models/
270+
│ ├─ community/
271+
│ ├─ official/
272+
│ ├─ orbit/
273+
│ ├─ research/
274+
│ └─ ...
275+
├─ scripts/
276+
│ └─ preprocessing/
277+
└─ workspace/
278+
└─ training_demo/
279+
245280
- Click :download:`here <scripts/partition_dataset.py>` to download the above script and save it inside ``TensorFlow/scripts/preprocessing``.
246281
- Then, ``cd`` into ``TensorFlow/scripts/preprocessing`` and run:
247282

@@ -286,35 +321,13 @@ Create TensorFlow Records
286321
Now that we have generated our annotations and split our dataset into the desired training and
287322
testing subsets, it is time to convert our annotations into the so called ``TFRecord`` format.
288323

289-
Before we proceed to describe the above steps, let's create a directory where we can store some
290-
scripts. Under the ``TensorFlow`` folder, create a new folder ``TensorFlow/scripts``, which we can
291-
use to store some useful scripts. To make things even tidier, let's create a new folder
292-
``TensorFlow/scripts/preprocessing``, where we shall store scripts that we can use to preprocess
293-
our training inputs. Below is out ``TensorFlow`` directory tree structure, up to now:
294-
295-
.. code-block:: default
296-
297-
TensorFlow/
298-
├─ addons/ (Optional)
299-
│ └─ labelImg/
300-
├─ models/
301-
│ ├─ community/
302-
│ ├─ official/
303-
│ ├─ orbit/
304-
│ ├─ research/
305-
│ └─ ...
306-
├─ scripts/
307-
│ └─ preprocessing/
308-
└─ workspace/
309-
└─ training_demo/
310-
311324

312325
Convert ``*.xml`` to ``*.record``
313326
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314327

315-
To do this we can write a simple script that iterates through all ``*.xml`` files in the ``training_demo/images/train`` and ``training_demo/images/test`` folders, and generates a ``*.record`` file for each of the two.
316-
317-
Here is an example script that allows us to do just that:
328+
To do this we can write a simple script that iterates through all ``*.xml`` files in the
329+
``training_demo/images/train`` and ``training_demo/images/test`` folders, and generates a
330+
``*.record`` file for each of the two. Here is an example script that allows us to do just that:
318331

319332
.. literalinclude:: scripts/generate_tfrecord.py
320333

@@ -401,6 +414,8 @@ model, you can download the model and after extracting its context the demo dire
401414
| └─ pipeline.config
402415
└─ ...
403416
417+
.. _training_pipeline_conf:
418+
404419
Configure the Training Pipeline
405420
*******************************
406421
Now that we have downloaded and extracted our pre-trained model, let's create a directory for our
@@ -530,7 +545,7 @@ The steps to run the evaluation are outlined below:
530545

531546
#. Secondly, we must modify the configuration pipeline (``*.config`` script).
532547

533-
- See lines 178 and 181 of the script in :ref:`config_training_pipeline_sec`.
548+
- See lines 178-179 of the script in :ref:`training_pipeline_conf`.
534549

535550
#. The third step is to actually run the evaluation. To do so, open a new `Terminal`, ``cd`` inside the ``training_demo`` folder and run the following command:
536551

@@ -611,8 +626,8 @@ following which you should be presented with a dashboard similar to the one show
611626

612627

613628

614-
Exporting a Trained Inference Graph
615-
-----------------------------------
629+
Exporting a Trained Model
630+
-------------------------
616631

617632
Once your training job is complete, you need to extract the newly trained inference graph, which
618633
will be later used to perform the object detection. This can be done as follows:
@@ -622,7 +637,23 @@ will be later used to perform the object detection. This can be done as follows:
622637

623638
.. code-block:: default
624639
625-
python .\exporter_main_v2.py --input_type image_tensor --pipeline_config_path .\models\my_efficientdet_d1\pipeline.config --trained_checkpoint_dir .\models\my_efficientdet_d1\ --output_directory .\trained-inference-graphs\output
640+
python .\exporter_main_v2.py --input_type image_tensor --pipeline_config_path .\models\my_efficientdet_d1\pipeline.config --trained_checkpoint_dir .\models\my_efficientdet_d1\ --output_directory .\exported-models\my_model
641+
642+
After the above process has completed, you should find a new folder ``my_model`` under the
643+
``training_demo/exported-models``, that has the following structure:
644+
645+
.. code-block:: default
646+
647+
training_demo/
648+
├─ ...
649+
├─ exported-models/
650+
│ └─ my_model/
651+
| ├─ checkpoint/
652+
| ├─ saved_model/
653+
| └─ pipeline.config
654+
└─ ...
655+
656+
This model can then be used to perform inference.
626657

627658
.. note::
628659

0 commit comments

Comments
 (0)