Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/regularization/spatial_dropout2d.py: 45%

22 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"""Contains the SpatialDropout2D layer.""" 

16 

17 

18import tensorflow.compat.v2 as tf 

19 

20from keras.src import backend 

21from keras.src.engine.input_spec import InputSpec 

22from keras.src.layers.regularization.dropout import Dropout 

23 

24# isort: off 

25from tensorflow.python.util.tf_export import keras_export 

26 

27 

28@keras_export("keras.layers.SpatialDropout2D") 

29class SpatialDropout2D(Dropout): 

30 """Spatial 2D version of Dropout. 

31 

32 This version performs the same function as Dropout, however, it drops 

33 entire 2D feature maps instead of individual elements. If adjacent pixels 

34 within feature maps are strongly correlated (as is normally the case in 

35 early convolution layers) then regular dropout will not regularize the 

36 activations and will otherwise just result in an effective learning rate 

37 decrease. In this case, SpatialDropout2D will help promote independence 

38 between feature maps and should be used instead. 

39 

40 Args: 

41 rate: Float between 0 and 1. Fraction of the input units to drop. 

42 data_format: 'channels_first' or 'channels_last'. In 'channels_first' 

43 mode, the channels dimension (the depth) is at index 1, in 

44 'channels_last' mode is it at index 3. It defaults to the 

45 `image_data_format` value found in your Keras config file at 

46 `~/.keras/keras.json`. If you never set it, then it will be 

47 "channels_last". 

48 Call arguments: 

49 inputs: A 4D tensor. 

50 training: Python boolean indicating whether the layer should behave in 

51 training mode (adding dropout) or in inference mode (doing nothing). 

52 Input shape: 

53 4D tensor with shape: `(samples, channels, rows, cols)` if 

54 data_format='channels_first' 

55 or 4D tensor with shape: `(samples, rows, cols, channels)` if 

56 data_format='channels_last'. 

57 Output shape: Same as input. 

58 References: - [Efficient Object Localization Using Convolutional 

59 Networks](https://arxiv.org/abs/1411.4280) 

60 """ 

61 

62 def __init__(self, rate, data_format=None, **kwargs): 

63 super().__init__(rate, **kwargs) 

64 if data_format is None: 

65 data_format = backend.image_data_format() 

66 if data_format not in {"channels_last", "channels_first"}: 

67 raise ValueError( 

68 '`data_format` must be "channels_last" or "channels_first". ' 

69 f"Received: data_format={data_format}." 

70 ) 

71 self.data_format = data_format 

72 self.input_spec = InputSpec(ndim=4) 

73 

74 def _get_noise_shape(self, inputs): 

75 input_shape = tf.shape(inputs) 

76 if self.data_format == "channels_first": 

77 return (input_shape[0], input_shape[1], 1, 1) 

78 elif self.data_format == "channels_last": 

79 return (input_shape[0], 1, 1, input_shape[3]) 

80