Skip to content

Commit bc4606c

Browse files
committed
Update
1 parent 126c932 commit bc4606c

4 files changed

Lines changed: 118 additions & 32 deletions

File tree

README.md

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,81 @@ kinks and TODO-items that I left while I was doing the work.
1313
My work-in-progress API documentation [current API
1414
documentation](https://migueldeicaza.github.io/TensorFlowSharp/).
1515

16-
# Getting Started
16+
# Using TensorFlowSharp
1717

18-
You can either use the TensorFlow C-library release binaries, or build your own
19-
from source.
18+
## Installation
19+
20+
The easiest way to get started is to use the NuGet package for
21+
TensorFlowSharp which contains both the .NET API as well as the
22+
native libraries for 64-bit Linux, Mac and Windows using the CPU backend.
23+
24+
You can install using NuGet like this:
25+
26+
```
27+
nuget install TensorFlowSharp
28+
```
29+
30+
Or select it from the NuGet packages UI on Visual Studio.
31+
32+
Alternatively, you can [download it](https://www.nuget.org/api/v2/package/TensorFlowSharp/0.96.0) directly.
33+
34+
## Using TensorFlowSharp
35+
36+
Your best source of information right now are the SampleTest that
37+
exercises various APIs of TensorFlowSharp, or the stand-alone samples
38+
located in "Examples".
39+
40+
This API binding is closer design-wise to the Java and Go bindings
41+
which use explicit TensorFlow graphs and sessions. Your application
42+
will typically create a graph (TFGraph) and setup the operations
43+
there, then create a session from it (TFSession), then use the session
44+
runner to setup inputs and outputs and execute the pipeline.
45+
46+
Something like this:
47+
48+
```
49+
var graph = new TFGraph ();
50+
graph.Import (File.ReadAllBytes ("MySavedModel");
51+
var session = new TFSession (graph);
52+
var runner = session.GetRunner ();
53+
runner.AddInput (graph ["input"] [0], tensor);
54+
runner.Fetch (graph ["output"] [0]);
55+
56+
var output = runner.Run ();
57+
58+
// Fethc the results from output:
59+
TFTensor result = output [0];
60+
```
61+
62+
In scenarios where you do not need to setup the graph independently,
63+
the session will create one for you. The following example shows how
64+
to abuse TensorFlow to compute the addition of two numbers:
65+
66+
```
67+
var s = new TFSession ();
68+
var g = s.Graph;
69+
70+
var a = g.Const (2);
71+
var b = g.Const (3);
72+
Console.WriteLine ("a=2 b=3");
73+
74+
// Add two constants
75+
var results = s.GetRunner ().Run (g.Add (a, b));
76+
var val = results [0].GetValue ();
77+
Console.WriteLine ("a+b={0}", val);
78+
79+
// Multiply two constants
80+
results = s.GetRunner ().Run (g.Mul (a, b));
81+
Console.WriteLine ("a*b={0}", results [0].GetValue ());
82+
```
83+
84+
# Working on TensorFlowSharp
85+
86+
TensorFlowSharp are bindings to the native TensorFlow library.
87+
88+
You can either use the TensorFlow C-library release binaries, or build
89+
your own from source. Here are some pre-built TensorFlow binaries you
90+
can use for each platform:
2091

2192
- Linux
2293
- CPU-only: https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.1.0.tar.gz
@@ -34,16 +105,10 @@ Once you do that, you need to open the solution file on the top
34105
level directory and build. This will produce both the TensorFlowSharp
35106
library as well as compile the tests and samples.
36107

37-
# Work in Progress
38-
39-
These instructions reflect what you need to get up and running with the
40-
current code as I am working on it. In the long-term, we will just have
41-
NuGet packages that eliminate all the manual steps required here.
42-
43-
## Building your own version
108+
## Building your own native TensorFlow library
44109

45110
To build the TensorFlow C library from source,
46-
[try this](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/README.md#building-the-tensorflow-c-library-from-source).
111+
[follow these instructions](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/README.md#building-the-tensorflow-c-library-from-source).
47112

48113
This includes checking out the Tensorflow sources, installing Bazel,
49114
and building the core.
@@ -83,9 +148,8 @@ sudo cp bazel-bin/tensorflow/libtensorflow.so /usr/local/lib/libtensorflow.dylib
83148

84149
## Running the test
85150

86-
I am currently using Xamarin Studio on a Mac to do the development, but this
87-
should work on Windows with VS and Linux with MonoDevelop, there is nothing
88-
Xamarin specific here.
151+
I am currently using Visual Studio for Mac to do the development, but this
152+
should work on Windows with VS and Linux with MonoDevelop.
89153

90154
Before the solution will run you will need the shared library generated to
91155
be on a location accessibly by the Mono runtime (for example /usr/local/lib).
@@ -98,9 +162,7 @@ Tensorflow is a 64-bit library, so you will need to use a 64-bit Mono to run,
98162
at home (where I am doing this work), I have a copy of 64-bit Mono on /mono,
99163
so you will want to set that in your project configuration, to do this:
100164

101-
Open the project options (double click on the "SampleTest" project), then
102-
select Run/Default, go to the "Advanced" tab, and select "Execute in .NET runtime"
103-
and make sure that you select one that is 64-bit enabled.
165+
Ensure that your Build/Compiler settings set "Platform Target" to "x64".
104166

105167
Open the solution file in the top directory, and when you hit run, this will
106168
run the API test.
@@ -122,16 +184,12 @@ https://github.com/tensorflow/models
122184

123185
### Packaging
124186

125-
x86: It is not clear to me how to distribute the native libtensorflow to users, as
126-
it is designed to be compiled for your host system. I would like to figure out
127-
how we can distribute packages that have been compiled with the optimal set of
128-
optimizations for users to consume.
129-
130187
Mobile: we need to package the library for consumption on Android and iOS.
131188

132-
### NuGet Package
189+
### Documentation Styling
133190

134-
Would love to have a NuGet package for all platforms.
191+
The API documentation has not been styled, I am using the barebones template
192+
for documentation, and it can use some work.
135193

136194
### Issues
137195

TensorFlowSharp/TensorFlowSharp.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
<RootNamespace>TensorFlowSharp</RootNamespace>
1010
<AssemblyName>TensorFlowSharp</AssemblyName>
1111
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12-
<ReleaseVersion>0.2.3</ReleaseVersion>
12+
<ReleaseVersion>0.2</ReleaseVersion>
1313
<PackOnBuild>true</PackOnBuild>
1414
<PackageId>TensorFlowSharp</PackageId>
15-
<PackageVersion>$(ReleaseVersion)</PackageVersion>
15+
<PackageVersion>0.96</PackageVersion>
1616
<Authors>Miguel de Icaza</Authors>
1717
<PackageLicenseUrl>https://github.com/migueldeicaza/TensorFlowSharp/blob/master/LICENSE</PackageLicenseUrl>
1818
<PackageProjectUrl>https://github.com/migueldeicaza/TensorFlowSharp/</PackageProjectUrl>
1919
<PackageTags>machine-learning, tensorflow, xamarin, c#, f#</PackageTags>
2020
<Description>.NET Bindings for TensorFlow</Description>
21+
<Owners>Miguel de Icaza</Owners>
22+
<Summary>.NET API for TensorFlow, Google's Machine Intelligence framework</Summary>
2123
</PropertyGroup>
2224
<ItemGroup>
2325
<_NativeFiles Include="..\native\*.*">
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
22
<PropertyGroup>
3-
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
4-
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
3+
<IsOSX Condition="Exists('/Library/Frameworks') and Exists ('/etc')">true</IsOSX>
4+
<IsLinux Condition="Exists ('/proc') and Exists ('/etc/')">true</IsLinux>
55
</PropertyGroup>
66
<ItemGroup>
77
<None Include="$(MSBuildThisFileDirectory)..\native\libtensorflow.so" Condition="'$(IsLinux)' == 'true'" CopyToOutputDirectory="PreserveNewest"/>
88
<None Include="$(MSBuildThisFileDirectory)..\native\libtensorflow.dylib" Condition="'$(IsOSX)' == 'true'" CopyToOutputDirectory="PreserveNewest"/>
9-
<None Include="$(MSBuildThisFileDirectory)..\native\tensorflow.dll" Condition="'$(OS)' != 'Unix'" CopyToOutputDirectory="PreserveNewest"/>
9+
<None Include="$(MSBuildThisFileDirectory)..\native\libtensorflow.dll" Condition="'$(OS)' != 'Unix'" CopyToOutputDirectory="PreserveNewest"/>
1010
</ItemGroup>
1111
</Project>

TensorFlowSharp/Tensorflow.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public static class TFCore {
5454
[DllImport (NativeBinding.TensorFlowLibrary)]
5555
static extern unsafe IntPtr TF_Version ();
5656

57+
static TFCore ()
58+
{
59+
CheckSize ();
60+
}
61+
5762
/// <summary>
5863
/// Returns the version of the TensorFlow runtime in use.
5964
/// </summary>
@@ -84,6 +89,22 @@ public static TFBuffer GetAllOpList ()
8489
{
8590
return new TFBuffer (TF_GetAllOpList ());
8691
}
92+
93+
internal static void CheckSize ()
94+
{
95+
unsafe
96+
{
97+
if (sizeof (IntPtr) == 4) {
98+
Console.Error.WriteLine (
99+
"The TensorFlow native libraries were compiled in 64 bit mode, you must run in 64 bit mode\n" +
100+
"With Mono, do that with mono --arch=64 executable.exe, if using an IDE like MonoDevelop,\n" +
101+
"Xamarin Studio or Visual Studio for Mac, Build/Compiler settings, make sure that " +
102+
"\"Platform Target\" has x64 selected.");
103+
throw new Exception ();
104+
105+
}
106+
}
107+
}
87108
}
88109

89110
/// <summary>
@@ -106,6 +127,11 @@ public abstract class TFDisposable : IDisposable
106127
/// <value>The handle.</value>
107128
public IntPtr Handle => handle;
108129

130+
static TFDisposable ()
131+
{
132+
TFCore.CheckSize ();
133+
}
134+
109135
/// <summary>
110136
/// Initializes a new instance of the <see cref="T:TensorFlow.TFDisposable"/> class.
111137
/// </summary>
@@ -124,9 +150,9 @@ public TFDisposable (IntPtr handle)
124150
/// <summary>
125151
/// Releases all resource used by the <see cref="T:TensorFlow.TFDisposable"/> object.
126152
/// </summary>
127-
/// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="T:TensorFlow.TFDisposable"/>. The
128-
/// <see cref="Dispose"/> method leaves the <see cref="T:TensorFlow.TFDisposable"/> in an unusable state. After
129-
/// calling <see cref="Dispose"/>, you must release all references to the <see cref="T:TensorFlow.TFDisposable"/> so
153+
/// <remarks>Call Dispose when you are finished using the <see cref="T:TensorFlow.TFDisposable"/>. The
154+
/// Dispose method leaves the <see cref="T:TensorFlow.TFDisposable"/> in an unusable state. After
155+
/// calling Dispose, you must release all references to the <see cref="T:TensorFlow.TFDisposable"/> so
130156
/// the garbage collector can reclaim the memory that the <see cref="T:TensorFlow.TFDisposable"/> was occupying.</remarks>
131157
public void Dispose ()
132158
{

0 commit comments

Comments
 (0)