Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/max_pooling2d.py: 89%

9 statements  

« 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"""Max pooling 2D layer.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src.layers.pooling.base_pooling2d import Pooling2D 

21 

22# isort: off 

23from tensorflow.python.util.tf_export import keras_export 

24 

25 

26@keras_export("keras.layers.MaxPooling2D", "keras.layers.MaxPool2D") 

27class MaxPooling2D(Pooling2D): 

28 """Max pooling operation for 2D spatial data. 

29 

30 Downsamples the input along its spatial dimensions (height and width) 

31 by taking the maximum value over an input window 

32 (of size defined by `pool_size`) for each channel of the input. 

33 The window is shifted by `strides` along each dimension. 

34 

35 The resulting output, 

36 when using the `"valid"` padding option, has a spatial shape 

37 (number of rows or columns) of: 

38 `output_shape = math.floor((input_shape - pool_size) / strides) + 1` 

39 (when `input_shape >= pool_size`) 

40 

41 The resulting output shape when using the `"same"` padding option is: 

42 `output_shape = math.floor((input_shape - 1) / strides) + 1` 

43 

44 For example, for `strides=(1, 1)` and `padding="valid"`: 

45 

46 >>> x = tf.constant([[1., 2., 3.], 

47 ... [4., 5., 6.], 

48 ... [7., 8., 9.]]) 

49 >>> x = tf.reshape(x, [1, 3, 3, 1]) 

50 >>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), 

51 ... strides=(1, 1), padding='valid') 

52 >>> max_pool_2d(x) 

53 <tf.Tensor: shape=(1, 2, 2, 1), dtype=float32, numpy= 

54 array([[[[5.], 

55 [6.]], 

56 [[8.], 

57 [9.]]]], dtype=float32)> 

58 

59 For example, for `strides=(2, 2)` and `padding="valid"`: 

60 

61 >>> x = tf.constant([[1., 2., 3., 4.], 

62 ... [5., 6., 7., 8.], 

63 ... [9., 10., 11., 12.]]) 

64 >>> x = tf.reshape(x, [1, 3, 4, 1]) 

65 >>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), 

66 ... strides=(2, 2), padding='valid') 

67 >>> max_pool_2d(x) 

68 <tf.Tensor: shape=(1, 1, 2, 1), dtype=float32, numpy= 

69 array([[[[6.], 

70 [8.]]]], dtype=float32)> 

71 

72 Usage Example: 

73 

74 >>> input_image = tf.constant([[[[1.], [1.], [2.], [4.]], 

75 ... [[2.], [2.], [3.], [2.]], 

76 ... [[4.], [1.], [1.], [1.]], 

77 ... [[2.], [2.], [1.], [4.]]]]) 

78 >>> output = tf.constant([[[[1], [0]], 

79 ... [[0], [1]]]]) 

80 >>> model = tf.keras.models.Sequential() 

81 >>> model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), 

82 ... input_shape=(4, 4, 1))) 

83 >>> model.compile('adam', 'mean_squared_error') 

84 >>> model.predict(input_image, steps=1) 

85 array([[[[2.], 

86 [4.]], 

87 [[4.], 

88 [4.]]]], dtype=float32) 

89 

90 For example, for stride=(1, 1) and padding="same": 

91 

92 >>> x = tf.constant([[1., 2., 3.], 

93 ... [4., 5., 6.], 

94 ... [7., 8., 9.]]) 

95 >>> x = tf.reshape(x, [1, 3, 3, 1]) 

96 >>> max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), 

97 ... strides=(1, 1), padding='same') 

98 >>> max_pool_2d(x) 

99 <tf.Tensor: shape=(1, 3, 3, 1), dtype=float32, numpy= 

100 array([[[[5.], 

101 [6.], 

102 [6.]], 

103 [[8.], 

104 [9.], 

105 [9.]], 

106 [[8.], 

107 [9.], 

108 [9.]]]], dtype=float32)> 

109 

110 Args: 

111 pool_size: integer or tuple of 2 integers, 

112 window size over which to take the maximum. 

113 `(2, 2)` will take the max value over a 2x2 pooling window. 

114 If only one integer is specified, the same window length 

115 will be used for both dimensions. 

116 strides: Integer, tuple of 2 integers, or None. 

117 Strides values. Specifies how far the pooling window moves 

118 for each pooling step. If None, it will default to `pool_size`. 

119 padding: One of `"valid"` or `"same"` (case-insensitive). 

120 `"valid"` means no padding. `"same"` results in padding evenly to 

121 the left/right or up/down of the input such that output has the same 

122 height/width dimension as the input. 

123 data_format: A string, 

124 one of `channels_last` (default) or `channels_first`. 

125 The ordering of the dimensions in the inputs. 

126 `channels_last` corresponds to inputs with shape 

127 `(batch, height, width, channels)` while `channels_first` 

128 corresponds to inputs with shape 

129 `(batch, channels, height, width)`. 

130 It defaults to the `image_data_format` value found in your 

131 Keras config file at `~/.keras/keras.json`. 

132 If you never set it, then it will be "channels_last". 

133 

134 Input shape: 

135 - If `data_format='channels_last'`: 

136 4D tensor with shape `(batch_size, rows, cols, channels)`. 

137 - If `data_format='channels_first'`: 

138 4D tensor with shape `(batch_size, channels, rows, cols)`. 

139 

140 Output shape: 

141 - If `data_format='channels_last'`: 

142 4D tensor with shape `(batch_size, pooled_rows, pooled_cols, channels)`. 

143 - If `data_format='channels_first'`: 

144 4D tensor with shape `(batch_size, channels, pooled_rows, pooled_cols)`. 

145 

146 Returns: 

147 A tensor of rank 4 representing the maximum pooled values. See above for 

148 output shape. 

149 """ 

150 

151 def __init__( 

152 self, 

153 pool_size=(2, 2), 

154 strides=None, 

155 padding="valid", 

156 data_format=None, 

157 **kwargs 

158 ): 

159 super().__init__( 

160 tf.compat.v1.nn.max_pool, 

161 pool_size=pool_size, 

162 strides=strides, 

163 padding=padding, 

164 data_format=data_format, 

165 **kwargs 

166 ) 

167 

168 

169# Alias 

170 

171MaxPool2D = MaxPooling2D 

172