Example #1
0
        public static void Evaluate(this Function func, Dictionary <string, Value> arguments, Dictionary <string, Value> outputs, DeviceDescriptor computeDevice)
        {
            // Evaluate the rootFunction.
            var argMap = new UnorderedMapVariableValuePtr();

            foreach (var p in arguments)
            {
                var variable = func.Arguments.Where(v => string.Equals(v.Name, p.Key)).Single();
                if (variable == null)
                {
                    throw new KeyNotFoundException("No input variable '" + p.Key + "' found.");
                }
                argMap.Add(variable, p.Value);
            }

            var outMap = new UnorderedMapVariableValuePtr();

            foreach (var p in outputs)
            {
                var variable = func.Outputs.Where(v => string.Equals(v.Name, p.Key)).Single();
                if (variable == null)
                {
                    throw new KeyNotFoundException("No output variable '" + p.Key + "' found.");
                }
                outMap.Add(variable, p.Value);
            }

            func.Evaluate(argMap, outMap, computeDevice);

            foreach (var p in outMap)
            {
                outputs[p.Key.Name] = p.Value;
            }
        }
Example #2
0
        /// <summary>
        /// Evaluate the model.
        /// </summary>
        /// <param name="featureData">The data to evaluate the model on</param>
        /// <param name="model">The model to use (defaults to trained model)</param>
        /// <returns>The output of the model</returns>
        public IList <IList <float> > Evaluate(float[][] featureData, CNTK.Function model = null)
        {
            // get the current model
            if (model == null)
            {
                model = this.Model;
            }

            // get the current batch
            var featureBatch = (SequenceLength == 1) ?
                               features.GetBatch(featureData, 0, featureData.Length) :
                               features.GetSequenceBatch(SequenceLength, featureData, 0, featureData.Length);

            // evaluate the model
            var inputs = new Dictionary <CNTK.Variable, CNTK.Value>()
            {
                { features, featureBatch }
            };
            var outputs = new Dictionary <CNTK.Variable, CNTK.Value>()
            {
                { model.Output, null }
            };

            model.Evaluate(inputs, outputs, NetUtil.CurrentDevice);

            // return result
            var result     = outputs[model.Output];
            var outputData = result.GetDenseData <float>(model.Output);

            return(outputData);
        }
Example #3
0
        /// <summary>
        /// Reshapes the dataset to new specified shape.
        /// </summary>
        /// <param name="shape">The new shape on the dataset.</param>
        /// <exception cref="System.ArgumentException"></exception>
        public void Reshape(params int[] shape)
        {
            CNTK.Variable features = CNTK.Variable.InputVariable(new int[] { Shape[1], Shape[0] }, DataType.Float);
            int           total    = Shape.Aggregate((d1, d2) => d1 * d2);

            if (shape.Aggregate((d1, d2) => d1 * d2) != total)
            {
                throw new ArgumentException(string.Format("Cannot reshape array of size {0} into shape {1}", total, string.Concat(shape)));
            }
            //shape.ToList().Insert(0, Data.Count);
            CNTK.Variable outfeatures = CNTK.Variable.InputVariable(shape, DataType.Float);

            //Variable outfeatures = new Variable(shape, VariableKind.Output, DataType.Float, null, false, new AxisVector(), false, "", "");
            CNTK.Function reshapeFunc = CNTKLib.Reshape(features, shape);

            List <float> vectorData = new List <float>();

            foreach (var item in Data)
            {
                vectorData.AddRange(item);
            }

            Value v = Value.CreateBatch <float>(Shape, vectorData, GlobalParameters.Device);
            Dictionary <CNTK.Variable, Value> inputs = new Dictionary <CNTK.Variable, Value>()
            {
                { features, v }
            };
            Dictionary <CNTK.Variable, Value> outputs = new Dictionary <CNTK.Variable, Value>()
            {
                { outfeatures, null }
            };

            reshapeFunc.Evaluate(inputs, outputs, GlobalParameters.Device);
            var res = outputs[outfeatures].GetDenseData <float>(outfeatures);

            Data = new List <List <float> >();
            foreach (var item in res)
            {
                Data.Add(item.ToList());
            }
        }
Example #4
0
        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;
            }
        }
Example #5
0
        /// <summary>
        /// Predicts the specified BMP.
        /// </summary>
        /// <param name="bmp">The image in bitmap format.</param>
        /// <param name="topK">The top k accurate result to return.</param>
        /// <returns></returns>
        public List <PredResult> Predict(Bitmap bmp, int topK = 3)
        {
            try
            {
                Variable inputVar = modelFunc.Arguments[0];

                NDShape inputShape  = inputVar.Shape;
                int     imageWidth  = inputShape[0];
                int     imageHeight = inputShape[1];

                var          resized    = bmp.Resize(imageWidth, imageHeight, true);
                List <float> resizedCHW = resized.ParallelExtractCHW();

                // Create input data map
                var inputDataMap = new Dictionary <Variable, Value>();
                var inputVal     = Value.CreateBatch(inputShape, resizedCHW, GlobalParameters.Device);
                inputDataMap.Add(inputVar, inputVal);
                inputVar = modelFunc.Arguments[1];
                //inputDataMap.Add(inputVar, null);

                Variable outputVar = modelFunc.Outputs.Where(x => (x.Shape.TotalSize == 1000)).ToList()[0];

                // Create output data map. Using null as Value to indicate using system allocated memory.
                // Alternatively, create a Value object and add it to the data map.
                var outputDataMap = new Dictionary <Variable, Value>();
                outputDataMap.Add(outputVar, null);

                // Start evaluation on the device
                modelFunc.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device);

                // Get evaluate result as dense output
                var outputVal  = outputDataMap[outputVar];
                var outputData = outputVal.GetDenseData <float>(outputVar);
                Dictionary <int, float> outputPred = new Dictionary <int, float>();

                for (int i = 0; i < outputData[0].Count; i++)
                {
                    outputPred.Add(i, outputData[0][i]);
                }

                var topList = outputPred.OrderByDescending(x => (x.Value)).Take(topK).ToList();
                List <PredResult> result        = new List <PredResult>();
                float             sumpredresult = outputPred.Sum(x => (x.Value));
                float             avgpredresult = outputPred.Average(x => (x.Value));
                float             min           = outputPred.Min(x => (x.Value));
                float             max           = outputPred.Max(x => (x.Value));

                foreach (var item in topList)
                {
                    result.Add(new PredResult()
                    {
                        Score = item.Value,
                        Name  = actualValues[item.Key]
                    });
                }

                Logging.WriteTrace("Prediction Completed");

                return(result);
            }
            catch (Exception ex)
            {
                Logging.WriteTrace(ex);
                throw ex;
            }
        }
Example #6
0
        /// <summary>
        /// Predicts the specified image.
        /// </summary>
        /// <param name="bmp">The image in bitmap format.</param>
        /// <param name="confidence">The confidence level for the prediction result.</param>
        /// <returns></returns>
        public List <PredResult> Predict(Bitmap bmp, double confidence = 0.5)
        {
            try
            {
                proposedBoxes = new List <Rectangle>();

                var          resized    = bmp.Resize(1000, 1000, true);
                List <float> roiList    = GenerateROIS(resized, model);
                List <float> resizedCHW = resized.ParallelExtractCHW();

                //CalculateROI(resizedCHW);
                // Create input data map
                var inputDataMap = new Dictionary <Variable, Value>();
                var inputVal1    = Value.CreateBatch(modelFunc.Arguments.First().Shape, resizedCHW, GlobalParameters.Device);
                inputDataMap.Add(modelFunc.Arguments.First(), inputVal1);

                var inputVal2 = Value.CreateBatch(modelFunc.Arguments[1].Shape, roiList, GlobalParameters.Device);
                inputDataMap.Add(modelFunc.Arguments[1], inputVal2);

                Variable outputVar = GetOutputVar(model);

                // Create output data map. Using null as Value to indicate using system allocated memory.
                // Alternatively, create a Value object and add it to the data map.
                var outputDataMap = new Dictionary <Variable, Value>();
                outputDataMap.Add(outputVar, null);

                // Start evaluation on the device
                modelFunc.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device);

                // Get evaluate result as dense output
                var outputVal            = outputDataMap[outputVar];
                var outputData           = outputVal.GetDenseData <float>(outputVar);
                List <PredResult> result = new List <PredResult>();

                var labels    = GetLabels(model);
                int numLabels = labels.Length;
                int numRois   = outputData[0].Count / numLabels;

                int numBackgroundRois = 0;
                for (int i = 0; i < numRois; i++)
                {
                    var outputForRoi = outputData[0].Skip(i * numLabels).Take(numLabels).ToList();

                    // Retrieve the predicted label as the argmax over all predictions for the current ROI
                    var max = outputForRoi.IndexOf(outputForRoi.Max());

                    if (max > 0)
                    {
                        result.Add(new PredResult()
                        {
                            Name  = labels[max],
                            BBox  = proposedBoxes[i],
                            Score = outputForRoi.Max()
                        });

                        //Console.WriteLine("Outcome for ROI {0}: {1} \t({2})", i, max, labels[max]);
                    }
                    else
                    {
                        numBackgroundRois++;
                    }
                }

                var groupBoxes = result.GroupBy(x => (x.Name)).ToList();
                result = new List <PredResult>();
                foreach (var item in groupBoxes)
                {
                    int       counter   = 0;
                    Rectangle unionRect = new Rectangle();

                    foreach (var rect in item.ToList())
                    {
                        if (counter == 0)
                        {
                            unionRect = rect.BBox;
                            continue;
                        }

                        unionRect = Rectangle.Union(unionRect, rect.BBox);
                    }

                    //var orderedList = item.ToList().OrderByDescending(x => (x.BBox.Width * x.BBox.Height)).ToList();
                    foreach (var rect in item.ToList())
                    {
                        unionRect = Rectangle.Intersect(unionRect, rect.BBox);
                    }

                    var goodPred = item.ToList().OrderByDescending(x => (x.Score)).ToList()[0];
                    goodPred.BBox = unionRect;
                    result.Add(goodPred);
                }

                //foreach (var item in result)
                //{
                //    img.Draw(item.BBox, new Bgr(0, 255, 0));
                //}

                //img.Save("objdet_pred.jpg");

                Logging.WriteTrace("Prediction Completed");

                return(result);
            }
            catch (Exception ex)
            {
                Logging.WriteTrace(ex);
                throw ex;
            }
        }