Пример #1
0
        //-----------------------------------------------------------------------------------------
        //  Simplest Classification (using MyCaffeControl and MyCaffeImageDatabase)
        //-----------------------------------------------------------------------------------------

        /// <summary>
        /// The SimplestClassification shows how to the MyCaffeControl (and internal MyCaffeImageDatabase) to train and test on the MNIST dataset.
        /// </summary>
        /// <param name="sender">Specifies the event sender.</param>
        /// <param name="e">Specifies the event args.</param>
        private void btnSimplestClassification_Click(object sender, EventArgs e)
        {
            DatasetFactory factory  = new DatasetFactory();
            Stopwatch      sw       = new Stopwatch();
            SettingsCaffe  settings = new SettingsCaffe();

            // Load all images into memory before training.
            settings.ImageDbLoadMethod = IMAGEDB_LOAD_METHOD.LOAD_ALL;
            // Use GPU ID = 0.
            settings.GpuIds = "0";

            string strSolver;
            string strModel;

            // Load the descriptors from their respective files (installed by MyCaffe Test Application install)
            load_descriptors("mnist", out strSolver, out strModel);

            // NOTE: model fixup not needed for we will use the DATA layer which pulls data from SQL or SQLEXPRESS via the MyCaffeImageDatabase.
            // Set the interval beyond the iterations to skip testing during solving.
            strSolver = fixup_solver(strSolver, 10000);

            // Load the MNIST dataset descriptor.
            DatasetDescriptor ds = factory.LoadDataset("MNIST");

            // Create a test project with the dataset and descriptors.
            ProjectEx project = new ProjectEx("Test");

            project.SetDataset(ds);
            project.ModelDescription  = strModel;
            project.SolverDescription = strSolver;
            project.WeightsState      = null;

            // Create the MyCaffeControl
            MyCaffeControl <float> mycaffe = new MyCaffeControl <float>(settings, m_log, m_evtCancel);

            // Load the project, using the TRAIN phase.
            mycaffe.Load(Phase.TRAIN, project);

            // Trian the model for 5000 interations (which uses the internal solver and internal training net)
            int nIterations = 5000;

            mycaffe.Train(nIterations);

            // Test the model for 100 iterations (which uses the internal solver and internal testing net)
            nIterations = 100;
            double dfAccuracy = mycaffe.Test(nIterations);

            // Report the testing accuracy.
            m_log.WriteLine("Accuracy = " + dfAccuracy.ToString("P"));

            MessageBox.Show("Average Accuracy = " + dfAccuracy.ToString("P"), "Traing/Test on MNIST Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void TestIndexQuery()
        {
            PreTest.Init();
            Log log = new Log("Test Dataset Factory");

            log.EnableTrace = true;

            string         strDs   = "MNIST";
            DatasetFactory factory = new DatasetFactory();
            Stopwatch      sw      = new Stopwatch();

            try
            {
                DatasetDescriptor ds = factory.LoadDataset(strDs);
                factory.Open(ds.TrainingSource.ID);

                sw.Start();
                List <DbItem> rgItems = factory.LoadImageIndexes(false);
                sw.Stop();

                log.CHECK_EQ(rgItems.Count, ds.TrainingSource.ImageCount, "The query count should match the image count!");
                factory.Close();

                log.WriteLine("Query time = " + sw.Elapsed.TotalMilliseconds.ToString("N5") + " ms.");

                sw.Restart();

                int nMin = int.MaxValue;
                int nMax = -int.MaxValue;
                for (int i = 0; i < rgItems.Count; i++)
                {
                    nMin = Math.Min(rgItems[i].Label, nMin);
                    nMax = Math.Max(rgItems[i].Label, nMax);
                }

                List <DbItem> rgBoosted = rgItems.Where(p => p.Boost > 0).ToList();

                for (int nLabel = nMin; nLabel <= nMax; nLabel++)
                {
                    List <DbItem> rgLabel = rgItems.Where(p => p.Label == nLabel).ToList();
                }

                sw.Stop();

                log.WriteLine("Query time (profile) = " + sw.Elapsed.TotalMilliseconds.ToString("N5") + " ms.");
            }
            finally
            {
                factory.Dispose();
            }
        }
        private ProjectEx getProject()
        {
            ProjectEx p = new ProjectEx("AlexNet Project");

            DatasetFactory    factory = new DatasetFactory();
            DatasetDescriptor ds      = factory.LoadDataset("CIFAR-10");

            p.SetDataset(ds);

            string strModelFile  = getTestPath("\\MyCaffe\\test_data\\models\\alexnet\\cifar\\alexnet_cifar_train_val.prototxt");
            string strSolverFile = getTestPath("\\MyCaffe\\test_data\\models\\alexnet\\cifar\\alexnet_cifar_solver.prototxt");

            p.LoadModelFile(strModelFile);
            RawProto proto = RawProtoFile.LoadFromFile(strSolverFile);

            RawProto iter = proto.FindChild("max_iter");

            iter.Value = m_nMaxIteration.ToString();

            p.SolverDescription = proto.ToString();

            return(p);
        }
Пример #4
0
        static void Main(string[] args)
        {
            if (!sqlCheck())
            {
                return;
            }

            Log log = new Log("test");

            log.OnWriteLine += Log_OnWriteLine;
            CancelEvent   cancel   = new CancelEvent();
            SettingsCaffe settings = new SettingsCaffe();

            // Load all images into memory before training.
            settings.ImageDbLoadMethod = IMAGEDB_LOAD_METHOD.LOAD_ALL;
            // Use GPU ID = 0
            settings.GpuIds = "0";

            // Load the descriptors from their respective files
            string strSolver = load_file("C:\\ProgramData\\MyCaffe\\test_data\\models\\siamese\\mnist\\solver.prototxt");
            string strModel  = load_file("C:\\ProgramData\\MyCaffe\\test_data\\models\\siamese\\mnist\\train_val.prototxt");

            RawProto       proto     = RawProto.Parse(strModel);
            NetParameter   net_param = NetParameter.FromProto(proto);
            LayerParameter layer     = net_param.FindLayer(LayerParameter.LayerType.DECODE);

            layer.decode_param.target = DecodeParameter.TARGET.CENTROID;
            proto    = net_param.ToProto("root");
            strModel = proto.ToString();

            // Load the MNIST data descriptor.
            DatasetFactory    factory = new DatasetFactory();
            DatasetDescriptor ds      = factory.LoadDataset("MNIST");

            // Create a test project with the dataset and descriptors
            ProjectEx project = new ProjectEx("Test");

            project.SetDataset(ds);
            project.ModelDescription  = strModel;
            project.SolverDescription = strSolver;

            // Crate the MyCaffeControl (with the 'float' base type)
            string strCudaPath             = "C:\\Program Files\\SignalPop\\MyCaffe\\cuda_11.3\\CudaDnnDll.11.3.dll";
            MyCaffeControl <float> mycaffe = new MyCaffeControl <float>(settings, log, cancel, null, null, null, null, strCudaPath);

            // Load the project, using the TRAIN phase.
            mycaffe.Load(Phase.TRAIN, project);

            // Train the model for 4000 iterations
            // (which uses the internal solver and internal training net)
            int nIterations = 4000;

            mycaffe.Train(nIterations);

            // Test the model for 100 iterations
            // (which uses the internal testing net)
            nIterations = 100;
            double dfAccuracy = mycaffe.Test(nIterations);

            // Report the testing accuracy.
            log.WriteLine("Accuracy = " + dfAccuracy.ToString("P"));

            mycaffe.Dispose();

            Console.Write("Press any key...");
            Console.ReadKey();
        }