public override LearningImage Project(LearningImage image)
        {
            LearningImage o = new LearningImage(image);
            double[] maxs = new double[image.Plane];
            for (int h = 0; h < o.Height; h++)
            {
                for (int w = 0; w < o.Width; w++)
                {
                    for (int p = 0; p < o.Plane; p++)
                    {
                        double v = o.Data[(o.Width * h + w) * o.Plane + p];
                        if (maxs[p] < Math.Abs(v)) maxs[p] = Math.Abs(v);
                    }
                }
            }

            for (int h = 0; h < o.Height; h++)
            {
                for (int w = 0; w < o.Width; w++)
                {
                    for (int p = 0; p < o.Plane; p++)
                    {
                        if (maxs[p] == 0) continue;
                        o.Data[(o.Width * h + w) * o.Plane + p] /= maxs[p];
                    }
                }
            }
            return o;
        }
예제 #2
0
 public override LearningImage BackProject(LearningImage i)
 {
     List<double> list = new List<double>(i.Data);
     LearningImage image = new LearningImage(Height, Width, Plane);
     LearningImage tmp = new LearningImage(Height, Width, Plane);
     for (int m = 0; m < MainMax; m++)
     {
     //				double length = LearningImage.EuclideanLength(_MainImages[m]);	本来はlist[m]*length
         LearningImage.Sacle(_MainImages[m], tmp, list[m]);
         LearningImage.Add(image, tmp, image);
     }
     return image;
 }
예제 #3
0
 public override LearningImage BackProject(LearningImage image)
 {
     int step = FrameIn.Height;
     int height = image.Height * step;
     int width = image.Width * step;
     LearningImage backed = new LearningImage(height, width, image.Plane);
     for (int h = 0; h < height; h++)
     {
         for (int w = 0; w < width; w++)
         {
             var p = image.GetPlane((double)h / (double)step, (double)w / (double)step);
             backed.SetPlane(h, w, p);
         }
     }
     return backed;
 }
예제 #4
0
 public override LearningImage Project(LearningImage image)
 {
     int step = FrameIn.Height;
     int height = image.Height / step;
     int width = image.Width / step;
     LearningImage projected = new LearningImage(height, width, image.Plane);
     for (int h = 0; h < image.Height; h += step)
     {
         for(int w = 0; w < image.Width; w += step)
         {
             var list = image.GetPlanes(new Rectangle(w, h, step, step));
             var best = list.OrderByDescending(x => x.SpecialEuclidean()).First();
             projected.SetPlane(h / step, w / step, best);
         }
     }
     return projected;
 }
 public override LearningImage Project(LearningImage i)
 {
     List<double> results = new List<double>();
     int scaledH = 0;
     int scaledW = 0;
     for (int h = -Height / 2; h < i.Height - Height / 2; h += this.Scale)
     {
         scaledW = 0;
         for (int w = -Width / 2; w < i.Width - Width / 2; w += this.Scale)
         {
             var trimed = i.Trim(new Rectangle(w, h, Width, Height));
             var projected = base.Project(trimed);
             results.AddRange(projected.Data);
             scaledW++;
         }
         scaledH++;
     }
     return new LearningImage(scaledH, scaledW, this.FrameOut.Plane, results.ToArray());
 }
        public override LearningImage BackProject(LearningImage i)
        {
            int Scale = this.Scale;
            ListImage li = new ListImage(i.Height * Scale + Height, i.Width * Scale + Width);
            for (int h = 0; h < i.Height; h++)
            {
                for (int w = 0; w < i.Width; w++)
                {
                    LearningImage trimed = i.Trim(new Rectangle(w, h, 1, 1));
                    LearningImage pasting = base.BackProject(trimed);
                    for (int hh = 0; hh < pasting.Height; hh++)
                        for (int ww = 0; ww < pasting.Width; ww++)
                            li.Add(h * Scale + hh, w * Scale + ww, pasting.GetPlane(hh, ww));
                }
            }

            LearningImage o = new LearningImage(i.Height * Scale, i.Width * Scale, this.FrameIn.Plane);
            for (int h = 0; h < o.Height; h++)
                for (int w = 0; w < o.Width; w++)
                    o.SetPlane(h, w, li.Median(h + Height / 2, w + Width / 2));
            return o;
        }
예제 #7
0
 public void Paste(int x, int y, LearningImage image)
 {
     for (int h = 0; h < image.Height; h++)
     {
         for (int w = 0; w < image.Width; w++)
         {
             if (h + y >= this.Height || w + x >= this.Width) continue;
             int pt = (this.Width * (h + y) + (w + x)) * Plane;
             int pi = (image.Width * h + w) * Plane;
             for (int l = 0; l < Plane; l++)
                 this.Data[pt + l] = image.Data[pi + l];	// デフォルト
         }
     }
 }
예제 #8
0
 public static double DotProduct(LearningImage a, LearningImage b)
 {
     return Matrix.InnerProduct(a.Data, b.Data);
 }
예제 #9
0
 public static double EuclideanLength(LearningImage a)
 {
     return Norm.Euclidean(a.Data);
 }
예제 #10
0
 public override void Initialize()
 {
     _FrameNow = 0;
     _MainImages = new LearningImage[MainMax];
     _TmpImages = new LearningImage[MainMax];
     for (int m = 0; m < MainMax; m++)
     {
         _MainImages[m] = new LearningImage(Height, Width, Plane);
         _TmpImages[m] = new LearningImage(Height, Width, Plane);
     }
 }
예제 #11
0
 public static void Add(LearningImage a, LearningImage b, LearningImage o)
 {
     for (int l = 0; l < o.Length; l++) o.Data[l] = a.Data[l] + b.Data[l];
 }
예제 #12
0
 public static List<double> HighLow(LearningImage a)
 {
     return new List<double>() { a.Data.Max(), a.Data.Min() };
 }
예제 #13
0
        private LearningImage ForecastUnit(int index, LearningImage i)
        {
            if(index >= _Units.Count) return i;

            var c = _Units[index].Project(i);
            //c.SavePngAdjusted("../c" + index + ".png");
            var o = ForecastUnit(index + 1, c);
            //var e = _Units[index].BackProject(o);
            //e.SavePngAdjusted("../e" + index + ".png");
            return o;
        }
예제 #14
0
 public static double ParticularEuclideanLength(LearningImage a)
 {
     double amount = 0;
     for (int i = 1; i < a.Data.Length; i++) amount += a.Data[i] * a.Data[i];
     return Math.Sqrt(amount);
 }
예제 #15
0
        public static LearningImage LoadBin(string path)
        {
            BinaryReader reader = null;
            try
            {
                if (!File.Exists(path)) return null;

                byte[] bytes = File.ReadAllBytes(path);
                int shift = 0;
                int height = BitConverter.ToInt32(bytes, shift); shift += sizeof(int);
                int width = BitConverter.ToInt32(bytes, shift); shift += sizeof(int);
                int plane = BitConverter.ToInt32(bytes, shift); shift += sizeof(int);
                LearningImage image = new LearningImage(height, width, plane);
                for (int l = 0; l < image.Length; l++) image.Data[l] = BitConverter.ToDouble(bytes, l * sizeof(double) + shift);
                return image;
            }
            catch(Exception ex)
            {
                Log.Instance.Error(ex.Message + "@" + ex.StackTrace);
                return null;
            }
            finally
            {
                if (reader != null) reader.Close();
                reader = null;
            }
        }
예제 #16
0
        // 正規化
        public LearningImage Normalize()
        {
            double amount_x = 0;
            double amount_xx = 0;
            for(int j = 0; j < this.Length; j++)
            {
                double x = this.Data[j];
                amount_x += x;
                amount_xx += x * x;
            }
            double average = amount_x / this.Length;
            double deviation = Math.Sqrt(amount_xx / this.Length - average * average);
            if (deviation == 0) deviation = 1.0;

            LearningImage i = new LearningImage(this);
            for (int j = 0; j < Length; j++) i.Data[j] = (i.Data[j] - average) / deviation;
            return i;
        }
예제 #17
0
 // ドロップアウト
 public LearningImage DropOut(double rate)
 {
     LearningImage i = new LearningImage(this);
     Random random = new Random();
     for(int j = 0; j < i.Length; j++)
     {
         if (random.NextDouble() < rate) i.Data[j] = 0;
     }
     return i;
 }
예제 #18
0
 public void Blend(int x, int y, LearningImage image, double rate = 0.75)
 {
     for (int h = 0; h < image.Height; h++)
     {
         for (int w = 0; w < image.Width; w++)
         {
             if (h + y >= this.Height || w + x >= this.Width) continue;
             int pt = (this.Width * (h + y) + (w + x)) * Plane;
             int pi = (image.Width * h + w) * Plane;
             for (int l = 0; l < Plane; l++) this.Data[pt + l] = this.Data[pt + l] * (1 - rate) + image.Data[pi + l] * rate;
         }
     }
 }
예제 #19
0
 // ノイズ追加、
 public LearningImage AddNoise(double range)
 {
     LearningImage i = new LearningImage(this);
     Random random = new Random();
     for (int j = 0; j < i.Length; j++)
     {
         i.Data[j] += (random.NextDouble() - 0.5) * range;
     }
     return i;
 }
예제 #20
0
 public static void Sacle(LearningImage i, LearningImage o, double scale = 1, double bias = 0)
 {
     for (int l = 0; l < o.Length; l++) o.Data[l] = i.Data[l] * scale + bias;
 }
예제 #21
0
 public virtual LearningImage Forecast(LearningImage image)
 {
     return image;
 }
예제 #22
0
 public virtual LearningImage Project(LearningImage image)
 {
     return image;
 }
예제 #23
0
 public LearningImage(LearningImage image)
     : this(image._Frame, image.Data)
 {
 }
예제 #24
0
 public static unsafe LearningImage FromBitmap(Bitmap src, ColorType color = ColorType.Color)
 {
     try
     {
         PixelFormat format = PixelFormat.Format24bppRgb;
         int plane = 3;
         if (color == ColorType.Gray)
         {
             format = PixelFormat.Format8bppIndexed;
             plane = 1;
         }
         BitmapData srcData = src.LockBits(new Rectangle(Point.Empty, src.Size), ImageLockMode.ReadOnly, format);
         LearningImage i = new LearningImage(src.Height, src.Width, plane);
         for (int h = 0; h < srcData.Height; h++)
         {
             byte* ps = (byte*)srcData.Scan0 + srcData.Stride * h;
             for (int w = 0; w < srcData.Width; w++, ps += i.Plane)
             {
                 int postion = (i.Width * h + w) * i.Plane;
                 for (int l = 0; l < i.Plane; l++)
                     i.Data[postion + l] = (double)ps[l] / 255;
             }
         }
         src.UnlockBits(srcData);
         return i;
     }
     catch(Exception ex)
     {
         Log.Instance.Error(ex.Message + "@" + ex.StackTrace);
         return null;
     }
 }
예제 #25
0
 public virtual void ParallelProject(List<LearningImage> inputs, out List<LearningImage> outputs)
 {
     LearningImage[] array = new LearningImage[inputs.Count];
     Parallel.For(0, inputs.Count, GetParallelOptions(), i => array[i] = Project(inputs[i]));
     outputs = new List<LearningImage>(array);
 }
예제 #26
0
        public LearningImage Trim(Rectangle r)
        {
            LearningImage i = new LearningImage(r.Height, r.Width, Plane);
            for(int h = r.Top; h < r.Bottom; h++)
            {
                for(int w = r.Left; w < r.Right; w++)
                {
                    int sh = h;
                    int sw = w;
                    if (sh < 0) sh = 0;
                    if (sh >= this.Height) sh = this.Height - 1;
                    if (sw < 0) sw = 0;
                    if (sw >= this.Width) sw = this.Width - 1;

                    int d = i.Width * (h - r.Top) + (w - r.Left);
                    int s = sh * this.Width + sw;
                    Array.Copy(this.Data, s * Plane, i.Data, d * Plane, Plane);
                }
            }
            return i;
        }
예제 #27
0
 public override LearningImage Project(LearningImage image)
 {
     double[] data = _Network.Compute(image.Homogenize());
     return new LearningImage(FrameOut, data);
 }
예제 #28
0
 // 0から1へ値を調整
 public LearningImage ZeroToOne()
 {
     LearningImage i = new LearningImage(this);
     for (int j = 0; j < Length; j++) i.Data[j] = (i.Data[j] + 1.0) * 0.5;
     return i;
 }
예제 #29
0
        protected LearningImage MakeOutimageByDirectory(int height, int width, string path)
        {
            string dir = Path.GetDirectoryName(path);
            int index = dir.LastIndexOf('\\');
            string group = dir.Substring(index + 1);
            index = group.IndexOf('-');
            if (index == 0) return null;	// 頭に-がついているフォルダは無視
            if (index > 0) group = group.Substring(0, index);
            int number = 0;
            if (!int.TryParse(group, out number)) return null;

            LearningImage result = new LearningImage(height, width, 1);
            if (0 <= number && number < height * width) result.Data[number] = 1;
            return result;
        }
예제 #30
0
 public LearningImagePair(LearningImage i, LearningImage o)
 {
     In = i; Out = o;
 }