Browse Source

Merge branch 'master' of https://github.com/SciSharp/TensorFlow.NET

# Conflicts:
#	README.md
#	src/TensorFlowNET.Core/Sessions/_FetchMapper.cs
tags/v0.9
c q 6 years ago
parent
commit
fb23b9f43c
5 changed files with 57 additions and 21 deletions
  1. +5
    -4
      README.md
  2. +2
    -6
      src/TensorFlowNET.Core/Sessions/_FetchMapper.cs
  3. +40
    -5
      test/TensorFlowNET.Examples/KMeansClustering.cs
  4. +2
    -6
      test/TensorFlowNET.Examples/ObjectDetection.cs
  5. +8
    -0
      test/TensorFlowNET.UnitTest/OperationsTest.cs

+ 5
- 4
README.md View File

@@ -1,6 +1,7 @@
# TensorFlow.NET # TensorFlow.NET
TensorFlow.NET provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework. TensorFlow.NET provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework.
Here is a simple test
TensorFlow.NET (TF.NET) provides a .NET Standard binding for [TensorFlow](https://www.tensorflow.org/). It aims to implement the complete Tensorflow API in CSharp which allows .NET developers to develop, train and deploy Machine Learning models with the cross-platform .NET Standard framework.

[![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community) [![Join the chat at https://gitter.im/publiclab/publiclab](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sci-sharp/community)
[![Tensorflow.NET](https://ci.appveyor.com/api/projects/status/wx4td43v2d3f2xj6?svg=true)](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net) [![Tensorflow.NET](https://ci.appveyor.com/api/projects/status/wx4td43v2d3f2xj6?svg=true)](https://ci.appveyor.com/project/Haiping-Chen/tensorflow-net)
[![codecov](https://codecov.io/gh/SciSharp/NumSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SciSharp/NumSharp) [![codecov](https://codecov.io/gh/SciSharp/NumSharp/branch/master/graph/badge.svg)](https://codecov.io/gh/SciSharp/NumSharp)
@@ -8,7 +9,7 @@ Here is a simple test
[![Documentation Status](https://readthedocs.org/projects/tensorflownet/badge/?version=latest)](https://tensorflownet.readthedocs.io/en/latest/?badge=latest) [![Documentation Status](https://readthedocs.org/projects/tensorflownet/badge/?version=latest)](https://tensorflownet.readthedocs.io/en/latest/?badge=latest)
[![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US) [![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/en_US)


TensorFlow.NET is a member project of [SciSharp STACK](https://github.com/SciSharp).
TF.NET is a member project of [SciSharp STACK](https://github.com/SciSharp).


![tensors_flowing](docs/assets/tensors_flowing.gif) ![tensors_flowing](docs/assets/tensors_flowing.gif)


@@ -24,14 +25,14 @@ In comparison to other projects, like for instance TensorFlowSharp which only pr


### How to use ### How to use


Install TensorFlow.NET through NuGet.
Install TF.NET through NuGet.
```sh ```sh
PM> Install-Package TensorFlow.NET PM> Install-Package TensorFlow.NET
``` ```


If you are using Linux or Mac OS, please download the pre-compiled dll [here](tensorflowlib) and place it in the working folder. This is only need for Linux and Mac OS, and already packed into NuGet for Windows. If you are using Linux or Mac OS, please download the pre-compiled dll [here](tensorflowlib) and place it in the working folder. This is only need for Linux and Mac OS, and already packed into NuGet for Windows.


Import tensorflow.net.
Import TF.NET.


```cs ```cs
using Tensorflow; using Tensorflow;


+ 2
- 6
src/TensorFlowNET.Core/Sessions/_FetchMapper.cs View File

@@ -17,10 +17,10 @@ namespace Tensorflow
if (fetch.GetType().IsArray) if (fetch.GetType().IsArray)
return new _ListFetchMapper(fetches); return new _ListFetchMapper(fetches);
else else
return new _ElementFetchMapper(fetches, (List<object> fetched_vals) => fetched_vals[0]);
return new _ElementFetchMapper(fetches, (List<NDArray> fetched_vals) => fetched_vals[0]);
} }


public virtual NDArray build_results(List<object> values)
public virtual NDArray build_results(List<NDArray> values)
{ {
var type = values[0].GetType(); var type = values[0].GetType();
var nd = new NDArray(type, values.Count); var nd = new NDArray(type, values.Count);
@@ -31,16 +31,12 @@ namespace Tensorflow
nd.SetData(values.Select(x => (float)x).ToArray()); nd.SetData(values.Select(x => (float)x).ToArray());
break; break;
case "NDArray": case "NDArray":
// nd.SetData<NDArray>(values.ToArray());
//NDArray[] arr = new NDArray[values.Count];
//for (int i=0; i<values.Count; i++)
NDArray[] arr = values.Select(x => (NDArray)x).ToArray(); NDArray[] arr = values.Select(x => (NDArray)x).ToArray();
nd = new NDArray(arr); nd = new NDArray(arr);
break; break;
default: default:
break; break;
} }

return nd; return nd;
} }




+ 40
- 5
test/TensorFlowNET.Examples/KMeansClustering.cs View File

@@ -33,19 +33,54 @@ namespace TensorFlowNET.Examples


public bool Run() public bool Run()
{ {
PrepareData();

var graph = tf.Graph().as_default();

tf.train.import_meta_graph("kmeans.meta"); tf.train.import_meta_graph("kmeans.meta");


// Input images // Input images
var X = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features));
var X = graph.get_operation_by_name("Placeholder").output; // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_features));
// Labels (for assigning a label to a centroid and testing) // Labels (for assigning a label to a centroid and testing)
var Y = tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes));
var Y = graph.get_operation_by_name("Placeholder_1").output; // tf.placeholder(tf.float32, shape: new TensorShape(-1, num_classes));


// K-Means Parameters // K-Means Parameters
var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true);
//var kmeans = new KMeans(X, k, distance_metric: KMeans.COSINE_DISTANCE, use_mini_batch: true);


// Build KMeans graph // Build KMeans graph
var training_graph = kmeans.training_graph();
//var training_graph = kmeans.training_graph();

var init_vars = tf.global_variables_initializer();
Tensor init_op = graph.get_operation_by_name("cond/Merge");
var train_op = graph.get_operation_by_name("group_deps");
Tensor avg_distance = graph.get_operation_by_name("Mean");
Tensor cluster_idx = graph.get_operation_by_name("Squeeze_1");

with(tf.Session(graph), sess =>
{
sess.run(init_vars, new FeedItem(X, full_data_x));
sess.run(init_op, new FeedItem(X, full_data_x));

// Training
NDArray result = null;
foreach(var i in range(1, num_steps + 1))
{
result = sess.run(new ITensorOrOperation[] { train_op, avg_distance, cluster_idx }, new FeedItem(X, full_data_x));
if (i % 2 == 0 || i == 1)
print($"Step {i}, Avg Distance: {result[1]}");
}

var idx = result[2];

// Assign a label to each centroid
// Count total number of labels per centroid, using the label of each training
// sample to their closest centroid (given by 'idx')
var counts = np.zeros(k, num_classes);
foreach (var i in range(idx.len))
counts[idx[i]] += mnist.train.labels[i];

});

return false; return false;
} }




+ 2
- 6
test/TensorFlowNET.Examples/ObjectDetection.cs View File

@@ -50,13 +50,9 @@ namespace TensorFlowNET.Examples
with(tf.Session(graph), sess => with(tf.Session(graph), sess =>
{ {
var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr)); var results = sess.run(outTensorArr, new FeedItem(imgTensor, imgArr));

//NDArray scores = results.Array.GetValue(2) as NDArray;

//floatscores.Data<float>();
NDArray[] resultArr = results.Data<NDArray>(); NDArray[] resultArr = results.Data<NDArray>();

//float[] scores = resultArr[2].Data<float>();
buildOutputImage(resultArr); buildOutputImage(resultArr);
}); });




+ 8
- 0
test/TensorFlowNET.UnitTest/OperationsTest.cs View File

@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using Tensorflow; using Tensorflow;
using Buffer = Tensorflow.Buffer; using Buffer = Tensorflow.Buffer;
@@ -20,6 +21,13 @@ namespace TensorFlowNET.UnitTest
var handle = c_api.TF_GetAllOpList(); var handle = c_api.TF_GetAllOpList();
var buffer = new Buffer(handle); var buffer = new Buffer(handle);
var op_list = OpList.Parser.ParseFrom(buffer); var op_list = OpList.Parser.ParseFrom(buffer);

var _registered_ops = new Dictionary<string, OpDef>();
foreach (var op_def in op_list.Op)
_registered_ops[op_def.Name] = op_def;

// r1.14 added NN op
var op = _registered_ops.FirstOrDefault(x => x.Key == "NearestNeighbors");
Assert.IsTrue(op_list.Op.Count > 1000); Assert.IsTrue(op_list.Op.Count > 1000);
} }




Loading…
Cancel
Save