Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/layers/reshaping/flatten.py: 28%

53 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 flatten layer.""" 

16 

17 

18import functools 

19import operator 

20 

21import numpy as np 

22import tensorflow.compat.v2 as tf 

23 

24from keras.src.engine.base_layer import Layer 

25from keras.src.engine.input_spec import InputSpec 

26from keras.src.utils import conv_utils 

27 

28# isort: off 

29from tensorflow.python.util.tf_export import keras_export 

30 

31 

32@keras_export("keras.layers.Flatten") 

33class Flatten(Layer): 

34 """Flattens the input. Does not affect the batch size. 

35 

36 Note: If inputs are shaped `(batch,)` without a feature axis, then 

37 flattening adds an extra channel dimension and output shape is `(batch, 1)`. 

38 

39 Args: 

40 data_format: A string, 

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

42 The ordering of the dimensions in the inputs. 

43 `channels_last` corresponds to inputs with shape 

44 `(batch, ..., channels)` while `channels_first` corresponds to 

45 inputs with shape `(batch, channels, ...)`. 

46 When unspecified, uses 

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

48 `~/.keras/keras.json` (if exists) else 'channels_last'. 

49 Defaults to 'channels_last'. 

50 

51 Example: 

52 

53 >>> model = tf.keras.Sequential() 

54 >>> model.add(tf.keras.layers.Conv2D(64, 3, 3, input_shape=(3, 32, 32))) 

55 >>> model.output_shape 

56 (None, 1, 10, 64) 

57 

58 >>> model.add(Flatten()) 

59 >>> model.output_shape 

60 (None, 640) 

61 

62 """ 

63 

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

65 super().__init__(**kwargs) 

66 self.data_format = conv_utils.normalize_data_format(data_format) 

67 self.input_spec = InputSpec(min_ndim=1) 

68 self._channels_first = self.data_format == "channels_first" 

69 

70 def call(self, inputs): 

71 if self._channels_first: 

72 rank = inputs.shape.rank 

73 if rank and rank > 1: 

74 # Switch to channels-last format. 

75 permutation = [0] 

76 permutation.extend(range(2, rank)) 

77 permutation.append(1) 

78 inputs = tf.transpose(inputs, perm=permutation) 

79 

80 if tf.executing_eagerly(): 

81 # Full static shape is guaranteed to be available. 

82 # Performance: Using `constant_op` is much faster than passing a 

83 # list. 

84 flattened_shape = tf.constant([inputs.shape[0], -1]) 

85 return tf.reshape(inputs, flattened_shape) 

86 else: 

87 input_shape = inputs.shape 

88 rank = input_shape.rank 

89 if rank == 1: 

90 return tf.expand_dims(inputs, axis=1) 

91 else: 

92 batch_dim = tf.compat.dimension_value(input_shape[0]) 

93 non_batch_dims = input_shape[1:] 

94 # Reshape in a way that preserves as much shape info as 

95 # possible. 

96 if non_batch_dims.is_fully_defined(): 

97 last_dim = int( 

98 functools.reduce(operator.mul, non_batch_dims) 

99 ) 

100 flattened_shape = tf.constant([-1, last_dim]) 

101 elif batch_dim is not None: 

102 flattened_shape = tf.constant([int(batch_dim), -1]) 

103 else: 

104 flattened_shape = [tf.shape(inputs)[0], -1] 

105 return tf.reshape(inputs, flattened_shape) 

106 

107 def compute_output_shape(self, input_shape): 

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

109 if not input_shape: 

110 output_shape = tf.TensorShape([1]) 

111 else: 

112 output_shape = [input_shape[0]] 

113 if np.all(input_shape[1:]): 

114 output_shape += [np.prod(input_shape[1:], dtype=int)] 

115 else: 

116 output_shape += [None] 

117 return tf.TensorShape(output_shape) 

118 

119 def get_config(self): 

120 config = super().get_config() 

121 config.update({"data_format": self.data_format}) 

122 return config 

123