Skip to content

Commit b44fda9

Browse files
committed
remove Microsoft.ML.TensorFlow.Redist dependency.
1 parent 4cd985a commit b44fda9

File tree

11 files changed

+132
-55
lines changed

11 files changed

+132
-55
lines changed

src/TensorFlowNET.Core/APIs/c_api.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using System.Net;
1920
using System.Runtime.InteropServices;
2021
using System.Text;
2122

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*****************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. 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 System;
18+
using System.Collections.Generic;
19+
using System.IO;
20+
using System.IO.Compression;
21+
using System.Net;
22+
using System.Text;
23+
using System.Threading;
24+
using System.Threading.Tasks;
25+
26+
namespace Tensorflow
27+
{
28+
public class c_api_util
29+
{
30+
static bool isDllDownloaded = false;
31+
static object locker = new object();
32+
public static void DownloadLibrary()
33+
{
34+
string dll = $"{c_api.TensorFlowLibName}.dll";
35+
36+
string runtime = "win-x64";
37+
38+
switch (Environment.OSVersion.Platform)
39+
{
40+
case PlatformID.Win32NT:
41+
runtime = "win-x64";
42+
break;
43+
default:
44+
throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}");
45+
}
46+
47+
if (isDllDownloaded || File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dll)))
48+
{
49+
isDllDownloaded = true;
50+
return;
51+
}
52+
53+
string url = $"https://github.com/SciSharp/TensorFlow.NET/raw/master/tensorflowlib/runtimes/{runtime}/native/tensorflow.zip";
54+
55+
lock (locker)
56+
{
57+
if (!File.Exists("tensorflow.zip"))
58+
{
59+
var wc = new WebClient();
60+
Console.WriteLine($"Downloading Tensorflow library...");
61+
var download = Task.Run(() => wc.DownloadFile(url, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tensorflow.zip")));
62+
while (!download.IsCompleted)
63+
{
64+
Thread.Sleep(1000);
65+
Console.Write(".");
66+
}
67+
Console.WriteLine("");
68+
Console.WriteLine($"Downloaded successfully.");
69+
}
70+
71+
Console.WriteLine($"Extracting...");
72+
var task = Task.Run(() =>
73+
{
74+
ZipFile.ExtractToDirectory("tensorflow.zip", AppDomain.CurrentDomain.BaseDirectory);
75+
});
76+
77+
while (!task.IsCompleted)
78+
{
79+
Thread.Sleep(100);
80+
Console.Write(".");
81+
}
82+
83+
Console.WriteLine("");
84+
Console.WriteLine("Extraction is completed.");
85+
}
86+
87+
isDllDownloaded = true;
88+
}
89+
90+
public static TF_Output tf_output(IntPtr c_op, int index) => new TF_Output(c_op, index);
91+
92+
public static ImportGraphDefOptions ScopedTFImportGraphDefOptions() => new ImportGraphDefOptions();
93+
94+
public static Buffer tf_buffer(byte[] data) => new Buffer(data);
95+
96+
public static IEnumerable<Operation> new_tf_operations(Graph graph)
97+
{
98+
foreach (var c_op in tf_operations(graph))
99+
{
100+
if (graph._get_operation_by_tf_operation(c_op) == null)
101+
yield return c_op;
102+
}
103+
}
104+
105+
public static IEnumerable<Operation> tf_operations(Graph graph)
106+
{
107+
uint pos = 0;
108+
IntPtr c_op;
109+
while ((c_op = c_api.TF_GraphNextOperation(graph, ref pos)) != IntPtr.Zero)
110+
{
111+
yield return c_op;
112+
}
113+
}
114+
}
115+
}

src/TensorFlowNET.Core/Framework/c_api_util.py.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public partial class Graph : IPython, IDisposable
106106

107107
public Graph()
108108
{
109+
c_api_util.DownloadLibrary();
110+
109111
_handle = c_api.TF_NewGraph();
110112
Status = new Status();
111113
_nodes_by_id = new Dictionary<int, ITensorOrOperation>();
@@ -116,6 +118,8 @@ public Graph()
116118

117119
public Graph(IntPtr handle)
118120
{
121+
c_api_util.DownloadLibrary();
122+
119123
_handle = handle;
120124
Status = new Status();
121125
_nodes_by_id = new Dictionary<int, ITensorOrOperation>();

src/TensorFlowNET.Core/Status/Status.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class Status : IDisposable
4040

4141
public Status()
4242
{
43+
c_api_util.DownloadLibrary();
4344
_handle = c_api.TF_NewStatus();
4445
}
4546

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ Learn more about .NET AI: https://medium.com/scisharp</Description>
5959

6060
<ItemGroup>
6161
<PackageReference Include="Google.Protobuf" Version="3.8.0" />
62-
<PackageReference Include="Microsoft.ML.TensorFlow.Redist" Version="0.14.0" />
6362
<PackageReference Include="NumSharp" Version="0.10.3" />
6463
</ItemGroup>
6564

src/TensorFlowNET.Core/tf.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public static partial class tf
3838

3939
public static Session defaultSession;
4040

41-
public static RefVariable Variable<T>(T data,
41+
public static RefVariable Variable<T>(T data,
4242
bool trainable = true,
4343
bool validate_shape = true,
44-
string name = null,
44+
string name = null,
4545
TF_DataType dtype = TF_DataType.DtInvalid)
4646
{
47-
return Tensorflow.variable_scope.default_variable_creator(data,
47+
return Tensorflow.variable_scope.default_variable_creator(data,
4848
trainable: trainable,
4949
validate_shape: validate_shape,
5050
name: name,
@@ -62,7 +62,14 @@ public static void enable_eager_execution()
6262
context.default_execution_mode = Context.EAGER_MODE;
6363
}
6464

65-
public static string VERSION => c_api.StringPiece(c_api.TF_Version());
65+
public static string VERSION
66+
{
67+
get
68+
{
69+
c_api_util.DownloadLibrary();
70+
return c_api.StringPiece(c_api.TF_Version());
71+
}
72+
}
6673

6774
public static Session Session()
6875
{

tensorflowlib/runtimes/linux-x64/native/libtensorflow.so

Whitespace-only changes.

tensorflowlib/runtimes/linux-x64/native/libtensorflow_framework.so

Whitespace-only changes.
56.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)