Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
fdb70c22
Commit
fdb70c22
authored
Jul 07, 2017
by
derekjchow
Committed by
GitHub
Jul 07, 2017
Browse files
Merge pull request #1861 from derekjchow/master
Update directories in running_pets.md
parents
d71cbd0c
88a05515
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
206 additions
and
2 deletions
+206
-2
object_detection/README.md
object_detection/README.md
+11
-0
object_detection/g3doc/img/example_cat.jpg
object_detection/g3doc/img/example_cat.jpg
+0
-0
object_detection/g3doc/using_your_own_dataset.md
object_detection/g3doc/using_your_own_dataset.md
+157
-0
object_detection/utils/label_map_util.py
object_detection/utils/label_map_util.py
+15
-1
object_detection/utils/label_map_util_test.py
object_detection/utils/label_map_util_test.py
+23
-1
No files found.
object_detection/README.md
View file @
fdb70c22
...
...
@@ -54,6 +54,17 @@ Extras:
Exporting a trained model for inference
</a><br>
*
<a
href=
'g3doc/defining_your_own_model.md'
>
Defining your own model architecture
</a><br>
*
<a
href=
'g3doc/using_your_own_dataset.md'
>
Bringing in your own dataset
</a><br>
## Getting Help
Please report bugs to the tensorflow/models/ Github
[
issue tracker
](
https://github.com/tensorflow/models/issues
)
, prefixing the
issue name with "object_detection". To get help with issues you may encounter
using the Tensorflow Object Detection API, create a new question on
[
StackOverflow
](
https://stackoverflow.com/
)
with the tags "tensorflow" and
"object-detection".
## Release information
...
...
object_detection/g3doc/img/example_cat.jpg
0 → 100644
View file @
fdb70c22
238 KB
object_detection/g3doc/using_your_own_dataset.md
0 → 100644
View file @
fdb70c22
# Preparing Inputs
To use your own dataset in Tensorflow Object Detection API, you must convert it
into the
[
TFRecord file format
](
https://www.tensorflow.org/api_guides/python/python_io#tfrecords_format_details
)
.
This document outlines how to write a script to generate the TFRecord file.
## Label Maps
Each dataset is required to have a label map associated with it. This label map
defines a mapping from string class names to integer class Ids. The label map
should be a
`StringIntLabelMap`
text protobuf. Sample label maps can be found in
object_detection/data. Label maps should always start from id 1.
## Dataset Requirements
For every example in your dataset, you should have the following information:
1.
An RGB image for the dataset encoded as jpeg or png.
2.
A list of bounding boxes for the image. Each bounding box should contain:
1.
A bounding box coordinates (with origin in top left corner) defined by 4
floating point numbers [ymin, xmin, ymax, xmax]. Note that we store the
_normalized_ coordinates (x / width, y / height) in the TFRecord dataset.
2.
The class of the object in the bounding box.
# Example Image
Consider the following image:

with the following label map:
```
item {
id: 1
name: 'Cat'
}
item {
id: 2
name: 'Dog'
}
```
We can generate a tf.Example proto for this image using the following code:
```
python
def
create_cat_tf_example
(
encoded_cat_image_data
):
"""Creates a tf.Example proto from sample cat image.
Args:
encoded_cat_image_data: The jpg encoded data of the cat image.
Returns:
example: The created tf.Example.
"""
height
=
1032.0
width
=
1200.0
filename
=
'example_cat.jpg'
image_format
=
b
'jpg'
xmins
=
[
322.0
/
1200.0
]
xmaxs
=
[
1062.0
/
1200.0
]
ymins
=
[
174.0
/
1032.0
]
ymaxs
=
[
761.0
/
1032.0
]
classes_text
=
[
'Cat'
]
classes
=
[
1
]
tf_example
=
tf
.
train
.
Example
(
features
=
tf
.
train
.
Features
(
feature
=
{
'image/height'
:
dataset_util
.
int64_feature
(
height
),
'image/width'
:
dataset_util
.
int64_feature
(
width
),
'image/filename'
:
dataset_util
.
bytes_feature
(
filename
),
'image/source_id'
:
dataset_util
.
bytes_feature
(
filename
),
'image/encoded'
:
dataset_util
.
bytes_feature
(
encoded_image_data
),
'image/format'
:
dataset_util
.
bytes_feature
(
image_format
),
'image/object/bbox/xmin'
:
dataset_util
.
float_list_feature
(
xmins
),
'image/object/bbox/xmax'
:
dataset_util
.
float_list_feature
(
xmaxs
),
'image/object/bbox/ymin'
:
dataset_util
.
float_list_feature
(
ymins
),
'image/object/bbox/ymax'
:
dataset_util
.
float_list_feature
(
ymaxs
),
'image/object/class/text'
:
dataset_util
.
bytes_list_feature
(
classes_text
),
'image/object/class/label'
:
dataset_util
.
int64_list_feature
(
classes
),
}))
return
tf_example
```
## Conversion Script Outline
A typical conversion script will look like the following:
```
python
import
tensorflow
as
tf
from
object_detection.utils
import
dataset_util
flags
=
tf
.
app
.
flags
flags
.
DEFINE_string
(
'output_path'
,
''
,
'Path to output TFRecord'
)
FLAGS
=
flags
.
FLAGS
def
create_tf_example
(
example
):
# TODO(user): Populate the following variables from your example.
height
=
None
# Image height
width
=
None
# Image width
filename
=
None
# Filename of the image. Empty if image is not from file
encoded_image_data
=
None
# Encoded image bytes
image_format
=
None
# b'jpeg' or b'png'
xmins
=
[]
# List of normalized left x coordinates in bounding box (1 per box)
xmaxs
=
[]
# List of normalized right x coordinates in bounding box
# (1 per box)
ymins
=
[]
# List of normalized top y coordinates in bounding box (1 per box)
ymaxs
=
[]
# List of normalized bottom y coordinates in bounding box
# (1 per box)
classes_text
=
[]
# List of string class name of bounding box (1 per box)
classes
=
[]
# List of integer class id of bounding box (1 per box)
tf_example
=
tf
.
train
.
Example
(
features
=
tf
.
train
.
Features
(
feature
=
{
'image/height'
:
dataset_util
.
int64_feature
(
height
),
'image/width'
:
dataset_util
.
int64_feature
(
width
),
'image/filename'
:
dataset_util
.
bytes_feature
(
filename
),
'image/source_id'
:
dataset_util
.
bytes_feature
(
filename
),
'image/encoded'
:
dataset_util
.
bytes_feature
(
encoded_image_data
),
'image/format'
:
dataset_util
.
bytes_feature
(
image_format
),
'image/object/bbox/xmin'
:
dataset_util
.
float_list_feature
(
xmins
),
'image/object/bbox/xmax'
:
dataset_util
.
float_list_feature
(
xmaxs
),
'image/object/bbox/ymin'
:
dataset_util
.
float_list_feature
(
ymins
),
'image/object/bbox/ymax'
:
dataset_util
.
float_list_feature
(
ymaxs
),
'image/object/class/text'
:
dataset_util
.
bytes_list_feature
(
classes_text
),
'image/object/class/label'
:
dataset_util
.
int64_list_feature
(
classes
),
}))
return
tf_example
def
main
(
_
):
writer
=
tf
.
python_io
.
TFRecordWriter
(
FLAGS
.
output_path
)
# TODO(user): Write code to read in your dataset to examples variable
for
example
in
examples
:
tf_example
=
create_tf_example
(
example
)
writer
.
write
(
tf_example
.
SerializeToString
())
writer
.
close
()
if
__name__
==
'__main__'
:
tf
.
app
.
run
()
```
Note: You may notice additional fields in some other datasets. They are
currently unused by the API and are optional.
object_detection/utils/label_map_util.py
View file @
fdb70c22
...
...
@@ -22,6 +22,20 @@ from google.protobuf import text_format
from
object_detection.protos
import
string_int_label_map_pb2
def
_validate_label_map
(
label_map
):
"""Checks if a label map is valid.
Args:
label_map: StringIntLabelMap to validate.
Raises:
ValueError: if label map is invalid.
"""
for
item
in
label_map
.
item
:
if
item
.
id
<
1
:
raise
ValueError
(
'Label map ids should be >= 1.'
)
def
create_category_index
(
categories
):
"""Creates dictionary of COCO compatible categories keyed by category id.
...
...
@@ -91,7 +105,6 @@ def convert_label_map_to_categories(label_map,
return
categories
# TODO: double check documentaion.
def
load_labelmap
(
path
):
"""Loads label map proto.
...
...
@@ -107,6 +120,7 @@ def load_labelmap(path):
text_format
.
Merge
(
label_map_string
,
label_map
)
except
text_format
.
ParseError
:
label_map
.
ParseFromString
(
label_map_string
)
_validate_label_map
(
label_map
)
return
label_map
...
...
object_detection/utils/label_map_util_test.py
View file @
fdb70c22
...
...
@@ -53,6 +53,28 @@ class LabelMapUtilTest(tf.test.TestCase):
self
.
assertEqual
(
label_map_dict
[
'dog'
],
1
)
self
.
assertEqual
(
label_map_dict
[
'cat'
],
2
)
def
test_load_bad_label_map
(
self
):
label_map_string
=
"""
item {
id:0
name:'class that should not be indexed at zero'
}
item {
id:2
name:'cat'
}
item {
id:1
name:'dog'
}
"""
label_map_path
=
os
.
path
.
join
(
self
.
get_temp_dir
(),
'label_map.pbtxt'
)
with
tf
.
gfile
.
Open
(
label_map_path
,
'wb'
)
as
f
:
f
.
write
(
label_map_string
)
with
self
.
assertRaises
(
ValueError
):
label_map_util
.
load_labelmap
(
label_map_path
)
def
test_keep_categories_with_unique_id
(
self
):
label_map_proto
=
string_int_label_map_pb2
.
StringIntLabelMap
()
label_map_string
=
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment