Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/reshaping/zero_padding2d.py: 28%
46 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Keras zero-padding layer for 2D input."""
18import tensorflow.compat.v2 as tf
20from keras.src import backend
21from keras.src.engine.base_layer import Layer
22from keras.src.engine.input_spec import InputSpec
23from keras.src.utils import conv_utils
25# isort: off
26from tensorflow.python.util.tf_export import keras_export
29@keras_export("keras.layers.ZeroPadding2D")
30class ZeroPadding2D(Layer):
31 """Zero-padding layer for 2D input (e.g. picture).
33 This layer can add rows and columns of zeros
34 at the top, bottom, left and right side of an image tensor.
36 Examples:
38 >>> input_shape = (1, 1, 2, 2)
39 >>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
40 >>> print(x)
41 [[[[0 1]
42 [2 3]]]]
43 >>> y = tf.keras.layers.ZeroPadding2D(padding=1)(x)
44 >>> print(y)
45 tf.Tensor(
46 [[[[0 0]
47 [0 0]
48 [0 0]
49 [0 0]]
50 [[0 0]
51 [0 1]
52 [2 3]
53 [0 0]]
54 [[0 0]
55 [0 0]
56 [0 0]
57 [0 0]]]], shape=(1, 3, 4, 2), dtype=int64)
59 Args:
60 padding: Int, or tuple of 2 ints, or tuple of 2 tuples of 2 ints.
61 - If int: the same symmetric padding
62 is applied to height and width.
63 - If tuple of 2 ints:
64 interpreted as two different
65 symmetric padding values for height and width:
66 `(symmetric_height_pad, symmetric_width_pad)`.
67 - If tuple of 2 tuples of 2 ints:
68 interpreted as
69 `((top_pad, bottom_pad), (left_pad, right_pad))`
70 data_format: A string,
71 one of `channels_last` (default) or `channels_first`.
72 The ordering of the dimensions in the inputs.
73 `channels_last` corresponds to inputs with shape
74 `(batch_size, height, width, channels)` while `channels_first`
75 corresponds to inputs with shape
76 `(batch_size, channels, height, width)`.
77 When unspecified, uses
78 `image_data_format` value found in your Keras config file at
79 `~/.keras/keras.json` (if exists) else 'channels_last'.
80 Defaults to 'channels_last'.
82 Input shape:
83 4D tensor with shape:
84 - If `data_format` is `"channels_last"`:
85 `(batch_size, rows, cols, channels)`
86 - If `data_format` is `"channels_first"`:
87 `(batch_size, channels, rows, cols)`
89 Output shape:
90 4D tensor with shape:
91 - If `data_format` is `"channels_last"`:
92 `(batch_size, padded_rows, padded_cols, channels)`
93 - If `data_format` is `"channels_first"`:
94 `(batch_size, channels, padded_rows, padded_cols)`
95 """
97 def __init__(self, padding=(1, 1), data_format=None, **kwargs):
98 super().__init__(**kwargs)
99 self.data_format = conv_utils.normalize_data_format(data_format)
100 if isinstance(padding, int):
101 self.padding = ((padding, padding), (padding, padding))
102 elif hasattr(padding, "__len__"):
103 if len(padding) != 2:
104 raise ValueError(
105 f"`padding` should have two elements. Received: {padding}."
106 )
107 height_padding = conv_utils.normalize_tuple(
108 padding[0], 2, "1st entry of padding", allow_zero=True
109 )
110 width_padding = conv_utils.normalize_tuple(
111 padding[1], 2, "2nd entry of padding", allow_zero=True
112 )
113 self.padding = (height_padding, width_padding)
114 else:
115 raise ValueError(
116 "`padding` should be either an int, "
117 "a tuple of 2 ints "
118 "(symmetric_height_pad, symmetric_width_pad), "
119 "or a tuple of 2 tuples of 2 ints "
120 "((top_pad, bottom_pad), (left_pad, right_pad)). "
121 f"Received: {padding}."
122 )
123 self.input_spec = InputSpec(ndim=4)
125 def compute_output_shape(self, input_shape):
126 input_shape = tf.TensorShape(input_shape).as_list()
127 if self.data_format == "channels_first":
128 if input_shape[2] is not None:
129 rows = input_shape[2] + self.padding[0][0] + self.padding[0][1]
130 else:
131 rows = None
132 if input_shape[3] is not None:
133 cols = input_shape[3] + self.padding[1][0] + self.padding[1][1]
134 else:
135 cols = None
136 return tf.TensorShape([input_shape[0], input_shape[1], rows, cols])
137 elif self.data_format == "channels_last":
138 if input_shape[1] is not None:
139 rows = input_shape[1] + self.padding[0][0] + self.padding[0][1]
140 else:
141 rows = None
142 if input_shape[2] is not None:
143 cols = input_shape[2] + self.padding[1][0] + self.padding[1][1]
144 else:
145 cols = None
146 return tf.TensorShape([input_shape[0], rows, cols, input_shape[3]])
148 def call(self, inputs):
149 return backend.spatial_2d_padding(
150 inputs, padding=self.padding, data_format=self.data_format
151 )
153 def get_config(self):
154 config = {"padding": self.padding, "data_format": self.data_format}
155 base_config = super().get_config()
156 return dict(list(base_config.items()) + list(config.items()))