Skip to content

Commit 991cfec

Browse files
committed
Add TimeSeries example.
1 parent bedbd28 commit 991cfec

16 files changed

Lines changed: 240 additions & 295 deletions

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
# SciSharp STACK Examples
2+
23
This repo contains many practical examples written in SciSharp's machine learning libraries. If you still don't know how to use .NET for deep learning, getting started from these examples is your best choice.
34

45
[![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
56

6-
7-
87
Requirements:
98

10-
* [.NET Core 3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1)
9+
* [.NET Core 5.0](https://dotnet.microsoft.com/download/dotnet-core/5.0)
1110

1211
* [Visual Studio 2019](https://visualstudio.microsoft.com/vs/) or [Visual Studio Code](https://code.visualstudio.com/)
1312

14-
15-
1613
Run specific example in shell:
1714

1815
#### C#
@@ -41,7 +38,6 @@ dotnet run --project src/TensorFlowNET.Examples.FSharp -ex "Linear Regression (E
4138
dotnet TensorFlowNET.Examples.FSharp.dll -ex "MNIST CNN (Eager)"
4239
```
4340

44-
4541
Example runner will download all the required files like training data and model pb files.
4642

4743
#### Basic Model
@@ -76,12 +72,17 @@ Example runner will download all the required files like training data and model
7672
* CNN In Your Own Dataset [C#](src/TensorFlowNET.Examples/ImageProcessing/CnnInYourOwnData.cs), [F#](src/TensorFlowNET.Examples.FSharp/ImageProcessing/CnnInYourOwnData.fs)
7773

7874
#### Natural Language Processing
75+
7976
* Binary Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
8077
* CNN Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
8178
* Named Entity Recognition [C#](src/TensorFlowNET.Examples/TextProcessing/NER)
8279

80+
#### Time Series
81+
82+
* Weather Prediction [C#](src/TensorFlowNET.Examples/TimeSeries/WeatherPrediction.cs)
8383

8484
#### Welcome to PR your example to us.
85+
8586
Your contribution will make .NET community better than ever.
8687
<br>
8788
<a href="http://scisharpstack.org"><img src="https://github.com/SciSharp/SciSharp/blob/master/art/scisharp-stack.png" width="391" height="100" /></a>

src/SharpCV.Examples/SharpCV.Examples.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
<ItemGroup>
1010
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.4.0.20200915" />
11-
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.6.0-rc0" />
12-
<PackageReference Include="SharpCV" Version="0.10.0" />
11+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.7.0" />
12+
<PackageReference Include="SharpCV" Version="0.11.0" />
1313
</ItemGroup>
1414

1515
</Project>

src/TensorFlowNET.Examples/ImageProcessing/CnnInYourOwnData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private Tensor conv_layer(Tensor x, int filter_size, int num_filters, int stride
378378
// var tf.summary.histogram("weight", W);
379379
var b = bias_variable("b", new[] { num_filters });
380380
// tf.summary.histogram("bias", b);
381-
var layer = tf.nn.conv2d(x, W,
381+
var layer = tf.nn.conv2d(x, W.AsTensor(),
382382
strides: new int[] { 1, stride, stride, 1 },
383383
padding: "SAME");
384384
layer += b.AsTensor();

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public override void Predict()
107107
var result = task.Predict(input);
108108
long output = np.argmax(y_test[0]);
109109
Debug.Assert(result.Label == output.ToString());
110+
111+
input = x_test["1:2"];
112+
result = task.Predict(input);
113+
output = np.argmax(y_test[1]);
114+
Debug.Assert(result.Label == output.ToString());
110115
}
111116

112117
public override void PrepareData()

src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCnnEager.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ public bool Run()
6464
tf.enable_eager_execution();
6565

6666
PrepareData();
67+
Train();
68+
Test();
69+
70+
return accuracy_test >= 0.80;
71+
}
6772

68-
// Store layers weight & bias
69-
73+
public override void Train()
74+
{
7075
// A random value generator to initialize weights.
7176
var random_normal = tf.initializers.random_normal_initializer();
7277

@@ -101,17 +106,16 @@ public bool Run()
101106
print($"step: {step}, loss: {(float)loss}, accuracy: {(float)acc}");
102107
}
103108
}
109+
}
104110

111+
public override void Test()
112+
{
105113
// Test model on validation set.
106-
{
107-
x_test = x_test["::100"];
108-
y_test = y_test["::100"];
109-
var pred = conv_net(x_test);
110-
accuracy_test = (float)accuracy(pred, y_test);
111-
print($"Test Accuracy: {accuracy_test}");
112-
}
113-
114-
return accuracy_test >= 0.80;
114+
x_test = x_test["::100"];
115+
y_test = y_test["::100"];
116+
var pred = conv_net(x_test);
117+
accuracy_test = (float)accuracy(pred, y_test);
118+
print($"Test Accuracy: {accuracy_test}");
115119
}
116120

117121
void run_optimization(OptimizerV2 optimizer, Tensor x, Tensor y)
@@ -130,7 +134,7 @@ void run_optimization(OptimizerV2 optimizer, Tensor x, Tensor y)
130134

131135
Tensor conv2d(Tensor x, IVariableV1 W, IVariableV1 b, int strides = 1)
132136
{
133-
x = tf.nn.conv2d(x, W, new int[] { 1, strides, strides, 1 }, padding: "SAME");
137+
x = tf.nn.conv2d(x, W.AsTensor(), new int[] { 1, strides, strides, 1 }, padding: "SAME");
134138
x = tf.nn.bias_add(x, b);
135139
return tf.nn.relu(x);
136140
}

src/TensorFlowNET.Examples/ImageProcessing/ImageClassificationKeras.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@ public override void PrepareData()
9696

9797
train_ds = train_ds.shuffle(1000).prefetch(buffer_size: -1);
9898
val_ds = val_ds.prefetch(buffer_size: -1);
99-
100-
foreach (var (img, label) in train_ds)
101-
{
102-
print($"images: {img.shape}");
103-
print($"labels: {label.numpy()}");
104-
}
10599
}
106100
}
107101
}

src/TensorFlowNET.Examples/ImageProcessing/MnistCnnKerasSubclass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void run_optimization(ConvNet conv_net, OptimizerV2 optimizer, Tensor x, Tensor
111111

112112
Tensor conv2d(Tensor x, IVariableV1 W, IVariableV1 b, int strides = 1)
113113
{
114-
x = tf.nn.conv2d(x, W, new int[] { 1, strides, strides, 1 }, padding: "SAME");
114+
x = tf.nn.conv2d(x, W.AsTensor(), new int[] { 1, strides, strides, 1 }, padding: "SAME");
115115
x = tf.nn.bias_add(x, b);
116116
return tf.nn.relu(x);
117117
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*****************************************************************************
2+
Copyright 2021 Haiping Chen. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using SciSharp.Models;
18+
using SciSharp.Models.ObjectDetection;
19+
using System;
20+
using System.IO;
21+
22+
namespace TensorFlowNET.Examples
23+
{
24+
public class MnistInYOLOv3 : SciSharpExample, IExample
25+
{
26+
YoloConfig cfg;
27+
float accuracy_test = 0f;
28+
YoloDataset trainingData, testingData;
29+
30+
public ExampleConfig InitConfig()
31+
=> Config = new ExampleConfig
32+
{
33+
Name = "MNIST in YOLOv3",
34+
Enabled = false
35+
};
36+
37+
public bool Run()
38+
{
39+
cfg = new YoloConfig("YOLOv3");
40+
(trainingData, testingData) = PrepareData();
41+
Train();
42+
return true;
43+
}
44+
45+
public override void Train()
46+
{
47+
// using wizard to train model
48+
var wizard = new ModelWizard();
49+
var task = wizard.AddObjectDetectionTask<YOLOv3>(new TaskOptions
50+
{
51+
InputShape = (28, 28, 1),
52+
NumberOfClass = 10,
53+
});
54+
task.SetModelArgs(cfg);
55+
56+
task.Train(new YoloTrainingOptions
57+
{
58+
TrainingData = trainingData,
59+
TestingData = testingData
60+
});
61+
}
62+
63+
public override void Test()
64+
{
65+
var wizard = new ModelWizard();
66+
var task = wizard.AddObjectDetectionTask<YOLOv3>(new TaskOptions
67+
{
68+
ModelPath = @"./YOLOv3/yolov3.h5"
69+
});
70+
var result = task.Test(new TestingOptions
71+
{
72+
73+
});
74+
accuracy_test = result.Accuracy;
75+
}
76+
77+
public (YoloDataset, YoloDataset) PrepareData()
78+
{
79+
string dataDir = Path.Combine("YOLOv3", "data");
80+
Directory.CreateDirectory(dataDir);
81+
82+
var trainset = new YoloDataset("train", cfg);
83+
var testset = new YoloDataset("test", cfg);
84+
return (trainset, testset);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)