Esempio n. 1
0
 public JobCNNClassify(GRasterLayer featureRasterLayer, GRasterLayer labelRasterLayer, int epochs, int model, int width, int height, int channel)
 {
     _t = new Thread(() => {
         ImageClassifyEnv env = new ImageClassifyEnv(featureRasterLayer, labelRasterLayer);
         CNN cnn = new CNN(new int[] { channel, width, height }, env.ActionNum);
         //training
         Summary = "模型训练中";
         for (int i = 0; i < epochs; i++)
         {
             int batchSize       = cnn.BatchSize;
             var(states, labels) = env.RandomEval(batchSize);
             double[][] inputX   = new double[batchSize][];
             for (int j = 0; j < batchSize; j++)
             {
                 inputX[j] = states[j];
             }
             double loss = cnn.Train(inputX, labels);
             Process     = (double)i / epochs;
         }
         //classify
         Summary = "分类应用中";
         IRasterLayerCursorTool pRasterLayerCursorTool = new GRasterLayerCursorTool();
         pRasterLayerCursorTool.Visit(featureRasterLayer);
         //GDI graph
         Bitmap classificationBitmap = new Bitmap(featureRasterLayer.XSize, featureRasterLayer.YSize);
         Graphics g = Graphics.FromImage(classificationBitmap);
         //
         int seed        = 0;
         int totalPixels = featureRasterLayer.XSize * featureRasterLayer.YSize;
         //应用dqn对图像分类
         for (int i = 0; i < featureRasterLayer.XSize; i++)
         {
             for (int j = 0; j < featureRasterLayer.YSize; j++)
             {
                 //get normalized input raw value
                 double[] normal = pRasterLayerCursorTool.PickNormalValue(i, j);
                 //}{debug
                 double[] action = cnn.Predict(normal);
                 //convert action to raw byte value
                 int gray = env.RandomSeedKeys[NP.Argmax(action)];
                 //后台绘制,报告进度
                 Color c          = Color.FromArgb(gray, gray, gray);
                 Pen p            = new Pen(c);
                 SolidBrush brush = new SolidBrush(c);
                 g.FillRectangle(brush, new Rectangle(i, j, 1, 1));
                 //report progress
                 Process = (double)(seed++) / totalPixels;
             }
         }
         //保存结果至tmp
         string fullFileName = Directory.GetCurrentDirectory() + @"\tmp\" + DateTime.Now.ToFileTimeUtc() + ".png";
         classificationBitmap.Save(fullFileName);
         //complete
         Summary  = "CNN训练分类完成";
         Complete = true;
         OnTaskComplete?.Invoke(Name, fullFileName);
     });
 }
Esempio n. 2
0
        public void ClassificationByCNN()
        {
            //loss
            double _loss = 1.0;
            //training epochs
            int epochs = 100;
            //
            GRasterLayer featureLayer = new GRasterLayer(featureFullFilename);
            GRasterLayer labelLayer   = new GRasterLayer(trainFullFilename);
            //create environment for agent exploring
            IEnv env = new ImageClassifyEnv(featureLayer, labelLayer);
            //assume 18dim equals 3x6 (image)
            CNN cnn = new CNN(new int[] { 1, 3, 6 }, env.ActionNum);

            //training
            for (int i = 0; i < epochs; i++)
            {
                int batchSize = cnn.BatchSize;
                var(states, labels) = env.RandomEval(batchSize);
                double[][] inputX = new double[batchSize][];
                for (int j = 0; j < batchSize; j++)
                {
                    inputX[j] = states[j];
                }
                _loss = cnn.Train(inputX, labels);
            }
            //in general, loss is less than 5
            Assert.IsTrue(_loss < 5.0);
            //apply cnn to classify featureLayer
            IRasterLayerCursorTool pRasterLayerCursorTool = new GRasterLayerCursorTool();

            pRasterLayerCursorTool.Visit(featureLayer);
            //get normalized input raw value
            double[] normal        = pRasterLayerCursorTool.PickNormalValue(50, 50);
            double[] action        = cnn.Predict(normal);
            int      landCoverType = env.RandomSeedKeys[NP.Argmax(action)];

            //do something as you need. i.e. draw landCoverType to bitmap at position ( i , j )
            //the classification results are not stable because of the training epochs are too few.
            Assert.IsTrue(landCoverType >= 0);
        }