File size: 4,793 Bytes
9a393e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================

"""Test for create_kitti_tf_record.py."""

import os

import numpy as np
import PIL.Image
import tensorflow as tf

from object_detection.dataset_tools import create_kitti_tf_record


class CreateKittiTFRecordTest(tf.test.TestCase):

  def _assertProtoEqual(self, proto_field, expectation):
    """Helper function to assert if a proto field equals some value.

    Args:
      proto_field: The protobuf field to compare.
      expectation: The expected value of the protobuf field.
    """
    proto_list = [p for p in proto_field]
    self.assertListEqual(proto_list, expectation)

  def test_dict_to_tf_example(self):
    image_file_name = 'tmp_image.jpg'
    image_data = np.random.rand(256, 256, 3)
    save_path = os.path.join(self.get_temp_dir(), image_file_name)
    image = PIL.Image.fromarray(image_data, 'RGB')
    image.save(save_path)

    annotations = {}
    annotations['2d_bbox_left'] = np.array([64])
    annotations['2d_bbox_top'] = np.array([64])
    annotations['2d_bbox_right'] = np.array([192])
    annotations['2d_bbox_bottom'] = np.array([192])
    annotations['type'] = ['car']
    annotations['truncated'] = np.array([1])
    annotations['alpha'] = np.array([2])
    annotations['3d_bbox_height'] = np.array([10])
    annotations['3d_bbox_width'] = np.array([11])
    annotations['3d_bbox_length'] = np.array([12])
    annotations['3d_bbox_x'] = np.array([13])
    annotations['3d_bbox_y'] = np.array([14])
    annotations['3d_bbox_z'] = np.array([15])
    annotations['3d_bbox_rot_y'] = np.array([4])

    label_map_dict = {
        'background': 0,
        'car': 1,
    }

    example = create_kitti_tf_record.prepare_example(
        save_path,
        annotations,
        label_map_dict)

    self._assertProtoEqual(
        example.features.feature['image/height'].int64_list.value, [256])
    self._assertProtoEqual(
        example.features.feature['image/width'].int64_list.value, [256])
    self._assertProtoEqual(
        example.features.feature['image/filename'].bytes_list.value,
        [save_path])
    self._assertProtoEqual(
        example.features.feature['image/source_id'].bytes_list.value,
        [save_path])
    self._assertProtoEqual(
        example.features.feature['image/format'].bytes_list.value, ['png'])
    self._assertProtoEqual(
        example.features.feature['image/object/bbox/xmin'].float_list.value,
        [0.25])
    self._assertProtoEqual(
        example.features.feature['image/object/bbox/ymin'].float_list.value,
        [0.25])
    self._assertProtoEqual(
        example.features.feature['image/object/bbox/xmax'].float_list.value,
        [0.75])
    self._assertProtoEqual(
        example.features.feature['image/object/bbox/ymax'].float_list.value,
        [0.75])
    self._assertProtoEqual(
        example.features.feature['image/object/class/text'].bytes_list.value,
        ['car'])
    self._assertProtoEqual(
        example.features.feature['image/object/class/label'].int64_list.value,
        [1])
    self._assertProtoEqual(
        example.features.feature['image/object/truncated'].float_list.value,
        [1])
    self._assertProtoEqual(
        example.features.feature['image/object/alpha'].float_list.value,
        [2])
    self._assertProtoEqual(example.features.feature[
        'image/object/3d_bbox/height'].float_list.value, [10])
    self._assertProtoEqual(
        example.features.feature['image/object/3d_bbox/width'].float_list.value,
        [11])
    self._assertProtoEqual(example.features.feature[
        'image/object/3d_bbox/length'].float_list.value, [12])
    self._assertProtoEqual(
        example.features.feature['image/object/3d_bbox/x'].float_list.value,
        [13])
    self._assertProtoEqual(
        example.features.feature['image/object/3d_bbox/y'].float_list.value,
        [14])
    self._assertProtoEqual(
        example.features.feature['image/object/3d_bbox/z'].float_list.value,
        [15])
    self._assertProtoEqual(
        example.features.feature['image/object/3d_bbox/rot_y'].float_list.value,
        [4])


if __name__ == '__main__':
  tf.test.main()