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.

common.cs 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using Tensorflow;
  7. using static Tensorflow.Binding;
  8. namespace TensorFlowNET.Examples.ImageProcessing.YOLO
  9. {
  10. class common
  11. {
  12. public static Tensor convolutional(Tensor input_data, int[] filters_shape, Tensor trainable,
  13. string name, bool downsample = false, bool activate = true,
  14. bool bn = true)
  15. {
  16. return tf_with(tf.variable_scope(name), scope =>
  17. {
  18. int[] strides;
  19. string padding;
  20. if (downsample)
  21. {
  22. (int pad_h, int pad_w) = ((int)Math.Floor((filters_shape[0] - 2) / 2.0f) + 1, (int)Math.Floor((filters_shape[1] - 2) / 2.0f) + 1);
  23. var paddings = tf.constant(new int[,] { { 0, 0 }, { pad_h, pad_h }, { pad_w, pad_w }, { 0, 0 } });
  24. input_data = tf.pad(input_data, paddings, "CONSTANT");
  25. strides = new[] { 1, 2, 2, 1 };
  26. padding = "VALID";
  27. }
  28. else
  29. {
  30. strides = new int[] { 1, 1, 1, 1 };
  31. padding = "SAME";
  32. }
  33. var weight = tf.get_variable(name: "weight", dtype: tf.float32, trainable: true,
  34. shape: filters_shape, initializer: tf.random_normal_initializer(stddev: 0.01f));
  35. var conv = tf.nn.conv2d(input: input_data, filter: weight, strides: strides, padding: padding);
  36. if (bn)
  37. {
  38. conv = tf.layers.batch_normalization(conv, beta_initializer: tf.zeros_initializer,
  39. gamma_initializer: tf.ones_initializer,
  40. moving_mean_initializer: tf.zeros_initializer,
  41. moving_variance_initializer: tf.ones_initializer, training: trainable);
  42. }
  43. else
  44. {
  45. var bias = tf.get_variable(name: "bias", shape: filters_shape.Last(), trainable: true,
  46. dtype: tf.float32, initializer: tf.constant_initializer(0.0f));
  47. conv = tf.nn.bias_add(conv, bias);
  48. }
  49. if (activate)
  50. conv = tf.nn.leaky_relu(conv, alpha: 0.1f);
  51. return conv;
  52. });
  53. }
  54. public static Tensor upsample(Tensor input_data, string name, string method = "deconv")
  55. {
  56. Debug.Assert(new[] { "resize", "deconv" }.Contains(method));
  57. Tensor output = null;
  58. if (method == "resize")
  59. {
  60. }
  61. else if(method == "deconv")
  62. {
  63. }
  64. return output;
  65. }
  66. public static Tensor residual_block(Tensor input_data, int input_channel, int filter_num1,
  67. int filter_num2, Tensor trainable, string name)
  68. {
  69. var short_cut = input_data;
  70. return tf_with(tf.variable_scope(name), scope =>
  71. {
  72. input_data = convolutional(input_data, filters_shape: new int[] { 1, 1, input_channel, filter_num1 },
  73. trainable: trainable, name: "conv1");
  74. input_data = convolutional(input_data, filters_shape: new int[] { 3, 3, filter_num1, filter_num2 },
  75. trainable: trainable, name: "conv2");
  76. var residual_output = input_data + short_cut;
  77. return residual_output;
  78. });
  79. }
  80. }
  81. }