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

11 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"""Global average pooling 3D layer.""" 

16 

17 

18from keras.src import backend 

19from keras.src.layers.pooling.base_global_pooling3d import GlobalPooling3D 

20 

21# isort: off 

22from tensorflow.python.util.tf_export import keras_export 

23 

24 

25@keras_export( 

26 "keras.layers.GlobalAveragePooling3D", "keras.layers.GlobalAvgPool3D" 

27) 

28class GlobalAveragePooling3D(GlobalPooling3D): 

29 """Global Average pooling operation for 3D data. 

30 

31 Args: 

32 data_format: A string, 

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

34 The ordering of the dimensions in the inputs. 

35 `channels_last` corresponds to inputs with shape 

36 `(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)` 

37 while `channels_first` corresponds to inputs with shape 

38 `(batch, channels, spatial_dim1, spatial_dim2, spatial_dim3)`. 

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

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

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

42 keepdims: A boolean, whether to keep the spatial dimensions or not. 

43 If `keepdims` is `False` (default), the rank of the tensor is reduced 

44 for spatial dimensions. 

45 If `keepdims` is `True`, the spatial dimensions are retained with 

46 length 1. 

47 The behavior is the same as for `tf.reduce_mean` or `np.mean`. 

48 

49 Input shape: 

50 - If `data_format='channels_last'`: 

51 5D tensor with shape: 

52 `(batch_size, spatial_dim1, spatial_dim2, spatial_dim3, channels)` 

53 - If `data_format='channels_first'`: 

54 5D tensor with shape: 

55 `(batch_size, channels, spatial_dim1, spatial_dim2, spatial_dim3)` 

56 

57 Output shape: 

58 - If `keepdims`=False: 

59 2D tensor with shape `(batch_size, channels)`. 

60 - If `keepdims`=True: 

61 - If `data_format='channels_last'`: 

62 5D tensor with shape `(batch_size, 1, 1, 1, channels)` 

63 - If `data_format='channels_first'`: 

64 5D tensor with shape `(batch_size, channels, 1, 1, 1)` 

65 """ 

66 

67 def call(self, inputs): 

68 if self.data_format == "channels_last": 

69 return backend.mean(inputs, axis=[1, 2, 3], keepdims=self.keepdims) 

70 else: 

71 return backend.mean(inputs, axis=[2, 3, 4], keepdims=self.keepdims) 

72 

73 

74# Alias 

75 

76GlobalAvgPool3D = GlobalAveragePooling3D 

77