Skip to content

Commit e72d7ca

Browse files
authored
Merge pull request #285 from Esther2013/master
download tensorflow library automatically on Linux.
2 parents b44fda9 + ddde883 commit e72d7ca

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/TensorFlowNET.Core/Framework/c_api_util.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,40 @@ public class c_api_util
3131
static object locker = new object();
3232
public static void DownloadLibrary()
3333
{
34-
string dll = $"{c_api.TensorFlowLibName}.dll";
35-
36-
string runtime = "win-x64";
34+
string dll = c_api.TensorFlowLibName;
35+
string directory = AppDomain.CurrentDomain.BaseDirectory;
36+
string file = "";
37+
string url = "";
3738

3839
switch (Environment.OSVersion.Platform)
3940
{
4041
case PlatformID.Win32NT:
41-
runtime = "win-x64";
42+
dll = $"{dll}.dll";
43+
file = Path.Combine(directory, "tensorflow.zip");
44+
break;
45+
case PlatformID.Unix:
46+
dll = $"lib{dll}.so";
47+
file = Path.Combine(directory, "libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz");
48+
url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz";
4249
break;
4350
default:
4451
throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}");
4552
}
4653

47-
if (isDllDownloaded || File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dll)))
54+
if (isDllDownloaded || File.Exists($"{directory}/{dll}"))
4855
{
4956
isDllDownloaded = true;
5057
return;
5158
}
5259

53-
string url = $"https://github.com/SciSharp/TensorFlow.NET/raw/master/tensorflowlib/runtimes/{runtime}/native/tensorflow.zip";
5460

5561
lock (locker)
5662
{
57-
if (!File.Exists("tensorflow.zip"))
63+
if (!File.Exists(file))
5864
{
5965
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")));
66+
Console.WriteLine($"Downloading Tensorflow library from {url}...");
67+
var download = Task.Run(() => wc.DownloadFile(url, file));
6268
while (!download.IsCompleted)
6369
{
6470
Thread.Sleep(1000);
@@ -71,7 +77,19 @@ public static void DownloadLibrary()
7177
Console.WriteLine($"Extracting...");
7278
var task = Task.Run(() =>
7379
{
74-
ZipFile.ExtractToDirectory("tensorflow.zip", AppDomain.CurrentDomain.BaseDirectory);
80+
switch (Environment.OSVersion.Platform)
81+
{
82+
case PlatformID.Win32NT:
83+
ZipFile.ExtractToDirectory(file, directory);
84+
break;
85+
case PlatformID.Unix:
86+
Util.CmdHelper.Bash($"tar xvzf {file} ./lib/");
87+
Util.CmdHelper.Bash($"mv {directory}/lib/* {directory}");
88+
Util.CmdHelper.Bash($"rm -r {directory}/lib");
89+
break;
90+
default:
91+
throw new RuntimeError($"Unknown OS environment: {Environment.OSVersion.Platform}");
92+
}
7593
});
7694

7795
while (!task.IsCompleted)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Text;
6+
using System.Threading;
7+
8+
namespace Tensorflow.Util
9+
{
10+
public static class CmdHelper
11+
{
12+
public static void Bash(string command)
13+
{
14+
Process proc = new System.Diagnostics.Process ();
15+
proc.StartInfo.FileName = "/bin/bash";
16+
proc.StartInfo.Arguments = "-c \" " + command + " \"";
17+
proc.StartInfo.UseShellExecute = false;
18+
proc.StartInfo.RedirectStandardOutput = true;
19+
proc.Start ();
20+
21+
while (!proc.StandardOutput.EndOfStream) {
22+
Console.WriteLine (proc.StandardOutput.ReadLine ());
23+
}
24+
}
25+
}
26+
}
-56.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)