Пример #1
0
        public void TestEstimatorSaveLoad()
        {
            using (var env = new ConsoleEnvironment())
            {
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));

                var pipe = new ImageLoaderEstimator(env, imageFolder, ("ImagePath", "ImageReal"))
                           .Append(new ImageResizerEstimator(env, "ImageReal", "ImageReal", 100, 100))
                           .Append(new ImagePixelExtractorEstimator(env, "ImageReal", "ImagePixels"))
                           .Append(new ImageGrayscaleEstimator(env, ("ImageReal", "ImageGray")));

                pipe.GetOutputSchema(Core.Data.SchemaShape.Create(data.Schema));
                var model = pipe.Fit(data);

                using (var file = env.CreateTempFile())
                {
                    using (var fs = file.CreateWriteStream())
                        model.SaveTo(env, fs);
                    var model2 = TransformerChain.LoadFrom(env, file.OpenReadStream());

                    var newCols = ((ImageLoaderTransform)model2.First()).Columns;
                    var oldCols = ((ImageLoaderTransform)model.First()).Columns;
                    Assert.True(newCols
                                .Zip(oldCols, (x, y) => x == y)
                                .All(x => x));
                }
            }
            Done();
        }
Пример #2
0
        public void TestEstimatorChain()
        {
            using (var env = new ConsoleEnvironment())
            {
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var invalidData = env.CreateLoader("Text{col=ImagePath:R4:0}", new MultiFileSource(dataFile));

                var pipe = new ImageLoaderEstimator(env, imageFolder, ("ImagePath", "ImageReal"))
                           .Append(new ImageResizerEstimator(env, "ImageReal", "ImageReal", 100, 100))
                           .Append(new ImagePixelExtractorEstimator(env, "ImageReal", "ImagePixels"))
                           .Append(new ImageGrayscaleEstimator(env, ("ImageReal", "ImageGray")));

                TestEstimatorCore(pipe, data, null, invalidData);
            }
            Done();
        }
Пример #3
0
        public void TensorFlowTransformInceptionTest()
        {
            var model_location = @"C:\models\TensorFlow\tensorflow_inception_graph.pb";

            using (var env = new ConsoleEnvironment(seed: 1, conc: 1))
            {
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var images      = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Source = "ImageReal", Name = "ImageCropped", ImageHeight = 224, ImageWidth = 224, Resizing = ImageResizerTransform.ResizingKind.IsoCrop
                        }
                    }
                }, images);
                var pixels = ImagePixelExtractorTransform.Create(env, new ImagePixelExtractorTransform.Arguments()
                {
                    Column = new ImagePixelExtractorTransform.Column[1] {
                        new ImagePixelExtractorTransform.Column()
                        {
                            Source = "ImageCropped", Name = "input", UseAlpha = false, InterleaveArgb = true, Convert = true
                        }
                    }
                }, cropped);

                var tf = TensorFlowTransform.Create(env, pixels, model_location, "softmax2_pre_activation", "input");

                tf.Schema.TryGetColumnIndex("input", out int input);
                tf.Schema.TryGetColumnIndex("softmax2_pre_activation", out int b);
                using (var curs = tf.GetRowCursor(col => col == b || col == input))
                {
                    var get         = curs.GetGetter <VBuffer <float> >(b);
                    var getInput    = curs.GetGetter <VBuffer <float> >(input);
                    var buffer      = default(VBuffer <float>);
                    var inputBuffer = default(VBuffer <float>);
                    while (curs.MoveNext())
                    {
                        getInput(ref inputBuffer);
                        get(ref buffer);
                    }
                }
            }
        }
Пример #4
0
        public void TensorFlowTransformCifarInvalidShape()
        {
            var model_location = "cifar_model/frozen_model.pb";

            using (var env = new ConsoleEnvironment())
            {
                var imageHeight = 28;
                var imageWidth  = 28;
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));

                var images = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Source = "ImageReal", Name = "ImageCropped", ImageHeight = imageHeight, ImageWidth = imageWidth, Resizing = ImageResizerTransform.ResizingKind.IsoCrop
                        }
                    }
                }, images);

                var pixels = ImagePixelExtractorTransform.Create(env, new ImagePixelExtractorTransform.Arguments()
                {
                    Column = new ImagePixelExtractorTransform.Column[1] {
                        new ImagePixelExtractorTransform.Column()
                        {
                            Source = "ImageCropped", Name = "Input", UseAlpha = false, InterleaveArgb = true
                        }
                    }
                }, cropped);

                var thrown = false;
                try
                {
                    IDataView trans = TensorFlowTransform.Create(env, pixels, model_location, "Output", "Input");
                }
                catch
                {
                    thrown = true;
                }
                Assert.True(thrown);
            }
        }
Пример #5
0
        public void TestSaveImages()
        {
            using (var env = new ConsoleEnvironment())
            {
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var images      = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);

                IDataView cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Name = "ImageCropped", Source = "ImageReal", ImageHeight = 100, ImageWidth = 100, Resizing = ImageResizerTransform.ResizingKind.IsoPad
                        }
                    }
                }, images);

                cropped.Schema.TryGetColumnIndex("ImagePath", out int pathColumn);
                cropped.Schema.TryGetColumnIndex("ImageCropped", out int cropBitmapColumn);
                using (var cursor = cropped.GetRowCursor((x) => true))
                {
                    var    pathGetter       = cursor.GetGetter <DvText>(pathColumn);
                    DvText path             = default;
                    var    bitmapCropGetter = cursor.GetGetter <Bitmap>(cropBitmapColumn);
                    Bitmap bitmap           = default;
                    while (cursor.MoveNext())
                    {
                        pathGetter(ref path);
                        bitmapCropGetter(ref bitmap);
                        Assert.NotNull(bitmap);
                        var fileToSave = GetOutputPath(Path.GetFileNameWithoutExtension(path.ToString()) + ".cropped.jpg");
                        bitmap.Save(fileToSave, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
            Done();
        }
Пример #6
0
        public void TestBackAndForthConversionWithoutAlphaNoInterleaveNoOffset()
        {
            using (var env = new ConsoleEnvironment())
            {
                var imageHeight = 100;
                var imageWidth  = 130;
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var images      = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Source = "ImageReal", Name = "ImageCropped", ImageHeight = imageHeight, ImageWidth = imageWidth, Resizing = ImageResizerTransform.ResizingKind.IsoCrop
                        }
                    }
                }, images);

                var pixels = ImagePixelExtractorTransform.Create(env, new ImagePixelExtractorTransform.Arguments()
                {
                    InterleaveArgb = false,
                    Column         = new ImagePixelExtractorTransform.Column[1] {
                        new ImagePixelExtractorTransform.Column()
                        {
                            Source = "ImageCropped", Name = "ImagePixels", UseAlpha = false
                        }
                    }
                }, cropped);

                IDataView backToBitmaps = new VectorToImageTransform(env, new VectorToImageTransform.Arguments()
                {
                    InterleaveArgb = false,
                    Column         = new VectorToImageTransform.Column[1] {
                        new VectorToImageTransform.Column()
                        {
                            Source = "ImagePixels", Name = "ImageRestored", ImageHeight = imageHeight, ImageWidth = imageWidth, ContainsAlpha = false
                        }
                    }
                }, pixels);

                var fname = nameof(TestBackAndForthConversionWithoutAlphaNoInterleaveNoOffset) + "_model.zip";

                var fh = env.CreateOutputFile(fname);
                using (var ch = env.Start("save"))
                    TrainUtils.SaveModel(env, ch, fh, null, new RoleMappedData(backToBitmaps));

                backToBitmaps = ModelFileUtils.LoadPipeline(env, fh.OpenReadStream(), new MultiFileSource(dataFile));
                DeleteOutputPath(fname);


                backToBitmaps.Schema.TryGetColumnIndex("ImageRestored", out int bitmapColumn);
                backToBitmaps.Schema.TryGetColumnIndex("ImageCropped", out int cropBitmapColumn);
                using (var cursor = backToBitmaps.GetRowCursor((x) => true))
                {
                    var    bitmapGetter   = cursor.GetGetter <Bitmap>(bitmapColumn);
                    Bitmap restoredBitmap = default;

                    var    bitmapCropGetter = cursor.GetGetter <Bitmap>(cropBitmapColumn);
                    Bitmap croppedBitmap    = default;
                    while (cursor.MoveNext())
                    {
                        bitmapGetter(ref restoredBitmap);
                        Assert.NotNull(restoredBitmap);
                        bitmapCropGetter(ref croppedBitmap);
                        Assert.NotNull(croppedBitmap);
                        for (int x = 0; x < imageWidth; x++)
                        {
                            for (int y = 0; y < imageHeight; y++)
                            {
                                var c = croppedBitmap.GetPixel(x, y);
                                var r = restoredBitmap.GetPixel(x, y);
                                Assert.True(c.R == r.R && c.G == r.G && c.B == r.B);
                            }
                        }
                    }
                }
            }
            Done();
        }
Пример #7
0
        public void TestGreyscaleTransformImages()
        {
            using (var env = new ConsoleEnvironment())
            {
                var imageHeight = 150;
                var imageWidth  = 100;
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var images      = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Name = "ImageCropped", Source = "ImageReal", ImageHeight = imageHeight, ImageWidth = imageWidth, Resizing = ImageResizerTransform.ResizingKind.IsoCrop
                        }
                    }
                }, images);

                IDataView grey = ImageGrayscaleTransform.Create(env, new ImageGrayscaleTransform.Arguments()
                {
                    Column = new ImageGrayscaleTransform.Column[1] {
                        new ImageGrayscaleTransform.Column()
                        {
                            Name = "ImageGrey", Source = "ImageCropped"
                        }
                    }
                }, cropped);

                var fname = nameof(TestGreyscaleTransformImages) + "_model.zip";

                var fh = env.CreateOutputFile(fname);
                using (var ch = env.Start("save"))
                    TrainUtils.SaveModel(env, ch, fh, null, new RoleMappedData(grey));

                grey = ModelFileUtils.LoadPipeline(env, fh.OpenReadStream(), new MultiFileSource(dataFile));
                DeleteOutputPath(fname);

                grey.Schema.TryGetColumnIndex("ImageGrey", out int greyColumn);
                using (var cursor = grey.GetRowCursor((x) => true))
                {
                    var    bitmapGetter = cursor.GetGetter <Bitmap>(greyColumn);
                    Bitmap bitmap       = default;
                    while (cursor.MoveNext())
                    {
                        bitmapGetter(ref bitmap);
                        Assert.NotNull(bitmap);
                        for (int x = 0; x < imageWidth; x++)
                        {
                            for (int y = 0; y < imageHeight; y++)
                            {
                                var pixel = bitmap.GetPixel(x, y);
                                // greyscale image has same values for R,G and B
                                Assert.True(pixel.R == pixel.G && pixel.G == pixel.B);
                            }
                        }
                    }
                }
            }
            Done();
        }
Пример #8
0
        public void TensorFlowTransformObjectDetectionTest()
        {
            var model_location = @"C:\models\TensorFlow\ssd_mobilenet_v1_coco_2018_01_28\frozen_inference_graph.pb";

            using (var env = new ConsoleEnvironment(seed: 1, conc: 1))
            {
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var images      = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Source = "ImageReal", Name = "ImageCropped", ImageHeight = 32, ImageWidth = 32, Resizing = ImageResizerTransform.ResizingKind.IsoCrop
                        }
                    }
                }, images);
                var pixels = ImagePixelExtractorTransform.Create(env, new ImagePixelExtractorTransform.Arguments()
                {
                    Column = new ImagePixelExtractorTransform.Column[1] {
                        new ImagePixelExtractorTransform.Column()
                        {
                            Source = "ImageCropped", Name = "image_tensor", UseAlpha = false, InterleaveArgb = true, Convert = false
                        }
                    }
                }, cropped);

                var tf = TensorFlowTransform.Create(env, pixels, model_location,
                                                    new[] { "detection_boxes", "detection_scores", "num_detections", "detection_classes" },
                                                    new[] { "image_tensor" });

                tf.Schema.TryGetColumnIndex("image_tensor", out int input);
                tf.Schema.TryGetColumnIndex("detection_boxes", out int boxes);
                tf.Schema.TryGetColumnIndex("detection_scores", out int scores);
                tf.Schema.TryGetColumnIndex("num_detections", out int num);
                tf.Schema.TryGetColumnIndex("detection_classes", out int classes);
                using (var curs = tf.GetRowCursor(col => col == classes || col == num || col == scores || col == boxes || col == input))
                {
                    var getInput    = curs.GetGetter <VBuffer <byte> >(input);
                    var getBoxes    = curs.GetGetter <VBuffer <float> >(boxes);
                    var getScores   = curs.GetGetter <VBuffer <float> >(scores);
                    var getNum      = curs.GetGetter <VBuffer <float> >(num);
                    var getClasses  = curs.GetGetter <VBuffer <float> >(classes);
                    var buffer      = default(VBuffer <float>);
                    var inputBuffer = default(VBuffer <byte>);
                    while (curs.MoveNext())
                    {
                        getInput(ref inputBuffer);
                        getBoxes(ref buffer);
                        getScores(ref buffer);
                        getNum(ref buffer);
                        getClasses(ref buffer);
                    }
                }
            }
        }
Пример #9
0
        public void TensorFlowTransformCifar()
        {
            var model_location = "cifar_model/frozen_model.pb";

            using (var env = new ConsoleEnvironment())
            {
                var imageHeight = 32;
                var imageWidth  = 32;
                var dataFile    = GetDataPath("images/images.tsv");
                var imageFolder = Path.GetDirectoryName(dataFile);
                var data        = env.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
                var images      = ImageLoaderTransform.Create(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = ImageResizerTransform.Create(env, new ImageResizerTransform.Arguments()
                {
                    Column = new ImageResizerTransform.Column[1] {
                        new ImageResizerTransform.Column()
                        {
                            Source = "ImageReal", Name = "ImageCropped", ImageHeight = imageHeight, ImageWidth = imageWidth, Resizing = ImageResizerTransform.ResizingKind.IsoCrop
                        }
                    }
                }, images);

                var pixels = ImagePixelExtractorTransform.Create(env, new ImagePixelExtractorTransform.Arguments()
                {
                    Column = new ImagePixelExtractorTransform.Column[1] {
                        new ImagePixelExtractorTransform.Column()
                        {
                            Source = "ImageCropped", Name = "Input", UseAlpha = false, InterleaveArgb = true
                        }
                    }
                }, cropped);


                IDataView trans = TensorFlowTransform.Create(env, pixels, model_location, "Output", "Input");

                trans.Schema.TryGetColumnIndex("Output", out int output);
                using (var cursor = trans.GetRowCursor(col => col == output))
                {
                    var buffer  = default(VBuffer <float>);
                    var getter  = cursor.GetGetter <VBuffer <float> >(output);
                    var numRows = 0;
                    while (cursor.MoveNext())
                    {
                        getter(ref buffer);
                        Assert.Equal(10, buffer.Length);
                        numRows += 1;
                    }
                    Assert.Equal(3, numRows);
                }
            }
        }