Skip to content

Commit e3de673

Browse files
committed
fix StringVar testing.
1 parent 6c8c2e5 commit e3de673

8 files changed

Lines changed: 105 additions & 13 deletions

File tree

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
<Description>Google's TensorFlow full binding in .NET Standard.
1919
Docs: https://tensorflownet.readthedocs.io</Description>
2020
<AssemblyVersion>0.11.0.0</AssemblyVersion>
21-
<PackageReleaseNotes>Changes since v0.10.0:</PackageReleaseNotes>
21+
<PackageReleaseNotes>Changes since v0.10.0:
22+
1. Upgrade NumSharp to v0.20.
23+
2. Add DisposableObject class to manage object lifetime.
24+
3. Add tf.no_op, tf.nn.in_top_k, tf.GraphKeys and tf.trainable_variables.
25+
4. Change tensorflow to non-static class in order to execute some initialization process.
26+
5. Overloade session.run(), make syntax simpler.</PackageReleaseNotes>
2227
<LangVersion>7.3</LangVersion>
2328
<FileVersion>0.11.0.0</FileVersion>
2429
<PackageLicenseFile>LICENSE</PackageLicenseFile>

src/TensorFlowNET.Core/Variables/RefVariable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void _init_from_args(object initial_value,
158158
// Or get the initial value from a Tensor or Python object.
159159
else
160160
{
161-
_initial_value = ops.convert_to_tensor(initial_value, name: "initial_value");
161+
_initial_value = ops.convert_to_tensor(initial_value, name: "initial_value", dtype: dtype);
162162

163163
var shape = _initial_value.shape;
164164
dtype = _initial_value.dtype;

src/TensorFlowNET.Core/ops.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ public static Tensor internal_convert_to_tensor(object value, TF_DataType dtype
488488

489489
switch (value)
490490
{
491+
case String str:
492+
return constant_op.constant(str, dtype: TF_DataType.TF_STRING, name: name);
491493
case NDArray nd:
492494
return constant_op.constant(nd, dtype: dtype, name: name);
493495
case Tensor tensor:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TensorFlowNET.Examples.ImageProcessing.YOLO
6+
{
7+
public class Dataset
8+
{
9+
string annot_path;
10+
public int Length = 0;
11+
12+
public Dataset(string dataset_type, Config cfg)
13+
{
14+
annot_path = dataset_type == "train" ? cfg.TRAIN.ANNOT_PATH : cfg.TEST.ANNOT_PATH;
15+
}
16+
}
17+
}

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

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

20+
#region args
2121
Dictionary<int, string> classes;
22-
Config config;
22+
int num_classes;
23+
float learn_rate_init;
24+
float learn_rate_end;
25+
int first_stage_epochs;
26+
int second_stage_epochs;
27+
int warmup_periods;
28+
string time;
29+
float moving_ave_decay;
30+
int max_bbox_per_scale;
31+
int steps_per_period;
32+
33+
Dataset trainset, testset;
34+
35+
Config cfg;
2336

2437
Tensor input_data;
2538
Tensor label_sbbox;
@@ -28,7 +41,8 @@ public class Main : IExample
2841
Tensor true_sbboxes;
2942
Tensor true_mbboxes;
3043
Tensor true_lbboxes;
31-
Tensor trainable;
44+
Tensor trainable;
45+
#endregion
3246

3347
public bool Run()
3448
{
@@ -90,14 +104,28 @@ public void Predict(Session sess)
90104

91105
public void PrepareData()
92106
{
93-
config = new Config(Name);
107+
cfg = new Config(Name);
94108

95109
string dataDir = Path.Combine(Name, "data");
96110
Directory.CreateDirectory(dataDir);
97111

98112
classes = new Dictionary<int, string>();
99-
foreach (var line in File.ReadAllLines(config.CLASSES))
113+
foreach (var line in File.ReadAllLines(cfg.YOLO.CLASSES))
100114
classes[classes.Count] = line;
115+
num_classes = classes.Count;
116+
117+
learn_rate_init = cfg.TRAIN.LEARN_RATE_INIT;
118+
learn_rate_end = cfg.TRAIN.LEARN_RATE_END;
119+
first_stage_epochs = cfg.TRAIN.FISRT_STAGE_EPOCHS;
120+
second_stage_epochs = cfg.TRAIN.SECOND_STAGE_EPOCHS;
121+
warmup_periods = cfg.TRAIN.WARMUP_EPOCHS;
122+
DateTime now = DateTime.Now;
123+
time = $"{now.Year}-{now.Month}-{now.Day}-{now.Hour}-{now.Minute}-{now.Minute}";
124+
moving_ave_decay = cfg.YOLO.MOVING_AVE_DECAY;
125+
max_bbox_per_scale = 150;
126+
trainset = new Dataset("train", cfg);
127+
testset = new Dataset("test", cfg);
128+
steps_per_period = trainset.Length;
101129
}
102130
}
103131
}

test/TensorFlowNET.Examples/ImageProcessing/YOLO/config.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,52 @@ namespace TensorFlowNET.Examples.ImageProcessing.YOLO
77
{
88
public class Config
99
{
10-
string _root;
11-
public string CLASSES;
10+
public YoloConfig YOLO;
11+
public TrainConfig TRAIN;
12+
public TrainConfig TEST;
1213

1314
public Config(string root)
1415
{
15-
_root = root;
16-
CLASSES = Path.Combine(_root, "data", "classes", "coco.names");
16+
YOLO = new YoloConfig(root);
17+
TRAIN = new TrainConfig(root);
18+
}
19+
20+
public class YoloConfig
21+
{
22+
string _root;
23+
24+
public string CLASSES;
25+
public float MOVING_AVE_DECAY = 0.9995f;
26+
public int[] STRIDES = new int[] { 8, 16, 32 };
27+
28+
public YoloConfig(string root)
29+
{
30+
_root = root;
31+
CLASSES = Path.Combine(_root, "data", "classes", "coco.names");
32+
}
33+
}
34+
35+
public class TrainConfig
36+
{
37+
string _root;
38+
39+
public int BATCH_SIZE = 6;
40+
public int[] INPUT_SIZE = new int[] { 320, 352, 384, 416, 448, 480, 512, 544, 576, 608 };
41+
public bool DATA_AUG = true;
42+
public float LEARN_RATE_INIT = 1e-4f;
43+
public float LEARN_RATE_END = 1e-6f;
44+
public int WARMUP_EPOCHS = 2;
45+
public int FISRT_STAGE_EPOCHS = 20;
46+
public int SECOND_STAGE_EPOCHS = 30;
47+
public string INITIAL_WEIGHT;
48+
public string ANNOT_PATH;
49+
50+
public TrainConfig(string root)
51+
{
52+
_root = root;
53+
INITIAL_WEIGHT = Path.Combine(_root, "data", "checkpoint", "yolov3_coco_demo.ckpt");
54+
ANNOT_PATH = Path.Combine(_root, "data", "dataset", "voc_train.txt");
55+
}
1756
}
1857
}
1958
}

test/TensorFlowNET.UnitTest/VariableTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void Initializer()
2424
[TestMethod]
2525
public void StringVar()
2626
{
27-
var mammal1 = tf.Variable("Elephant", name: "var1", dtype: tf.chars);
27+
var mammal1 = tf.Variable("Elephant", name: "var1", dtype: tf.@string);
2828
var mammal2 = tf.Variable("Tiger");
2929
}
3030

test/TensorFlowNET.UnitTest/control_flow_ops_test/WhileContextTestCase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class WhileContextTestCase : PythonTest
1111
/// <summary>
1212
/// https://www.tensorflow.org/api_docs/python/tf/while_loop
1313
/// </summary>
14+
[Ignore]
1415
[TestMethod]
1516
public void SimpleWhileLoop()
1617
{

0 commit comments

Comments
 (0)