From 38f1a1053488dcb1c70ee9c2d37d9e87973ca6b0 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 31 Aug 2019 09:27:06 -0500 Subject: [PATCH] Finish backbone.darknet53() for YOLOv3. #360 --- .../ImageProcessing/YOLO/YOLOv3.cs | 12 +++++++++ .../ImageProcessing/YOLO/backbone.cs | 23 +++++++++++++++++ .../ImageProcessing/YOLO/common.cs | 25 +++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs index de5f0acc..a197aef4 100644 --- a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs +++ b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs @@ -58,6 +58,18 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO { Tensor route_1, route_2; (route_1, route_2, input_data) = backbone.darknet53(input_data, trainable); + input_data = common.convolutional(input_data, new[] { 1, 1, 1024, 512 }, trainable, "conv52"); + input_data = common.convolutional(input_data, new[] { 3, 3, 512, 1024 }, trainable, "conv53"); + input_data = common.convolutional(input_data, new[] { 1, 1, 1024, 512 }, trainable, "conv54"); + input_data = common.convolutional(input_data, new[] { 3, 3, 512, 1024 }, trainable, "conv55"); + input_data = common.convolutional(input_data, new[] { 1, 1, 1024, 512 }, trainable, "conv56"); + + var conv_lobj_branch = common.convolutional(input_data, new[] { 3, 3, 512, 1024 }, trainable, name: "conv_lobj_branch"); + var conv_lbbox = common.convolutional(conv_lobj_branch, new[] { 1, 1, 1024, 3 * (num_class + 5) }, + trainable: trainable, name: "conv_lbbox", activate: false, bn: false); + + input_data = common.convolutional(input_data, new[] { 1, 1, 512, 256 }, trainable, "conv57"); + input_data = common.upsample(input_data, name: "upsample0", method: upsample_method); return (conv_lbbox, conv_mbbox, conv_sbbox); } diff --git a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/backbone.cs b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/backbone.cs index 0e7b1446..6bddae05 100644 --- a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/backbone.cs +++ b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/backbone.cs @@ -18,8 +18,31 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO foreach (var i in range(1)) input_data = common.residual_block(input_data, 64, 32, 64, trainable: trainable, name: $"residual{i + 0}"); + input_data = common.convolutional(input_data, filters_shape: new[] { 3, 3, 64, 128 }, + trainable: trainable, name: "conv4", downsample: true); + + foreach (var i in range(2)) + input_data = common.residual_block(input_data, 128, 64, 128, trainable: trainable, name: $"residual{i + 1}"); + + input_data = common.convolutional(input_data, filters_shape: new[] { 3, 3, 128, 256 }, + trainable: trainable, name: "conv9", downsample: true); + + foreach (var i in range(8)) + input_data = common.residual_block(input_data, 256, 128, 256, trainable: trainable, name: $"residual{i + 3}"); + var route_1 = input_data; + input_data = common.convolutional(input_data, filters_shape: new int[] { 3, 3, 256, 512 }, + trainable: trainable, name: "conv26", downsample: true); + + foreach (var i in range(8)) + input_data = common.residual_block(input_data, 512, 256, 512, trainable: trainable, name: $"residual{i + 11}"); + var route_2 = input_data; + input_data = common.convolutional(input_data, filters_shape: new[] { 3, 3, 512, 1024 }, + trainable: trainable, name: "conv43", downsample: true); + + foreach (var i in range(4)) + input_data = common.residual_block(input_data, 1024, 512, 1024, trainable: trainable, name: $"residual{i + 19}"); return (route_1, route_2, input_data); }); diff --git a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/common.cs b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/common.cs index 52aafa97..a3ffb147 100644 --- a/test/TensorFlowNET.Examples/ImageProcessing/YOLO/common.cs +++ b/test/TensorFlowNET.Examples/ImageProcessing/YOLO/common.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; using System.Text; using Tensorflow; using static Tensorflow.Binding; @@ -22,7 +24,8 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO (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); var paddings = tf.constant(new int[,] { { 0, 0 }, { pad_h, pad_h }, { pad_w, pad_w }, { 0, 0 } }); input_data = tf.pad(input_data, paddings, "CONSTANT"); - throw new NotImplementedException(""); + strides = new[] { 1, 2, 2, 1 }; + padding = "VALID"; } else { @@ -44,7 +47,9 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO } else { - throw new NotImplementedException(""); + var bias = tf.get_variable(name: "bias", shape: filters_shape.Last(), trainable: true, + dtype: tf.float32, initializer: tf.constant_initializer(0.0f)); + conv = tf.nn.bias_add(conv, bias); } if (activate) @@ -54,6 +59,22 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO }); } + public static Tensor upsample(Tensor input_data, string name, string method = "deconv") + { + Debug.Assert(new[] { "resize", "deconv" }.Contains(method)); + Tensor output = null; + if (method == "resize") + { + + } + else if(method == "deconv") + { + + } + + return output; + } + public static Tensor residual_block(Tensor input_data, int input_channel, int filter_num1, int filter_num2, Tensor trainable, string name) {