| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e8ff9f0 commit e1db2ba
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ | |||
| 5 | 5 | <AssemblyName>TensorFlow.NET</AssemblyName> | |
| 6 | 6 | <RootNamespace>Tensorflow</RootNamespace> | |
| 7 | 7 | <TargetTensorFlow>1.14.0</TargetTensorFlow> | |
| 8 | - <Version>0.11.0</Version> | ||
| 8 | + <Version>0.11.1</Version> | ||
| 9 | 9 | <Authors>Haiping Chen, Meinrad Recheis</Authors> | |
| 10 | 10 | <Company>SciSharp STACK</Company> | |
| 11 | 11 | <GeneratePackageOnBuild>true</GeneratePackageOnBuild> | |
@@ -17,15 +17,16 @@ | |||
| 17 | 17 | <PackageTags>TensorFlow, NumSharp, SciSharp, MachineLearning, TensorFlow.NET, C#</PackageTags> | |
| 18 | 18 | <Description>Google's TensorFlow full binding in .NET Standard. | |
| 19 | 19 | Docs: https://tensorflownet.readthedocs.io</Description> | |
| 20 | - <AssemblyVersion>0.11.0.0</AssemblyVersion> | ||
| 20 | + <AssemblyVersion>0.11.1.0</AssemblyVersion> | ||
| 21 | 21 | <PackageReleaseNotes>Changes since v0.10.0: | |
| 22 | 22 | 1. Upgrade NumSharp to v0.20. | |
| 23 | 23 | 2. Add DisposableObject class to manage object lifetime. | |
| 24 | 24 | 3. Add tf.no_op, tf.nn.in_top_k, tf.GraphKeys and tf.trainable_variables. | |
| 25 | 25 | 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> | ||
| 27 | 28 | <LangVersion>7.3</LangVersion> | |
| 28 | - <FileVersion>0.11.0.0</FileVersion> | ||
| 29 | + <FileVersion>0.11.1.0</FileVersion> | ||
| 29 | 30 | <PackageLicenseFile>LICENSE</PackageLicenseFile> | |
| 30 | 31 | <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> | |
| 31 | 32 | <SignAssembly>true</SignAssembly> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,9 +68,9 @@ public Session Session() | |||
| 68 | 68 | return defaultSession; | |
| 69 | 69 | } | |
| 70 | 70 | ||
| 71 | - public Session Session(Graph graph) | ||
| 71 | + public Session Session(Graph graph, SessionOptions opts = null) | ||
| 72 | 72 | { | |
| 73 | - return new Session(graph); | ||
| 73 | + return new Session(graph, opts: opts); | ||
| 74 | 74 | } | |
| 75 | 75 | ||
| 76 | 76 | public Session Session(SessionOptions opts) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,17 +1,54 @@ | |||
| 1 | - using System; | ||
| 1 | + using NumSharp; | ||
| 2 | + using System; | ||
| 2 | 3 | using System.Collections.Generic; | |
| 4 | + using System.IO; | ||
| 3 | 5 | using System.Text; | |
| 6 | + using static Tensorflow.Binding; | ||
| 4 | 7 | ||
| 5 | 8 | namespace TensorFlowNET.Examples.ImageProcessing.YOLO | |
| 6 | 9 | { | |
| 7 | 10 | public class Dataset | |
| 8 | 11 | { | |
| 9 | 12 | 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 | + | ||
| 10 | 27 | public int Length = 0; | |
| 11 | 28 | ||
| 12 | 29 | public Dataset(string dataset_type, Config cfg) | |
| 13 | 30 | { | |
| 14 | 31 | 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); | ||
| 15 | 52 | } | |
| 16 | 53 | } | |
| 17 | 54 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO | |||
| 13 | 13 | /// </summary> | |
| 14 | 14 | public class Main : IExample | |
| 15 | 15 | { | |
| 16 | - public bool Enabled { get; set; } = false; | ||
| 16 | + public bool Enabled { get; set; } = true; | ||
| 17 | 17 | public bool IsImportingGraph { get; set; } = false; | |
| 18 | 18 | public string Name => "YOLOv3"; | |
| 19 | 19 | ||
@@ -41,7 +41,10 @@ public class Main : IExample | |||
| 41 | 41 | Tensor true_sbboxes; | |
| 42 | 42 | Tensor true_mbboxes; | |
| 43 | 43 | Tensor true_lbboxes; | |
| 44 | - Tensor trainable; | ||
| 44 | + Tensor trainable; | ||
| 45 | + | ||
| 46 | + Session sess; | ||
| 47 | + YOLOv3 model; | ||
| 45 | 48 | #endregion | |
| 46 | 49 | ||
| 47 | 50 | public bool Run() | |
@@ -50,7 +53,9 @@ public bool Run() | |||
| 50 | 53 | ||
| 51 | 54 | var graph = IsImportingGraph ? ImportGraph() : BuildGraph(); | |
| 52 | 55 | ||
| 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)) | ||
| 54 | 59 | { | |
| 55 | 60 | Train(sess); | |
| 56 | 61 | } | |
@@ -86,7 +91,7 @@ public Graph BuildGraph() | |||
| 86 | 91 | ||
| 87 | 92 | tf_with(tf.name_scope("define_loss"), scope => | |
| 88 | 93 | { | |
| 89 | - //model = new YOLOv3(input_data, trainable); | ||
| 94 | + model = new YOLOv3(cfg, input_data, trainable); | ||
| 90 | 95 | }); | |
| 91 | 96 | ||
| 92 | 97 | return graph; | |
@@ -109,9 +114,7 @@ public void PrepareData() | |||
| 109 | 114 | string dataDir = Path.Combine(Name, "data"); | |
| 110 | 115 | Directory.CreateDirectory(dataDir); | |
| 111 | 116 | ||
| 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); | ||
| 115 | 118 | num_classes = classes.Count; | |
| 116 | 119 | ||
| 117 | 120 | learn_rate_init = cfg.TRAIN.LEARN_RATE_INIT; | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,50 @@ | |||
| 1 | - using System; | ||
| 1 | + using NumSharp; | ||
| 2 | + using System; | ||
| 2 | 3 | using System.Collections.Generic; | |
| 3 | 4 | using System.Text; | |
| 5 | + using Tensorflow; | ||
| 6 | + using static Tensorflow.Binding; | ||
| 4 | 7 | ||
| 5 | 8 | namespace TensorFlowNET.Examples.ImageProcessing.YOLO | |
| 6 | 9 | { | |
| 7 | 10 | public class YOLOv3 | |
| 8 | 11 | { | |
| 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 | + } | ||
| 9 | 49 | } | |
| 10 | 50 | } | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments