From 44dd7b483e2720359145de7aba301550f678c309 Mon Sep 17 00:00:00 2001 From: Zhou Wang Date: Mon, 29 Aug 2016 11:53:33 +0200 Subject: [PATCH] Add check for existance of files needed to run the sample. --- .../CPPEvalClient/CPPEvalClient.cpp | 13 +++- Examples/Evaluation/CSEvalClient/Program.cs | 59 ++++++++++++++++--- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/Examples/Evaluation/CPPEvalClient/CPPEvalClient.cpp b/Examples/Evaluation/CPPEvalClient/CPPEvalClient.cpp index 2bc74af5d..60b1442d0 100644 --- a/Examples/Evaluation/CPPEvalClient/CPPEvalClient.cpp +++ b/Examples/Evaluation/CPPEvalClient/CPPEvalClient.cpp @@ -4,6 +4,7 @@ // // CPPEvalClient.cpp : Sample application using the evaluation interface from C++ // +#include #include "Eval.h" #ifdef _WIN32 #include "Windows.h" @@ -52,11 +53,17 @@ int main(int argc, char* argv[]) // This relative path assumes launching from CNTK's binary folder, e.g. build/release/bin/ const std::string modelWorkingDirectory = path + "/../../../Examples/Image/MNIST/Data/"; #endif - - GetEvalF(&model); - const std::string modelFilePath = modelWorkingDirectory + "../Output/Models/01_OneHidden"; + struct stat statBuf; + if (stat(modelFilePath.c_str(), &statBuf) != 0) + { + printf("Error: The model %s does not exist. Please run the example in /Examples/Image/MNIST to create the model.\n", modelFilePath.c_str()); + exit(1); + } + + GetEvalF(&model); + // Load model with desired outputs std::string networkConfiguration; // Uncomment the following line to re-define the outputs (include h1.z AND the output ol.z) diff --git a/Examples/Evaluation/CSEvalClient/Program.cs b/Examples/Evaluation/CSEvalClient/Program.cs index 99a6ff0ba..c8c7bb39c 100644 --- a/Examples/Evaluation/CSEvalClient/Program.cs +++ b/Examples/Evaluation/CSEvalClient/Program.cs @@ -110,6 +110,12 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient { // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\01_OneHidden"); + if (!File.Exists(modelFilePath)) + { + Console.WriteLine("Error: The model {0} does not exist. Please run the example in /Examples/Image/MNIST to create the model.", modelFilePath); + throw new FileNotFoundException(string.Format("File {0} not found.", modelFilePath)); + } + model.CreateNetwork(string.Format("modelPath=\"{0}\"", modelFilePath), deviceId: -1); // Generate random input values in the appropriate structure and size @@ -159,6 +165,12 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\01_OneHidden"); + if (!File.Exists(modelFilePath)) + { + Console.WriteLine("Error: The model {0} does not exist. Please run the example in /Examples/Image/MNIST to create the model.", modelFilePath); + throw new FileNotFoundException(string.Format("File {0} not found.", modelFilePath)); + } + var desiredOutputLayers = new List() { hiddenLayerName, outputLayerName }; model.CreateNetwork(string.Format("modelPath=\"{0}\"", modelFilePath), deviceId: -1, outputNodeNames: desiredOutputLayers); @@ -210,7 +222,14 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient // Create the network // This network (AddOperatorConstant.cntk) is a simple network consisting of a single binary operator (Plus) // operating over a single input and a constant - string networkDescription = File.ReadAllText(Path.Combine(workingDirectory, @"AddOperatorConstant.cntk")); + string networkFilePath = Path.Combine(workingDirectory, @"AddOperatorConstant.cntk"); + if (!File.Exists(networkFilePath)) + { + Console.WriteLine("Error: The network configuration file {0} does not exist.", networkFilePath); + throw new FileNotFoundException(string.Format("File {0} not found.", networkFilePath)); + } + + string networkDescription = File.ReadAllText(networkFilePath); model.CreateNetwork(networkDescription, deviceId: -1); // Prepare input value in the appropriate structure and size @@ -253,7 +272,14 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient // Create the network // This network (AddOperatorConstantNoInput.cntk) is a simple network consisting of a single binary operator (Plus) // operating over a two constants, therefore no input is necessary. - string networkDescription = File.ReadAllText(Path.Combine(workingDirectory, @"AddOperatorConstantNoInput.cntk")); + string networkFilePath = Path.Combine(workingDirectory, @"AddOperatorConstantNoInput.cntk"); + if (!File.Exists(networkFilePath)) + { + Console.WriteLine("Error: The network configuration file {0} does not exist.", networkFilePath); + throw new FileNotFoundException(string.Format("File {0} not found.", networkFilePath)); + } + + string networkDescription = File.ReadAllText(networkFilePath); model.CreateNetwork(networkDescription, deviceId: -1); // We can call the evaluate method and get back the results (single layer)... @@ -289,11 +315,6 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient try { - // The examples assume the executable is running from the data folder - // We switch the current directory to the data folder (assuming the executable is in the /x64/Debug|Release folder - string workingDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Other\Simple2d\Config"); - Environment.CurrentDirectory = initialDirectory; - using (var model = new ModelEvaluationExtendedF()) { // Create the network @@ -352,14 +373,26 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient // The examples assume the executable is running from the data folder // We switch the current directory to the data folder (assuming the executable is in the /x64/Debug|Release folder Environment.CurrentDirectory = Path.Combine(initialDirectory, @"..\..\Examples\Image\MNIST\Data\"); + // Load model string modelFilePath = Path.Combine(Environment.CurrentDirectory, @"..\Output\Models\02_Convolution"); + if (!File.Exists(modelFilePath)) + { + Console.WriteLine("Error: The model {0} does not exist. Please run the example in /Examples/Image/MNIST to create the model.", modelFilePath); + throw new FileNotFoundException(string.Format("File {0} not found.", modelFilePath)); + } // Initializes the model instances ModelEvaluator.Initialize(numConcurrentModels, modelFilePath); string testfile = Path.Combine(Environment.CurrentDirectory, @"Test-28x28_cntk_text.txt"); + if (!File.Exists(testfile)) + { + Console.WriteLine("Error: The test file {0} does not exist. Please read README.md in /Examples/Image/MNIST to download the data file", testfile); + throw new FileNotFoundException(string.Format("File {0} not found.", testfile)); + } + Stopwatch sw = new Stopwatch(); sw.Start(); @@ -436,6 +469,12 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient using (var model = new IEvaluateModelManagedF()) { string modelFilePath = Path.Combine(workingDirectory, "ResNet_18.model"); + if (!File.Exists(modelFilePath)) + { + Console.WriteLine("Error: The model {0} does not exist. Please download the model from https://www.cntk.ai/resnet/ResNet_18.model and save it under ..\\..\\Examples\\Image\\Miscellaneous\\ImageNet\\ResNet.", modelFilePath); + throw new FileNotFoundException(string.Format("File {0} not found.", modelFilePath)); + } + model.CreateNetwork(string.Format("modelPath=\"{0}\"", modelFilePath), deviceId: -1); // Prepare input value in the appropriate structure and size @@ -447,6 +486,12 @@ namespace Microsoft.MSR.CNTK.Extensibility.Managed.CSEvalClient // Transform the image string imageFileName = Path.Combine(workingDirectory, "zebra.jpg"); + if (!File.Exists(imageFileName)) + { + Console.WriteLine("Error: The test image file {0} does not exist.", imageFileName); + throw new FileNotFoundException(string.Format("File {0} not found.", imageFileName)); + } + Bitmap bmp = new Bitmap(Bitmap.FromFile(imageFileName)); var resized = bmp.Resize(224, 224, true);