FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

common.convolutional, residual_block #359 · feelsyt/TensorFlow.NET@e1db2ba · GitHub

Commit e1db2ba

Browse files
committed
common.convolutional, residual_block SciSharp#359
1 parent e8ff9f0 commit e1db2ba

9 files changed

Lines changed: 260 additions & 16 deletions

File tree

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<AssemblyName>TensorFlow.NET</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>1.14.0</TargetTensorFlow>
8-
<Version>0.11.0</Version>
8+
<Version>0.11.1</Version>
99
<Authors>Haiping Chen, Meinrad Recheis</Authors>
1010
<Company>SciSharp STACK</Company>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -17,15 +17,16 @@
1717
<PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags>
1818
<Description>Google's TensorFlow full binding in .NET Standard.
1919
Docs: https://tensorflownet.readthedocs.io</Description>
20-
<AssemblyVersion>0.11.0.0</AssemblyVersion>
20+
<AssemblyVersion>0.11.1.0</AssemblyVersion>
2121
<PackageReleaseNotes>Changes since v0.10.0:
2222
1. Upgrade NumSharp to v0.20.
2323
2. Add DisposableObject class to manage object lifetime.
2424
3. Add tf.no_op, tf.nn.in_top_k, tf.GraphKeys and tf.trainable_variables.
2525
4. Change tensorflow to non-static class in order to execute some initialization process.
26-
5. Overloade session.run(), make syntax simpler.</PackageReleaseNotes>
26+
5. Overload session.run(), make syntax simpler.
27+
6. Add Local Response Normalization.</PackageReleaseNotes>
2728
<LangVersion>7.3</LangVersion>
28-
<FileVersion>0.11.0.0</FileVersion>
29+
<FileVersion>0.11.1.0</FileVersion>
2930
<PackageLicenseFile>LICENSE</PackageLicenseFile>
3031
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
3132
<SignAssembly>true</SignAssembly>

‎src/TensorFlowNET.Core/tensorflow.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public Session Session()
6868
return defaultSession;
6969
}
7070

71-
public Session Session(Graph graph)
71+
public Session Session(Graph graph, SessionOptions opts = null)
7272
{
73-
return new Session(graph);
73+
return new Session(graph, opts: opts);
7474
}
7575

7676
public Session Session(SessionOptions opts)
Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,54 @@
1-
using System;
1+
using NumSharp;
2+
using System;
23
using System.Collections.Generic;
4+
using System.IO;
35
using System.Text;
6+
using static Tensorflow.Binding;
47

58
namespace TensorFlowNET.Examples.ImageProcessing.YOLO
69
{
710
public class Dataset
811
{
912
string annot_path;
13+
int[] input_sizes;
14+
int batch_size;
15+
bool data_aug;
16+
int[] train_input_sizes;
17+
NDArray strides;
18+
NDArray anchors;
19+
Dictionary<int, string> classes;
20+
int num_classes;
21+
int anchor_per_scale;
22+
int max_bbox_per_scale;
23+
string[] annotations;
24+
int num_samples;
25+
int batch_count;
26+
1027
public int Length = 0;
1128

1229
public Dataset(string dataset_type, Config cfg)
1330
{
1431
annot_path = dataset_type == "train" ? cfg.TRAIN.ANNOT_PATH : cfg.TEST.ANNOT_PATH;
32+
input_sizes = dataset_type == "train" ? cfg.TRAIN.INPUT_SIZE : cfg.TEST.INPUT_SIZE;
33+
batch_size = dataset_type == "train" ? cfg.TRAIN.BATCH_SIZE : cfg.TEST.BATCH_SIZE;
34+
data_aug = dataset_type == "train" ? cfg.TRAIN.DATA_AUG : cfg.TEST.DATA_AUG;
35+
train_input_sizes = cfg.TRAIN.INPUT_SIZE;
36+
strides = np.array(cfg.YOLO.STRIDES);
37+
38+
classes = Utils.read_class_names(cfg.YOLO.CLASSES);
39+
num_classes = classes.Count;
40+
anchors = np.array(Utils.get_anchors(cfg.YOLO.ANCHORS));
41+
anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE;
42+
max_bbox_per_scale = 150;
43+
44+
annotations = load_annotations();
45+
num_samples = len(annotations);
46+
batch_count = 0;
47+
}
48+
49+
string[] load_annotations()
50+
{
51+
return File.ReadAllLines(annot_path);
1552
}
1653
}
1754
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO
1313
/// </summary>
1414
public class Main : IExample
1515
{
16-
public bool Enabled { get; set; } = false;
16+
public bool Enabled { get; set; } = true;
1717
public bool IsImportingGraph { get; set; } = false;
1818
public string Name => "YOLOv3";
1919

@@ -41,7 +41,10 @@ public class Main : IExample
4141
Tensor true_sbboxes;
4242
Tensor true_mbboxes;
4343
Tensor true_lbboxes;
44-
Tensor trainable;
44+
Tensor trainable;
45+
46+
Session sess;
47+
YOLOv3 model;
4548
#endregion
4649

4750
public bool Run()
@@ -50,7 +53,9 @@ public bool Run()
5053

5154
var graph = IsImportingGraph ? ImportGraph() : BuildGraph();
5255

53-
using (var sess = tf.Session(graph))
56+
var options = new SessionOptions();
57+
options.SetConfig(new ConfigProto { AllowSoftPlacement = true });
58+
using (var sess = tf.Session(graph, opts: options))
5459
{
5560
Train(sess);
5661
}
@@ -86,7 +91,7 @@ public Graph BuildGraph()
8691

8792
tf_with(tf.name_scope("define_loss"), scope =>
8893
{
89-
//model = new YOLOv3(input_data, trainable);
94+
model = new YOLOv3(cfg, input_data, trainable);
9095
});
9196

9297
return graph;
@@ -109,9 +114,7 @@ public void PrepareData()
109114
string dataDir = Path.Combine(Name, "data");
110115
Directory.CreateDirectory(dataDir);
111116

112-
classes = new Dictionary<int, string>();
113-
foreach (var line in File.ReadAllLines(cfg.YOLO.CLASSES))
114-
classes[classes.Count] = line;
117+
classes = Utils.read_class_names(cfg.YOLO.CLASSES);
115118
num_classes = classes.Count;
116119

117120
learn_rate_init = cfg.TRAIN.LEARN_RATE_INIT;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NumSharp;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace TensorFlowNET.Examples.ImageProcessing.YOLO
9+
{
10+
class Utils
11+
{
12+
public static Dictionary<int, string> read_class_names(string file)
13+
{
14+
var classes = new Dictionary<int, string>();
15+
foreach (var line in File.ReadAllLines(file))
16+
classes[classes.Count] = line;
17+
return classes;
18+
}
19+
20+
public static NDArray get_anchors(string file)
21+
{
22+
return np.array(File.ReadAllText(file).Split(',')
23+
.Select(x => float.Parse(x))
24+
.ToArray()).reshape(3, 3, 2);
25+
}
26+
}
27+
}
Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
1-
using System;
1+
using NumSharp;
2+
using System;
23
using System.Collections.Generic;
34
using System.Text;
5+
using Tensorflow;
6+
using static Tensorflow.Binding;
47

58
namespace TensorFlowNET.Examples.ImageProcessing.YOLO
69
{
710
public class YOLOv3
811
{
12+
Config cfg;
13+
Tensor trainable;
14+
Tensor input_data;
15+
Dictionary<int, string> classes;
16+
int num_class;
17+
NDArray strides;
18+
NDArray anchors;
19+
int anchor_per_scale;
20+
float iou_loss_thresh;
21+
string upsample_method;
22+
Tensor conv_lbbox;
23+
Tensor conv_mbbox;
24+
Tensor conv_sbbox;
25+
26+
public YOLOv3(Config cfg_, Tensor input_data_, Tensor trainable_)
27+
{
28+
cfg = cfg_;
29+
input_data = input_data_;
30+
trainable = trainable_;
31+
classes = Utils.read_class_names(cfg.YOLO.CLASSES);
32+
num_class = len(classes);
33+
strides = np.array(cfg.YOLO.STRIDES);
34+
anchors = Utils.get_anchors(cfg.YOLO.ANCHORS);
35+
anchor_per_scale = cfg.YOLO.ANCHOR_PER_SCALE;
36+
iou_loss_thresh = cfg.YOLO.IOU_LOSS_THRESH;
37+
upsample_method = cfg.YOLO.UPSAMPLE_METHOD;
38+
39+
(conv_lbbox, conv_mbbox, conv_sbbox) = __build_nework(input_data);
40+
}
41+
42+
private (Tensor, Tensor, Tensor) __build_nework(Tensor input_data)
43+
{
44+
Tensor route_1, route_2;
45+
(route_1, route_2, input_data) = backbone.darknet53(input_data, trainable);
46+
47+
return (conv_lbbox, conv_mbbox, conv_sbbox);
48+
}
949
}
1050
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow;
5+
using static Tensorflow.Binding;
6+
7+
namespace TensorFlowNET.Examples.ImageProcessing.YOLO
8+
{
9+
class backbone
10+
{
11+
public static (Tensor, Tensor, Tensor) darknet53(Tensor input_data, Tensor trainable)
12+
{
13+
return tf_with(tf.variable_scope("darknet"), scope =>
14+
{
15+
input_data = common.convolutional(input_data, filters_shape: new int[] { 3, 3, 3, 32 }, trainable: trainable, name: "conv0");
16+
input_data = common.convolutional(input_data, filters_shape: new int[] { 3, 3, 32, 64 }, trainable: trainable, name: "conv1", downsample: true);
17+
18+
foreach (var i in range(1))
19+
input_data = common.residual_block(input_data, 64, 32, 64, trainable: trainable, name: $"residual{i + 0}");
20+
21+
var route_1 = input_data;
22+
var route_2 = input_data;
23+
24+
return (route_1, route_2, input_data);
25+
});
26+
}
27+
}
28+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow;
5+
using static Tensorflow.Binding;
6+
7+
namespace TensorFlowNET.Examples.ImageProcessing.YOLO
8+
{
9+
class common
10+
{
11+
public static Tensor convolutional(Tensor input_data, int[] filters_shape, Tensor trainable,
12+
string name, bool downsample = false, bool activate = true,
13+
bool bn = true)
14+
{
15+
return tf_with(tf.variable_scope(name), scope =>
16+
{
17+
int[] strides;
18+
string padding;
19+
20+
if (downsample)
21+
{
22+
throw new NotImplementedException("");
23+
}
24+
else
25+
{
26+
strides = new int[] { 1, 1, 1, 1 };
27+
padding = "SAME";
28+
}
29+
30+
var weight = tf.get_variable(name: "weight", dtype: tf.float32, trainable: true,
31+
shape: filters_shape, initializer: tf.random_normal_initializer(stddev: 0.01f));
32+
33+
var conv = tf.nn.conv2d(input: input_data, filter: weight, strides: strides, padding: padding);
34+
35+
if (bn)
36+
{
37+
conv = tf.layers.batch_normalization(conv, beta_initializer: tf.zeros_initializer,
38+
gamma_initializer: tf.ones_initializer,
39+
moving_mean_initializer: tf.zeros_initializer,
40+
moving_variance_initializer: tf.ones_initializer, training: trainable);
41+
}
42+
else
43+
{
44+
throw new NotImplementedException("");
45+
}
46+
47+
if (activate)
48+
conv = tf.nn.leaky_relu(conv, alpha: 0.1f);
49+
50+
return conv;
51+
});
52+
}
53+
54+
public static Tensor residual_block(Tensor input_data, int input_channel, int filter_num1,
55+
int filter_num2, Tensor trainable, string name)
56+
{
57+
var short_cut = input_data;
58+
59+
return tf_with(tf.variable_scope(name), scope =>
60+
{
61+
input_data = convolutional(input_data, filters_shape: new int[] { 1, 1, input_channel, filter_num1 },
62+
trainable: trainable, name: "conv1");
63+
input_data = convolutional(input_data, filters_shape: new int[] { 3, 3, filter_num1, filter_num2 },
64+
trainable: trainable, name: "conv2");
65+
66+
var residual_output = input_data + short_cut;
67+
68+
return residual_output;
69+
});
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL