Пример #1
0
        private static Data load_data_super(string[] paths, int n, int m, int w, int h, int scale)
        {
            if (m != 0)
            {
                paths = get_random_paths(paths, n, m);
            }
            Data d = new Data();

            d.Shallow = 0;

            int i;

            d.X.Rows = n;
            d.X.Vals = new float[n][];
            d.X.Cols = w * h * 3;

            d.Y.Rows = n;
            d.Y.Vals = new float[n][];
            d.Y.Cols = w * scale * h * scale * 3;

            for (i = 0; i < n; ++i)
            {
                Image im   = LoadArgs.load_image_color(paths[i], 0, 0);
                Image crop = LoadArgs.random_crop_image(im, w * scale, h * scale);
                int   flip = Utils.Rand.Next() % 2;
                if (flip != 0)
                {
                    LoadArgs.flip_image(crop);
                }
                Image resize = LoadArgs.resize_image(crop, w, h);
                d.X.Vals[i] = resize.Data;
                d.Y.Vals[i] = crop.Data;
            }
            return(d);
        }
Пример #2
0
        public static Thread load_data_in_thread(LoadArgs args)
        {
            Thread thread = new Thread(() => load_thread(args));

            thread.Start();
            return(thread);
        }
Пример #3
0
        private static void load_threads(LoadArgs ptr)
        {
            int i;

            if (ptr.Threads == 0)
            {
                ptr.Threads = 1;
            }
            Data output = ptr.D;
            int  total  = ptr.N;

            Data[]   buffers = new Data[ptr.Threads];
            Thread[] threads = new Thread[ptr.Threads];
            for (i = 0; i < ptr.Threads; ++i)
            {
                ptr.D      = buffers[i];
                ptr.N      = (i + 1) * total / ptr.Threads - i * total / ptr.Threads;
                threads[i] = load_data_in_thread(ptr);
            }
            for (i = 0; i < ptr.Threads; ++i)
            {
                threads[i].Join();
            }
            output         = concat_datas(buffers, ptr.Threads);
            output.Shallow = 0;
            for (i = 0; i < ptr.Threads; ++i)
            {
                buffers[i].Shallow = 1;
            }
        }
Пример #4
0
        private static void load_thread(LoadArgs ptr)
        {
            if (ptr.Exposure == 0)
            {
                ptr.Exposure = 1;
            }
            if (ptr.Saturation == 0)
            {
                ptr.Saturation = 1;
            }
            if (ptr.Aspect == 0)
            {
                ptr.Aspect = 1;
            }

            if (ptr.Type == DataType.OldClassificationData)
            {
                ptr.D = load_data_old(ptr.Paths, ptr.N, ptr.M, ptr.Labels, ptr.Classes, ptr.W, ptr.H);
            }
            else if (ptr.Type == DataType.ClassificationData)
            {
                ptr.D = load_data_augment(ptr.Paths, ptr.N, ptr.M, ptr.Labels, ptr.Classes, ptr.Hierarchy, ptr.Min, ptr.Max, ptr.Size, ptr.Angle, ptr.Aspect, ptr.Hue, ptr.Saturation, ptr.Exposure);
            }
            else if (ptr.Type == DataType.SuperData)
            {
                ptr.D = load_data_super(ptr.Paths, ptr.N, ptr.M, ptr.W, ptr.H, ptr.Scale);
            }
            else if (ptr.Type == DataType.WritingData)
            {
                ptr.D = load_data_writing(ptr.Paths, ptr.N, ptr.M, ptr.W, ptr.H, ptr.OutW, ptr.OutH);
            }
            else if (ptr.Type == DataType.RegionData)
            {
                ptr.D = load_data_region(ptr.N, ptr.Paths, ptr.M, ptr.W, ptr.H, ptr.NumBoxes, ptr.Classes, ptr.Jitter, ptr.Hue, ptr.Saturation, ptr.Exposure);
            }
            else if (ptr.Type == DataType.DetectionData)
            {
                ptr.D = load_data_detection(ptr.N, ptr.Paths, ptr.M, ptr.W, ptr.H, ptr.NumBoxes, ptr.Classes, ptr.Jitter, ptr.Hue, ptr.Saturation, ptr.Exposure);
            }
            else if (ptr.Type == DataType.SwagData)
            {
                ptr.D = load_data_swag(ptr.Paths, ptr.N, ptr.Classes, ptr.Jitter);
            }
            else if (ptr.Type == DataType.CompareData)
            {
                ptr.D = load_data_compare(ptr.N, ptr.Paths, ptr.M, ptr.Classes, ptr.W, ptr.H);
            }
            else if (ptr.Type == DataType.ImageData)
            {
                ptr.Im      = LoadArgs.load_image_color(ptr.Path, 0, 0);
                ptr.Resized = LoadArgs.resize_image((ptr.Im), ptr.W, ptr.H);
            }
            else if (ptr.Type == DataType.TagData)
            {
                ptr.D = load_data_tag(ptr.Paths, ptr.N, ptr.M, ptr.Classes, ptr.Min, ptr.Max, ptr.Size, ptr.Angle, ptr.Aspect, ptr.Hue, ptr.Saturation, ptr.Exposure);
            }
        }
Пример #5
0
        private static Data load_data_swag(string[] paths, int n, int classes, float jitter)
        {
            int    index      = Utils.Rand.Next() % n;
            string randomPath = paths[index];

            Image orig = LoadArgs.load_image_color(randomPath, 0, 0);
            int   h    = orig.H;
            int   w    = orig.W;

            Data d = new Data();

            d.Shallow = 0;
            d.W       = w;
            d.H       = h;

            d.X.Rows = 1;
            d.X.Vals = new float[d.X.Rows][];
            d.X.Cols = h * w * 3;

            int k = (4 + classes) * 30;

            d.Y = new Matrix(1, k);

            int dw = (int)(w * jitter);
            int dh = (int)(h * jitter);

            int pleft  = (int)Utils.rand_uniform(-dw, dw);
            int pright = (int)Utils.rand_uniform(-dw, dw);
            int ptop   = (int)Utils.rand_uniform(-dh, dh);
            int pbot   = (int)Utils.rand_uniform(-dh, dh);

            int swidth  = w - pleft - pright;
            int sheight = h - ptop - pbot;

            float sx = (float)swidth / w;
            float sy = (float)sheight / h;

            int   flip    = Utils.Rand.Next() % 2;
            Image cropped = LoadArgs.crop_image(orig, pleft, ptop, swidth, sheight);

            float dx = ((float)pleft / w) / sx;
            float dy = ((float)ptop / h) / sy;

            Image sized = LoadArgs.resize_image(cropped, w, h);

            if (flip != 0)
            {
                LoadArgs.flip_image(sized);
            }
            d.X.Vals[0] = sized.Data;

            fill_truth_swag(randomPath, d.Y.Vals[0], classes, flip, dx, dy, 1.0f / sx, 1.0f / sy);

            return(d);
        }
Пример #6
0
        private static Data load_data_region(int n, string[] paths, int m, int w, int h, int size, int classes, float jitter, float hue, float saturation, float exposure)
        {
            string[] randomPaths = get_random_paths(paths, n, m);
            int      i;
            Data     d = new Data();

            d.Shallow = 0;

            d.X.Rows = n;
            d.X.Vals = new float[d.X.Rows][];
            d.X.Cols = h * w * 3;


            int k = size * size * (5 + classes);

            d.Y = new Matrix(n, k);
            for (i = 0; i < n; ++i)
            {
                Image orig = LoadArgs.load_image_color(randomPaths[i], 0, 0);

                int oh = orig.H;
                int ow = orig.W;

                int dw = (int)(ow * jitter);
                int dh = (int)(oh * jitter);

                int pleft  = (int)Utils.rand_uniform(-dw, dw);
                int pright = (int)Utils.rand_uniform(-dw, dw);
                int ptop   = (int)Utils.rand_uniform(-dh, dh);
                int pbot   = (int)Utils.rand_uniform(-dh, dh);

                int swidth  = ow - pleft - pright;
                int sheight = oh - ptop - pbot;

                float sx = (float)swidth / ow;
                float sy = (float)sheight / oh;

                int   flip    = Utils.Rand.Next() % 2;
                Image cropped = LoadArgs.crop_image(orig, pleft, ptop, swidth, sheight);

                float dx = ((float)pleft / ow) / sx;
                float dy = ((float)ptop / oh) / sy;

                Image sized = LoadArgs.resize_image(cropped, w, h);
                if (flip != 0)
                {
                    LoadArgs.flip_image(sized);
                }
                LoadArgs.random_distort_image(sized, hue, saturation, exposure);
                d.X.Vals[i] = sized.Data;

                fill_truth_region(randomPaths[i], d.Y.Vals[i], classes, size, flip, dx, dy, 1.0f / sx, 1.0f / sy);
            }
            return(d);
        }
Пример #7
0
        private static Matrix load_image_paths(string[] paths, int n, int w, int h)
        {
            int    i;
            Matrix x = new Matrix(n);

            for (i = 0; i < n; ++i)
            {
                Image im = LoadArgs.load_image_color(paths[i], w, h);
                x.Vals[i] = im.Data;
                x.Cols    = im.H * im.W * im.C;
            }
            return(x);
        }
Пример #8
0
        private static Matrix load_image_paths_gray(string[] paths, int n, int w, int h)
        {
            int    i;
            Matrix x = new Matrix(n);

            for (i = 0; i < n; ++i)
            {
                Image im = LoadArgs.load_image(paths[i], w, h, 3);

                Image gray = LoadArgs.grayscale_image(im);
                im = gray;

                x.Vals[i] = im.Data;
                x.Cols    = im.H * im.W * im.C;
            }
            return(x);
        }
Пример #9
0
        private static Matrix load_image_augment_paths(string[] paths, int n, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
        {
            int    i;
            Matrix x = new Matrix(n);

            for (i = 0; i < n; ++i)
            {
                Image im   = LoadArgs.load_image_color(paths[i], 0, 0);
                Image crop = LoadArgs.random_augment_image(im, angle, aspect, min, max, size);
                int   flip = Utils.Rand.Next() % 2;
                if (flip != 0)
                {
                    LoadArgs.flip_image(crop);
                }
                LoadArgs.random_distort_image(crop, hue, saturation, exposure);

                x.Vals[i] = crop.Data;
                x.Cols    = crop.H * crop.W * crop.C;
            }
            return(x);
        }
Пример #10
0
        public static Thread load_data(LoadArgs args)
        {
            Thread thread = new Thread(() => load_threads(args));

            return(thread);
        }
Пример #11
0
        private static Data load_data_compare(int n, string[] paths, int m, int classes, int w, int h)
        {
            if (m != 0)
            {
                paths = get_random_paths(paths, 2 * n, m);
            }
            int  i, j;
            Data d = new Data();

            d.Shallow = 0;

            d.X.Rows = n;
            d.X.Vals = new float[d.X.Rows][];
            d.X.Cols = h * w * 6;

            int k = 2 * (classes);

            d.Y = new Matrix(n, k);
            for (i = 0; i < n; ++i)
            {
                Image im1 = LoadArgs.load_image_color(paths[i * 2], w, h);
                Image im2 = LoadArgs.load_image_color(paths[i * 2 + 1], w, h);

                d.X.Vals[i] = new float[d.X.Cols];
                Array.Copy(im1.Data, d.X.Vals[i], im1.Data.Length);
                Array.Copy(im2.Data, 0, d.X.Vals[i], im1.Data.Length, im2.Data.Length);

                int   id;
                float iou;

                string imlabel1 = paths[i * 2].Replace("imgs", "labels");
                imlabel1 = imlabel1.Replace("jpg", "txt");

                var lines1 = File.ReadAllLines(imlabel1);
                foreach (var line in lines1)
                {
                    var parts = line.Split(' ');
                    if (parts.Length != 2)
                    {
                        continue;
                    }
                    id  = int.Parse(parts[0]);
                    iou = float.Parse(parts[1]);
                    if (d.Y.Vals[i][2 * id] < iou)
                    {
                        d.Y.Vals[i][2 * id] = iou;
                    }
                }

                string imlabel2 = paths[i * 2 + 1].Replace("imgs", "labels");
                imlabel2 = imlabel2.Replace("jpg", "txt");

                var lines2 = File.ReadAllLines(imlabel2);
                foreach (var line in lines2)
                {
                    var parts = line.Split(' ');
                    if (parts.Length != 2)
                    {
                        continue;
                    }
                    id  = int.Parse(parts[0]);
                    iou = float.Parse(parts[1]);

                    if (d.Y.Vals[i][2 * id + 1] < iou)
                    {
                        d.Y.Vals[i][2 * id + 1] = iou;
                    }
                }

                for (j = 0; j < classes; ++j)
                {
                    if (d.Y.Vals[i][2 * j] > .5 && d.Y.Vals[i][2 * j + 1] < .5)
                    {
                        d.Y.Vals[i][2 * j]     = 1;
                        d.Y.Vals[i][2 * j + 1] = 0;
                    }
                    else if (d.Y.Vals[i][2 * j] < .5 && d.Y.Vals[i][2 * j + 1] > .5)
                    {
                        d.Y.Vals[i][2 * j]     = 0;
                        d.Y.Vals[i][2 * j + 1] = 1;
                    }
                    else
                    {
                        d.Y.Vals[i][2 * j]     = Utils.SecretNum;
                        d.Y.Vals[i][2 * j + 1] = Utils.SecretNum;
                    }
                }
            }
            return(d);
        }