classification_input.py 3.2 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Vishnu Banna's avatar
Vishnu Banna committed
2
3
4
5
6
7
8
9
10
11
12
13
#
# 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.
Vishnu Banna's avatar
config  
Vishnu Banna committed
14

Vishnu Banna's avatar
Vishnu Banna committed
15
"""Classification decoder and parser."""
Vishnu Banna's avatar
config  
Vishnu Banna committed
16
import tensorflow as tf
Abdullah Rashwan's avatar
Abdullah Rashwan committed
17
18
from official.vision.dataloaders import classification_input
from official.vision.ops import preprocess_ops
Vishnu Banna's avatar
config  
Vishnu Banna committed
19
20


Vishnu Banna's avatar
Vishnu Banna committed
21
class Parser(classification_input.Parser):
Vishnu Banna's avatar
config  
Vishnu Banna committed
22
23
  """Parser to parse an image and its annotations into a dictionary of tensors."""

Vishnu Banna's avatar
Vishnu Banna committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  def _parse_train_image(self, decoded_tensors):
    """Parses image data for training."""
    image_bytes = decoded_tensors[self._image_field_key]

    if self._decode_jpeg_only:
      image_shape = tf.image.extract_jpeg_shape(image_bytes)

      # Crops image.
      cropped_image = preprocess_ops.random_crop_image_v2(
          image_bytes, image_shape)
      image = tf.cond(
          tf.reduce_all(tf.equal(tf.shape(cropped_image), image_shape)),
          lambda: preprocess_ops.center_crop_image_v2(image_bytes, image_shape),
          lambda: cropped_image)
Vishnu Banna's avatar
config  
Vishnu Banna committed
38
    else:
Vishnu Banna's avatar
Vishnu Banna committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
      # Decodes image.
      image = tf.io.decode_image(image_bytes, channels=3)
      image.set_shape([None, None, 3])

      # Crops image.
      cropped_image = preprocess_ops.random_crop_image(image)

      image = tf.cond(
          tf.reduce_all(tf.equal(tf.shape(cropped_image), tf.shape(image))),
          lambda: preprocess_ops.center_crop_image(image),
          lambda: cropped_image)

    if self._aug_rand_hflip:
      image = tf.image.random_flip_left_right(image)
Vishnu Banna's avatar
config  
Vishnu Banna committed
53

Vishnu Banna's avatar
Vishnu Banna committed
54
55
56
57
58
59
    # Resizes image.
    image = tf.image.resize(
        image, self._output_size, method=tf.image.ResizeMethod.BILINEAR)
    image.set_shape([self._output_size[0], self._output_size[1], 3])

    # Apply autoaug or randaug.
Vishnu Banna's avatar
config  
Vishnu Banna committed
60
61
62
    if self._augmenter is not None:
      image = self._augmenter.distort(image)

Vishnu Banna's avatar
Vishnu Banna committed
63
64
    # Convert image to self._dtype.
    image = tf.image.convert_image_dtype(image, self._dtype)
65
    image = image / 255.0
Vishnu Banna's avatar
Vishnu Banna committed
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
    return image

  def _parse_eval_image(self, decoded_tensors):
    """Parses image data for evaluation."""
    image_bytes = decoded_tensors[self._image_field_key]

    if self._decode_jpeg_only:
      image_shape = tf.image.extract_jpeg_shape(image_bytes)

      # Center crops.
      image = preprocess_ops.center_crop_image_v2(image_bytes, image_shape)
    else:
      # Decodes image.
      image = tf.io.decode_image(image_bytes, channels=3)
      image.set_shape([None, None, 3])

      # Center crops.
      image = preprocess_ops.center_crop_image(image)

    image = tf.image.resize(
        image, self._output_size, method=tf.image.ResizeMethod.BILINEAR)
    image.set_shape([self._output_size[0], self._output_size[1], 3])

    # Convert image to self._dtype.
    image = tf.image.convert_image_dtype(image, self._dtype)
91
    image = image / 255.0
Vishnu Banna's avatar
Vishnu Banna committed
92
    return image