File size: 14,424 Bytes
506da10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# coding=utf-8
# Copyright 2021 The Deeplab2 Authors.
#
# 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.

"""Contains common utility functions and classes for building dataset."""

import collections
import io

import numpy as np
from PIL import Image
from PIL import ImageOps
import tensorflow as tf

from deeplab2 import common

_PANOPTIC_LABEL_FORMAT = 'raw'


def read_image(image_data):
  """Decodes image from in-memory data.

  Args:
    image_data: Bytes data representing encoded image.

  Returns:
    Decoded PIL.Image object.
  """
  image = Image.open(io.BytesIO(image_data))

  try:
    image = ImageOps.exif_transpose(image)
  except TypeError:
    # capture and ignore this bug:
    # https://github.com/python-pillow/Pillow/issues/3973
    pass

  return image


def get_image_dims(image_data, check_is_rgb=False):
  """Decodes image and return its height and width.

  Args:
    image_data: Bytes data representing encoded image.
    check_is_rgb: Whether to check encoded image is RGB.

  Returns:
    Decoded image size as a tuple of (height, width)

  Raises:
    ValueError: If check_is_rgb is set and input image has other format.
  """
  image = read_image(image_data)

  if check_is_rgb and image.mode != 'RGB':
    raise ValueError('Expects RGB image data, gets mode: %s' % image.mode)

  width, height = image.size
  return height, width


def _int64_list_feature(values):
  """Returns a TF-Feature of int64_list.

  Args:
    values: A scalar or an iterable of integer values.

  Returns:
    A TF-Feature.
  """
  if not isinstance(values, collections.Iterable):
    values = [values]

  return tf.train.Feature(int64_list=tf.train.Int64List(value=values))


def _bytes_list_feature(values):
  """Returns a TF-Feature of bytes.

  Args:
    values: A string.

  Returns:
    A TF-Feature.
  """
  if isinstance(values, str):
    values = values.encode()

  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))


def create_features(image_data,
                    image_format,
                    filename,
                    label_data=None,
                    label_format=None):
  """Creates image/segmentation features.

  Args:
    image_data: String or byte stream of encoded image data.
    image_format: String, image data format, should be either 'jpeg' or 'png'.
    filename: String, image filename.
    label_data: String or byte stream of (potentially) encoded label data. If
      None, we skip to write it to tf.train.Example.
    label_format: String, label data format, should be either 'png' or 'raw'. If
      None, we skip to write it to tf.train.Example.

  Returns:
    A dictionary of feature name to tf.train.Feature maaping.
  """
  if image_format not in ('jpeg', 'png'):
    raise ValueError('Unsupported image format: %s' % image_format)

  # Check color mode, and convert grey image to rgb image.
  image = read_image(image_data)
  if image.mode != 'RGB':
    image = image.convert('RGB')
    image_data = io.BytesIO()
    image.save(image_data, format=image_format)
    image_data = image_data.getvalue()

  height, width = get_image_dims(image_data, check_is_rgb=True)

  feature_dict = {
      common.KEY_ENCODED_IMAGE: _bytes_list_feature(image_data),
      common.KEY_IMAGE_FILENAME: _bytes_list_feature(filename),
      common.KEY_IMAGE_FORMAT: _bytes_list_feature(image_format),
      common.KEY_IMAGE_HEIGHT: _int64_list_feature(height),
      common.KEY_IMAGE_WIDTH: _int64_list_feature(width),
      common.KEY_IMAGE_CHANNELS: _int64_list_feature(3),
  }

  if label_data is None:
    return feature_dict

  if label_format == 'png':
    label_height, label_width = get_image_dims(label_data)
    if (label_height, label_width) != (height, width):
      raise ValueError('Image (%s) and label (%s) shape mismatch' %
                       ((height, width), (label_height, label_width)))
  elif label_format == 'raw':
    # Raw label encodes int32 array.
    expected_label_size = height * width * np.dtype(np.int32).itemsize
    if len(label_data) != expected_label_size:
      raise ValueError('Expects raw label data length %d, gets %d' %
                       (expected_label_size, len(label_data)))
  else:
    raise ValueError('Unsupported label format: %s' % label_format)

  feature_dict.update({
      common.KEY_ENCODED_LABEL: _bytes_list_feature(label_data),
      common.KEY_LABEL_FORMAT: _bytes_list_feature(label_format)
  })

  return feature_dict


def create_tfexample(image_data,
                     image_format,
                     filename,
                     label_data=None,
                     label_format=None):
  """Converts one image/segmentation pair to TF example.

  Args:
    image_data: String or byte stream of encoded image data.
    image_format: String, image data format, should be either 'jpeg' or 'png'.
    filename: String, image filename.
    label_data: String or byte stream of (potentially) encoded label data. If
      None, we skip to write it to tf.train.Example.
    label_format: String, label data format, should be either 'png' or 'raw'. If
      None, we skip to write it to tf.train.Example.

  Returns:
    TF example proto.
  """
  feature_dict = create_features(image_data, image_format, filename, label_data,
                                 label_format)
  return tf.train.Example(features=tf.train.Features(feature=feature_dict))


def create_video_tfexample(image_data,
                           image_format,
                           filename,
                           sequence_id,
                           image_id,
                           label_data=None,
                           label_format=None,
                           prev_image_data=None,
                           prev_label_data=None):
  """Converts one video frame/panoptic segmentation pair to TF example.

  Args:
    image_data: String or byte stream of encoded image data.
    image_format: String, image data format, should be either 'jpeg' or 'png'.
    filename: String, image filename.
    sequence_id: ID of the video sequence as a string.
    image_id: ID of the image as a string.
    label_data: String or byte stream of (potentially) encoded label data. If
      None, we skip to write it to tf.train.Example.
    label_format: String, label data format, should be either 'png' or 'raw'. If
      None, we skip to write it to tf.train.Example.
    prev_image_data: An optional string or byte stream of encoded previous image
      data.
    prev_label_data: An optional string or byte stream of (potentially) encoded
      previous label data.

  Returns:
    TF example proto.
  """
  feature_dict = create_features(image_data, image_format, filename, label_data,
                                 label_format)
  feature_dict.update({
      common.KEY_SEQUENCE_ID: _bytes_list_feature(sequence_id),
      common.KEY_FRAME_ID: _bytes_list_feature(image_id)
  })
  if prev_image_data is not None:
    feature_dict[common.KEY_ENCODED_PREV_IMAGE] = _bytes_list_feature(
        prev_image_data)
  if prev_label_data is not None:
    feature_dict[common.KEY_ENCODED_PREV_LABEL] = _bytes_list_feature(
        prev_label_data)
  return tf.train.Example(features=tf.train.Features(feature=feature_dict))


def create_video_and_depth_tfexample(image_data,
                                     image_format,
                                     filename,
                                     sequence_id,
                                     image_id,
                                     label_data=None,
                                     label_format=None,
                                     next_image_data=None,
                                     next_label_data=None,
                                     depth_data=None,
                                     depth_format=None):
  """Converts an image/segmentation pair and depth of first frame to TF example.

    The image pair contains the current frame and the next frame with the
    current frame including depth label.

  Args:
    image_data: String or byte stream of encoded image data.
    image_format: String, image data format, should be either 'jpeg' or 'png'.
    filename: String, image filename.
    sequence_id: ID of the video sequence as a string.
    image_id: ID of the image as a string.
    label_data: String or byte stream of (potentially) encoded label data. If
      None, we skip to write it to tf.train.Example.
    label_format: String, label data format, should be either 'png' or 'raw'. If
      None, we skip to write it to tf.train.Example.
    next_image_data: An optional string or byte stream of encoded next image
      data.
    next_label_data: An optional string or byte stream of (potentially) encoded
      next label data.
    depth_data: An optional string or byte sream of encoded depth data.
    depth_format: String, depth data format, should be either 'png' or 'raw'.

  Returns:
    TF example proto.
  """
  feature_dict = create_features(image_data, image_format, filename, label_data,
                                 label_format)
  feature_dict.update({
      common.KEY_SEQUENCE_ID: _bytes_list_feature(sequence_id),
      common.KEY_FRAME_ID: _bytes_list_feature(image_id)
  })
  if next_image_data is not None:
    feature_dict[common.KEY_ENCODED_NEXT_IMAGE] = _bytes_list_feature(
        next_image_data)
  if next_label_data is not None:
    feature_dict[common.KEY_ENCODED_NEXT_LABEL] = _bytes_list_feature(
        next_label_data)
  if depth_data is not None:
    feature_dict[common.KEY_ENCODED_DEPTH] = _bytes_list_feature(
        depth_data)
    feature_dict[common.KEY_DEPTH_FORMAT] = _bytes_list_feature(
        depth_format)
  return tf.train.Example(features=tf.train.Features(feature=feature_dict))


class SegmentationDecoder(object):
  """Basic parser to decode serialized tf.Example."""

  def __init__(self,
               is_panoptic_dataset=True,
               is_video_dataset=False,
               use_two_frames=False,
               use_next_frame=False,
               decode_groundtruth_label=True):
    self._is_panoptic_dataset = is_panoptic_dataset
    self._is_video_dataset = is_video_dataset
    self._use_two_frames = use_two_frames
    self._use_next_frame = use_next_frame
    self._decode_groundtruth_label = decode_groundtruth_label
    string_feature = tf.io.FixedLenFeature((), tf.string)
    int_feature = tf.io.FixedLenFeature((), tf.int64)
    self._keys_to_features = {
        common.KEY_ENCODED_IMAGE: string_feature,
        common.KEY_IMAGE_FILENAME: string_feature,
        common.KEY_IMAGE_FORMAT: string_feature,
        common.KEY_IMAGE_HEIGHT: int_feature,
        common.KEY_IMAGE_WIDTH: int_feature,
        common.KEY_IMAGE_CHANNELS: int_feature,
    }
    if decode_groundtruth_label:
      self._keys_to_features[common.KEY_ENCODED_LABEL] = string_feature
    if self._is_video_dataset:
      self._keys_to_features[common.KEY_SEQUENCE_ID] = string_feature
      self._keys_to_features[common.KEY_FRAME_ID] = string_feature
    # Two-frame specific processing.
    if self._use_two_frames:
      self._keys_to_features[common.KEY_ENCODED_PREV_IMAGE] = string_feature
      if decode_groundtruth_label:
        self._keys_to_features[common.KEY_ENCODED_PREV_LABEL] = string_feature
    # Next-frame specific processing.
    if self._use_next_frame:
      self._keys_to_features[common.KEY_ENCODED_NEXT_IMAGE] = string_feature
      if decode_groundtruth_label:
        self._keys_to_features[common.KEY_ENCODED_NEXT_LABEL] = string_feature

  def _decode_image(self, parsed_tensors, key):
    """Decodes image udner key from parsed tensors."""
    image = tf.io.decode_image(
        parsed_tensors[key],
        channels=3,
        dtype=tf.dtypes.uint8,
        expand_animations=False)
    image.set_shape([None, None, 3])
    return image

  def _decode_label(self, parsed_tensors, label_key):
    """Decodes segmentation label under label_key from parsed tensors."""
    if self._is_panoptic_dataset:
      flattened_label = tf.io.decode_raw(
          parsed_tensors[label_key], out_type=tf.int32)
      label_shape = tf.stack([
          parsed_tensors[common.KEY_IMAGE_HEIGHT],
          parsed_tensors[common.KEY_IMAGE_WIDTH], 1
      ])
      label = tf.reshape(flattened_label, label_shape)
      return label

    label = tf.io.decode_image(parsed_tensors[label_key], channels=1)
    label.set_shape([None, None, 1])
    return label

  def __call__(self, serialized_example):
    parsed_tensors = tf.io.parse_single_example(
        serialized_example, features=self._keys_to_features)
    return_dict = {
        'image':
            self._decode_image(parsed_tensors, common.KEY_ENCODED_IMAGE),
        'image_name':
            parsed_tensors[common.KEY_IMAGE_FILENAME],
        'height':
            tf.cast(parsed_tensors[common.KEY_IMAGE_HEIGHT], dtype=tf.int32),
        'width':
            tf.cast(parsed_tensors[common.KEY_IMAGE_WIDTH], dtype=tf.int32),
    }
    return_dict['label'] = None
    if self._decode_groundtruth_label:
      return_dict['label'] = self._decode_label(parsed_tensors,
                                                common.KEY_ENCODED_LABEL)
    if self._is_video_dataset:
      return_dict['sequence'] = parsed_tensors[common.KEY_SEQUENCE_ID]
    if self._use_two_frames:
      return_dict['prev_image'] = self._decode_image(
          parsed_tensors, common.KEY_ENCODED_PREV_IMAGE)
      if self._decode_groundtruth_label:
        return_dict['prev_label'] = self._decode_label(
            parsed_tensors, common.KEY_ENCODED_PREV_LABEL)
    if self._use_next_frame:
      return_dict['next_image'] = self._decode_image(
          parsed_tensors, common.KEY_ENCODED_NEXT_IMAGE)
      if self._decode_groundtruth_label:
        return_dict['next_label'] = self._decode_label(
            parsed_tensors, common.KEY_ENCODED_NEXT_LABEL)
    return return_dict