private void OnClassifyImage(object sender, EventArgs e)
        {
            try
            {
                _loggingService.Log("Classify drawing has started");

                var imagePreprocessor = new ImagePreprocessor();

                double[] pixels = imagePreprocessor.Preprocess(_uploadImageView.Image);

                IPredictionModel predictionModel = Global.PredictionModel;

                double[] prediction = predictionModel.Predict(pixels);

                _uploadImageView.ProcessPrediction(prediction);

                _loggingService.Log("Classify drawing has completed");
            }
            catch (NullReferenceException exception)
            {
                _loggingService.Log(exception);

                _messageService.ShowMessage("No image was uploaded. Please upload an image and try again.", "Upload error", icon: MessageBoxIcon.Information);
            }
            catch (Exception exception)
            {
                _loggingService.Log(exception);

                _messageService.ShowMessage("An error ocurred while classyfing the drawing. Please try again.", "Classification error", icon: MessageBoxIcon.Information);
            }
        }
        public double computeRMSE(Users validationUsers, Items validationItems, IPredictionModel model)
        {
            double sse = 0;
            double actualRating;
            double predictedRating;
            int    n = 0;

            foreach (User user in validationUsers)
            {
                foreach (string itemId in user.GetRatedItems())
                {
                    Item item = validationItems.GetItemById(itemId);

                    actualRating    = user.GetRating(itemId);
                    predictedRating = model.Predict(user, item);

                    if (predictedRating == -1) //in case the user and item are not exist the prediction is the average rating
                    {
                        predictedRating = avgRating;
                    }

                    sse += Math.Pow(actualRating - predictedRating, 2);
                    n++;
                }
            }
            return(Math.Sqrt(sse / n));
        }
Пример #3
0
        private void OnClassifyDrawing(object sender, EventArgs e)
        {
            try
            {
                _loggingService.Log("Classify drawing has started");

                var imagePreprocessor = new ImagePreprocessor();

                IPredictionModel predictionModel = Global.PredictionModel;

                Image img = _slidingWindowView.Drawing;

                foreach (Size windowSize in WindowSizes)
                {
                    foreach (BoundingBox boundingBox in ImageUtilities.SlidingWindow(img, windowSize, 112))
                    {
                        try
                        {
                            double[] pixels = imagePreprocessor.Preprocess(boundingBox.Image);

                            double[] prediction = predictionModel.Predict(pixels);

                            // If classification is over 99% draw a bounding box at this location
                            int    predicted         = prediction.ArgMax();
                            double predictedAccuracy = prediction[prediction.ArgMax()];

                            if (predictedAccuracy >= 0.95)
                            {
                                _slidingWindowView.DrawBoundingBox(boundingBox, predicted, predictedAccuracy);
                            }
                        }
                        catch (Exception exception)
                        {
                            _loggingService.Log(exception);
                        }
                    }
                }

                _loggingService.Log("Classify drawing has completed");
            }
            catch (Exception exception)
            {
                _loggingService.Log(exception);

                _messageService.ShowMessage("An error ocurred while classyfing the drawing. Please try again.", "Classification error", icon: MessageBoxIcon.Information);
            }
        }
Пример #4
0
        private List <string> GetTopItems(IPredictionModel predictionModel, string sUserId, int cRecommendations)
        {
            var currentUser = testUsers.getUserById(sUserId);

            var currentItems   = trainUsers.getUserById(sUserId).GetRatedItems();  //in case the user is also in the training set we want to filter out those rated items from train set
            var candidateItems = trainItems.GetAllItemsIds().Except(currentItems); //select items that current user is not yet rated

            var candidateItemsDic = candidateItems.ToDictionary(item => item, item => predictionModel.Predict(currentUser, trainItems.GetItemById(item)));
            var orderByPrediction = candidateItemsDic.OrderByDescending(item => item.Value);

            return(orderByPrediction.Select(item => item.Key).Take(cRecommendations).ToList());
        }
        public Tuple <double, double> computeConfidence(Users validationUsers, Items validationItems, IPredictionModel modelA, IPredictionModel modelB)
        {
            double aCounter = 0;
            double bCounter = 0;
            double aPrediction;
            double bPrediction;
            double aError;
            double bError;
            double actualRating;

            // calcualte number wins for each model
            foreach (User user in validationUsers)
            {
                foreach (string itemId in user.GetRatedItems())
                {
                    Item item = validationItems.GetItemById(itemId);
                    actualRating = user.GetRating(itemId);

                    aPrediction = modelA.Predict(user, item);
                    bPrediction = modelB.Predict(user, item);

                    aError = Math.Abs(actualRating - aPrediction);
                    bError = Math.Abs(actualRating - bPrediction);

                    if (aError < bError)
                    {
                        aCounter++;
                    }
                    else if (aError > bError)
                    {
                        bCounter++;
                    }
                    else
                    {
                        aCounter += 0.5;
                        bCounter += 0.5;
                    }
                }
            }

            int n = (int)(aCounter + bCounter);

            // calcualte pA
            double sum = 0;

            for (int i = (int)aCounter; i < n; i++)
            {
                sum += MathUtils.Factorial(n) / (MathUtils.Factorial(n - i) * MathUtils.Factorial(i));
            }

            double pA = (1 - Math.Pow(0.5, n) * sum);

            // calculate pB
            sum = 0;
            for (int i = (int)bCounter; i < n; i++)
            {
                sum += MathUtils.Factorial(n) / (MathUtils.Factorial(n - i) * MathUtils.Factorial(i));
            }

            double pB = (1 - Math.Pow(0.5, n) * sum);

            return(Tuple.Create(pA, pB));
        }