示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("======== Evaluate model using C# GPU Build ========");

            Console.WriteLine(" ====== Run evaluation on CPU =====");

            // Evalaute a single image.
            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);

            // Evaluate a batch of images
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);

            // Evaluate multiple sample requests in parallel
            CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.CPUDevice);

            // Evaluate a single sequence with one-hot vector
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);

            // Evalaute a batch of variable length sequences with one-hot vector
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);

            // Use GPU for evaluation.
            Console.WriteLine(" ====== Run evaluation on GPU =====");
            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.GPUDevice(0));

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#2
0
文件: Program.cs 项目: tao2yue/CNTK
        static void Main(string[] args)
        {
            Console.WriteLine("======== Evaluate model using C# CPUOnly Build ========");

            // Evalaute a single image.
            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);

            // Evaluate a batch of images
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);

            // Evaluate an image asynchronously
            Task evalTask = CNTKLibraryManagedExamples.EvaluationSingleImageAsync(DeviceDescriptor.CPUDevice);

            evalTask.Wait();

            // Evaluate multiple sample requests in parallel
            CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.CPUDevice);

            // Evaluate a single sequence with one-hot vector
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);

            // Evalaute a batch of variable length sequences with one-hot vector
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);

            Console.WriteLine("======== Evaluation completes. ========");
        }
 public static void WriteOutputs()
 {
     // enforce GC.
     GC.Collect();
     GC.WaitForPendingFinalizers();
     Console.WriteLine("\nPrint out saved object references.");
     try
     {
         Console.WriteLine("Device0: " + Device0.AsString() + ", Type: " + Device0.Type);
         Console.WriteLine("Axis0: " + Axis0.Name + ", IsStaticAxis: " + Axis0.IsStatic);
         Console.WriteLine("OutputVar: " + OutputVar.AsString() + ", Name: " + OutputVar.Name + ", Kind: " + Utils.VariableKindName(OutputVar.Kind) + ", Shape: " + OutputVar.Shape.AsString());
         Console.WriteLine("OutputVar0: " + OutputVar0.AsString() + ", Name: " + OutputVar0.Name + ", Kind: " + Utils.VariableKindName(OutputVar.Kind) + ", Shape: " + OutputVar0.Shape.AsString());
         Console.WriteLine("InputVar0: " + InputVar0.AsString() + ", Name: " + InputVar0.Name + ", Kind: " + Utils.VariableKindName(OutputVar.Kind) + ", Shape: " + InputVar0.Shape.AsString());
         Console.WriteLine("ArgumentVar0: " + ArgumentVar0.AsString() + ", Name: " + ArgumentVar0.Name + ", Kind: " + Utils.VariableKindName(OutputVar.Kind) + ", Shape: " + ArgumentVar0.Shape.AsString());
         Console.WriteLine("OutputVal: " + ", Device: " + OutputVal.Device.AsString() + ", Storage: " + OutputVal.StorageFormat + ", Shape: " + OutputVal.Shape.AsString() + "Data:");
         var outputData = OutputVal.GetDenseData <float>(OutputVar);
         CNTKLibraryManagedExamples.PrintOutput(OutputVar.Shape.TotalSize, outputData);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Memory Tests Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
         throw ex;
     }
     Console.WriteLine("\nAll saved object references are printed.");
 }
示例#4
0
        static void Main(string[] args)
        {
#if CPUONLY
            Console.WriteLine("======== Evaluate model on CPU using CPUOnly build ========");
#else
            Console.WriteLine("======== Evaluate model on CPU using GPU build ========");
#endif

            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);
            CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.CPUDevice);
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);

            if (IsGPUAvailable())
            {
                Console.WriteLine(" ====== Evaluate model on GPU =====");
                CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.GPUDevice(0));
            }

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#5
0
        static void Main(string[] args)
        {
#if CPUONLY
            Console.WriteLine("======== Evaluate model on CPU using CPUOnly build ========");
#else
            Console.WriteLine("======== Evaluate model on CPU using GPU build ========");
#endif

            if (ShouldRunOnCpu())
            {
                CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);
                CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.CPUDevice);
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);
                // It is sufficient to test loading model from memory buffer only on CPU.
                CNTKLibraryManagedExamples.LoadModelFromMemory(DeviceDescriptor.CPUDevice);
            }

            if (ShouldRunOnGpu())
            {
                Console.WriteLine(" ====== Evaluate model on GPU =====");
                CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.GPUDevice(0));
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.GPUDevice(0));
            }

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#6
0
文件: Program.cs 项目: zhal-x/CNTK
        static void Main(string[] args)
        {
            Console.WriteLine("======== Evaluate model using C# GPU Build ========");

            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.GPUDevice(0));

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#7
0
        static void Main(string[] args)
        {
            Console.WriteLine("======== Evaluate model using C# GPU Build ========");

            Console.WriteLine(" ====== Run evaluation on CPU =====");

            // Evaluate a single image.
            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);

            // Evaluate a batch of images.
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);

            // Evaluate multiple sample requests in parallel.
            CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallelAsync(DeviceDescriptor.CPUDevice).Wait();

            // Evaluate an image asynchronously.
            Task evalTask = CNTKLibraryManagedExamples.EvaluationSingleImageAsync(DeviceDescriptor.CPUDevice);

            evalTask.Wait();

            // Evaluate a single sequence using one-hot vector input.
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);

            // Evaluate a batch of variable length sequences with one-hot vector input.
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);

            // Evaluate a sequence using sparse input.
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingSparse(DeviceDescriptor.CPUDevice);

            // Load model from memory buffer.
            CNTKLibraryManagedExamples.LoadModelFromMemory(DeviceDescriptor.CPUDevice);

            // Use GPU for evaluation.
            Console.WriteLine(" ====== Run evaluation on GPU =====");
            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.GPUDevice(0));

            // Evaluate an image asynchronously
            evalTask = CNTKLibraryManagedExamples.EvaluationSingleImageAsync(DeviceDescriptor.GPUDevice(0));
            evalTask.Wait();

            CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallelAsync(DeviceDescriptor.GPUDevice(0)).Wait();
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingSparse(DeviceDescriptor.GPUDevice(0));
            CNTKLibraryManagedExamples.LoadModelFromMemory(DeviceDescriptor.GPUDevice(0));

            // Evaluate intermediate layer.
            CNTKLibraryManagedExamples.EvaluateIntermediateLayer(DeviceDescriptor.GPUDevice(0));

            // Evaluate combined outputs.
            CNTKLibraryManagedExamples.EvaluateCombinedOutputs(DeviceDescriptor.GPUDevice(0));

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#8
0
文件: Program.cs 项目: yaronyg/CNTK
        static void Main(string[] args)
        {
#if CPUONLY
            Console.WriteLine("======== Evaluate model on CPU using CPUOnly build ========");
#else
            Console.WriteLine("======== Evaluate model on CPU using GPU build ========");
#endif

            if (ShouldRunOnCpu())
            {
                var device = DeviceDescriptor.CPUDevice;

                CNTKLibraryManagedExamples.EvaluationSingleImage(device);
                // Run memory tests.
                MemoryTests.ValidateObjectReferences(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfImages(device);
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(device);
                // Run memory tests again.
                MemoryTests.ValidateObjectReferences(device);

                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingSparse(device);
                // It is sufficient to test loading model from memory buffer only on CPU.
                CNTKLibraryManagedExamples.LoadModelFromMemory(device);

                MemoryTests.WriteOutputs();
            }

            if (ShouldRunOnGpu())
            {
                Console.WriteLine(" ====== Evaluate model on GPU =====");
                var device = DeviceDescriptor.GPUDevice(0);
                // Run memory tests.
                MemoryTests.ValidateObjectReferences(device);
                CNTKLibraryManagedExamples.EvaluationSingleImage(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfImages(device);
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallel(device);
                // Run memory tests.
                MemoryTests.ValidateObjectReferences(device);

                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingSparse(device);

                // Run memory tests again.
                MemoryTests.WriteOutputs();
            }

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#9
0
        static void Main(string[] args)
        {
            Console.WriteLine("======== Evaluate model using C# ========");

            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);
            //TODO: Add examples with OneHot.
            //EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);
            //EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);

            // TODO: using GPU.
            //EvaluationSingleImage(DeviceDescriptor.GPUDevice(0));
            //EvaluationBatchOfImages(DeviceDescriptor.GPUDevice(0));

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#10
0
文件: Program.cs 项目: wybosys/CNTK
        static void Main(string[] args)
        {
            Console.WriteLine("======== Evaluate model using C# CPUOnly Build ========");

            // Evalaute a single image.
            CNTKLibraryManagedExamples.EvaluationSingleImage(DeviceDescriptor.CPUDevice);

            // Evaluate a batch of images
            CNTKLibraryManagedExamples.EvaluationBatchOfImages(DeviceDescriptor.CPUDevice);

            // Evaluate a single sequence with one-hot vector
            CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(DeviceDescriptor.CPUDevice);

            // Evalaute a batch of variable length sequences with one-hot vector
            CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor.CPUDevice);

            Console.WriteLine("======== Evaluation completes. ========");
        }
        static void Main(string[] args)
        {
            // Todo: move to a separate unit test.
            Console.WriteLine("Test Utils");

            int maxThreads = Utils.GetMaxNumCPUThreads();

            Utils.SetMaxNumCPUThreads(2);
            Console.WriteLine("MaxNumCPUThreads: before: " + maxThreads + ", after " + Utils.GetMaxNumCPUThreads());
            Utils.SetMaxNumCPUThreads(maxThreads);
            Console.WriteLine("reset MaxNumCPuThreads to " + Utils.GetMaxNumCPUThreads());

            var level = Utils.GetTraceLevel();

            Utils.SetTraceLevel(TraceLevel.Info);
            Console.WriteLine("TraceLevel: before: " + level + ", after " + Utils.GetTraceLevel());
            Utils.SetTraceLevel(level);
            Console.WriteLine("reset TraceLevel to " + Utils.GetTraceLevel());

            Console.WriteLine(Utils.DataTypeName(DataType.Float));
            Console.WriteLine(Utils.DataTypeSize(DataType.Double));
            Console.WriteLine(Utils.DeviceKindName(DeviceDescriptor.CPUDevice.Type));
            Console.WriteLine(Utils.DeviceKindName(DeviceKind.GPU));
            Console.WriteLine(Utils.IsSparseStorageFormat(StorageFormat.Dense));
            Console.WriteLine(Utils.IsSparseStorageFormat(StorageFormat.SparseCSC));
            Console.WriteLine(Utils.IsSparseStorageFormat(StorageFormat.SparseBlockCol));
            Console.WriteLine(Utils.VariableKindName(VariableKind.Constant));
            Console.WriteLine(Utils.VariableKindName(VariableKind.Placeholder));
            Console.WriteLine(Utils.VariableKindName(VariableKind.Input));
            Console.WriteLine(Utils.VariableKindName(VariableKind.Output));
            Console.WriteLine(Utils.VariableKindName(VariableKind.Parameter));

#if CPUONLY
            Console.WriteLine("======== Evaluate model on CPU using CPUOnly build ========");
#else
            Console.WriteLine("======== Evaluate model on CPU using GPU build ========");
#endif
            if (args.Length >= 1)
            {
                Console.WriteLine($"-------- running with test data in {args[0]} --------");
                CNTKLibraryManagedExamples.ExampleTestDataDir = args[0];
            }
            CNTKLibraryManagedExamples.Setup();

            if (ShouldRunOnCpu())
            {
                var device = DeviceDescriptor.CPUDevice;

                CNTKLibraryManagedExamples.EvaluationSingleImage(device);

                // Run memory tests.
                MemoryTests.ValidateObjectReferences(device);

                CNTKLibraryManagedExamples.EvaluationBatchOfImages(device);

                MemoryTests.WriteOutputs();
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallelAsync(device).Wait();

                // Run memory tests again.
                MemoryTests.ValidateObjectReferences(device);

                Task evalTask = CNTKLibraryManagedExamples.EvaluationSingleImageAsync(device);
                evalTask.Wait();

                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingSparse(device);
                // It is sufficient to test loading model from memory buffer only on CPU.
                CNTKLibraryManagedExamples.LoadModelFromMemory(device);

                MemoryTests.WriteOutputs();

                MemoryTests.ValueCopyToSparseCSCTest <float>(device);
                MemoryTests.ValueCopyToSparseCSCTest <double>(device);

                CNTKLibraryManagedExamples.EvaluateIntermediateLayer(device);
                CNTKLibraryManagedExamples.EvaluateCombinedOutputs(device);
            }

            if (ShouldRunOnGpu())
            {
                Console.WriteLine(" ====== Evaluate model on GPU =====");
                var device = DeviceDescriptor.GPUDevice(0);
                // Run memory tests.
                MemoryTests.ValidateObjectReferences(device);
                CNTKLibraryManagedExamples.EvaluationSingleImage(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfImages(device);
                CNTKLibraryManagedExamples.EvaluateMultipleImagesInParallelAsync(device).Wait();
                // Run memory tests.
                MemoryTests.ValidateObjectReferences(device);

                Task evalTask = CNTKLibraryManagedExamples.EvaluationSingleImageAsync(device);
                evalTask.Wait();

                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationBatchOfSequencesUsingOneHot(device);
                CNTKLibraryManagedExamples.EvaluationSingleSequenceUsingSparse(device);

                // Run memory tests again.
                MemoryTests.WriteOutputs();

                MemoryTests.ValueCopyToSparseCSCTest <float>(device);
                MemoryTests.ValueCopyToSparseCSCTest <double>(device);

                CNTKLibraryManagedExamples.EvaluateIntermediateLayer(device);
                CNTKLibraryManagedExamples.EvaluateCombinedOutputs(device);
            }

            Console.WriteLine("======== Evaluation completes. ========");
        }
示例#12
0
        public void SetupUsingResetModel(DeviceDescriptor device)
        {
            try
            {
                Console.WriteLine("\n===== Setup memory tests using Resnet Model =====");

                var deviceList = DeviceDescriptor.AllDevices();
                MemoryTests.Device0 = deviceList[0];

                // Load the model.
                string modelFilePath = "resnet20.dnn";
                CNTKLibraryManagedExamples.ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/Image/Classification/ResNet to create the model.", modelFilePath));
                Function modelFunc = Function.LoadModel(modelFilePath, device);

                Variable inputVar = modelFunc.Arguments.Single();
                MemoryTests.ArgumentVar0 = inputVar;
                MemoryTests.InputVar0    = modelFunc.Inputs.First();

                MemoryTests.OutputVar0 = modelFunc.Outputs[0];
                MemoryTests.OutputVar  = modelFunc.Output;
                Variable outputVar = MemoryTests.OutputVar;

                MemoryTests.Axis0 = outputVar.DynamicAxes.FirstOrDefault();

                // Get shape data for the input variable
                NDShape inputShape    = inputVar.Shape;
                int     imageWidth    = inputShape[0];
                int     imageHeight   = inputShape[1];
                int     imageChannels = inputShape[2];
                int     imageSize     = inputShape.TotalSize;
                var     inputDataMap  = new Dictionary <Variable, Value>();
                var     outputDataMap = new Dictionary <Variable, Value>();

                var imageList = new List <string>()
                {
                    "00000.png", "00001.png", "00002.png"
                };
                foreach (var image in imageList)
                {
                    CNTKLibraryManagedExamples.ThrowIfFileNotExist(image, string.Format("Error: The sample image '{0}' does not exist. Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.", image));
                }
                Bitmap       bmp, resized;
                List <float> resizedCHW;
                var          seqData = new List <float>();
                for (int sampleIndex = 0; sampleIndex < imageList.Count; sampleIndex++)
                {
                    bmp        = new Bitmap(Bitmap.FromFile(imageList[sampleIndex]));
                    resized    = bmp.Resize((int)imageWidth, (int)imageHeight, true);
                    resizedCHW = resized.ParallelExtractCHW();
                    seqData.AddRange(resizedCHW);
                }

                var inputVal = Value.CreateBatch(inputVar.Shape, seqData, device);
                inputDataMap.Add(inputVar, inputVal);
                outputDataMap.Add(outputVar, null);
                modelFunc.Evaluate(inputDataMap, outputDataMap, device);

                var outputVal  = outputDataMap[outputVar];
                var outputData = outputVal.GetDenseData <float>(outputVar);

                MemoryTests.OutputVal = outputVal;

                Console.WriteLine("\nTest object reference inside SetupUsingResetModel.\n");
                MemoryTests.WriteOutputs();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
                throw ex;
            }
        }
        public void SetupUsingResetModel(DeviceDescriptor device)
        {
            try
            {
                Console.WriteLine("\n===== Setup memory tests using Resnet Model =====");

                var deviceList = DeviceDescriptor.AllDevices();
                MemoryTests.Device0 = deviceList[0];

                // Load the model.
                string modelFilePath = "resnet20.dnn";
                CNTKLibraryManagedExamples.ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/Image/Classification/ResNet to create the model.", modelFilePath));
                Function modelFunc = Function.Load(modelFilePath, device);

                Variable inputVar = modelFunc.Arguments.Single();
                MemoryTests.ArgumentVar0 = inputVar;
                MemoryTests.InputVar0    = modelFunc.Inputs.First();

                MemoryTests.OutputVar0 = modelFunc.Outputs[0];
                MemoryTests.OutputVar  = modelFunc.Output;
                Variable outputVar = MemoryTests.OutputVar;

                MemoryTests.Axis0 = outputVar.DynamicAxes.FirstOrDefault();

                // Get shape data for the input variable
                NDShape inputShape    = inputVar.Shape;
                int     imageWidth    = inputShape[0];
                int     imageHeight   = inputShape[1];
                int     imageChannels = inputShape[2];
                int     imageSize     = inputShape.TotalSize;

                var imageList = new List <string>()
                {
                    "00000.png", "00001.png", "00002.png"
                };
                foreach (var image in imageList)
                {
                    CNTKLibraryManagedExamples.ThrowIfFileNotExist(image, string.Format("Error: The sample image '{0}' does not exist. Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset.", image));
                }
                Bitmap       bmp, resized;
                List <float> resizedCHW;
                var          seqData1 = new List <float>();
                var          seqData2 = new List <float>();
                for (int sampleIndex = 0; sampleIndex < imageList.Count; sampleIndex++)
                {
                    bmp        = new Bitmap(Bitmap.FromFile(imageList[sampleIndex]));
                    resized    = bmp.Resize((int)imageWidth, (int)imageHeight, true);
                    resizedCHW = resized.ParallelExtractCHW();
                    if (sampleIndex < imageList.Count - 1)
                    {
                        seqData1.AddRange(resizedCHW);
                    }
                    seqData2.AddRange(resizedCHW);
                }

                var inputDataMap1  = new Dictionary <Variable, Value>();
                var outputDataMap1 = new Dictionary <Variable, Value>();
                var inputVal1      = Value.CreateBatch(inputVar.Shape, seqData1, device);
                inputDataMap1.Add(inputVar, inputVal1);
                outputDataMap1.Add(outputVar, null);

                // Using temprary Value object returned by Evaluate().
                modelFunc.Evaluate(inputDataMap1, outputDataMap1, device);
                var outputVal1  = outputDataMap1[outputVar];
                var outputData1 = outputVal1.GetDenseData <float>(outputVar);

                // Using cloned persistent Value object returned by Evaluate().
                var outputDataMap1WithClone = new Dictionary <Variable, Value>();
                outputDataMap1WithClone.Add(outputVar, null);
                modelFunc.Evaluate(inputDataMap1, outputDataMap1WithClone, true, device);

                // Using temprary Value object which overwrites the one returned by the previous Evaluate().
                var inputDataMap2  = new Dictionary <Variable, Value>();
                var outputDataMap2 = new Dictionary <Variable, Value>();
                var inputVal2      = Value.CreateBatch(inputVar.Shape, seqData2, device);
                inputDataMap2.Add(inputVar, inputVal2);
                outputDataMap2.Add(outputVar, null);
                modelFunc.Evaluate(inputDataMap2, outputDataMap2, device);

                // Test access to the persistent Value object, which should be still valid.
                var outputVal1WithClone  = outputDataMap1WithClone[outputVar];
                var outputData1WithClone = outputVal1WithClone.GetDenseData <float>(outputVar);

                // Test access to the temprary Value object returned by the latest Evaluate().
                var outputVal2  = outputDataMap2[outputVar];
                var outputData2 = outputVal2.GetDenseData <float>(outputVar);

                // Test access to the temprary Value object returned by the previous Evaluate(), which is not valid any more.
                bool exceptionCaught = false;
                try
                {
                    var data = outputVal1.GetDenseData <float>(outputVar);
                }
                catch (Exception ex)
                {
                    if (ex is ApplicationException && ex.Message.StartsWith("This Value object is invalid and can no longer be accessed."))
                    {
                        exceptionCaught = true;
                    }
                }
                if (exceptionCaught == false)
                {
                    throw new ApplicationException("The expected exception has not been caught.");
                }

                MemoryTests.OutputVal = outputVal1WithClone;

                Console.WriteLine("\nTest object reference inside SetupUsingResetModel.\n");
                MemoryTests.WriteOutputs();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception");
                throw ex;
            }
        }