MinibatchSource CreateMinibatchSource(string mapFilePath, string featuresName, string targetsName, int numberOfClasses, int[] inputShape, bool augmentation) { var transforms = new List <CNTKDictionary>(); if (augmentation) { var randomSideTransform = CNTKLib.ReaderCrop( cropType: "RandomSide", cropSize: new Tuple <int, int>(0, 0), sideRatio: new Tuple <float, float>(0.8f, 1.0f), areaRatio: new Tuple <float, float>(0.0f, 0.0f), aspectRatio: new Tuple <float, float>(1.0f, 1.0f), jitterType: "uniRatio"); transforms.Add(randomSideTransform); } var scaleTransform = CNTKLib.ReaderScale(inputShape[0], inputShape[1], inputShape[2]); transforms.Add(scaleTransform); var imageDeserializer = CNTKLib.ImageDeserializer(mapFilePath, targetsName, (uint)numberOfClasses, featuresName, transforms); var minibatchSourceConfig = new MinibatchSourceConfig(new DictionaryVector() { imageDeserializer }); return(CNTKLib.CreateCompositeMinibatchSource(minibatchSourceConfig)); }
private static MinibatchSource CreateMinibatchSource(string mapFilePath, string meanFilePath, int[] imageDims, int numClasses, uint maxSweeps) { List <CNTKDictionary> transforms = new List <CNTKDictionary> { CNTKLib.ReaderCrop("RandomSide", new Tuple <int, int>(0, 0), new Tuple <float, float>(0.8f, 1.0f), new Tuple <float, float>(0.0f, 0.0f), new Tuple <float, float>(1.0f, 1.0f), "uniRatio"), CNTKLib.ReaderScale(imageDims[0], imageDims[1], imageDims[2]), CNTKLib.ReaderMean(meanFilePath) }; var deserializerConfiguration = CNTKLib.ImageDeserializer(mapFilePath, "labels", (uint)numClasses, "features", transforms); MinibatchSourceConfig config = new MinibatchSourceConfig(new List <CNTKDictionary> { deserializerConfiguration }) { MaxSweeps = maxSweeps }; return(CNTKLib.CreateCompositeMinibatchSource(config)); }
private static MinibatchSource CreateMinibatchSource(string map_file, int[] image_dims, int num_classes) { List <CNTKDictionary> transforms = new List <CNTKDictionary> { CNTKLib.ReaderScale(image_dims[0], image_dims[1], image_dims[2], "linear") }; var deserializerConfiguration = CNTKLib.ImageDeserializer(map_file, "labels", (uint)num_classes, "image", transforms); MinibatchSourceConfig config = new MinibatchSourceConfig(new List <CNTKDictionary> { deserializerConfiguration }); return(CNTKLib.CreateCompositeMinibatchSource(config)); }
MinibatchSource CreateMinibatchSource(string filePath, string featuresName, string targetsName, int numberOfClasses, int[] inputShape, bool randomize) { var inputSize = inputShape.Aggregate((d1, d2) => d1 * d2); var streamConfigurations = new StreamConfigurationVector { new StreamConfiguration(featuresName, inputSize, isSparse: true), new StreamConfiguration(targetsName, numberOfClasses, isSparse: false) }; var deserializer = CNTKLib.CTFDeserializer(filePath, streamConfigurations); var minibatchSourceConfig = new MinibatchSourceConfig(new DictionaryVector() { deserializer }); return(CNTKLib.CreateCompositeMinibatchSource(minibatchSourceConfig)); }
//Function CreateConvolutionalNeuralNetwork(Variable features, int outDims, DeviceDescriptor device, string classifierName) //{ // // 28x28x1 -> 14x14x4 // int kernelWidth1 = 3, kernelHeight1 = 3, numInputChannels1 = 3, outFeatureMapCount1 = 4; // int hStride1 = 2, vStride1 = 2; // int poolingWindowWidth1 = 3, poolingWindowHeight1 = 3; // Function pooling1 = ConvolutionWithMaxPooling(features, device, kernelWidth1, kernelHeight1, // numInputChannels1, outFeatureMapCount1, hStride1, vStride1, poolingWindowWidth1, poolingWindowHeight1); // // 14x14x4 -> 7x7x8 // int kernelWidth2 = 3, kernelHeight2 = 3, numInputChannels2 = outFeatureMapCount1, outFeatureMapCount2 = 8; // int hStride2 = 2, vStride2 = 2; // int poolingWindowWidth2 = 3, poolingWindowHeight2 = 3; // Function pooling2 = ConvolutionWithMaxPooling(pooling1, device, kernelWidth2, kernelHeight2, // numInputChannels2, outFeatureMapCount2, hStride2, vStride2, poolingWindowWidth2, poolingWindowHeight2); // Function denseLayer = Dense(pooling2, outDims, device, Activation.None, classifierName); // return denseLayer; //} private void ImageLoader(string MapFilePath) { List <CNTKDictionary> transforms = new List <CNTKDictionary> { CNTKLib.ReaderScale(ImageDim[0], ImageDim[1], ImageDim[2]) // CNTKLib.ReaderMean(meanFilePath) }; var deserializerConfiguration = CNTKLib.ImageDeserializer(MapFilePath, "labels", (uint)NumClasses, "features", transforms); MinibatchSourceConfig config = new MinibatchSourceConfig(new List <CNTKDictionary> { deserializerConfiguration }) { MaxSweeps = 50 }; minibatchSource = CNTKLib.CreateCompositeMinibatchSource(config); imageStreamInfo = minibatchSource.StreamInfo("features"); labelStreamInfo = minibatchSource.StreamInfo("labels"); }
static MinibatchSource CreateTrainMinibatchSource( IReadOnlyDictionary <string, string> channelNameToMapFilePath, string ctfFilePath, int outputShape, int maxEpochsOrSweeps) { var imageDeserializers = channelNameToMapFilePath.Select(p => { var deserializer = CNTKLib.ImageDeserializer(p.Value, p.Key + LabelsName, (uint)1, p.Key + FeaturesName); AddGrayScaleTrue(deserializer); return(deserializer); }).ToArray(); var targetCtfDeserializer = CreateCtfDeserializer(ctfFilePath, outputShape); var deserializerList = new List <CNTKDictionary>(imageDeserializers) { targetCtfDeserializer }; MinibatchSourceConfig config = new MinibatchSourceConfig(deserializerList) { MaxSweeps = (uint)maxEpochsOrSweeps, }; return(CNTKLib.CreateCompositeMinibatchSource(config)); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(MinibatchSourceConfig obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public void Main() { // Create minibatch source var transforms = new List <CNTKDictionary> { CNTKLib.ReaderCrop("RandomSide", new Tuple <int, int>(0, 0), new Tuple <float, float>(0.8f, 1.0f), new Tuple <float, float>(0.0f, 0.0f), new Tuple <float, float>(1.0f, 1.0f), "uniRatio"), CNTKLib.ReaderScale(imageDim[0], imageDim[1], imageDim[2]) }; var conf = CNTKLib.ImageDeserializer(@"D:\Microsoft\Presentations\FY18 AI\DotNext17\Data\Train.txt", "labels", (uint)numClasses, "features", transforms); MinibatchSourceConfig config = new MinibatchSourceConfig(new List <CNTKDictionary> { conf }); var minibatchSource = CNTKLib.CreateCompositeMinibatchSource(config); var imageStreamInfo = minibatchSource.StreamInfo("features"); var labelStreamInfo = minibatchSource.StreamInfo("labels"); // build a model var imageInput = CNTKLib.InputVariable(imageDim, imageStreamInfo.m_elementType, "Images"); var labelsVar = CNTKLib.InputVariable(new int[] { numClasses }, labelStreamInfo.m_elementType, "Labels"); var C1 = ConvolutionLayer(imageInput, 5, 5, 3, 32); var C2 = ConvolutionLayer(C1, 5, 5, 32, 1); var C3 = ConvolutionLayer(C2, 5, 5, 64, 2); var C4 = DenseLayer(C3, 64, true); var C5 = DenseLayer(C4, 64, true); var z = DenseLayer(C5, numClasses, true); var loss = CNTKLib.CrossEntropyWithSoftmax(z, labelsVar); var evalError = CNTKLib.ClassificationError(z, labelsVar); // prepare for training CNTK.TrainingParameterScheduleDouble learningRatePerSample = new CNTK.TrainingParameterScheduleDouble(0.02, 1); IList <Learner> parameterLearners = new List <Learner>() { Learner.SGDLearner(z.Parameters(), learningRatePerSample) }; var trainer = Trainer.CreateTrainer(z, loss, evalError, parameterLearners); uint minibatchSize = 64; // train the model for (int ep = 0; ep < 1000; ep++) { var data = minibatchSource.GetNextMinibatch(minibatchSize); trainer.TrainMinibatch(new Dictionary <Variable, MinibatchData>() { { imageInput, data[imageStreamInfo] }, { labelsVar, data[labelStreamInfo] } }, device); if (ep % 50 == 0) { var _loss = trainer.PreviousMinibatchLossAverage(); var _eval = trainer.PreviousMinibatchEvaluationAverage(); WriteLine($"Epoch={ep}, loss={_loss}, eval={_eval}"); } } }
static void Main(string[] args) { var device = DeviceDescriptor.CPUDevice; int numOutputClasses = 2; // need to know these from the start uint inputDim = 6000; // also these uint batchSize = 50; // not sure how to make this increase to 100% of the file, for now // full path works "C:\\... //string CurrentFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); //string dataPath_test = "Data\\YXFFData6001Test.txt"; //string dataPath = Path.Combine(CurrentFolder, dataPath_test); string dataPath_model = "C:\\Users\\ITRI\\Documents\\Programming\\Csharp\\Learning_CNTK\\Data\\mModelZ1.dnn"; string dataPath_train = "C:\\Users\\ITRI\\Documents\\Programming\\Csharp\\Learning_CNTK\\Data\\YXFFData6001Train.txt"; // load saved model Function model = Function.Load(dataPath_model, device); // the model output needs to be processed still // out = C.softmax(z) var modelOut = CNTKLib.Softmax(model); var feature_fromModel = modelOut.Arguments[0]; var label_fromModel = modelOut.Output; string featureStreamName = "features"; string labelsStreamName = "labels"; var streamConfig = new StreamConfigurationVector { new StreamConfiguration(featureStreamName, inputDim), new StreamConfiguration(labelsStreamName, numOutputClasses) }; var deserializerConfig_train = CNTKLib.CTFDeserializer(dataPath_train, streamConfig); //StreamConfigurationVector streams = new StreamConfigurationVector //{ //new StreamConfiguration("feature", 100), //new StreamConfiguration("label", 10) //}; //var deserializerConfiguration = CNTKLib.CTFDeserializer(ctfFilePath, streams); MinibatchSourceConfig MBconfig_train = new MinibatchSourceConfig(new List <CNTKDictionary> { deserializerConfig_train }) { MaxSweeps = 1000, randomizationWindowInChunks = 0, randomizationWindowInSamples = 100000, }; var MBsource_train = CNTK.CNTKLib.CreateCompositeMinibatchSource(MBconfig_train); var featureStreamInfo_train = MBsource_train.StreamInfo(featureStreamName); var labelStreamInfo_train = MBsource_train.StreamInfo(labelsStreamName); var nextBatch_train = MBsource_train.GetNextMinibatch(batchSize, device); var MBdensefeature_train = nextBatch_train[featureStreamInfo_train].data; var MBdenseLabel_train = nextBatch_train[labelStreamInfo_train].data.GetDenseData <float>(label_fromModel); //Variable feature = modelOut.Arguments[0]; //Variable label = Variable.InputVariable(new int[] { numOutputClasses }, DataType.Float); //define input and output variable and connecting to the stream configuration var feature = Variable.InputVariable(new NDShape(1, inputDim), DataType.Float, featureStreamName); var label = Variable.InputVariable(new NDShape(1, numOutputClasses), DataType.Float, labelsStreamName); ////Step 2: define values, and variables //Variable x = Variable.InputVariable(new int[] { 1 }, DataType.Float, "input"); //Variable y = Variable.InputVariable(new int[] { 1 }, DataType.Float, "output"); ////Step 2: define training data set from table above //var xValues = Value.CreateBatch(new NDShape(1, 1), new float[] { 1f, 2f, 3f, 4f, 5f }, device); //var yValues = Value.CreateBatch(new NDShape(1, 1), new float[] { 3f, 5f, 7f, 9f, 11f }, device); //var features = Value.CreateBatch(NDShape sampleShape, IEnumerable<T> batch, DeviceDescriptor device); //Value.CreateBatch(inputDim, ,device); // prepare the training data //var featureStreamInfo = minibatchSource_train.StreamInfo(featureStreamName); //var labelStreamInfo = minibatchSource_train.StreamInfo(labelsStreamName); //var minibatchData = minibatchSource_train.GetNextMinibatch((uint)batchSize, device); //input Variable inputVar = modelOut.Arguments.Single(); var inputDataMap = new Dictionary <Variable, Value>(); inputDataMap.Add(inputVar, MBdensefeature_train); //output var outputDataMap = new Dictionary <Variable, Value>(); Variable outputVar = modelOut.Output; outputDataMap.Add(outputVar, null); // evaluate with loaded data modelOut.Evaluate(inputDataMap, outputDataMap, device); var outputData = outputDataMap[outputVar].GetDenseData <float>(outputVar); var actualLabels = outputData.Select((IList <float> l) => l.IndexOf(l.Max())).ToList(); //var loss = CNTKLib.CrossEntropyWithSoftmax(classifierOutput, labelVariable); //var evalError = CNTKLib.ClassificationError(classifierOutput, labelVariable); IList <int> expectedLabels = MBdenseLabel_train.Select(l => l.IndexOf(1.0F)).ToList(); int misMatches = actualLabels.Zip(expectedLabels, (a, b) => a.Equals(b) ? 0 : 1).Sum(); int labelsLength = actualLabels.Count; string correctness(bool comparison) { if (comparison) { return("Correct prediction"); } else { return("Incorrect prediction"); } } for (int i = 0; i < labelsLength; i++) { Console.WriteLine($"{i+1}.\tPredicted value: {actualLabels[i]};\tExpected value: {expectedLabels[i]};\t{correctness(actualLabels[i] == expectedLabels[i])}."); } Console.WriteLine($"Validating Model: Total Samples = {batchSize}, Misclassify Count = {misMatches}."); Console.Write("Success"); }
/// <summary> /// Retrains existing tiny YOLO v2 onnx model on new data /// </summary> /// <param name="device"></param> public static void RetrainTinyYoloV2(DeviceDescriptor device) { string[] labels = new string[] { "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" }; float[,] anchorCoordintes = { { 1.08f, 1.19f }, { 3.42f, 4.41f }, { 6.63f, 11.38f }, { 9.42f, 5.11f }, { 16.62f, 10.52f } }; // pairs of height and weight for every anchor box float threshold = 0.3f; // load model Function model = Function.Load(TinyYoloV2ModelPath, device, ModelFormat.ONNX); model = YoloDetectionLayer(model, anchorCoordintes, labels.Length, device); // get inputVariable and outputVariable of the model var inputVariable = model.Arguments.First(); var outputVariable = model.Output; // create a labelVariable Variable labelVariable = Variable.InputVariable(outputVariable.Shape, outputVariable.DataType); // create loss and evaluationError Functions var loss = YoloV2Loss(outputVariable, labelVariable, anchorCoordintes.GetLength(0), labels.Length, device); var evalError = YoloV2EvaluationError(outputVariable, labelVariable, anchorCoordintes.GetLength(0), labels.Length, threshold, device); // create a trainer // todo: set the right learning parameters TrainingParameterScheduleDouble learningRatePerSample = new TrainingParameterScheduleDouble(0.0001, 1); IList <Learner> parameterLearners = new List <Learner>() { Learner.SGDLearner(model.Parameters(), learningRatePerSample) }; var trainer = Trainer.CreateTrainer(outputVariable, loss, evalError, parameterLearners); // create minibatchSource // before any continuation is possible this issue has to be resolved: https://github.com/Microsoft/CNTK/issues/3497 // based on https://docs.microsoft.com/en-us/cognitive-toolkit/brainscript-and-python---understanding-and-extending-readers#configuring-a-reader-minibatch-source-in-python // based on https://github.com/Microsoft/CNTK/blob/master/Manual/Manual_How_to_feed_data.ipynb // todo find a way to define a userDeserializar. This is blocking. See: https://cntk.ai/pythondocs/cntk.io.html#cntk.io.UserMinibatchSource & https://www.cntk.ai/pythondocs/Manual_How_to_write_a_custom_deserializer.html // todo: lookup the code for FasterRCNN, and replicate that in c#. See: https://github.com/Microsoft/CNTK/tree/release/latest/Examples/Image/Detection var deserializerConfiguration = CNTKLib.ImageDeserializer("mapFilePath", "noUse", 0, "features"); // todo: maybe add streamDef in the list. see: https://github.com/Microsoft/CNTK/blob/master/Manual/Manual_How_to_feed_data.ipynb // todo: maybe use the line below, but set sparse to true, and transform the label date pre training var labelDeserializer = CNTKLib.CTFDeserializer("path", new StreamConfigurationVector(new List <StreamConfiguration>() { new StreamConfiguration("label", 10, false) })); // todo this must be a custom deserializer MinibatchSourceConfig config = new MinibatchSourceConfig(new List <CNTKDictionary> { deserializerConfiguration, labelDeserializer }) // todo: lookup how to sync the two deserializers { MaxSweeps = MinibatchSource.InfinitelyRepeat }; var minibatchSource = CNTKLib.CreateCompositeMinibatchSource(config); // todo: create minibatch transformet to transform the annotated data to the label variable format // todo: make sure that the input is transposed from HWC to CHW // train the network // save model // todo: Edit function below so it saves in ModelFormat.ONNX. See: https://github.com/Microsoft/CNTK/issues/3412 //model.Save(Path.Join(Program.RootPath, @"Output\Retrained tiny YOLO v2.onnx")); }