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 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. namespace Tensorflow.Keras.Layers
  19. {
  20. public class Conv2DTranspose : Conv2D
  21. {
  22. public Conv2DTranspose(Conv2DArgs args) : base(args)
  23. {
  24. }
  25. protected override void build(Tensors inputs)
  26. {
  27. var input_shape = inputs.shape;
  28. if (len(input_shape) != 4)
  29. throw new ValueError($"Inputs should have rank 4. Received input shape: {input_shape}");
  30. var channel_axis = _get_channel_axis();
  31. var input_dim = input_shape[-1];
  32. var kernel_shape = new TensorShape(kernel_size[0], kernel_size[1], filters, input_dim);
  33. kernel = add_weight(name: "kernel",
  34. shape: kernel_shape,
  35. initializer: kernel_initializer,
  36. regularizer: kernel_regularizer,
  37. trainable: true,
  38. dtype: inputs.dtype);
  39. if (use_bias)
  40. bias = add_weight(name: "bias",
  41. shape: filters,
  42. initializer: bias_initializer,
  43. trainable: true,
  44. dtype: inputs.dtype);
  45. built = true;
  46. }
  47. protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
  48. {
  49. var inputs_shape = array_ops.shape(inputs);
  50. var batch_size = inputs_shape[0];
  51. var (h_axis, w_axis) = (1, 2);
  52. if (data_format == "channels_first")
  53. (h_axis, w_axis) = (2, 3);
  54. var (height, width) = (-1, -1);
  55. if(inputs.shape.rank > -1)
  56. {
  57. var dims = inputs.shape.dims;
  58. (height, width) = ((int)dims[h_axis], (int)dims[w_axis]);
  59. }
  60. var (kernel_h, kernel_w) = kernel_size;
  61. var (stride_h, stride_w) = strides;
  62. var (out_pad_h, out_pad_w) = (-1, -1);
  63. // Infer the dynamic output shape:
  64. var out_height = conv_utils.deconv_output_length(height,
  65. (int)kernel_h,
  66. padding: padding,
  67. output_padding: out_pad_h,
  68. stride: (int)stride_h,
  69. dilation: (int)dilation_rate[0]);
  70. var out_width = conv_utils.deconv_output_length(width,
  71. (int)kernel_w,
  72. padding: padding,
  73. output_padding: out_pad_w,
  74. stride: (int)stride_w,
  75. dilation: (int)dilation_rate[1]);
  76. Tensor output_shape_tensor;
  77. if (data_format == "channels_first")
  78. output_shape_tensor = array_ops.stack(new object[] { batch_size, filters, out_height, out_width });
  79. else
  80. output_shape_tensor = array_ops.stack(new object[] { batch_size, out_height, out_width, filters });
  81. var outputs = keras.backend.conv2d_transpose(
  82. inputs,
  83. kernel,
  84. output_shape_tensor,
  85. strides: strides,
  86. padding: padding,
  87. data_format: data_format,
  88. dilation_rate: dilation_rate);
  89. if (!tf.Context.executing_eagerly())
  90. {
  91. var out_shape = ComputeOutputShape(inputs.shape);
  92. outputs.set_shape(out_shape);
  93. }
  94. if (use_bias)
  95. throw new NotImplementedException("");
  96. if (activation != null)
  97. return activation(outputs);
  98. return outputs;
  99. }
  100. public override TensorShape ComputeOutputShape(TensorShape input_shape)
  101. {
  102. var output_shape = input_shape.dims;
  103. var (c_axis, h_axis, w_axis) = (3, 1, 2);
  104. if (data_format == "channels_first")
  105. (c_axis, h_axis, w_axis) = (1, 2, 3);
  106. var (kernel_h, kernel_w) = kernel_size;
  107. var (stride_h, stride_w) = strides;
  108. var (out_pad_h, out_pad_w) = (-1, -1);
  109. output_shape[c_axis] = filters;
  110. output_shape[h_axis] = conv_utils.deconv_output_length(
  111. (int)output_shape[h_axis],
  112. (int)kernel_h,
  113. padding: padding,
  114. output_padding: out_pad_h,
  115. stride: (int)stride_h,
  116. dilation: (int)dilation_rate[0]);
  117. output_shape[w_axis] = conv_utils.deconv_output_length(
  118. (int)output_shape[w_axis],
  119. (int)kernel_w,
  120. padding: padding,
  121. output_padding: out_pad_w,
  122. stride: (int)stride_w,
  123. dilation: (int)dilation_rate[1]);
  124. return new TensorShape(output_shape);
  125. }
  126. }
  127. }