Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ src/TensorFlowNET.Native/bazel-*
src/TensorFlowNET.Native/c_api.h
/.vscode
test/TensorFlowNET.Examples/mnist


# training model resources
.resources
28 changes: 20 additions & 8 deletions test/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
using System;
using System.Diagnostics;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using Tensorflow.Hub;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples
Expand All @@ -39,7 +39,7 @@ public class KMeansClustering : IExample
public int? test_size = null;
public int batch_size = 1024; // The number of samples per batch

Datasets<DataSetMnist> mnist;
Datasets<MnistDataSet> mnist;
NDArray full_data_x;
int num_steps = 20; // Total steps to train
int k = 25; // The number of clusters
Expand All @@ -62,19 +62,31 @@ public bool Run()

public void PrepareData()
{
mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size:validation_size, test_size:test_size);
full_data_x = mnist.train.data;
var loader = new MnistModelLoader();

var setting = new ModelLoadSetting
{
TrainDir = ".resources/mnist",
OneHot = true,
TrainSize = train_size,
ValidationSize = validation_size,
TestSize = test_size
};

mnist = loader.LoadAsync(setting).Result;

full_data_x = mnist.Train.Data;

// download graph meta data
string url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/kmeans.meta";
Web.Download(url, "graph", "kmeans.meta");
loader.DownloadAsync(url, ".resources/graph", "kmeans.meta").Wait();
}

public Graph ImportGraph()
{
var graph = tf.Graph().as_default();

tf.train.import_meta_graph("graph/kmeans.meta");
tf.train.import_meta_graph(".resources/graph/kmeans.meta");

return graph;
}
Expand Down Expand Up @@ -132,7 +144,7 @@ public void Train(Session sess)
sw.Start();
foreach (var i in range(idx.Length))
{
var x = mnist.train.labels[i];
var x = mnist.Train.Labels[i];
counts[idx[i]] += x;
}

Expand All @@ -153,7 +165,7 @@ public void Train(Session sess)
var accuracy_op = tf.reduce_mean(cast);

// Test Model
var (test_x, test_y) = (mnist.test.data, mnist.test.labels);
var (test_x, test_y) = (mnist.Test.Data, mnist.Test.Labels);
result = sess.run(accuracy_op, new FeedItem(X, test_x), new FeedItem(Y, test_y));
accuray_test = result;
print($"Test Accuracy: {accuray_test}");
Expand Down
26 changes: 13 additions & 13 deletions test/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ limitations under the License.
using System.Diagnostics;
using System.IO;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using Tensorflow.Hub;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples
Expand All @@ -45,7 +45,7 @@ public class LogisticRegression : IExample
private float learning_rate = 0.01f;
private int display_step = 1;

Datasets<DataSetMnist> mnist;
Datasets<MnistDataSet> mnist;

public bool Run()
{
Expand Down Expand Up @@ -84,11 +84,11 @@ public bool Run()
sw.Start();

var avg_cost = 0.0f;
var total_batch = mnist.train.num_examples / batch_size;
var total_batch = mnist.Train.NumOfExamples / batch_size;
// Loop over all batches
foreach (var i in range(total_batch))
{
var (batch_xs, batch_ys) = mnist.train.next_batch(batch_size);
var (batch_xs, batch_ys) = mnist.Train.GetNextBatch(batch_size);
// Run optimization op (backprop) and cost op (to get loss value)
var result = sess.run(new object[] { optimizer, cost },
new FeedItem(x, batch_xs),
Expand All @@ -115,7 +115,7 @@ public bool Run()
var correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1));
// Calculate accuracy
var accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32));
float acc = accuracy.eval(new FeedItem(x, mnist.test.data), new FeedItem(y, mnist.test.labels));
float acc = accuracy.eval(new FeedItem(x, mnist.Test.Data), new FeedItem(y, mnist.Test.Labels));
print($"Accuracy: {acc.ToString("F4")}");

return acc > 0.9;
Expand All @@ -124,31 +124,31 @@ public bool Run()

public void PrepareData()
{
mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: train_size, validation_size: validation_size, test_size: test_size);
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, trainSize: train_size, validationSize: validation_size, testSize: test_size).Result;
}

public void SaveModel(Session sess)
{
var saver = tf.train.Saver();
var save_path = saver.save(sess, "logistic_regression/model.ckpt");
tf.train.write_graph(sess.graph, "logistic_regression", "model.pbtxt", as_text: true);
var save_path = saver.save(sess, ".resources/logistic_regression/model.ckpt");
tf.train.write_graph(sess.graph, ".resources/logistic_regression", "model.pbtxt", as_text: true);

FreezeGraph.freeze_graph(input_graph: "logistic_regression/model.pbtxt",
FreezeGraph.freeze_graph(input_graph: ".resources/logistic_regression/model.pbtxt",
input_saver: "",
input_binary: false,
input_checkpoint: "logistic_regression/model.ckpt",
input_checkpoint: ".resources/logistic_regression/model.ckpt",
output_node_names: "Softmax",
restore_op_name: "save/restore_all",
filename_tensor_name: "save/Const:0",
output_graph: "logistic_regression/model.pb",
output_graph: ".resources/logistic_regression/model.pb",
clear_devices: true,
initializer_nodes: "");
}

public void Predict(Session sess)
{
var graph = new Graph().as_default();
graph.Import(Path.Join("logistic_regression", "model.pb"));
graph.Import(Path.Join(".resources/logistic_regression", "model.pb"));

// restoring the model
// var saver = tf.train.import_meta_graph("logistic_regression/tensorflowModel.ckpt.meta");
Expand All @@ -159,7 +159,7 @@ public void Predict(Session sess)
var input = x.outputs[0];

// predict
var (batch_xs, batch_ys) = mnist.train.next_batch(10);
var (batch_xs, batch_ys) = mnist.Train.GetNextBatch(10);
var results = sess.run(output, new FeedItem(input, batch_xs[np.arange(1)]));

if (results.argmax() == (batch_ys[0] as NDArray).argmax())
Expand Down
10 changes: 5 additions & 5 deletions test/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
using NumSharp;
using System;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using Tensorflow.Hub;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples
Expand All @@ -31,7 +31,7 @@ public class NearestNeighbor : IExample
{
public bool Enabled { get; set; } = true;
public string Name => "Nearest Neighbor";
Datasets<DataSetMnist> mnist;
Datasets<MnistDataSet> mnist;
NDArray Xtr, Ytr, Xte, Yte;
public int? TrainSize = null;
public int ValidationSize = 5000;
Expand Down Expand Up @@ -84,10 +84,10 @@ public bool Run()

public void PrepareData()
{
mnist = MNIST.read_data_sets("mnist", one_hot: true, train_size: TrainSize, validation_size:ValidationSize, test_size:TestSize);
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true, trainSize: TrainSize, validationSize: ValidationSize, testSize: TestSize).Result;
// In this example, we limit mnist data
(Xtr, Ytr) = mnist.train.next_batch(TrainSize==null ? 5000 : TrainSize.Value / 100); // 5000 for training (nn candidates)
(Xte, Yte) = mnist.test.next_batch(TestSize==null ? 200 : TestSize.Value / 100); // 200 for testing
(Xtr, Ytr) = mnist.Train.GetNextBatch(TrainSize == null ? 5000 : TrainSize.Value / 100); // 5000 for training (nn candidates)
(Xte, Yte) = mnist.Test.GetNextBatch(TestSize == null ? 200 : TestSize.Value / 100); // 200 for testing
}

public Graph ImportGraph()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
using System;
using System.Diagnostics;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using Tensorflow.Hub;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples.ImageProcess
Expand Down Expand Up @@ -46,7 +46,7 @@ public class DigitRecognitionCNN : IExample
int epochs = 5; // accuracy > 98%
int batch_size = 100;
float learning_rate = 0.001f;
Datasets<DataSetMnist> mnist;
Datasets<MnistDataSet> mnist;

// Network configuration
// 1st Convolutional Layer
Expand Down Expand Up @@ -310,14 +310,14 @@ private Tensor fc_layer(Tensor x, int num_units, string name, bool use_relu = tr

public void PrepareData()
{
mnist = MNIST.read_data_sets("mnist", one_hot: true);
(x_train, y_train) = Reformat(mnist.train.data, mnist.train.labels);
(x_valid, y_valid) = Reformat(mnist.validation.data, mnist.validation.labels);
(x_test, y_test) = Reformat(mnist.test.data, mnist.test.labels);
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true).Result;
(x_train, y_train) = Reformat(mnist.Train.Data, mnist.Train.Labels);
(x_valid, y_valid) = Reformat(mnist.Validation.Data, mnist.Validation.Labels);
(x_test, y_test) = Reformat(mnist.Test.Data, mnist.Test.Labels);

print("Size of:");
print($"- Training-set:\t\t{len(mnist.train.data)}");
print($"- Validation-set:\t{len(mnist.validation.data)}");
print($"- Training-set:\t\t{len(mnist.Train.Data)}");
print($"- Validation-set:\t{len(mnist.Validation.Data)}");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
using NumSharp;
using System;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using Tensorflow.Hub;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples.ImageProcess
Expand All @@ -44,7 +44,7 @@ public class DigitRecognitionNN : IExample
int batch_size = 100;
float learning_rate = 0.001f;
int h1 = 200; // number of nodes in the 1st hidden layer
Datasets<DataSetMnist> mnist;
Datasets<MnistDataSet> mnist;

Tensor x, y;
Tensor loss, accuracy;
Expand Down Expand Up @@ -121,13 +121,13 @@ private Tensor fc_layer(Tensor x, int num_units, string name, bool use_relu = tr

public void PrepareData()
{
mnist = MNIST.read_data_sets("mnist", one_hot: true);
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true).Result;
}

public void Train(Session sess)
{
// Number of training iterations in each epoch
var num_tr_iter = mnist.train.labels.len / batch_size;
var num_tr_iter = mnist.Train.Labels.len / batch_size;

var init = tf.global_variables_initializer();
sess.run(init);
Expand All @@ -139,13 +139,13 @@ public void Train(Session sess)
{
print($"Training epoch: {epoch + 1}");
// Randomly shuffle the training data at the beginning of each epoch
var (x_train, y_train) = randomize(mnist.train.data, mnist.train.labels);
var (x_train, y_train) = mnist.Randomize(mnist.Train.Data, mnist.Train.Labels);

foreach (var iteration in range(num_tr_iter))
{
var start = iteration * batch_size;
var end = (iteration + 1) * batch_size;
var (x_batch, y_batch) = get_next_batch(x_train, y_train, start, end);
var (x_batch, y_batch) = mnist.GetNextBatch(x_train, y_train, start, end);

// Run optimization op (backprop)
sess.run(optimizer, new FeedItem(x, x_batch), new FeedItem(y, y_batch));
Expand All @@ -161,7 +161,8 @@ public void Train(Session sess)
}

// Run validation after every epoch
var results1 = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.validation.data), new FeedItem(y, mnist.validation.labels));
var results1 = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.Validation.Data), new FeedItem(y, mnist.Validation.Labels));

loss_val = results1[0];
accuracy_val = results1[1];
print("---------------------------------------------------------");
Expand All @@ -172,35 +173,12 @@ public void Train(Session sess)

public void Test(Session sess)
{
var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.test.data), new FeedItem(y, mnist.test.labels));
var result = sess.run(new[] { loss, accuracy }, new FeedItem(x, mnist.Test.Data), new FeedItem(y, mnist.Test.Labels));
loss_test = result[0];
accuracy_test = result[1];
print("---------------------------------------------------------");
print($"Test loss: {loss_test.ToString("0.0000")}, test accuracy: {accuracy_test.ToString("P")}");
print("---------------------------------------------------------");
}

private (NDArray, NDArray) randomize(NDArray x, NDArray y)
{
var perm = np.random.permutation(y.shape[0]);

np.random.shuffle(perm);
return (mnist.train.data[perm], mnist.train.labels[perm]);
}

/// <summary>
/// selects a few number of images determined by the batch_size variable (if you don't know why, read about Stochastic Gradient Method)
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
private (NDArray, NDArray) get_next_batch(NDArray x, NDArray y, int start, int end)
{
var x_batch = x[$"{start}:{end}"];
var y_batch = y[$"{start}:{end}"];
return (x_batch, y_batch);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
using NumSharp;
using System;
using Tensorflow;
using TensorFlowNET.Examples.Utility;
using Tensorflow.Hub;
using static Tensorflow.Python;

namespace TensorFlowNET.Examples.ImageProcess
Expand Down Expand Up @@ -45,7 +45,7 @@ public class DigitRecognitionRNN : IExample
int n_inputs = 28;
int n_outputs = 10;

Datasets<DataSetMnist> mnist;
Datasets<MnistDataSet> mnist;

Tensor x, y;
Tensor loss, accuracy, cls_prediction;
Expand Down Expand Up @@ -143,15 +143,15 @@ public void Test(Session sess)

public void PrepareData()
{
mnist = MNIST.read_data_sets("mnist", one_hot: true);
(x_train, y_train) = (mnist.train.data, mnist.train.labels);
(x_valid, y_valid) = (mnist.validation.data, mnist.validation.labels);
(x_test, y_test) = (mnist.test.data, mnist.test.labels);
mnist = MnistModelLoader.LoadAsync(".resources/mnist", oneHot: true).Result;
(x_train, y_train) = (mnist.Train.Data, mnist.Train.Labels);
(x_valid, y_valid) = (mnist.Validation.Data, mnist.Validation.Labels);
(x_test, y_test) = (mnist.Test.Data, mnist.Test.Labels);

print("Size of:");
print($"- Training-set:\t\t{len(mnist.train.data)}");
print($"- Validation-set:\t{len(mnist.validation.data)}");
print($"- Test-set:\t\t{len(mnist.test.data)}");
print($"- Training-set:\t\t{len(mnist.Train.Data)}");
print($"- Validation-set:\t{len(mnist.Validation.Data)}");
print($"- Test-set:\t\t{len(mnist.Test.Data)}");
}

public Graph ImportGraph() => throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<ProjectReference Include="..\..\src\KerasNET.Core\Keras.Core.csproj" />
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
<ProjectReference Include="..\..\src\TensorFlowText\TensorFlowText.csproj" />
<ProjectReference Include="..\..\src\TensorFlowHub\TensorFlowHub.csproj" />
</ItemGroup>
</Project>
Loading