Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/base_pooling2d.py: 26%
43 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"""Private base class for pooling 2D layers."""
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
26class Pooling2D(Layer):
27 """Pooling layer for arbitrary pooling functions, for 2D data (e.g. images).
29 This class only exists for code reuse. It will never be an exposed API.
31 Args:
32 pool_function: The pooling function to apply, e.g. `tf.nn.max_pool2d`.
33 pool_size: An integer or tuple/list of 2 integers:
34 (pool_height, pool_width)
35 specifying the size of the pooling window.
36 Can be a single integer to specify the same value for
37 all spatial dimensions.
38 strides: An integer or tuple/list of 2 integers,
39 specifying the strides of the pooling operation.
40 Can be a single integer to specify the same value for
41 all spatial dimensions.
42 padding: A string. The padding method, either 'valid' or 'same'.
43 Case-insensitive.
44 data_format: A string, one of `channels_last` (default) or
45 `channels_first`.
46 The ordering of the dimensions in the inputs.
47 `channels_last` corresponds to inputs with shape
48 `(batch, height, width, channels)` while `channels_first` corresponds to
49 inputs with shape `(batch, channels, height, width)`.
50 name: A string, the name of the layer.
51 """
53 def __init__(
54 self,
55 pool_function,
56 pool_size,
57 strides,
58 padding="valid",
59 data_format=None,
60 name=None,
61 **kwargs
62 ):
63 super().__init__(name=name, **kwargs)
64 if data_format is None:
65 data_format = backend.image_data_format()
66 if strides is None:
67 strides = pool_size
68 self.pool_function = pool_function
69 self.pool_size = conv_utils.normalize_tuple(pool_size, 2, "pool_size")
70 self.strides = conv_utils.normalize_tuple(
71 strides, 2, "strides", allow_zero=True
72 )
73 self.padding = conv_utils.normalize_padding(padding)
74 self.data_format = conv_utils.normalize_data_format(data_format)
75 self.input_spec = InputSpec(ndim=4)
77 def call(self, inputs):
78 if self.data_format == "channels_last":
79 pool_shape = (1,) + self.pool_size + (1,)
80 strides = (1,) + self.strides + (1,)
81 else:
82 pool_shape = (1, 1) + self.pool_size
83 strides = (1, 1) + self.strides
84 outputs = self.pool_function(
85 inputs,
86 ksize=pool_shape,
87 strides=strides,
88 padding=self.padding.upper(),
89 data_format=conv_utils.convert_data_format(self.data_format, 4),
90 )
91 return outputs
93 def compute_output_shape(self, input_shape):
94 input_shape = tf.TensorShape(input_shape).as_list()
95 if self.data_format == "channels_first":
96 rows = input_shape[2]
97 cols = input_shape[3]
98 else:
99 rows = input_shape[1]
100 cols = input_shape[2]
101 rows = conv_utils.conv_output_length(
102 rows, self.pool_size[0], self.padding, self.strides[0]
103 )
104 cols = conv_utils.conv_output_length(
105 cols, self.pool_size[1], self.padding, self.strides[1]
106 )
107 if self.data_format == "channels_first":
108 return tf.TensorShape([input_shape[0], input_shape[1], rows, cols])
109 else:
110 return tf.TensorShape([input_shape[0], rows, cols, input_shape[3]])
112 def get_config(self):
113 config = {
114 "pool_size": self.pool_size,
115 "padding": self.padding,
116 "strides": self.strides,
117 "data_format": self.data_format,
118 }
119 base_config = super().get_config()
120 return dict(list(base_config.items()) + list(config.items()))