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;
        }
 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;
 }
 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;
 }
 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;
        }
 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];	// デフォルト
         }
     }
 }
 public static double DotProduct(LearningImage a, LearningImage b)
 {
     return Matrix.InnerProduct(a.Data, b.Data);
 }
 public static double EuclideanLength(LearningImage a)
 {
     return Norm.Euclidean(a.Data);
 }
 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);
     }
 }
 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];
 }
 public static List<double> HighLow(LearningImage a)
 {
     return new List<double>() { a.Data.Max(), a.Data.Min() };
 }
        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;
        }
 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);
 }
        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;
            }
        }
        // 正規化
        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;
        }
 // ドロップアウト
 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;
 }
 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;
         }
     }
 }
 // ノイズ追加、
 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;
 }
 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;
 }
 public virtual LearningImage Forecast(LearningImage image)
 {
     return image;
 }
 public virtual LearningImage Project(LearningImage image)
 {
     return image;
 }
 public LearningImage(LearningImage image)
     : this(image._Frame, image.Data)
 {
 }
 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;
     }
 }
 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);
 }
        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;
        }
Example #27
0
 public override LearningImage Project(LearningImage image)
 {
     double[] data = _Network.Compute(image.Homogenize());
     return new LearningImage(FrameOut, data);
 }
 // 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;
 }
        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;
        }
 public LearningImagePair(LearningImage i, LearningImage o)
 {
     In = i; Out = o;
 }