Example #1
0
        /// <summary>
        /// Args:
        /// 0 - image path
        /// 1 (optional, default: model.txt) - explicit model path
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string resourcesPath = "../MovieClassifierLearner/MLSecondaryDataSet";
            string modelPath     = "model.txt";


            //Load network from file
            Network net = Network.LoadNetworkFromFile(modelPath);

            //Load data set from dir
            Console.WriteLine("Loading images...");
            ImageLearningData[] data = ImageDataPreparer.PrepareImages(resourcesPath, 50, 25);
            List <LabelID>      ids  = new List <LabelID>();

            Console.WriteLine("Calculating averages...");
            int a = 0;

            foreach (ImageLearningData d in data)
            {
                Console.Write("\r" + a + "/" + data.Count());
                net.PushInputValues(d.data.ToArray());
                List <double> currentOutput = net.GetOutput();
                if (ids.Exists(x => x.label == d.label))
                {
                    LabelID i = ids.Find(x => x.label == d.label);
                    for (int j = 0; j < i.expectedOutput.Length; j++)
                    {
                        i.expectedOutput[j] += currentOutput[j];
                    }
                    i.hits++;
                }
                else
                {
                    LabelID i = new LabelID();
                    i.label          = d.label;
                    i.expectedOutput = currentOutput.ToArray();
                    i.hits           = 1;
                    ids.Add(i);
                }
                a++;
            }

            foreach (LabelID i in ids)
            {
                Console.WriteLine(i.label + " is " + i.expectedOutput.MaxAt());
                for (int j = 0; j < i.expectedOutput.Length; j++)
                {
                    Console.Write((i.expectedOutput[j] / i.hits) + " ");
                }
                Console.WriteLine();
            }

            ids = ids.OrderBy(x => x.expectedOutput.MaxAt()).ToList();
            Console.WriteLine("So the label.txt should look like this");
            foreach (LabelID i in ids)
            {
                Console.WriteLine(i.label);
            }
        }
Example #2
0
        public static double[][][] Load(string path, int outputCount, int imageWidth, int imageHeight, string labelFilePath = "")
        {
            /*data:
             * [0] -> Input Data to be evaluated
             * [1] -> Expected Output Data
             * [2] -> Test Input Data
             * [3] -> Test Output Data*/
            double[][][] finalData = new double[4][][];

            List <double[]> inputData          = new List <double[]>();
            List <double[]> expectedOutputData = new List <double[]>();
            List <double[]> testInputData      = new List <double[]>();
            List <double[]> testOutputData     = new List <double[]>();
            List <string>   uniqueLabels       = new List <string>();

            if (labelFilePath != "")
            {
                string[] labels = File.ReadAllLines(labelFilePath);
                uniqueLabels = labels.ToList();
            }

            {
                //Ensure that ImageLearningData[] will be disposed after scope exit
                ImageLearningData[] data = ImageDataPreparer.PrepareImages("Resources", imageWidth, imageHeight);
                //Pack data into double Data table

                for (int i = 0; i < data.Length; i++)
                {
                    int labelIndex = uniqueLabels.IndexOf(data[i].label);
                    if (labelIndex == -1)
                    {
                        uniqueLabels.Add(data[i].label);
                        labelIndex = uniqueLabels.Count - 1;
                    }

                    //Assign expected output
                    double[] output = new double[outputCount];
                    output[labelIndex] = 1.0;

                    //Assign input values
                    double[] input = data[i].data.ToArray();

                    //Decide between test set and learning set
                    if (i % 10 == 0)
                    {
                        testInputData.Add(input);
                        testOutputData.Add(output);
                    }
                    else
                    {
                        inputData.Add(input);
                        expectedOutputData.Add(output);
                    }
                }
            }

            //Shuffling
            int[] numbers = new int[inputData.Count];
            for (int i = 0; i < numbers.Length; i++)
            {
                numbers[i] = i;
            }
            Shuffler.Shuffle(numbers);
            List <double[]> tmpInputData  = new List <double[]>();
            List <double[]> tmpOutputData = new List <double[]>();

            for (int i = 0; i < numbers.Length; i++)
            {
                tmpInputData.Add(inputData[numbers[i]]);
                tmpOutputData.Add(expectedOutputData[numbers[i]]);
            }
            inputData          = tmpInputData;
            expectedOutputData = tmpOutputData;

            //Pack everything
            finalData[0] = inputData.ToArray();
            inputData.Clear();
            finalData[1] = expectedOutputData.ToArray();
            expectedOutputData.Clear();
            finalData[2] = testInputData.ToArray();
            testInputData.Clear();
            finalData[3] = testOutputData.ToArray();
            testOutputData.Clear();

            //Output labels
            File.WriteAllLines("labels.txt", uniqueLabels);

            return(finalData);
        }