示例#1
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. ========");
        }
        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. ========");
        }