Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/TensorFlowNET.Core/Framework/c_api_util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,40 @@ public class c_api_util
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);
Expand All @@ -71,7 +77,19 @@ public static void DownloadLibrary()
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)
Expand Down
26 changes: 26 additions & 0 deletions src/TensorFlowNET.Core/Util/CmdHelper.cs
Original file line number Diff line number Diff line change
@@ -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 ());
}
}
}
}
Binary file not shown.