public IEnumerable <ULeData> CreateTeacherImg_fromFile(int ctrlWDMax = 4)
        {
            int Nrow = 10;

            dSize = (DigitImg.Height + 2) / (DigitImg.Width / 10);
            for (int nx = 0; ; nx++)
            {
                int ky = nx / Nrow, kx = nx % Nrow;
                if (ky >= (dSize - 1))
                {
                    break;
                }
                //  if(kx==0)  continue;

                int ys = ky * 33; Range rgY = new Range(ys, ys + 32);
                int xs = kx * 33; Range rgX = new Range(xs, xs + 32);
                Mat mg09 = new Mat();
                DigitImg.SubMat(rgY, rgX).CopyTo(mg09);
                for (int ctrlWD = 0; ctrlWD < ctrlWDMax; ctrlWD++)
                {
                    Mat mg09Nml = _PTNormalize(mg09, ctrlWD, 200);    //## Pattern normalization
                    var Q       = new ULeData(mg09Nml, kx);
                    pUDLstA.Add(Q);
                    yield return(Q);
                }
            }
            yield break;
        }
        private ULeData _AddRandomPoint2(ULeData P, double rX, bool DispB = false)
        {
            ULeData Q = new ULeData(P.pUData, P.ans);

            Q.pUData = P.pUData.Clone();
            Mat        Pimg     = Q.pUData;
            List <int> blackLst = new List <int>();
            int        KK       = (int)(szP.Height * szP.Width * rX);
            byte       b255     = (byte)255;

            if (P.ans > 0)
            {
                unsafe {
                    byte *S = Pimg.DataPointer;
                    for (int k = 0; k < KK; k++)
                    {
                        int r = rdm.Next(1023);
                        S[r] = (byte)(b255 - S[r]);
                    }
                }
                if (DispB)
                {
                    new Window("AddRandomPoint2 Source", WindowMode.KeepRatio, P.pUData);
                    new Window("AddRandomPoint2 Result", WindowMode.KeepRatio, Pimg);
                    Cv2.WaitKey(0);
                }
            }

            return(Q);
        }
Esempio n. 3
0
        private void RecursiveCalcu_Propagation(MLUnit3 P, ULeData X, bool DoB, bool DispB)
        {
            //===== Forward calculation =====
//$$            ApplyDropout_____(P,DoB:P.DropOutEnable);
            P.Z_lst[0] = 1.0;
            var Q = P.nxtMLU;

            Q.U_lst = P.W_lst * P.Z_lst;
            Q.U_lst.Apply(Q.Z_lst, x => P.ActFunc(x)); //Activation Function

            //===== Backward calculation =====
            if (Q.nxtMLU != null)                             // Intermediate layer
            {
                RecursiveCalcu_Propagation(Q, X, DoB, DispB); // [Next layer]
            }
            else                                              // Final layer
            //DenseVector E = new DenseVector(Nout,0.0); E.Clear();
            //DenseVector E = DenseVector.Create(Nout,0.0); E[X.ans]=1.0;
            {
                DenseVector E = DenseVector.Create(Nout, p => (p == X.ans)?1.0:0.0);
                Q.D_lst = E - Q.Z_lst;
            }

            DenseVector SU = sigmoidFDash(P.U_lst);
            DenseMatrix Wt = (DenseMatrix)P.W_lst.Transpose();
            DenseVector WD = Wt * Q.D_lst;

            P.D_lst = eProduct(SU, WD);  //Hadamard product //②
//$$                ApplyDropout_____(P,DoB:true);
            P.dW_lst = vvProduct(Q.D_lst, P.Z_lst);
            return;
        }
 public IEnumerable <ULeData> CreateTeacherImg_Blank(int nn = 20) // Blank image
 {
     for (int k = 0; k < 20; k++)
     {
         var     mg0 = new Mat(new Size(32, 32), MatType.CV_8UC1, Scalar.White);
         ULeData Q   = new ULeData(mg0, 10); //## Blank image
         pUDLstA.Add(Q);
         yield return(Q);
     }
     yield break;
 }
Esempio n. 5
0
        private int _2B_DigitRecognition(Mat MMcel, int rc0, bool resDispB)
        {
            //Borderline erase
            _ClearFrame(MMcel, sf: 4, DispB: false); //sf:Width of frame to remove
            _IsAlmostEmpty(MMcel, rc0, cnt0: 10);

            Mat MMnml = MLn3._PTNormalize(MMcel, 0, 220);    //Pattern normalization

            ULeData X = new ULeData(MMnml, -1);

            X.CalcFeature();
            //        MLn3.MLXA3.CalculateByDropOut(X,DoB:false,DispB:false); //判別

            bool DoB = (gammaC < 1.0);

            MLn3.MLXA3.CalculateByDropOut(X, DoB: false, DispB: false); //判別

            return(X.est);
        }
        private ULeData _AddRandomPoint(ULeData P, double r0, double r255, bool DispB = false)
        {
            ULeData Q = new ULeData(P.pUData, P.ans);

            Q.pUData = P.pUData.Clone();
            Mat        Qimg     = Q.pUData;
            List <int> blackLst = new List <int>();
            //if(P.ans>0){
            int KK = (int)(szP.Height * szP.Width * r255);

            unsafe {
                byte *S = Qimg.DataPointer;
                for (int k = 0; k < KK; k++)
                {
                    S[rdm.Next(1023)] = 0;
                }
                byte *B = S;
                for (int k = 0; k < 1024; k++)
                {
                    if (*B++ < 128)
                    {
                        blackLst.Add(k);
                    }
                }
                int cnt = blackLst.Count;
                KK = (int)(cnt * r0);
                for (int k = 0; k < KK; k++)
                {
                    S[blackLst[rdm.Next(cnt)]] = (byte)255;
                }
            }
            if (DispB)
            {
                using (new Window("_AddRandomPoint P", WindowMode.KeepRatio, P.pUData))
                    using (new Window("_AddRandomPoint Q", WindowMode.KeepRatio, Qimg)){ Cv2.WaitKey(0); }
            }
            //}

            return(Q);
        }
        public IEnumerable <ULeData> CreateTeacherImg_addNoise(double AdditionRandomNoise = 0.0, bool autoDNB = false)
        {
            int nSz = pUDLstA.Count();

            for (int n = 0; n < nSz; n++)
            {
                ULeData P = pUDLstA[n];
                //if(P.ans==0) continue;
                if (AdditionRandomNoise == 0.0)
                {
                    var Q = _AddRandomPoint(P, 0.05, 0.05, DispB: false);
                    pUDLstA.Add(Q);
                    yield return(Q);
                }
                else if (!autoDNB)//denoising
                {
                    var Q = _AddRandomPoint2(P, AdditionRandomNoise);
                    pUDLstA.Add(Q);
                    yield return(Q);
                }
            }
            yield break;
        }
        public IEnumerable <ULeData> CreateTeacherImg_wideFont(double alp1, double alp2)
        {
            int nSz = pUDLstA.Count();

            for (int n = 0; n < nSz; n++)
            {
                ULeData UM = pUDLstA[n];
                //if(UM.ans==0) continue;

                for (int k = 0; k < 2; k++)
                {
                    double alp      = (k == 0)? alp1: alp2;
                    Mat    UMEnL    = UM.pUData.Enlarge(alp, alp);
                    Mat    UMEnLNml = _PTNormalize(UMEnL, 0, 200); //◆パターン正規化
                    //using( new Window("幅広字体生成 UMEnL",WindowMode.KeepRatio,UMEnL) )
                    //using( new Window("幅広字体生成 UMEnLNml",WindowMode.KeepRatio,UMEnLNml) ){ Cv2.WaitKey(0); }
                    var Q = new ULeData(UMEnLNml, UM.ans);
                    pUDLstA.Add(Q);
                    yield return(Q);
                }
            }
            yield break;
        }
        public IEnumerable <ULeData> CreateTeacherImg_fromAddFile( )
        {
            if (!Directory.Exists(AddedImage))
            {
                yield break;
            }
            foreach (string fx in Directory.GetFiles(AddedImage))
            {
                string st = Path.GetFileName(fx).Substring(0, 1);
                if (!st.IsNumeric())
                {
                    continue;
                }
                Mat mg = new Mat(fx, ImreadModes.GrayScale);
                //using( new Window("AddedImage",WindowMode.KeepRatio,mg) ){ Cv2.WaitKey(0); }
                int kx = int.Parse(st);
                var Q  = new ULeData(mg, kx);

                pUDLstA.Add(Q);
                yield return(Q);
            }
            yield break;
        }