Skip to content

Commit 38f1a10

Browse files
committed
Finish backbone.darknet53() for YOLOv3. SciSharp#360
1 parent 6badc99 commit 38f1a10

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎test/TensorFlowNET.Examples/ImageProcessing/YOLO/YOLOv3.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public YOLOv3(Config cfg_, Tensor input_data_, Tensor trainable_)
5858
{
5959
Tensor route_1, route_2;
6060
(route_1, route_2, input_data) = backbone.darknet53(input_data, trainable);
61+
input_data = common.convolutional(input_data, new[] { 1, 1, 1024, 512 }, trainable, "conv52");
62+
input_data = common.convolutional(input_data, new[] { 3, 3, 512, 1024 }, trainable, "conv53");
63+
input_data = common.convolutional(input_data, new[] { 1, 1, 1024, 512 }, trainable, "conv54");
64+
input_data = common.convolutional(input_data, new[] { 3, 3, 512, 1024 }, trainable, "conv55");
65+
input_data = common.convolutional(input_data, new[] { 1, 1, 1024, 512 }, trainable, "conv56");
66+
67+
var conv_lobj_branch = common.convolutional(input_data, new[] { 3, 3, 512, 1024 }, trainable, name: "conv_lobj_branch");
68+
var conv_lbbox = common.convolutional(conv_lobj_branch, new[] { 1, 1, 1024, 3 * (num_class + 5) },
69+
trainable: trainable, name: "conv_lbbox", activate: false, bn: false);
70+
71+
input_data = common.convolutional(input_data, new[] { 1, 1, 512, 256 }, trainable, "conv57");
72+
input_data = common.upsample(input_data, name: "upsample0", method: upsample_method);
6173

6274
return (conv_lbbox, conv_mbbox, conv_sbbox);
6375
}

‎test/TensorFlowNET.Examples/ImageProcessing/YOLO/backbone.cs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,31 @@ public static (Tensor, Tensor, Tensor) darknet53(Tensor input_data, Tensor train
1818
foreach (var i in range(1))
1919
input_data = common.residual_block(input_data, 64, 32, 64, trainable: trainable, name: $"residual{i + 0}");
2020

21+
input_data = common.convolutional(input_data, filters_shape: new[] { 3, 3, 64, 128 },
22+
trainable: trainable, name: "conv4", downsample: true);
23+
24+
foreach (var i in range(2))
25+
input_data = common.residual_block(input_data, 128, 64, 128, trainable: trainable, name: $"residual{i + 1}");
26+
27+
input_data = common.convolutional(input_data, filters_shape: new[] { 3, 3, 128, 256 },
28+
trainable: trainable, name: "conv9", downsample: true);
29+
30+
foreach (var i in range(8))
31+
input_data = common.residual_block(input_data, 256, 128, 256, trainable: trainable, name: $"residual{i + 3}");
32+
2133
var route_1 = input_data;
34+
input_data = common.convolutional(input_data, filters_shape: new int[] { 3, 3, 256, 512 },
35+
trainable: trainable, name: "conv26", downsample: true);
36+
37+
foreach (var i in range(8))
38+
input_data = common.residual_block(input_data, 512, 256, 512, trainable: trainable, name: $"residual{i + 11}");
39+
2240
var route_2 = input_data;
41+
input_data = common.convolutional(input_data, filters_shape: new[] { 3, 3, 512, 1024 },
42+
trainable: trainable, name: "conv43", downsample: true);
43+
44+
foreach (var i in range(4))
45+
input_data = common.residual_block(input_data, 1024, 512, 1024, trainable: trainable, name: $"residual{i + 19}");
2346

2447
return (route_1, route_2, input_data);
2548
});

‎test/TensorFlowNET.Examples/ImageProcessing/YOLO/common.cs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
35
using System.Text;
46
using Tensorflow;
57
using static Tensorflow.Binding;
@@ -22,7 +24,8 @@ public static Tensor convolutional(Tensor input_data, int[] filters_shape, Tenso
2224
(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);
2325
var paddings = tf.constant(new int[,] { { 0, 0 }, { pad_h, pad_h }, { pad_w, pad_w }, { 0, 0 } });
2426
input_data = tf.pad(input_data, paddings, "CONSTANT");
25-
throw new NotImplementedException("");
27+
strides = new[] { 1, 2, 2, 1 };
28+
padding = "VALID";
2629
}
2730
else
2831
{
@@ -44,7 +47,9 @@ public static Tensor convolutional(Tensor input_data, int[] filters_shape, Tenso
4447
}
4548
else
4649
{
47-
throw new NotImplementedException("");
50+
var bias = tf.get_variable(name: "bias", shape: filters_shape.Last(), trainable: true,
51+
dtype: tf.float32, initializer: tf.constant_initializer(0.0f));
52+
conv = tf.nn.bias_add(conv, bias);
4853
}
4954

5055
if (activate)
@@ -54,6 +59,22 @@ public static Tensor convolutional(Tensor input_data, int[] filters_shape, Tenso
5459
});
5560
}
5661

62+
public static Tensor upsample(Tensor input_data, string name, string method = "deconv")
63+
{
64+
Debug.Assert(new[] { "resize", "deconv" }.Contains(method));
65+
Tensor output = null;
66+
if (method == "resize")
67+
{
68+
69+
}
70+
else if(method == "deconv")
71+
{
72+
73+
}
74+
75+
return output;
76+
}
77+
5778
public static Tensor residual_block(Tensor input_data, int input_channel, int filter_num1,
5879
int filter_num2, Tensor trainable, string name)
5980
{

0 commit comments

Comments
 (0)