You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Conv2DTranspose.cs 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using static Tensorflow.Binding;
  15. using Tensorflow.Keras.ArgsDefinition;
  16. using Tensorflow.Keras.Utils;
  17. using static Tensorflow.KerasApi;
  18. using Tensorflow.Keras.Saving;
  19. using Tensorflow.Common.Types;
  20. namespace Tensorflow.Keras.Layers
  21. {
  22. public class Conv2DTranspose : Conv2D
  23. {
  24. public Conv2DTranspose(Conv2DArgs args) : base(InitializeUndefinedArgs(args))
  25. {
  26. }
  27. private static Conv2DArgs InitializeUndefinedArgs(Conv2DArgs args)
  28. {
  29. if (args.Strides is null)
  30. {
  31. args.Strides = (1, 1);
  32. }
  33. if (string.IsNullOrEmpty(args.Padding))
  34. {
  35. args.Padding = "valid";
  36. }
  37. if (args.DilationRate == 0)
  38. {
  39. args.DilationRate = (1, 1);
  40. }
  41. if (args.Groups == 0)
  42. {
  43. args.Groups = 1;
  44. }
  45. if (args.KernelInitializer is null)
  46. {
  47. args.KernelInitializer = tf.glorot_uniform_initializer;
  48. }
  49. if (args.BiasInitializer is null)
  50. {
  51. args.BiasInitializer = tf.zeros_initializer;
  52. }
  53. return args;
  54. }
  55. public override void build(KerasShapesWrapper input_shape)
  56. {
  57. var single_shape = input_shape.ToSingleShape();
  58. if (len(single_shape) != 4)
  59. throw new ValueError($"Inputs should have rank 4. Received input shape: {input_shape}");
  60. var channel_axis = _get_channel_axis();
  61. var input_dim = single_shape[-1];
  62. var kernel_shape = new Shape(kernel_size[0], kernel_size[1], filters, input_dim);
  63. kernel = add_weight(name: "kernel",
  64. shape: kernel_shape,
  65. initializer: kernel_initializer,
  66. regularizer: kernel_regularizer,
  67. trainable: true);
  68. if (use_bias)
  69. bias = add_weight(name: "bias",
  70. shape: filters,
  71. initializer: bias_initializer,
  72. trainable: true);
  73. built = true;
  74. _buildInputShape = input_shape;
  75. }
  76. <<<<<<< HEAD
  77. <<<<<<< HEAD
  78. protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null)
  79. =======
  80. protected override Tensors Call(Tensors inputs, Tensor mask = null, bool? training = null, Tensors initial_state = null, Tensors constants = null)
  81. >>>>>>> master
  82. =======
  83. protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null)
  84. >>>>>>> 90a65d7d98b92f26574ac32392ed802a57d4d2c8
  85. {
  86. var inputs_shape = array_ops.shape(inputs);
  87. var batch_size = inputs_shape[0];
  88. var (h_axis, w_axis) = (1, 2);
  89. if (data_format == "channels_first")
  90. (h_axis, w_axis) = (2, 3);
  91. var (height, width) = (-1, -1);
  92. if(inputs.shape.ndim > -1)
  93. {
  94. var dims = inputs.shape.dims;
  95. (height, width) = ((int)dims[h_axis], (int)dims[w_axis]);
  96. }
  97. var (kernel_h, kernel_w) = kernel_size;
  98. var (stride_h, stride_w) = strides;
  99. var (out_pad_h, out_pad_w) = (-1, -1);
  100. // Infer the dynamic output shape:
  101. var out_height = conv_utils.deconv_output_length(height,
  102. (int)kernel_h,
  103. padding: padding,
  104. output_padding: out_pad_h,
  105. stride: (int)stride_h,
  106. dilation: (int)dilation_rate[0]);
  107. var out_width = conv_utils.deconv_output_length(width,
  108. (int)kernel_w,
  109. padding: padding,
  110. output_padding: out_pad_w,
  111. stride: (int)stride_w,
  112. dilation: (int)dilation_rate[1]);
  113. Tensor output_shape_tensor;
  114. if (data_format == "channels_first")
  115. output_shape_tensor = array_ops.stack(new object[] { batch_size, filters, out_height, out_width });
  116. else
  117. output_shape_tensor = array_ops.stack(new object[] { batch_size, out_height, out_width, filters });
  118. var outputs = keras.backend.conv2d_transpose(
  119. inputs,
  120. kernel,
  121. output_shape_tensor,
  122. strides: strides,
  123. padding: padding,
  124. data_format: data_format,
  125. dilation_rate: dilation_rate);
  126. if (!tf.Context.executing_eagerly())
  127. {
  128. var out_shape = ComputeOutputShape(inputs.shape);
  129. outputs.shape = out_shape;
  130. }
  131. if (use_bias)
  132. tf.nn.bias_add(
  133. outputs,
  134. bias,
  135. data_format: conv_utils.convert_data_format(data_format, ndim: 4));
  136. if (activation != null)
  137. return activation.Apply(outputs);
  138. return outputs;
  139. }
  140. public override Shape ComputeOutputShape(Shape input_shape)
  141. {
  142. var output_shape = input_shape.dims;
  143. var (c_axis, h_axis, w_axis) = (3, 1, 2);
  144. if (data_format == "channels_first")
  145. (c_axis, h_axis, w_axis) = (1, 2, 3);
  146. var (kernel_h, kernel_w) = kernel_size;
  147. var (stride_h, stride_w) = strides;
  148. var (out_pad_h, out_pad_w) = (-1, -1);
  149. output_shape[c_axis] = filters;
  150. output_shape[h_axis] = conv_utils.deconv_output_length(
  151. (int)output_shape[h_axis],
  152. (int)kernel_h,
  153. padding: padding,
  154. output_padding: out_pad_h,
  155. stride: (int)stride_h,
  156. dilation: (int)dilation_rate[0]);
  157. output_shape[w_axis] = conv_utils.deconv_output_length(
  158. (int)output_shape[w_axis],
  159. (int)kernel_w,
  160. padding: padding,
  161. output_padding: out_pad_w,
  162. stride: (int)stride_w,
  163. dilation: (int)dilation_rate[1]);
  164. return new Shape(output_shape);
  165. }
  166. }
  167. }