Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/pooling/average_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"""Average 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.AveragePooling2D", "keras.layers.AvgPool2D") 

27class AveragePooling2D(Pooling2D): 

28 """Average pooling operation for spatial data. 

29 

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

31 by taking the average 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 when using `"valid"` padding option has a shape 

36 (number of rows or columns) of: 

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

38 (when `input_shape >= pool_size`) 

39 

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

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

42 

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

44 

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

46 ... [4., 5., 6.], 

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

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

49 >>> avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), 

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

51 >>> avg_pool_2d(x) 

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

53 array([[[[3.], 

54 [4.]], 

55 [[6.], 

56 [7.]]]], dtype=float32)> 

57 

58 For example, for `stride=(2, 2)` and `padding="valid"`: 

59 

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

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

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

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

64 >>> avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), 

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

66 >>> avg_pool_2d(x) 

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

68 array([[[[3.5], 

69 [5.5]]]], dtype=float32)> 

70 

71 For example, for `strides=(1, 1)` and `padding="same"`: 

72 

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

74 ... [4., 5., 6.], 

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

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

77 >>> avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), 

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

79 >>> avg_pool_2d(x) 

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

81 array([[[[3.], 

82 [4.], 

83 [4.5]], 

84 [[6.], 

85 [7.], 

86 [7.5]], 

87 [[7.5], 

88 [8.5], 

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

90 

91 Args: 

92 pool_size: integer or tuple of 2 integers, 

93 factors by which to downscale (vertical, horizontal). 

94 `(2, 2)` will halve the input in both spatial dimension. 

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

96 will be used for both dimensions. 

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

98 Strides values. 

99 If None, it will default to `pool_size`. 

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

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

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

103 height/width dimension as the input. 

104 data_format: A string, 

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

106 The ordering of the dimensions in the inputs. 

107 `channels_last` corresponds to inputs with shape 

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

109 corresponds to inputs with shape 

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

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

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

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

114 

115 Input shape: 

116 - If `data_format='channels_last'`: 

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

118 - If `data_format='channels_first'`: 

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

120 

121 Output shape: 

122 - If `data_format='channels_last'`: 

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

124 - If `data_format='channels_first'`: 

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

126 """ 

127 

128 def __init__( 

129 self, 

130 pool_size=(2, 2), 

131 strides=None, 

132 padding="valid", 

133 data_format=None, 

134 **kwargs 

135 ): 

136 super().__init__( 

137 tf.nn.avg_pool, 

138 pool_size=pool_size, 

139 strides=strides, 

140 padding=padding, 

141 data_format=data_format, 

142 **kwargs 

143 ) 

144 

145 

146# Alias 

147 

148AvgPool2D = AveragePooling2D 

149