|
| 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 | +} |
0 commit comments