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

35 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 global pooling 3D layers.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src.engine.base_layer import Layer 

21from keras.src.engine.input_spec import InputSpec 

22from keras.src.utils import conv_utils 

23 

24 

25class GlobalPooling3D(Layer): 

26 """Abstract class for different global pooling 3D layers.""" 

27 

28 def __init__(self, data_format=None, keepdims=False, **kwargs): 

29 super().__init__(**kwargs) 

30 self.data_format = conv_utils.normalize_data_format(data_format) 

31 self.input_spec = InputSpec(ndim=5) 

32 self.keepdims = keepdims 

33 

34 def _validate_reduction_axis(self, input_shape, axes): 

35 for axis in axes: 

36 if input_shape[axis] == 0: 

37 raise ValueError( 

38 f"Incorrect input shape {input_shape} " 

39 f"with dimension 0 at reduction axis {axis}." 

40 ) 

41 

42 def build(self, input_shape): 

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

44 if self.data_format == "channels_last": 

45 self._validate_reduction_axis(input_shape, [1, 2, 3]) 

46 else: 

47 self._validate_reduction_axis(input_shape, [2, 3, 4]) 

48 

49 def compute_output_shape(self, input_shape): 

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

51 if self.data_format == "channels_last": 

52 if self.keepdims: 

53 return tf.TensorShape([input_shape[0], 1, 1, 1, input_shape[4]]) 

54 else: 

55 return tf.TensorShape([input_shape[0], input_shape[4]]) 

56 else: 

57 if self.keepdims: 

58 return tf.TensorShape([input_shape[0], input_shape[1], 1, 1, 1]) 

59 else: 

60 return tf.TensorShape([input_shape[0], input_shape[1]]) 

61 

62 def call(self, inputs): 

63 raise NotImplementedError 

64 

65 def get_config(self): 

66 config = {"data_format": self.data_format, "keepdims": self.keepdims} 

67 base_config = super().get_config() 

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

69