diff --git a/src/TensorFlowNET.Core/Framework/c_api_util.cs b/src/TensorFlowNET.Core/Framework/c_api_util.cs index e1e602a7..efae3e55 100644 --- a/src/TensorFlowNET.Core/Framework/c_api_util.cs +++ b/src/TensorFlowNET.Core/Framework/c_api_util.cs @@ -31,34 +31,40 @@ namespace Tensorflow static object locker = new object(); public static void DownloadLibrary() { - string dll = $"{c_api.TensorFlowLibName}.dll"; - - string runtime = "win-x64"; + string dll = c_api.TensorFlowLibName; + string directory = AppDomain.CurrentDomain.BaseDirectory; + string file = ""; + string url = ""; switch (Environment.OSVersion.Platform) { case PlatformID.Win32NT: - runtime = "win-x64"; + dll = $"{dll}.dll"; + file = Path.Combine(directory, "tensorflow.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(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dll))) + if (isDllDownloaded || File.Exists($"{directory}/{dll}")) { isDllDownloaded = true; return; } - string url = $"https://github.com/SciSharp/TensorFlow.NET/raw/master/tensorflowlib/runtimes/{runtime}/native/tensorflow.zip"; lock (locker) { - if (!File.Exists("tensorflow.zip")) + if (!File.Exists(file)) { var wc = new WebClient(); - Console.WriteLine($"Downloading Tensorflow library..."); - var download = Task.Run(() => wc.DownloadFile(url, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tensorflow.zip"))); + Console.WriteLine($"Downloading Tensorflow library from {url}..."); + var download = Task.Run(() => wc.DownloadFile(url, file)); while (!download.IsCompleted) { Thread.Sleep(1000); @@ -71,7 +77,19 @@ namespace Tensorflow Console.WriteLine($"Extracting..."); var task = Task.Run(() => { - ZipFile.ExtractToDirectory("tensorflow.zip", AppDomain.CurrentDomain.BaseDirectory); + switch (Environment.OSVersion.Platform) + { + case PlatformID.Win32NT: + ZipFile.ExtractToDirectory(file, directory); + 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) diff --git a/src/TensorFlowNET.Core/Util/CmdHelper.cs b/src/TensorFlowNET.Core/Util/CmdHelper.cs new file mode 100644 index 00000000..d0178636 --- /dev/null +++ b/src/TensorFlowNET.Core/Util/CmdHelper.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Threading; + +namespace Tensorflow.Util +{ + public static class CmdHelper + { + public static void Bash(string command) + { + Process proc = new System.Diagnostics.Process (); + proc.StartInfo.FileName = "/bin/bash"; + proc.StartInfo.Arguments = "-c \" " + command + " \""; + proc.StartInfo.UseShellExecute = false; + proc.StartInfo.RedirectStandardOutput = true; + proc.Start (); + + while (!proc.StandardOutput.EndOfStream) { + Console.WriteLine (proc.StandardOutput.ReadLine ()); + } + } + } +} diff --git a/tensorflowlib/runtimes/linux-x64/native/tensorflow.zip b/tensorflowlib/runtimes/linux-x64/native/tensorflow.zip deleted file mode 100644 index f297f10e..00000000 Binary files a/tensorflowlib/runtimes/linux-x64/native/tensorflow.zip and /dev/null differ