public static void Evaluate(this Function func, Dictionary<Variable, Value> arguments, Dictionary<Variable, Value> outputs, DeviceDescriptor computeDevice) { // Evaluate the rootFunction. var argMap = new UnorderedMapVariableValuePtr(); foreach (var p in arguments) { argMap.Add(p.Key, p.Value); } var outMap = new UnorderedMapVariableValuePtr(); foreach (var p in outputs) { outMap.Add(p.Key, p.Value); } func.Evaluate(argMap, outMap, computeDevice); foreach (var p in outMap) { outputs[p.Key] = p.Value; } }
static Tuple <Function, Function> LSTMPCellWithSelfStabilization <ElementType>( Variable input, Variable prevOutput, Variable prevCellState, DeviceDescriptor device) { int outputDim = prevOutput.Shape[0]; int cellDim = prevCellState.Shape[0]; bool isFloatType = typeof(ElementType).Equals(typeof(float)); DataType dataType = isFloatType ? DataType.Float : DataType.Double; Func <int, Parameter> createBiasParam; if (isFloatType) { createBiasParam = (dim) => new Parameter(new int[] { dim }, 0.01f, device, ""); } else { createBiasParam = (dim) => new Parameter(new int[] { dim }, 0.01, device, ""); } uint seed2 = 1; Func <int, Parameter> createProjectionParam = (oDim) => new Parameter(new int[] { oDim, NDShape.InferredDimension }, dataType, CNTKLib.GlorotUniformInitializer(1.0, 1, 0, seed2++), device); Func <int, Parameter> createDiagWeightParam = (dim) => new Parameter(new int[] { dim }, dataType, CNTKLib.GlorotUniformInitializer(1.0, 1, 0, seed2++), device); Function stabilizedPrevOutput = Stabilize <ElementType>(prevOutput, device); Function stabilizedPrevCellState = Stabilize <ElementType>(prevCellState, device); Func <Variable> projectInput = () => createBiasParam(cellDim) + (createProjectionParam(cellDim) * input); // Input gate Function it = CNTKLib.Sigmoid( (Variable)(projectInput() + (createProjectionParam(cellDim) * stabilizedPrevOutput)) + CNTKLib.ElementTimes(createDiagWeightParam(cellDim), stabilizedPrevCellState)); Function bit = CNTKLib.ElementTimes( it, CNTKLib.Tanh(projectInput() + (createProjectionParam(cellDim) * stabilizedPrevOutput))); // Forget-me-not gate Function ft = CNTKLib.Sigmoid( (Variable)( projectInput() + (createProjectionParam(cellDim) * stabilizedPrevOutput)) + CNTKLib.ElementTimes(createDiagWeightParam(cellDim), stabilizedPrevCellState)); Function bft = CNTKLib.ElementTimes(ft, prevCellState); Function ct = (Variable)bft + bit; // Output gate Function ot = CNTKLib.Sigmoid( (Variable)(projectInput() + (createProjectionParam(cellDim) * stabilizedPrevOutput)) + CNTKLib.ElementTimes(createDiagWeightParam(cellDim), Stabilize <ElementType>(ct, device))); Function ht = CNTKLib.ElementTimes(ot, CNTKLib.Tanh(ct)); Function c = ct; Function h = (outputDim != cellDim) ? (createProjectionParam(outputDim) * Stabilize <ElementType>(ht, device)) : ht; return(new Tuple <Function, Function>(h, c)); }
/// <summary> /// Evaluate the model against dataset sored in the dataset file, and exports the result in csv format for further analysis /// </summary> /// <param name="mlF"> ml factory object contains members needed to evaluation process</param> /// <param name="mbs"> Minibatch source which provides helpers members needed for for evaluation</param> /// <param name="strDataSetPath"> file of dataset</param> /// <param name="modelPath"> models which will be evaluate</param> /// <param name="resultExportPath"> result file in which the result will be exported</param> /// <param name="device"> device for computation</param> public static void EvaluateModel(string mlconfigPath, string bestTrainedModelPath, DeviceDescriptor device) { //Load ML model configuration file var dicMParameters = MLFactory.LoadMLConfiguration(mlconfigPath); //add full path of model folder since model file doesn't contains any absolute path dicMParameters.Add("root", MLFactory.GetMLConfigFolder(mlconfigPath)); //get model daa paths var dicPath = MLFactory.GetMLConfigComponentPaths(dicMParameters["paths"]); //parse feature variables var projectValues = dicMParameters["training"].Split(MLFactory.m_cntkSpearator, StringSplitOptions.RemoveEmptyEntries); var trainedModelRelativePath = MLFactory.GetParameterValue(projectValues, "TrainedModel"); //Minibatch type var mbTypestr = MLFactory.GetParameterValue(projectValues, "Type"); MinibatchType mbType = (MinibatchType)Enum.Parse(typeof(MinibatchType), mbTypestr, true); //prepare MLFactory var f = MLFactory.CreateMLFactory(dicMParameters); //prepare data paths for mini-batch source var strTrainPath = $"{dicMParameters["root"]}\\{dicPath["Training"]}"; var strValidPath = $"{dicMParameters["root"]}\\{dicPath["Validation"]}"; var strResult = $"{dicMParameters["root"]}\\{dicPath["Result"]}"; var bestModelFullPath = $"{dicMParameters["root"]}\\{bestTrainedModelPath}"; //decide what data to evaluate var dataPath = strValidPath; //load model var model = Function.Load(bestModelFullPath, device); //get data for evaluation by calling GetFullBatch var minibatchData = MinibatchSourceEx.GetFullBatch(mbType, dataPath, f.StreamConfigurations.ToArray(), device); //input map creation for model evaluation var inputMap = new Dictionary <Variable, Value>(); foreach (var v in minibatchData) { var vv = model.Arguments.Where(x => x.Name == v.Key.m_name).FirstOrDefault(); var streamInfo = v.Key; if (vv != null) { inputMap.Add(vv, minibatchData[streamInfo].data); } } //output map var predictedDataMap = new Dictionary <Variable, Value>(); foreach (var outp in model.Outputs) { predictedDataMap.Add(outp, null); } //model evaluation model.Evaluate(inputMap, predictedDataMap, device); //retrieve actual and predicted values from model List <List <float> > actual = new List <List <float> >(); List <List <float> > predict = new List <List <float> >(); foreach (var output in model.Outputs) { //label stream var labelStream = minibatchData.Keys.Where(x => x.m_name == output.Name).First(); //actual values List <List <float> > av = MLValue.GetValues(output, minibatchData[labelStream].data); //predicted values List <List <float> > pv = MLValue.GetValues(output, predictedDataMap[output]); for (int i = 0; i < av.Count; i++) { //actual var act = av[i]; if (actual.Count <= i) { actual.Add(new List <float>()); } actual[i].AddRange(act); //prediction var prd = pv[i]; if (predict.Count <= i) { predict.Add(new List <float>()); } predict[i].AddRange(prd); } } //export result MLValue.ValueToFile(actual, predict, strResult); // Console.WriteLine(Environment.NewLine); Console.WriteLine($"*******************Model Evaluation**************"); Console.WriteLine(Environment.NewLine); Console.WriteLine($"Model Evaluation successfully exported result into file {strResult}!"); Console.WriteLine(Environment.NewLine); }
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 outputBuffer = new List <List <float> >(); var outputVal = outputDataMap[outputVar]; outputVal.CopyVariableValueTo(outputVar, outputBuffer); 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; } }
/// <summary> /// The example shows /// - how to prepare input data as sequence using sparse input. /// </summary> /// <param name="device">Specify on which device to run the evaluation</param> public static void EvaluationSingleSequenceUsingSparse(DeviceDescriptor device) { try { Console.WriteLine("\n===== Evaluate single sequence using sparse input ====="); // The model atis.dnn is trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py // Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model. string modelFilePath = "atis.dnn"; ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS to create the model.", modelFilePath)); Function modelFunc = Function.Load(modelFilePath, device); // Read word and slot index files. string vocabFile = "query.wl"; string labelFile = "slots.wl"; ThrowIfFileNotExist(vocabFile, string.Format("Error: The file '{0}' does not exist. Please copy it from <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/ to the output directory.", vocabFile)); ThrowIfFileNotExist(labelFile, string.Format("Error: The file '{0}' does not exist. Please copy it from <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/ to the output directory.", labelFile)); var vocabToIndex = buildVocabIndex(vocabFile); var indexToSlots = buildSlotIndex(labelFile); // Get input variable var inputVar = modelFunc.Arguments.Single(); int vocabSize = inputVar.Shape.TotalSize; var inputSentence = "BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS"; // Get the index of each word in the sentence. string[] inputWords = inputSentence.Split(' '); var seqLen = inputWords.Length; // For this example, only 1 non-zero value for each sample. var numNonZeroValues = seqLen * 1; var colStarts = new int[seqLen + 1]; var rowIndices = new int[numNonZeroValues]; var nonZeroValues = new float[numNonZeroValues]; int count = 0; for (; count < seqLen; count++) { // Get the index of the word var nonZeroValueIndex = (int)vocabToIndex[inputWords[count]]; // Add the sample to the sequence nonZeroValues[count] = (float)1.0; rowIndices[count] = nonZeroValueIndex; colStarts[count] = count; } colStarts[count] = numNonZeroValues; // Create input value using OneHot vector data. var inputValue = Value.CreateSequence <float>(vocabSize, seqLen, colStarts, rowIndices, nonZeroValues, device); // Build input data map. var inputDataMap = new Dictionary <Variable, Value>(); inputDataMap.Add(inputVar, inputValue); // Prepare output Variable outputVar = modelFunc.Output; // Create ouput data map. Using null as Value to indicate using system allocated memory. var outputDataMap = new Dictionary <Variable, Value>(); outputDataMap.Add(outputVar, null); // Evalaute the model. modelFunc.Evaluate(inputDataMap, outputDataMap, device); // Get result var outputVal = outputDataMap[outputVar]; var outputData = outputVal.GetDenseData <float>(outputVar); // Output the result var outputSampleSize = (int)outputVar.Shape.TotalSize; if (outputData.Count != 1) { throw new ApplicationException("Only one sequence of slots is expected as output."); } var slotSeq = outputData[0]; if (slotSeq.Count % outputSampleSize != 0) { throw new ApplicationException("The number of elements in the slot sequence is not a multiple of sample size"); } var numOfSlotsInOutput = slotSeq.Count / outputSampleSize; if (inputWords.Count() != numOfSlotsInOutput) { throw new ApplicationException("The number of input words and the number of output slots do not match"); } for (int i = 0; i < numOfSlotsInOutput; i++) { var max = slotSeq[i * outputSampleSize]; var maxIndex = 0; for (int j = 1; j < outputSampleSize; j++) { if (slotSeq[i * outputSampleSize + j] > max) { max = slotSeq[i * outputSampleSize + j]; maxIndex = j; } } Console.WriteLine(String.Format(" {0, 10} ---- {1}", inputWords[i], indexToSlots[maxIndex])); } Console.WriteLine(); } 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; } }
/// <summary> /// The example shows /// - how to load model from a memory buffer. /// </summary> /// <param name="device">Specify on which device to run the evaluation.</param> public static void LoadModelFromMemory(DeviceDescriptor device) { try { Console.WriteLine("\n===== Load model from memory buffer ====="); // For demo purpose, we first read the the model into memory // The model resnet20.dnn is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/Models/TrainResNet_CIFAR10.py // Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model. string modelFilePath = "resnet20.dnn"; 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)); var modelBuffer = File.ReadAllBytes(modelFilePath); // Load model from memroy buffer Function modelFunc = Function.Load(modelBuffer, device); // Get input variable. The model has only one single input. // The same way described above for output variable can be used here to get input variable by name. Variable inputVar = modelFunc.Arguments.Single(); // 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; // The model has only one output. // If the model have more than one output, use the following way to get output variable by name. // Variable outputVar = modelFunc.Outputs.Where(variable => string.Equals(variable.Name, outputName)).Single(); Variable outputVar = modelFunc.Output; var inputDataMap = new Dictionary <Variable, Value>(); var outputDataMap = new Dictionary <Variable, Value>(); // Image preprocessing to match input requirements of the model. // This program uses images from the CIFAR-10 dataset for evaluation. // Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset. string sampleImage = "00000.png"; ThrowIfFileNotExist(sampleImage, 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.", sampleImage)); Bitmap bmp = new Bitmap(Bitmap.FromFile(sampleImage)); var resized = bmp.Resize((int)imageWidth, (int)imageHeight, true); List <float> resizedCHW = resized.ParallelExtractCHW(); // Create input data map var inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device); inputDataMap.Add(inputVar, inputVal); // Create ouput data map. Using null as Value to indicate using system allocated memory. // Alternatively, create a Value object and add it to the data map. outputDataMap.Add(outputVar, null); // Start evaluation on the device modelFunc.Evaluate(inputDataMap, outputDataMap, device); // Get evaluate result as dense output var outputVal = outputDataMap[outputVar]; var outputData = outputVal.GetDenseData <float>(outputVar); PrintOutput(outputVar.Shape.TotalSize, outputData); } 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; } }
/// <summary> /// GetDeviceDescription: Get a Description of the HMD and apply appropriate settings /// /// </summary> private void GetDeviceDescription() { _deviceDescriptor = GetComponent<DisplayInterface>().GetDeviceDescription(); if (_deviceDescriptor != null) { Debug.Log(_deviceDescriptor.ToString()); switch (_deviceDescriptor.DisplayMode) { case "full_screen": viewMode = ViewMode.mono; break; case "horz_side_by_side": case "vert_side_by_side": default: viewMode = ViewMode.stereo; break; } stereoAmount = Mathf.Clamp(_deviceDescriptor.OverlapPercent, 0, 100); SetResolution(_deviceDescriptor.Width, _deviceDescriptor.Height); //set resolution before FOV Camera.fieldOfView = Mathf.Clamp(_deviceDescriptor.MonocularVertical, 0, 180); //unity camera FOV is vertical SetDistortion(_deviceDescriptor.K1Red, _deviceDescriptor.K1Green, _deviceDescriptor.K1Blue, _deviceDescriptor.CenterProjX, _deviceDescriptor.CenterProjY); //set distortion shader //if the view needs to be rotated 180 degrees, create a parent game object that is flipped 180 degrees on the z axis. if (_deviceDescriptor.Rotate180 > 0) { GameObject vrHeadParent = new GameObject(); vrHeadParent.name = this.transform.name + "_parent"; vrHeadParent.transform.position = this.transform.position; vrHeadParent.transform.rotation = this.transform.rotation; if (this.transform.parent != null) { vrHeadParent.transform.parent = this.transform.parent; } this.transform.parent = vrHeadParent.transform; vrHeadParent.transform.Rotate(0, 0, 180, Space.Self); } } }
// // The example shows // - how to load model. // - how to prepare input data for a signle sample, a batch of samples in dense format. // - how to prepare input and output data map // - how to evaluate a model // - how to retrieve evaluation result and retrieve output data in dense format. // static void EvaluationWithDenseData(DeviceDescriptor device) { const string outputName = "Plus2060_output"; // Load the model. Function modelFunc = Function.LoadModel("z.model"); // Get output variable based on name Variable outputVar = modelFunc.Outputs.Where(variable => string.Equals(variable.Name, outputName)).Single(); // Get input variable. The model has only one single input. // The same way described above for output variable can be used here to get input variable by name. Variable inputVar = modelFunc.Arguments.Single(); // Get shape data for the input variable NDShape inputShape = inputVar.Shape; uint imageWidth = inputShape[0]; uint imageHeight = inputShape[1]; uint imageChannels = inputShape[2]; uint imageSize = inputShape.TotalSize; var outputDataMap = new Dictionary<Variable, Value>(); // Use case 1: Evaluate with single image Console.WriteLine("Evaluate single image"); // Image preprocessing to match input requirements of the model. Bitmap bmp = new Bitmap(Bitmap.FromFile("00000.png")); var resized = bmp.Resize((int)imageWidth, (int)imageHeight, true); List<float> resizedCHW = resized.ParallelExtractCHW(); // Create input data map var inputDataMap = new Dictionary<Variable, Value>(); var inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device); inputDataMap.Add(inputVar, inputVal); // Create ouput data map. Using null as Value to indicate using system allocated memory. // Alternatively, create a Value object and add it to the data map. outputDataMap.Add(outputVar, null); // Start evaluation on the device modelFunc.Evaluate(inputDataMap, outputDataMap, device); // Get evaluate result as dense output var outputData = new List<List<float>>(); Value outputVal = outputDataMap[outputVar]; outputVal.CopyTo(outputVar.Shape, outputData); // Use case 2: Evaluate with batch of images Console.WriteLine("Evaluate batch of images"); var fileList = new List<string>() { "00000.png", "00001.png", "00002.png" }; var seqData = new List<float>(); for (int sampleIndex = 0; sampleIndex < fileList.Count; sampleIndex++) { bmp = new Bitmap(Bitmap.FromFile(fileList[sampleIndex++])); resized = bmp.Resize((int)imageWidth, (int)imageHeight, true); resizedCHW = resized.ParallelExtractCHW(); // Aadd this sample to the data buffer. seqData.AddRange(resizedCHW); } // Create Value for the batch data. inputVal = Value.CreateBatch(inputVar.Shape, seqData, device); // Create input and output data map. inputDataMap[inputVar] = inputVal; outputDataMap[outputVar] = null; // Evaluate the model against the batch input modelFunc.Evaluate(inputDataMap, outputDataMap, device); // Retrieve the evaluation result. outputData = new List<List<float>>(); outputVal = outputDataMap[outputVar]; outputVal.CopyTo(outputVar.Shape, outputData); // Output result Console.WriteLine("The number of sequences in the batch: " + outputData.Count); int seqNo = 0; uint outputSampleSize = outputVar.Shape.TotalSize; foreach(var seq in outputData) { Console.WriteLine(String.Format("Sequence {0} contains {1} samples.", seqNo++, seq.Count/outputSampleSize)); uint i = 0; uint sampleNo = 0; foreach (var element in seq) { Console.Write(String.Format(" sample {0}: " + sampleNo)); Console.Write(element); if (++i % outputSampleSize == 0) { Console.WriteLine("."); sampleNo++; } else Console.WriteLine(","); } } }
private static Tuple <Function, Function> LSTMComponent <TElementType>(Variable input, NDShape outputShape, NDShape cellShape, Func <Variable, Function> recurrenceHookH, Func <Variable, Function> recurrenceHookC, bool enableSelfStabilization, DeviceDescriptor device) { var dh = Variable.PlaceholderVariable(outputShape, input.DynamicAxes); var dc = Variable.PlaceholderVariable(cellShape, input.DynamicAxes); var lstmCell = LSTMCell <TElementType>(input, dh, dc, enableSelfStabilization, device); var actualDh = recurrenceHookH(lstmCell.Item1); var actualDc = recurrenceHookC(lstmCell.Item2); (lstmCell.Item1).ReplacePlaceholders(new Dictionary <Variable, Variable> { { dh, actualDh }, { dc, actualDc } }); return(new Tuple <Function, Function>(lstmCell.Item1, lstmCell.Item2)); }
public void TestTrainingSession2() { // Data var features = DataSourceFactory.Create(new float[] { 0, 0, 0, 1, 1, 0, 1, 1, 3, 4, 3, 5, 4, 4, 4, 5 }, new int[] { 2, 1, -1 }); var labels = DataSourceFactory.Create(new float[] { 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 }, new int[] { 2, 1, -1 }); var sampler = new DataSourceSampler(new Dictionary <string, IDataSource <float> >() { { "input", features }, { "label", labels } }, 2); // Model var input = CNTKLib.InputVariable(new int[] { 2 }, false, DataType.Float, "input"); var h = Composite.Dense(input, new int[] { 100 }, CNTKLib.HeNormalInitializer(), true, null, false, 4, "relu", DeviceDescriptor.UseDefaultDevice(), ""); h = Composite.Dense(h, new int[] { 2 }, CNTKLib.GlorotNormalInitializer(), true, null, false, 4, "sigmoid", DeviceDescriptor.UseDefaultDevice(), ""); var output = h; var label = CNTKLib.InputVariable(new int[] { 2 }, DataType.Float, "label"); // Loss and metric functions var loss = CNTKLib.BinaryCrossEntropy(output, label); var error = CNTKLib.ClassificationError(output, label); // Train var lr = new TrainingParameterScheduleDouble(.01); var m = new TrainingParameterScheduleDouble(.9); var learner = Learner.MomentumSGDLearner(output.Parameters(), lr, m, true); var session = new TrainingSession(output, loss, error, learner, null, sampler, null); var iteration = session.GetEnumerator(); for (var i = 0; i < 1000; ++i) { iteration.MoveNext(); var dummy = iteration.Current; var valid = session.GetValidationMetric(); } Assert.IsTrue(session.Metric < 0.1); }
public TrainingSessionTest() { UnmanagedDllLoader.Load(@"..\..\..\..\lib"); DeviceDescriptor.TrySetDefaultDevice(DeviceDescriptor.CPUDevice); }
private void ConnectDevices() { if (_connectionInProgress) { return; } _connectionInProgress = true; if (PortableDeviceCollection.Instance == null) { PortableDeviceCollection.CreateInstance(AppName, AppMajorVersionNumber, AppMinorVersionNumber); PortableDeviceCollection.Instance.AutoConnectToPortableDevice = false; } _deviceEnumerator.RemoveDisconnected(); Log.Debug("Connection device start"); try { var devices = PortableDeviceCollection.Instance.Devices; foreach (PortableDevice portableDevice in devices) { Log.Debug("Connection device " + portableDevice.DeviceId); //TODO: avoid to load some mass storage in my computer need to find a general solution if (!portableDevice.DeviceId.StartsWith("\\\\?\\usb") && !portableDevice.DeviceId.StartsWith("\\\\?\\comp")) { continue; } // ignore some Canon cameras if (!SupportedCanonCamera(portableDevice.DeviceId)) { continue; } portableDevice.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber); if (_deviceEnumerator.GetByWpdId(portableDevice.DeviceId) == null && GetNativeDriver(portableDevice.Model) != null) { ICameraDevice cameraDevice; DeviceDescriptor descriptor = new DeviceDescriptor { WpdId = portableDevice.DeviceId }; cameraDevice = (ICameraDevice)Activator.CreateInstance(GetNativeDriver(portableDevice.Model)); MtpProtocol device = new MtpProtocol(descriptor.WpdId); device.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber); descriptor.StillImageDevice = device; cameraDevice.SerialNumber = StaticHelper.GetSerial(portableDevice.DeviceId); cameraDevice.Init(descriptor); if (string.IsNullOrWhiteSpace(cameraDevice.SerialNumber)) { cameraDevice.SerialNumber = StaticHelper.GetSerial(portableDevice.DeviceId); } ConnectedDevices.Add(cameraDevice); NewCameraConnected(cameraDevice); descriptor.CameraDevice = cameraDevice; _deviceEnumerator.Add(descriptor); } if (_deviceEnumerator.GetByWpdId(portableDevice.DeviceId) == null && GetNativeDriver(portableDevice.Model) == null) { var description = getDeviceDescription(portableDevice.Model); if (description != null) { CustomDevice cameraDevice = new CustomDevice(); DeviceDescriptor descriptor = new DeviceDescriptor { WpdId = portableDevice.DeviceId }; MtpProtocol device = new MtpProtocol(descriptor.WpdId); device.ConnectToDevice(AppName, AppMajorVersionNumber, AppMinorVersionNumber); descriptor.StillImageDevice = device; cameraDevice.SerialNumber = StaticHelper.GetSerial(portableDevice.DeviceId); cameraDevice.Init(descriptor, description); ConnectedDevices.Add(cameraDevice); NewCameraConnected(cameraDevice); descriptor.CameraDevice = cameraDevice; _deviceEnumerator.Add(descriptor); break; } } } } catch (Exception exception) { Log.Error("Unable to connect to cameras ", exception); } _connectionInProgress = false; }
/// <summary> /// This function will parse the device parameters from a device descriptor json file using Newstonsoft /// /// Returns a DeviceDescriptor object containing stored json values. /// </summary> public static DeviceDescriptor Parse(string deviceDescriptorJson) { if (deviceDescriptorJson == null) { throw new ArgumentNullException("deviceDescriptorJson"); } //create a device descriptor object for storing the parsed json in an object DeviceDescriptor deviceDescriptor; JsonTextReader reader; reader = new JsonTextReader(new StringReader(deviceDescriptorJson)); if (reader != null) { deviceDescriptor = new DeviceDescriptor(); } else { Debug.LogError("No Device Descriptor detected."); return null; } //parsey while (reader.Read()) { if (reader.Value != null && reader.ValueType == typeof(String)) { string parsedJson = reader.Value.ToString().ToLower(); switch (parsedJson) { case "vendor": deviceDescriptor.Vendor = reader.ReadAsString(); break; case "model": deviceDescriptor.Model = reader.ReadAsString(); break; case "version": deviceDescriptor.Version = reader.ReadAsString(); break; case "num_displays": deviceDescriptor.NumDisplays = int.Parse(reader.ReadAsString()); break; case "note": deviceDescriptor.Note = reader.ReadAsString(); break; case "monocular_horizontal": deviceDescriptor.MonocularHorizontal = float.Parse(reader.ReadAsString()); break; case "monocular_vertical": deviceDescriptor.MonocularVertical = float.Parse(reader.ReadAsString()); break; case "overlap_percent": deviceDescriptor.OverlapPercent = float.Parse(reader.ReadAsString()); break; case "pitch_tilt": deviceDescriptor.PitchTilt = float.Parse(reader.ReadAsString()); break; case "width": deviceDescriptor.Width = int.Parse(reader.ReadAsString()); break; case "height": deviceDescriptor.Height = int.Parse(reader.ReadAsString()); break; case "video_inputs": deviceDescriptor.VideoInputs = int.Parse(reader.ReadAsString()); break; case "display_mode": deviceDescriptor.DisplayMode = reader.ReadAsString(); break; case "swapeyes": deviceDescriptor.SwapEyes = int.Parse(reader.ReadAsString()); break; case "k1_red": deviceDescriptor.K1Red = float.Parse(reader.ReadAsString()); break; case "k1_green": deviceDescriptor.K1Green = float.Parse(reader.ReadAsString()); break; case "k1_blue": deviceDescriptor.K1Blue = float.Parse(reader.ReadAsString()); break; case "right_roll": deviceDescriptor.RightRoll = float.Parse(reader.ReadAsString()); break; case "left_roll": deviceDescriptor.LeftRoll = float.Parse(reader.ReadAsString()); break; case "center_proj_x": deviceDescriptor.CenterProjX = float.Parse(reader.ReadAsString()); break; case "center_proj_y": deviceDescriptor.CenterProjY = float.Parse(reader.ReadAsString()); break; case "rotate_180": deviceDescriptor.Rotate180 = int.Parse(reader.ReadAsString()); break; } } } return deviceDescriptor; }
/// <summary> /// GetDeviceDescription: Get a Description of the HMD and apply appropriate settings /// /// </summary> private void GetDeviceDescription() { _deviceDescriptor = _displayInterface.GetDeviceDescription(); if (_deviceDescriptor != null) { switch (_deviceDescriptor.DisplayMode) { case "full_screen": viewMode = ViewMode.mono; break; case "horz_side_by_side": case "vert_side_by_side": default: viewMode = ViewMode.stereo; break; } swapEyes = _deviceDescriptor.SwapEyes > 0; //swap left and right eye positions? stereoAmount = Mathf.Clamp(_deviceDescriptor.OverlapPercent, 0, 100); SetResolution(_deviceDescriptor.Width, _deviceDescriptor.Height); //set resolution before FOV Camera.fieldOfView = Mathf.Clamp(_deviceDescriptor.MonocularVertical, 0, 180); //unity camera FOV is vertical float aspectRatio = (float)_deviceDescriptor.Width / (float)_deviceDescriptor.Height; //aspect ratio per eye depends on how many displays the HMD has //for example, dSight has two 1920x1080 displays, so each eye should have 1.77 aspect //whereas HDK has one 1920x1080 display, each eye should have 0.88 aspect (half of 1.77) float aspectRatioPerEye = _deviceDescriptor.NumDisplays == 1 ? aspectRatio * 0.5f : aspectRatio; //set projection matrix for each eye Camera.projectionMatrix = Matrix4x4.Perspective(_deviceDescriptor.MonocularVertical, aspectRatioPerEye, Camera.nearClipPlane, Camera.farClipPlane); SetDistortion(_deviceDescriptor.K1Red, _deviceDescriptor.K1Green, _deviceDescriptor.K1Blue, _deviceDescriptor.CenterProjX, _deviceDescriptor.CenterProjY); //set distortion shader //if the view needs to be rotated 180 degrees, create a parent game object that is flipped 180 degrees on the z axis. if (_deviceDescriptor.Rotate180 > 0) { GameObject vrHeadParent = new GameObject(); vrHeadParent.name = this.transform.name + "_parent"; vrHeadParent.transform.position = this.transform.position; vrHeadParent.transform.rotation = this.transform.rotation; if (this.transform.parent != null) { vrHeadParent.transform.parent = this.transform.parent; } this.transform.parent = vrHeadParent.transform; vrHeadParent.transform.Rotate(0, 0, 180, Space.Self); } } }
/// <summary> /// Build a one direction recurrent neural network (RNN) with long-short-term-memory (LSTM) cells. /// http://colah.github.io/posts/2015-08-Understanding-LSTMs/ /// </summary> /// <param name="input">the input variable</param> /// <param name="numOutputClasses">number of output classes</param> /// <param name="embeddingDim">dimension of the embedding layer</param> /// <param name="LSTMDim">LSTM output dimension</param> /// <param name="cellDim">cell dimension</param> /// <param name="device">CPU or GPU device to run the model</param> /// <param name="outputName">name of the model output</param> /// <returns>the RNN model</returns> static Function LSTMSequenceClassifierNet(Variable input, int numOutputClasses, int embeddingDim, int LSTMDim, int cellDim, DeviceDescriptor device, string outputName) { Function embeddingFunction = Embedding(input, embeddingDim, device); Func <Variable, Function> pastValueRecurrenceHook = (x) => CNTKLib.PastValue(x); Function LSTMFunction = LSTMPComponentWithSelfStabilization <float>( embeddingFunction, new int[] { LSTMDim }, new int[] { cellDim }, pastValueRecurrenceHook, pastValueRecurrenceHook, device).Item1; Function thoughtVectorFunction = CNTKLib.SequenceLast(LSTMFunction); return(TestHelper.FullyConnectedLinearLayer(thoughtVectorFunction, numOutputClasses, device, outputName)); }
public static Function Build(Variable input, int lstmDimension, DeviceDescriptor device, int cellDimension = 0, bool enableSelfStabilization = false, string outputName = "lstm") { return(Build <float>(input, lstmDimension, device, cellDimension, enableSelfStabilization, outputName)); }
protected void TryConnect(RootDescriptor rootDescriptor) { DeviceConnection connection; string deviceUuid; using (_networkTracker.SharedControlPointData.Lock.EnterWrite()) { if (_connection != null) { return; } DeviceDescriptor rootDeviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); DeviceDescriptor backendServerDescriptor = rootDeviceDescriptor.FindFirstDevice( UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); if (backendServerDescriptor == null) { return; } deviceUuid = backendServerDescriptor.DeviceUUID; string friendlyName = backendServerDescriptor.FriendlyName; SystemName system = new SystemName(new Uri(rootDescriptor.SSDPRootEntry.PreferredLink.DescriptionLocation).Host); if (deviceUuid == _homeServerSystemId) { ServiceRegistration.Get <ILogger>().Debug("UPnPClientControlPoint: Found MP2 home server '{0}' (system ID '{1}') at host '{2}' (IP address: '{3}')", friendlyName, deviceUuid, system.HostName, system.Address); } else { ServiceRegistration.Get <ILogger>().Debug("UPnPClientControlPoint: Found foreign MP2 server '{0}' (system ID '{1}') at host '{2}' (IP address: '{3}')", friendlyName, deviceUuid, system.HostName, system.Address); return; } try { connection = _connection = _controlPoint.Connect(rootDescriptor, deviceUuid, UPnPExtendedDataTypes.ResolveDataType); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("UPnPClientControlPoint: Error connecting to UPnP MP2 backend server '{0}'", e, deviceUuid); return; } } connection.DeviceDisconnected += OnUPnPDeviceDisconnected; try { CpService cdsStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.CONTENT_DIRECTORY_SERVICE_ID); if (cdsStub == null) { throw new InvalidDataException("ContentDirectory service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } CpService risStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.RESOURCE_INFORMATION_SERVICE_ID); if (risStub == null) { throw new InvalidDataException("ResourceAccess service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } CpService scsStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.SERVER_CONTROLLER_SERVICE_ID); if (scsStub == null) { throw new InvalidDataException("ServerController service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } CpService updmStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.USER_PROFILE_DATA_MANAGEMENT_SERVICE_ID); if (updmStub == null) { throw new InvalidDataException("UserProfileDataManagement service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } using (_networkTracker.SharedControlPointData.Lock.EnterWrite()) { _contentDirectoryService = new UPnPContentDirectoryServiceProxy(cdsStub); _resourceInformationService = new UPnPResourceInformationServiceProxy(risStub); _serverControllerService = new UPnPServerControllerServiceProxy(scsStub); _userProfileDataManagementService = new UPnPUserProfileDataManagementServiceProxy(updmStub); } ICollection <UPnPServiceProxyBase> additionalServices = new List <UPnPServiceProxyBase>(); foreach (AdditionalServiceRegisterDlgt additionalServiceRegistration in _additionalServiceRegistrations) { try { additionalServices.Add(additionalServiceRegistration(connection)); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("UPnPClientControlPoint: Error registering user service for UPnP MP2 backend server '{0}'", e, deviceUuid); } } using (_networkTracker.SharedControlPointData.Lock.EnterWrite()) _additionalServices = additionalServices; } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("UPnPClientControlPoint: Error connecting to services of UPnP MP2 backend server '{0}'", e, deviceUuid); connection.DeviceDisconnected -= OnUPnPDeviceDisconnected; _controlPoint.Disconnect(deviceUuid); return; } InvokeBackendServerDeviceConnected(connection); }
/// <summary> /// This function creates an LSTM block that implements one step of recurrence. /// It accepts the previous state and outputs its new state as a two-valued tuple (output, cell state) /// </summary> /// <typeparam name="TElementType">The data type of the values. May be set to float or double</typeparam> /// <param name="input">The input to the LSTM</param> /// <param name="prevOutput">The output of the previous step of the LSTM</param> /// <param name="prevCellState">The cell state of the previous step of the LSTM</param> /// <param name="enableSelfStabilization">If True, then all state-related projection will contain a Stabilizer()</param> /// <param name="device">Device used for the computation of this cell</param> /// <returns>A function (prev_h, prev_c, input) -> (h, c) that implements one step of a recurrent LSTM layer</returns> public static Tuple <Function, Function> LSTMCell <TElementType>(Variable input, Variable prevOutput, Variable prevCellState, bool enableSelfStabilization, DeviceDescriptor device) { // TODO: Implements Peephole int lstmOutputDimension = prevOutput.Shape[0]; int lstmCellDimension = prevCellState.Shape[0]; bool isFloatType = typeof(TElementType) == typeof(float); DataType dataType = isFloatType ? DataType.Float : DataType.Double; if (enableSelfStabilization) { prevOutput = Stabilizer.Build(prevOutput, device, "StabilizedPrevOutput"); prevCellState = Stabilizer.Build(prevCellState, device, "StabilizedPrevCellState"); } uint seed = 1; Parameter W = new Parameter((NDShape) new[] { lstmCellDimension * 4, NDShape.InferredDimension }, dataType, CNTKLib.GlorotUniformInitializer(1.0, 1, 0, seed++), device, "W"); Parameter b = new Parameter((NDShape) new[] { lstmCellDimension * 4 }, dataType, CNTKLib.GlorotUniformInitializer(1.0, 1, 0, seed++), device, "b"); Variable linearCombination = CNTKLib.Times(W, input, "linearCombinationInput"); Variable linearCombinationPlusBias = CNTKLib.Plus(b, linearCombination, "linearCombinationInputPlusBias"); Parameter H = new Parameter((NDShape) new[] { lstmCellDimension * 4, lstmOutputDimension }, dataType, CNTKLib.GlorotUniformInitializer(1.0, 1, 0, seed++)); Variable linearCombinationPrevOutput = CNTKLib.Times(H, prevOutput, "linearCombinationPreviousOutput"); Variable gateInput = CNTKLib.Plus(linearCombinationPlusBias, linearCombinationPrevOutput, "gateInput"); Variable forgetProjection = CNTKLib.Slice(gateInput, new AxisVector() { new Axis(0) }, new IntVector() { lstmCellDimension * 0 }, new IntVector() { lstmCellDimension * 1 }); Variable inputProjection = CNTKLib.Slice(gateInput, new AxisVector() { new Axis(0) }, new IntVector() { lstmCellDimension * 1 }, new IntVector() { lstmCellDimension * 2 }); Variable outputProjection = CNTKLib.Slice(gateInput, new AxisVector() { new Axis(0) }, new IntVector() { lstmCellDimension * 2 }, new IntVector() { lstmCellDimension * 3 }); Variable candidateProjection = CNTKLib.Slice(gateInput, new AxisVector() { new Axis(0) }, new IntVector() { lstmCellDimension * 3 }, new IntVector() { lstmCellDimension * 4 }); Function ft = CNTKLib.Sigmoid(forgetProjection, "ForgetGate"); Function it = CNTKLib.Sigmoid(inputProjection, "InputGate"); Function ot = CNTKLib.Sigmoid(outputProjection, "OutputGate"); Function ctt = CNTKLib.Tanh(candidateProjection, "Candidate"); Function bft = CNTKLib.ElementTimes(prevCellState, ft); Function bit = CNTKLib.ElementTimes(it, ctt); Function ct = CNTKLib.Plus(bft, bit, "CellState"); // According to the TrainingCSharp example in CNTK repository, h (output) should be stabilized, // however, the Python binding only stabilizes the previous output and previous cell state Function h = CNTKLib.ElementTimes(ot, CNTKLib.Tanh(ct), "Output"); Function c = ct; if (lstmOutputDimension != lstmCellDimension) { Parameter P = new Parameter((NDShape) new[] { lstmOutputDimension, lstmCellDimension }, dataType, CNTKLib.GlorotUniformInitializer(1.0, 1, 0, seed++)); h = CNTKLib.Times(P, h, "StandarizedOutput"); } return(new Tuple <Function, Function>(h, c)); }
private static List<DeviceDescriptor> CloneDescriptors(IEnumerable<DeviceDescriptor> devices) { List<DeviceDescriptor> descriptors = new List<DeviceDescriptor>(); foreach (DeviceDescriptor device in devices) { DeviceDescriptor descriptor = new DeviceDescriptor(); ObjectCopier.CopyObject(device, descriptor); descriptors.Add(descriptor); } return descriptors; }
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; } }
/// <summary> /// The example shows /// - how to evaluate multiple sample requests in parallel. /// </summary> /// <param name="device">Specify on which device to run the evaluation.</param> public static void EvaluateMultipleImagesInParallel(DeviceDescriptor device) { Console.WriteLine("\n===== Evaluate multiple images in parallel ====="); string modelFilePath = "resnet20.dnn"; // This program uses images from the CIFAR-10 dataset for evaluation. // Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset. var imageList = new List <string>() { "00000.png", "00001.png", "00002.png", "00003.png", "00004.png" }; foreach (var image in imageList) { 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)); } 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)); int maximalNumOfParallelRequests = 3; BlockingCollection <Function> Models = new BlockingCollection <Function>(); // Load and clone the model. // The model resnet20.dnn is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/Models/TrainResNet_CIFAR10.py // Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model. var rootFunc = Function.Load(modelFilePath, device); Models.Add(rootFunc); // It is not thread-safe to perform concurrent evaluation requests using the same model function. // Use clone() to create copies of model function for parallel evaluation. // ParameterCloningMethod.Share specifies that model parameters are shared between cloned model functions, while // each model function instance has its own private state for evaluation. for (int i = 1; i < maximalNumOfParallelRequests; i++) { Models.Add(rootFunc.Clone(ParameterCloningMethod.Share)); } // Get shape data for the input variable var input = rootFunc.Arguments.Single(); NDShape inputShape = input.Shape; int imageWidth = inputShape[0]; int imageHeight = inputShape[1]; int imageChannels = inputShape[2]; int imageSize = inputShape.TotalSize; Object lockObj = new object(); // Start to evaluate samples in parallel. // If there are more evaluation requests than the number of available model function instances, some evaluation // requests will have to wait for a free model function instance. Console.WriteLine(string.Format("Evaluate {0} images in parallel using {1} model instances.", imageList.Count, maximalNumOfParallelRequests)); Parallel.ForEach(imageList, new ParallelOptions() { MaxDegreeOfParallelism = imageList.Count }, (image) => { var evaluatorFunc = Models.Take(); try { Variable outputVar = evaluatorFunc.Output; Variable inputVar = evaluatorFunc.Arguments.Single(); var inputDataMap = new Dictionary <Variable, Value>(); var outputDataMap = new Dictionary <Variable, Value>(); Bitmap bmp = new Bitmap(Bitmap.FromFile(image)); var resized = bmp.Resize((int)imageWidth, (int)imageHeight, true); List <float> resizedCHW = resized.ParallelExtractCHW(); // Create input data map var inputVal = Value.CreateBatch(inputVar.Shape, resizedCHW, device); inputDataMap.Add(inputVar, inputVal); // Create ouput data map. Using null as Value to indicate using system allocated memory. // Alternatively, create a Value object and add it to the data map. outputDataMap.Add(outputVar, null); // Start evaluation on the device evaluatorFunc.Evaluate(inputDataMap, outputDataMap, device); // Get evaluate result as dense output var outputVal = outputDataMap[outputVar]; var outputData = outputVal.GetDenseData <float>(outputVar); // Serialize output lock (lockObj) { Console.WriteLine(string.Format("Evaluation result for {0}:", image)); PrintOutput(outputVar.Shape.TotalSize, outputData); } } finally { Models.Add(evaluatorFunc); } }); }
public static float ValidateModelWithMinibatchSource(string modelFile, MinibatchSource testMinibatchSource, int[] imageDim, int numClasses, string featureInputName, string labelInputName, string outputName, DeviceDescriptor device, int maxCount = 1000) { Function model = Function.Load(modelFile, device); Variable imageInput = model.Arguments[0]; Variable labelOutput = model.Outputs.Single(o => o.Name == outputName); StreamInformation featureStreamInfo = testMinibatchSource.StreamInfo(featureInputName); StreamInformation labelStreamInfo = testMinibatchSource.StreamInfo(labelInputName); int batchSize = 50; int miscountTotal = 0, totalCount = 0; while (true) { UnorderedMapStreamInformationMinibatchData minibatchData = testMinibatchSource.GetNextMinibatch((uint)batchSize, device); if (minibatchData == null || minibatchData.Count == 0) { break; } totalCount += (int)minibatchData[featureStreamInfo].numberOfSamples; // Expected labels are in the minibatch data. IList <IList <float> > labelData = minibatchData[labelStreamInfo].data.GetDenseData <float>(labelOutput); List <int> expectedLabels = labelData.Select(l => l.IndexOf(l.Max())).ToList(); Dictionary <Variable, Value> inputDataMap = new Dictionary <Variable, Value>() { { imageInput, minibatchData[featureStreamInfo].data } }; Dictionary <Variable, Value> outputDataMap = new Dictionary <Variable, Value>() { { labelOutput, null } }; model.Evaluate(inputDataMap, outputDataMap, device); IList <IList <float> > outputData = outputDataMap[labelOutput].GetDenseData <float>(labelOutput); List <int> actualLabels = outputData.Select(l => l.IndexOf(l.Max())).ToList(); int misMatches = actualLabels.Zip(expectedLabels, (a, b) => a.Equals(b) ? 0 : 1).Sum(); miscountTotal += misMatches; Console.WriteLine($"Validating Model: Total Samples = {totalCount}, Misclassify Count = {miscountTotal}"); if (totalCount > maxCount) { break; } } float errorRate = 1.0F * miscountTotal / totalCount; Console.WriteLine($"Model Validation Error = {errorRate}"); return(errorRate); }
/// <summary> /// The example shows /// - how to load model. /// - how to prepare input data as batch of sequences with variable length. /// how to prepare data using one-hot vector format. /// - how to prepare input and output data map. /// - how to evaluate a model. /// </summary> /// <param name="device">Specify on which device to run the evaluation.</param> public static void EvaluationBatchOfSequencesUsingOneHot(DeviceDescriptor device) { try { Console.WriteLine("\n===== Evaluate batch of sequences with variable length using one-hot vector ====="); // The model atis.dnn is trained by <CNTK>/Examples/LanguageUnderstanding/ATIS/Python/LanguageUnderstanding.py // Please see README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS about how to train the model. string modelFilePath = "atis.dnn"; ThrowIfFileNotExist(modelFilePath, string.Format("Error: The model '{0}' does not exist. Please follow instructions in README.md in <CNTK>/Examples/LanguageUnderstanding/ATIS to create the model.", modelFilePath)); Function modelFunc = Function.Load(modelFilePath, device); // Read word and slot index files. string vocabFile = "query.wl"; string labelFile = "slots.wl"; ThrowIfFileNotExist(vocabFile, string.Format("Error: The file '{0}' does not exist. Please copy it from <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/ to the output directory.", vocabFile)); ThrowIfFileNotExist(labelFile, string.Format("Error: The file '{0}' does not exist. Please copy it from <CNTK>/Examples/LanguageUnderstanding/ATIS/BrainScript/ to the output directory.", labelFile)); var vocabToIndex = buildVocabIndex(vocabFile); var indexToSlots = buildSlotIndex(labelFile); // Get input variable var inputVar = modelFunc.Arguments.Single(); int vocabSize = inputVar.Shape.TotalSize; // Prepare the input data. // Each sample is represented by an index to the onehot vector, so the index of the non-zero value of each sample is saved in the inner list. // The outer list represents sequences contained in the batch. var inputBatch = new List <List <int> >(); // SeqStartFlagBatch is used to indicate whether this sequence is a new sequence (true) or concatenating the previous sequence (false). var seqStartFlagBatch = new List <bool>(); var inputSentences = new List <string>() { "BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS", "BOS flights from new york to seattle EOS" }; var inputWords = new List <string[]>(2); int numOfSequences = inputSentences.Count; for (int seqIndex = 0; seqIndex < numOfSequences; seqIndex++) { // The input for one sequence // Get the index of each word in the sentence. var substring = inputSentences[seqIndex].Split(' '); inputWords.Add(substring); var seqData = new List <int>(); foreach (var str in substring) { var index = vocabToIndex[str]; seqData.Add(index); } inputBatch.Add(seqData); seqStartFlagBatch.Add(true); } // Create the Value representing the batch data. var inputValue = Value.CreateBatchOfSequences <float>(vocabSize, inputBatch, seqStartFlagBatch, DeviceDescriptor.CPUDevice); // Build input data map. var inputDataMap = new Dictionary <Variable, Value>(); inputDataMap.Add(inputVar, inputValue); // Prepare output Variable outputVar = modelFunc.Output; // Create ouput data map. Using null as Value to indicate using system allocated memory. var outputDataMap = new Dictionary <Variable, Value>(); outputDataMap.Add(outputVar, null); // Evalaute the model modelFunc.Evaluate(inputDataMap, outputDataMap, device); // Get evaluation result. var outputVal = outputDataMap[outputVar]; var outputData = outputVal.GetDenseData <float>(outputVar); // output the result var outputSampleSize = (int)outputVar.Shape.TotalSize; if (outputData.Count != inputBatch.Count) { throw new ApplicationException("The number of sequence in output does not match that in input."); } Console.WriteLine("The number of sequences in the batch: " + outputData.Count); for (int seqno = 0; seqno < outputData.Count; seqno++) { var slotSeq = outputData[seqno]; Console.WriteLine("Sequence {0}: ", seqno); if (slotSeq.Count % outputSampleSize != 0) { throw new ApplicationException("The number of elements in the slot sequence is not a multiple of sample size"); } var numOfSlotsInOutput = slotSeq.Count / outputSampleSize; if (inputWords[seqno].Count() != numOfSlotsInOutput) { throw new ApplicationException("The number of input words and the number of output slots do not match."); } for (int i = 0; i < numOfSlotsInOutput; i++) { var max = slotSeq[i * outputSampleSize]; var maxIndex = 0; for (int j = 1; j < outputSampleSize; j++) { if (slotSeq[i * outputSampleSize + j] > max) { max = slotSeq[i * outputSampleSize + j]; maxIndex = j; } } Console.WriteLine(String.Format(" {0, 10} ---- {1}", inputWords[seqno][i], indexToSlots[maxIndex])); } Console.WriteLine(); } } 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; } }
private List <int> EvaluateBatch(Function model, IEnumerable <Example_201_Item> evalData2, Variable input, DeviceDescriptor device) { List <Example_201_Item> orderedData = evalData2.ToList(); Value inputData = Value.CreateBatch(input.Shape, orderedData.SelectMany(data => data.Image.Select(x => (double)x)).ToArray(), device); var inputDic = new Dictionary <Variable, Value>() { { input, inputData } }; var outputDic = new Dictionary <Variable, Value>() { { model.Output, null } }; model.Evaluate(inputDic, outputDic, device); Value outputValue = outputDic[model.Output]; IList <IList <double> > prediction = outputValue.GetDenseData <double>(model.Output); List <int> predictedLabels = prediction.Select((IList <double> l) => l.IndexOf(l.Max())).ToList(); return(predictedLabels); }
public virtual bool Init(DeviceDescriptor deviceDescriptor) { return(true); }
private void RunTraining(Trainer trainer, GenericMinibatchSource minibatchSource, int numMinibatchesToTrain, DeviceDescriptor device) { Debug.WriteLine($"Minibatch;CrossEntropyLoss;EvaluationCriterion;"); double aggregate_metric = 0; for (int minibatchCount = 0; minibatchCount < numMinibatchesToTrain; minibatchCount++) { IDictionary <Variable, MinibatchData> data = minibatchSource.GetNextRandomMinibatch(); trainer.TrainMinibatch(data, device); PrintTrainingProgress(trainer, minibatchCount); } }
public FeedForwaredNN(DeviceDescriptor device) { m_device = device; }
private void Evaluate(Function model, IEnumerable <Example_201_Item> evalData2, Variable input, DeviceDescriptor device, IDictionary <double, string> labelIndex) { List <Example_201_Item> orderedData = evalData2.ToList(); int minibatchSize = 32; List <Example_201_Item> queue = new List <Example_201_Item>(); List <int> predictedLabels = new List <int>(); for (int i = 0; i < orderedData.Count; i++) { queue.Add(orderedData[i]); if (queue.Count == minibatchSize || i == orderedData.Count - 1) { List <int> tempPred = EvaluateBatch(model, queue, input, device); predictedLabels.AddRange(tempPred); queue.Clear(); } } if (predictedLabels.Count != orderedData.Count) { throw new Exception("actualLabels.Count != orderedData.Count"); } Dictionary <int, int> nbSuccessByLabel = new Dictionary <int, int>(); Dictionary <int, int> nbItemsByLabel = new Dictionary <int, int>(); int nbSuccess = 0; for (int i = 0; i < predictedLabels.Count; i++) { int predictedClassification = predictedLabels[i]; int expectedClassification = (int)orderedData[i].Label; if (!nbItemsByLabel.ContainsKey(expectedClassification)) { nbItemsByLabel[expectedClassification] = 0; } nbItemsByLabel[expectedClassification]++; if (predictedClassification == expectedClassification) { nbSuccess++; if (!nbSuccessByLabel.ContainsKey(expectedClassification)) { nbSuccessByLabel[expectedClassification] = 0; } nbSuccessByLabel[expectedClassification]++; } } Debug.WriteLine($"Prediction accuracy : {nbSuccess / (double)orderedData.Count:p2}"); for (int i = 0; i < labelIndex.Count; i++) { int nbSuccessByLabelI; if (!nbSuccessByLabel.TryGetValue(i, out nbSuccessByLabelI)) { nbSuccessByLabelI = 0; } Debug.WriteLine($"Prediction accuracy (label = {labelIndex[i]}): { nbSuccessByLabelI / (double)nbItemsByLabel[i]:p2}"); } }
public static Function Build(Variable input, DeviceDescriptor device, string outputName = "Stabilizer") { return(Build <float>(input, device, outputName)); }
protected override Function BuildNetwork(Variable input, DeviceDescriptor device, string name) { var c1 = CNTKLib.ElementTimes(input, CNTKLib.ElementTimes(Constant.Scalar(DataType.Float, Beta), CNTKLib.Sigmoid(input, name))); return(c1); }
static Tuple <Function, Function> LSTMPComponentWithSelfStabilization <ElementType>(Variable input, NDShape outputShape, NDShape cellShape, Func <Variable, Function> recurrenceHookH, Func <Variable, Function> recurrenceHookC, DeviceDescriptor device) { var dh = Variable.PlaceholderVariable(outputShape, input.DynamicAxes); var dc = Variable.PlaceholderVariable(cellShape, input.DynamicAxes); var LSTMCell = LSTMPCellWithSelfStabilization <ElementType>(input, dh, dc, device); var actualDh = recurrenceHookH(LSTMCell.Item1); var actualDc = recurrenceHookC(LSTMCell.Item2); // Form the recurrence loop by replacing the dh and dc placeholders with the actualDh and actualDc (LSTMCell.Item1).ReplacePlaceholders(new Dictionary <Variable, Variable> { { dh, actualDh }, { dc, actualDc } }); return(new Tuple <Function, Function>(LSTMCell.Item1, LSTMCell.Item2)); }
protected override Function BuildNetwork(Variable input, DeviceDescriptor device, string name) { var c1 = CNTKLib.ELU(input, name); return(c1); }
/// <summary> /// Build and train a RNN model. /// </summary> /// <param name="device">CPU or GPU device to train and run the model</param> public static void Train(DeviceDescriptor device) { const int inputDim = 2000; const int cellDim = 25; const int hiddenDim = 25; const int embeddingDim = 50; const int numOutputClasses = 5; // build the model var featuresName = "features"; var features = Variable.InputVariable(new int[] { inputDim }, DataType.Float, featuresName, null, true /*isSparse*/); var labelsName = "labels"; var labels = Variable.InputVariable(new int[] { numOutputClasses }, DataType.Float, labelsName, new List <Axis>() { Axis.DefaultBatchAxis() }, true); var classifierOutput = LSTMSequenceClassifierNet(features, numOutputClasses, embeddingDim, hiddenDim, cellDim, device, "classifierOutput"); Function trainingLoss = CNTKLib.CrossEntropyWithSoftmax(classifierOutput, labels, "lossFunction"); Function prediction = CNTKLib.ClassificationError(classifierOutput, labels, "classificationError"); // prepare training data IList <StreamConfiguration> streamConfigurations = new StreamConfiguration[] { new StreamConfiguration(featuresName, inputDim, true, "x"), new StreamConfiguration(labelsName, numOutputClasses, false, "y") }; var minibatchSource = MinibatchSource.TextFormatMinibatchSource( Path.Combine(DataFolder, "Train.ctf"), streamConfigurations, MinibatchSource.InfinitelyRepeat, true); var featureStreamInfo = minibatchSource.StreamInfo(featuresName); var labelStreamInfo = minibatchSource.StreamInfo(labelsName); // prepare for training TrainingParameterScheduleDouble learningRatePerSample = new TrainingParameterScheduleDouble( 0.0005, 1); TrainingParameterScheduleDouble momentumTimeConstant = CNTKLib.MomentumAsTimeConstantSchedule(256); IList <Learner> parameterLearners = new List <Learner>() { Learner.MomentumSGDLearner(classifierOutput.Parameters(), learningRatePerSample, momentumTimeConstant, /*unitGainMomentum = */ true) }; var trainer = Trainer.CreateTrainer(classifierOutput, trainingLoss, prediction, parameterLearners); // train the model uint minibatchSize = 200; int outputFrequencyInMinibatches = 20; int miniBatchCount = 0; int numEpochs = 5; while (numEpochs > 0) { var minibatchData = minibatchSource.GetNextMinibatch(minibatchSize, device); var arguments = new Dictionary <Variable, MinibatchData> { { features, minibatchData[featureStreamInfo] }, { labels, minibatchData[labelStreamInfo] } }; trainer.TrainMinibatch(arguments, device); TestHelper.PrintTrainingProgress(trainer, miniBatchCount++, outputFrequencyInMinibatches); // Because minibatchSource is created with MinibatchSource.InfinitelyRepeat, // batching will not end. Each time minibatchSource completes an sweep (epoch), // the last minibatch data will be marked as end of a sweep. We use this flag // to count number of epochs. if (TestHelper.MiniBatchDataIsSweepEnd(minibatchData.Values)) { numEpochs--; } } classifierOutput.Save("../../../../Data/SequenceClassification/LSTM.model"); }
public override Function BuildNew(Variable input, DeviceDescriptor device, string name) { return(BuildNetwork(input, device, name)); }
// // The example shows // - how to use OneHot vector as input and output for evaluation // The input data contains multiple sequences and each sequence contains multiple samples. // There is only one non-zero value in each sample, so the sample can be represented by the index of this non-zero value // - use variable name, instead of Variable, for as parameters for evaluate. // static void EvaluationWithOneHot(DeviceDescriptor device) { // Todo: fill both index values var vocabToIndex = new Dictionary<string, uint>(); var indexToVocab = new Dictionary<uint, string>(); Function myFunc = Function.LoadModel("atis.model"); // Get input variable const string inputName = "features"; var inputVar = myFunc.Arguments.Where(variable => string.Equals(variable.Name, inputName)).Single(); uint vocabSize = inputVar.Shape.TotalSize; // Use case 1: Evalaute a single sequence using OneHot vector as input. var inputSentence = "BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS"; // Build input data for one sequence var seqData = new List<uint>(); // SeqStartFlagBatch is used to indicate whether this sequence is a new sequence (true) or concatenating the previous sequence (false). var seqStartFlag = true; // Get the index of each word in the sentence. string[] substring = inputSentence.Split(' '); foreach (var str in substring) { // Get the index of the word var index = vocabToIndex[str]; // Add the sample to the sequence seqData.Add(index); } // Create input value using OneHot vector data. var inputValue = Value.CreateSequence<float>(vocabSize, seqData, seqStartFlag, device); // Build input data map. var inputDataMap = new Dictionary<Variable, Value>(); inputDataMap.Add(inputVar, inputValue); // Prepare output const string outputName = "out.z_output"; Variable outputVar = myFunc.Outputs.Where(variable => string.Equals(variable.Name, outputName)).Single(); // Create ouput data map. Using null as Value to indicate using system allocated memory. var outputDataMap = new Dictionary<Variable, Value>(); outputDataMap.Add(outputVar, null); // Evalaute the model. myFunc.Evaluate(inputDataMap, outputDataMap, device); // Get output result var outputData = new List<List<uint>>(); Value outputVal = outputDataMap[outputVar]; outputVal.CopyTo<float>(vocabSize, outputData); // Use case 2: evaluate batch of sequences using OneHot vector as input. // Prepare the input data. // Each sample is represented by an index to the onehot vector, so the index of the non-zero value of each sample is saved in the inner list. // The outer list represents sequences contained in the batch. var inputBatch = new List<List<uint>>(); // SeqStartFlagBatch is used to indicate whether this sequence is a new sequence (true) or concatenating the previous sequence (false). var seqStartFlagBatch = new List<bool>(); var inputSentences = new List<string>() { "BOS i would like to find a flight from charlotte to las vegas that makes a stop in st. louis EOS", "BOS I want to book a flight from NewYork to Seattle EOS" }; int numOfSequences = inputSentences.Count; for (int seqIndex = 0; seqIndex < numOfSequences; seqIndex++) { // The input for one sequence seqData = new List<uint>(); // Get the index of each word in the sentence. substring = inputSentences[seqIndex].Split(' '); foreach (var str in substring) { var index = vocabToIndex[str]; seqData.Add(index); } inputBatch.Add(seqData); seqStartFlagBatch.Add(true); } // Create the Value representing the batch data. inputValue = Value.CreateBatchOfSequences<float>(vocabSize, inputBatch, seqStartFlagBatch, DeviceDescriptor.CPUDevice); // Build input and output data map inputDataMap[inputVar] = inputValue; outputDataMap[outputVar] = null; // Evalaute the model myFunc.Evaluate(inputDataMap, outputDataMap, device); // Get evaluation result. outputData = new List<List<uint>>(); outputVal = outputDataMap[outputVar]; outputVal.CopyTo<float>(vocabSize, outputData); // output the result var numOfElementsInSample = vocabSize; uint seqNo = 0; foreach (var seq in outputData) { Console.Write("Seq=" + seqNo + ":"); foreach (var index in seq) { // get the word based on index Console.Write(indexToVocab[index]); } Console.WriteLine(); // next sequence. seqNo++; } }
public static extern Error libusb_get_device_descriptor(IntPtr device, out DeviceDescriptor descriptor);
private void deviceListView_ItemChecked(object sender, ItemCheckedEventArgs e) { if (this.updatingListView) return; bool enabled = e.Item.Checked; List<DeviceDescriptor> descriptors = new List<DeviceDescriptor>(); MinerFormViewModel viewModel = GetViewModelToView(); DeviceViewModel device = viewModel.Devices[e.Item.Index]; DeviceDescriptor descriptor = new DeviceDescriptor(); ObjectCopier.CopyObject(device, descriptor); descriptors.Add(descriptor); ToggleDevices(descriptors, enabled); }
/// <summary> /// The example shows /// - how to load model. /// - how to prepare input data for a batch of samples. /// - how to prepare input and output data map. /// - how to evaluate a model. /// - how to retrieve evaluation result and retrieve output data in dense format. /// </summary> /// <param name="device">Specify on which device to run the evaluation.</param> public static void EvaluationBatchOfImages(DeviceDescriptor device) { try { Console.WriteLine("\n===== Evaluate batch of images ====="); string modelFilePath = "resnet20.dnn"; // This program uses images from the CIFAR-10 dataset for evaluation. // Please see README.md in <CNTK>/Examples/Image/DataSets/CIFAR-10 about how to download the CIFAR-10 dataset. var imageList = new List <string>() { "00000.png", "00001.png", "00002.png" }; foreach (var image in imageList) { 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)); } 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)); // Load the model. // The model resnet20.dnn is trained by <CNTK>/Examples/Image/Classification/ResNet/Python/Models/TrainResNet_CIFAR10.py // Please see README.md in <CNTK>/Examples/Image/Classification/ResNet about how to train the model. Function modelFunc = Function.Load(modelFilePath, device); // Get input variable. The model has only one single input. // The same way described above for output variable can be used here to get input variable by name. Variable inputVar = modelFunc.Arguments.Single(); // 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; // The model has only one output. // If the model have more than one output, use the following way to get output variable by name. // Variable outputVar = modelFunc.Outputs.Where(variable => string.Equals(variable.Name, outputName)).Single(); Variable outputVar = modelFunc.Output; var inputDataMap = new Dictionary <Variable, Value>(); var outputDataMap = new Dictionary <Variable, Value>(); 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(); // Aadd this sample to the data buffer. seqData.AddRange(resizedCHW); } // Create Value for the batch data. var inputVal = Value.CreateBatch(inputVar.Shape, seqData, device); // Create input data map. inputDataMap.Add(inputVar, inputVal); // Create ouput data map. Using null as Value to indicate using system allocated memory. // Alternatively, create a Value object and add it to the data map. outputDataMap.Add(outputVar, null); // Evaluate the model against the batch input modelFunc.Evaluate(inputDataMap, outputDataMap, device); // Retrieve the evaluation result. var outputVal = outputDataMap[outputVar]; var outputData = outputVal.GetDenseData <float>(outputVar); // Output result PrintOutput(outputVar.Shape.TotalSize, outputData); } 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; } }
private void deviceListView_ItemChecked(object sender, ItemCheckedEventArgs e) { if (updatingListView) return; bool enabled = e.Item.Checked; List<DeviceDescriptor> descriptors = new List<DeviceDescriptor>(); DeviceViewModel device = (DeviceViewModel)e.Item.Tag; DeviceDescriptor descriptor = new DeviceDescriptor(); ObjectCopier.CopyObject(device, descriptor); descriptors.Add(descriptor); app.ToggleDevices(descriptors, enabled); }