Пример #1
0
        private static int ColumnCount(ref CSVLoaderSettings settings, ref string[] _row)
        {
            var row   = _row;
            int count = row.Length;

            for (uint i = 0; i < row.Length; i++)
            {
                if (settings.filters.Exists(o => !o.Use(ref row, i)))
                {
                    count--;
                }
            }
            return(count);
        }
        // Prepare input data for prediction.
        public static void PreparePredictor(ref NetworkContainer container, ref ImageNetworkPredictSettings settings)
        {
            TestForErrors(ref settings);

            EncogWrapper.LoadNetworkFromFS(ref container, settings.trainedNetwork);

            List <ICSVFilter> baseFilters = new List <ICSVFilter>(1);
            ICSVFilter        quaternions = new CSVEvenColumnFilter();

            baseFilters.Add(quaternions);

            // Setup loader.
            CSVLoaderSettings CSVSettings = new CSVLoaderSettings
            {
                filePath = settings.predictData,
                trimUp   = 1,
                trimDown = 0,
                filters  = baseFilters
            };

            var data = CSVLoader <Vector3> .LoadData(ref CSVSettings);

            // Initialize image Transformer.
            ImageTransformerSettings imageSettings = new ImageTransformerSettings
            {
                focusJoints = (LeapMotionJoint[])Enum.GetValues(typeof(LeapMotionJoint)),
                samples     = data,
                size        = settings.imgSize
            };
            ImageTransformer imageTransformer = new ImageTransformer();


            if (settings.predictSettings.threshold.Equals(null))
            {
                settings.predictSettings = new EncogPredictSettings
                {
                    threshold = 0.9
                };
            }

            settings.predictSettings.data = imageTransformer.GetNeuralInput(imageSettings);

            if (settings.predictSettings.data.Length != container.network.InputCount)
            {
                throw new NoNetworkMatchException("Sample count doesn't match network input count.");
            }
        }
Пример #3
0
        // Convert data from a CSV file to the actual input array for the network.
        private static void ComputeData(
            uint sampleCount,
            string inputDataDirectory,
            ref double[][] outputData,
            ref double[][] outputAnswers,
            double outputValue,
            int index)
        {
            DirectoryInfo inputDirectory = new DirectoryInfo(inputDataDirectory);

            foreach (var file in inputDirectory.GetFiles("*.csv"))
            {
                List <ICSVFilter> baseFilters = new List <ICSVFilter>(1);
                ICSVFilter        quaternions = new CSVEvenColumnFilter();
                baseFilters.Add(quaternions);

                // Setup loader.
                CSVLoaderSettings settings = new CSVLoaderSettings
                {
                    filePath = file.FullName,
                    trimUp   = 1,
                    trimDown = 0,
                    filters  = baseFilters
                };

                var data = CSVLoader <Vector3> .LoadData(ref settings);

                // Initialize image Transformer.
                ImageTransformerSettings imageSettings = new ImageTransformerSettings
                {
                    focusJoints = (LeapMotionJoint[])Enum.GetValues(typeof(LeapMotionJoint)),
                    samples     = data,
                    size        = sampleCount
                };
                ImageTransformer imageTransformer = new ImageTransformer();

                BaseTrainHelper.Project1DInto2D(
                    imageTransformer.GetNeuralInput(imageSettings),
                    ref outputData,
                    index);

                // Set answer to given value.
                outputAnswers[index] = new double[] { outputValue };
                index++;
            }
        }
Пример #4
0
        public static void PreparePredictor(ref NetworkContainer container, ref CountNetworkPredictSettings settings)
        {
            TestForErrors(ref settings);

            EncogWrapper.LoadNetworkFromFS(ref container, settings.trainedNetwork);

            List <ICSVFilter> baseFilters = new List <ICSVFilter>(1);
            ICSVFilter        quaternions = new CSVEvenColumnFilter();

            baseFilters.Add(quaternions);

            // Setup loader.
            CSVLoaderSettings CSVSettings = new CSVLoaderSettings
            {
                filePath = settings.predictData,
                trimUp   = 1,
                trimDown = 0,
                filters  = baseFilters
            };

            var data = CSVLoader <Vector3> .LoadData(ref CSVSettings);

            // Initialize CountBased Transformer settings.
            IntervalBasedTransformerSettings countSettings = new IntervalBasedTransformerSettings
            {
                sampleList = data,
                count      = settings.sampleCount
            };
            CountBasedTransformer countTransformer = new CountBasedTransformer();

            if (settings.predictSettings.threshold.Equals(null))
            {
                settings.predictSettings = new EncogPredictSettings
                {
                    threshold = 0.9
                };
            }

            settings.predictSettings.data = countTransformer.GetNeuralInput(countSettings);

            if (settings.predictSettings.data.Length != container.network.InputCount)
            {
                throw new NoNetworkMatchException("Sample count doesn't match network input count.");
            }
        }
        private static void ComputeData(
            uint networkInputSize,
            string inputDataDirectory,
            ref double[][] outputData,
            ref double[][] outputAnswers,
            double outputValue,
            int index)
        {
            DirectoryInfo inputDirectory = new DirectoryInfo(inputDataDirectory);

            foreach (var file in inputDirectory.GetFiles("*.csv"))
            {
                List <ICSVFilter> baseFilters = new List <ICSVFilter>(1);
                ICSVFilter        quaternions = new CSVEvenColumnFilter();
                baseFilters.Add(quaternions);

                // Setup loader.
                CSVLoaderSettings settings = new CSVLoaderSettings
                {
                    filePath = file.FullName,
                    trimUp   = 1,
                    trimDown = 0,
                    filters  = baseFilters
                };

                var data = CSVLoader <Vector3> .LoadData(ref settings);

                // Initialize CountBased Transformer settings.
                IntervalBasedTransformerSettings countSettings = new IntervalBasedTransformerSettings
                {
                    sampleList = data,
                    count      = networkInputSize
                };
                CountBasedTransformer countTransformer = new CountBasedTransformer();

                BaseTrainHelper.Project1DInto2D(
                    countTransformer.GetNeuralInput(countSettings),
                    ref outputData,
                    index);

                // Set answer to given value.
                outputAnswers[index] = new double[] { outputValue };
                index++;
            }
        }
Пример #6
0
        public static Sample <T>[] LoadData(ref CSVLoaderSettings settings)
        {
            if (!File.Exists(settings.filePath))
            {
                throw new FileNotFoundException(settings.filePath);
            }

            // Create a new Table.
            var sampleList = new List <Sample <T> >();

            // If the file has a header then skip it.
            string[][] rows = File.ReadAllLines(settings.filePath)
                              .Select(line => line.Split(',')).ToArray();
            if (settings.CSVHasHeader)
            {
                rows = rows.Skip(1).ToArray();
            }

            // Trim top or bottom rows if needed
            if (settings.trimUp > 0 || settings.trimDown > 0)
            {
                rows = rows.Skip(settings.trimUp)
                       .Take(rows.Count() - settings.trimUp - settings.trimDown).ToArray();
            }

            if (rows.Count() == 0)
            {
                return(sampleList.ToArray());
            }

            int columnCount = ColumnCount(ref settings, ref rows[0]);

            // For each row a sample is created.
            for (uint rowIndex = 0; rowIndex < rows.Count(); rowIndex++)
            {
                // Check for unspecified end of file.
                if (string.IsNullOrEmpty(rows[rowIndex][0]))
                {
                    continue;
                }

                // Create new Sample.
                Sample <T> sample = new Sample <T>();

                // Parse required timestamp.
                sample.timestamp = float.Parse(rows[rowIndex][0]);
                sample.values    = new T[columnCount];
                uint valuesIndex = 0;

                for (uint i = 0; i < rows[rowIndex].Count(); i++)
                {
                    // Check if a filter is blocking the column.
                    if (settings.filters.Exists(o => !o.Use(ref rows[rowIndex], i)))
                    {
                        continue;
                    }

                    T vector = new T();
                    vector.parse(rows[rowIndex][i]);
                    sample.values[valuesIndex] = vector;
                    valuesIndex += 1;
                }
                sampleList.Add(sample);
            }
            return(sampleList.ToArray());
        }