| @@ -3,6 +3,7 @@ using ICSharpCode.SharpZipLib.Tar; | |||||
| using System; | using System; | ||||
| using System.IO; | using System.IO; | ||||
| using System.IO.Compression; | using System.IO.Compression; | ||||
| using System.Linq; | |||||
| using System.Threading; | using System.Threading; | ||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| @@ -12,6 +13,9 @@ namespace TensorFlowNET.Utility | |||||
| { | { | ||||
| public static void UnZip(String gzArchiveName, String destFolder) | public static void UnZip(String gzArchiveName, String destFolder) | ||||
| { | { | ||||
| var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin"; | |||||
| if (File.Exists(Path.Combine(destFolder, flag))) return; | |||||
| Console.WriteLine($"Extracting."); | Console.WriteLine($"Extracting."); | ||||
| var task = Task.Run(() => | var task = Task.Run(() => | ||||
| { | { | ||||
| @@ -24,12 +28,16 @@ namespace TensorFlowNET.Utility | |||||
| Console.Write("."); | Console.Write("."); | ||||
| } | } | ||||
| File.Create(Path.Combine(destFolder, flag)); | |||||
| Console.WriteLine(""); | Console.WriteLine(""); | ||||
| Console.WriteLine("Extracting is completed."); | Console.WriteLine("Extracting is completed."); | ||||
| } | } | ||||
| public static void ExtractTGZ(String gzArchiveName, String destFolder) | public static void ExtractTGZ(String gzArchiveName, String destFolder) | ||||
| { | { | ||||
| var flag = gzArchiveName.Split(Path.DirectorySeparatorChar).Last().Split('.').First() + ".bin"; | |||||
| if (File.Exists(Path.Combine(destFolder, flag))) return; | |||||
| Console.WriteLine($"Extracting."); | Console.WriteLine($"Extracting."); | ||||
| var task = Task.Run(() => | var task = Task.Run(() => | ||||
| { | { | ||||
| @@ -49,6 +57,7 @@ namespace TensorFlowNET.Utility | |||||
| Console.Write("."); | Console.Write("."); | ||||
| } | } | ||||
| File.Create(Path.Combine(destFolder, flag)); | |||||
| Console.WriteLine(""); | Console.WriteLine(""); | ||||
| Console.WriteLine("Extracting is completed."); | Console.WriteLine("Extracting is completed."); | ||||
| } | } | ||||
| @@ -88,8 +88,7 @@ namespace TensorFlowNET.Examples | |||||
| string zipFile = Path.Join(dir, "inception5h.zip"); | string zipFile = Path.Join(dir, "inception5h.zip"); | ||||
| Utility.Web.Download(url, zipFile); | Utility.Web.Download(url, zipFile); | ||||
| if (!File.Exists(Path.Join(dir, pbFile))) | |||||
| Utility.Compress.UnZip(zipFile, dir); | |||||
| Utility.Compress.UnZip(zipFile, dir); | |||||
| // download sample picture | // download sample picture | ||||
| string pic = Path.Join(dir, "img", "grace_hopper.jpg"); | string pic = Path.Join(dir, "img", "grace_hopper.jpg"); | ||||
| @@ -106,8 +106,7 @@ namespace TensorFlowNET.Examples | |||||
| string zipFile = Path.Join(dir, $"{pbFile}.tar.gz"); | string zipFile = Path.Join(dir, $"{pbFile}.tar.gz"); | ||||
| Utility.Web.Download(url, zipFile); | Utility.Web.Download(url, zipFile); | ||||
| if (!File.Exists(Path.Join(dir, pbFile))) | |||||
| Utility.Compress.ExtractTGZ(zipFile, dir); | |||||
| Utility.Compress.ExtractTGZ(zipFile, dir); | |||||
| // download sample picture | // download sample picture | ||||
| string pic = "grace_hopper.jpg"; | string pic = "grace_hopper.jpg"; | ||||
| @@ -3,12 +3,14 @@ using System.Collections.Generic; | |||||
| using System.IO; | using System.IO; | ||||
| using System.Text; | using System.Text; | ||||
| using Tensorflow; | using Tensorflow; | ||||
| using NumSharp.Core; | |||||
| namespace TensorFlowNET.Examples | namespace TensorFlowNET.Examples | ||||
| { | { | ||||
| public class TextClassificationWithMovieReviews : Python, IExample | public class TextClassificationWithMovieReviews : Python, IExample | ||||
| { | { | ||||
| string dir = "text_classification_with_movie_reviews"; | string dir = "text_classification_with_movie_reviews"; | ||||
| string dataFile = "imdb.zip"; | |||||
| public void Run() | public void Run() | ||||
| { | { | ||||
| @@ -17,17 +19,27 @@ namespace TensorFlowNET.Examples | |||||
| private void PrepareData() | private void PrepareData() | ||||
| { | { | ||||
| Directory.CreateDirectory(dir); | Directory.CreateDirectory(dir); | ||||
| // get model file | // get model file | ||||
| string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz"; | |||||
| string url = $"https://github.com/SciSharp/TensorFlow.NET/raw/master/data/{dataFile}"; | |||||
| string zipFile = Path.Join(dir, $"imdb.zip"); | string zipFile = Path.Join(dir, $"imdb.zip"); | ||||
| Utility.Web.Download(url, zipFile); | Utility.Web.Download(url, zipFile); | ||||
| Utility.Compress.UnZip(zipFile, dir); | |||||
| // prepare training dataset | |||||
| NDArray x_train = File.ReadAllLines(Path.Join(dir, "x_train.txt")); | |||||
| NDArray labels_train = File.ReadAllLines(Path.Join(dir, "y_train.txt")); | |||||
| NDArray indices_train = File.ReadAllLines(Path.Join(dir, "indices_train.txt")); | |||||
| x_train = x_train[indices_train]; | |||||
| labels_train = labels_train[indices_train]; | |||||
| if (!File.Exists(Path.Join(dir, zipFile))) | |||||
| Utility.Compress.ExtractTGZ(zipFile, dir); | |||||
| NDArray x_test = File.ReadAllLines(Path.Join(dir, "x_test.txt")); | |||||
| NDArray labels_test = File.ReadAllLines(Path.Join(dir, "y_test.txt")); | |||||
| NDArray indices_test = File.ReadAllLines(Path.Join(dir, "indices_test.txt")); | |||||
| x_test = x_test[indices_test]; | |||||
| labels_test = labels_test[indices_test]; | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||