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

47 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"""Private base class for pooling 3D layers.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

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 

24 

25 

26class Pooling3D(Layer): 

27 """Pooling layer for arbitrary pooling functions, for 3D inputs. 

28 

29 This class only exists for code reuse. It will never be an exposed API. 

30 

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 3 integers: 

34 (pool_depth, 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 3 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, depth, height, width, channels)` 

49 while `channels_first` corresponds to 

50 inputs with shape `(batch, channels, depth, height, width)`. 

51 name: A string, the name of the layer. 

52 """ 

53 

54 def __init__( 

55 self, 

56 pool_function, 

57 pool_size, 

58 strides, 

59 padding="valid", 

60 data_format="channels_last", 

61 name=None, 

62 **kwargs 

63 ): 

64 super().__init__(name=name, **kwargs) 

65 if data_format is None: 

66 data_format = backend.image_data_format() 

67 if strides is None: 

68 strides = pool_size 

69 self.pool_function = pool_function 

70 self.pool_size = conv_utils.normalize_tuple(pool_size, 3, "pool_size") 

71 self.strides = conv_utils.normalize_tuple( 

72 strides, 3, "strides", allow_zero=True 

73 ) 

74 self.padding = conv_utils.normalize_padding(padding) 

75 self.data_format = conv_utils.normalize_data_format(data_format) 

76 self.input_spec = InputSpec(ndim=5) 

77 

78 def call(self, inputs): 

79 pool_shape = (1,) + self.pool_size + (1,) 

80 strides = (1,) + self.strides + (1,) 

81 

82 if self.data_format == "channels_first": 

83 # TF does not support `channels_first` with 3D pooling operations, 

84 # so we must handle this case manually. 

85 # TODO(fchollet): remove this when TF pooling is feature-complete. 

86 inputs = tf.transpose(inputs, (0, 2, 3, 4, 1)) 

87 

88 outputs = self.pool_function( 

89 inputs, 

90 ksize=pool_shape, 

91 strides=strides, 

92 padding=self.padding.upper(), 

93 ) 

94 

95 if self.data_format == "channels_first": 

96 outputs = tf.transpose(outputs, (0, 4, 1, 2, 3)) 

97 return outputs 

98 

99 def compute_output_shape(self, input_shape): 

100 input_shape = tf.TensorShape(input_shape).as_list() 

101 if self.data_format == "channels_first": 

102 len_dim1 = input_shape[2] 

103 len_dim2 = input_shape[3] 

104 len_dim3 = input_shape[4] 

105 else: 

106 len_dim1 = input_shape[1] 

107 len_dim2 = input_shape[2] 

108 len_dim3 = input_shape[3] 

109 len_dim1 = conv_utils.conv_output_length( 

110 len_dim1, self.pool_size[0], self.padding, self.strides[0] 

111 ) 

112 len_dim2 = conv_utils.conv_output_length( 

113 len_dim2, self.pool_size[1], self.padding, self.strides[1] 

114 ) 

115 len_dim3 = conv_utils.conv_output_length( 

116 len_dim3, self.pool_size[2], self.padding, self.strides[2] 

117 ) 

118 if self.data_format == "channels_first": 

119 return tf.TensorShape( 

120 [input_shape[0], input_shape[1], len_dim1, len_dim2, len_dim3] 

121 ) 

122 else: 

123 return tf.TensorShape( 

124 [input_shape[0], len_dim1, len_dim2, len_dim3, input_shape[4]] 

125 ) 

126 

127 def get_config(self): 

128 config = { 

129 "pool_size": self.pool_size, 

130 "padding": self.padding, 

131 "strides": self.strides, 

132 "data_format": self.data_format, 

133 } 

134 base_config = super().get_config() 

135 return dict(list(base_config.items()) + list(config.items())) 

136