forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_api_util.cs
More file actions
135 lines (118 loc) · 5.31 KB
/
c_api_util.cs
File metadata and controls
135 lines (118 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace Tensorflow
{
public class c_api_util
{
static bool isDllDownloaded = false;
static object locker = new object();
public static void DownloadLibrary()
{
string dll = c_api.TensorFlowLibName;
string directory = AppDomain.CurrentDomain.BaseDirectory;
string file = "";
string url = "";
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
dll = $"{dll}.dll";
file = Path.Combine(directory, "libtensorflow-cpu-windows-x86_64-1.14.0.zip");
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.14.0.zip";
break;
case PlatformID.Unix:
dll = $"lib{dll}.so";
file = Path.Combine(directory, "libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz");
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz";
break;
default:
throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}");
}
if (isDllDownloaded || File.Exists($"{directory}/{dll}"))
{
isDllDownloaded = true;
return;
}
lock (locker)
{
if (!File.Exists(file))
{
var wc = new WebClient();
Binding.tf_output_redirect.WriteLine($"Downloading Tensorflow library from {url}...");
var download = Task.Run(() => wc.DownloadFile(url, file));
while (!download.IsCompleted)
{
Thread.Sleep(1000);
Binding.tf_output_redirect.Write(".");
}
Binding.tf_output_redirect.WriteLine("");
Binding.tf_output_redirect.WriteLine($"Downloaded successfully.");
}
Binding.tf_output_redirect.WriteLine($"Extracting...");
var task = Task.Run(() =>
{
switch (Environment.OSVersion.Platform)
{
case PlatformID.Win32NT:
ZipFile.ExtractToDirectory(file, directory);
Util.CmdHelper.Command($"move lib\\* .\\");
Util.CmdHelper.Command($"rm -r lib");
Util.CmdHelper.Command($"rm -r include");
break;
case PlatformID.Unix:
Util.CmdHelper.Bash($"tar xvzf {file} ./lib/");
Util.CmdHelper.Bash($"mv {directory}/lib/* {directory}");
Util.CmdHelper.Bash($"rm -r {directory}/lib");
break;
default:
throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}");
}
});
while (!task.IsCompleted)
{
Thread.Sleep(100);
Binding.tf_output_redirect.Write(".");
}
Binding.tf_output_redirect.WriteLine("");
Binding.tf_output_redirect.WriteLine("Extraction is completed.");
}
isDllDownloaded = true;
}
public static TF_Output tf_output(IntPtr c_op, int index) => new TF_Output(c_op, index);
public static ImportGraphDefOptions ScopedTFImportGraphDefOptions() => new ImportGraphDefOptions();
public static Buffer tf_buffer(byte[] data) => new Buffer(data);
public static IEnumerable<Operation> new_tf_operations(Graph graph)
{
foreach (var c_op in tf_operations(graph))
{
if (graph._get_operation_by_tf_operation(c_op) == null)
yield return c_op;
}
}
public static IEnumerable<Operation> tf_operations(Graph graph)
{
uint pos = 0;
IntPtr c_op;
while ((c_op = c_api.TF_GraphNextOperation(graph, ref pos)) != IntPtr.Zero)
{
yield return new Operation(c_op, graph);
}
}
}
}