public void TensorFlowTransformInceptionTest()
        {
            var model_location = @"C:\models\TensorFlow\tensorflow_inception_graph.pb";
            var mlContext      = new MLContext(seed: 1, conc: 1);
            var dataFile       = GetDataPath("images/images.tsv");
            var imageFolder    = Path.GetDirectoryName(dataFile);
            var data           = mlContext.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
            var images         = ImageLoaderTransform.Create(mlContext, new ImageLoaderTransform.Arguments()
            {
                Column = new ImageLoaderTransform.Column[1]
                {
                    new ImageLoaderTransform.Column()
                    {
                        Source = "ImagePath", Name = "ImageReal"
                    }
                },
                ImageFolder = imageFolder
            }, data);
            var cropped = ImageResizerTransform.Create(mlContext, 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(mlContext, 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(mlContext, pixels, model_location, new[] { "softmax2_pre_activation" }, new[] { "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);
                }
            }
        }
Example #2
0
        public static CommonOutputs.TransformOutput ImagePixelExtractor(IHostEnvironment env, ImagePixelExtractorTransform.Arguments input)
        {
            var h  = EntryPointUtils.CheckArgsAndCreateHost(env, "ImagePixelExtractorTransform", input);
            var xf = ImagePixelExtractorTransform.Create(h, input, input.Data);

            return(new CommonOutputs.TransformOutput()
            {
                Model = new TransformModel(h, xf, input.Data),
                OutputData = xf
            });
        }
        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        = TextLoader.Create(env, new TextLoader.Arguments()
                {
                    Column = new[]
                    {
                        new TextLoader.Column("ImagePath", DataKind.TX, 0),
                        new TextLoader.Column("Name", DataKind.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();
        }
        public void TensorFlowTransformObjectDetectionTest()
        {
            var model_location = @"C:\models\TensorFlow\ssd_mobilenet_v1_coco_2018_01_28\frozen_inference_graph.pb";
            var mlContext      = new MLContext(seed: 1, conc: 1);
            var dataFile       = GetDataPath("images/images.tsv");
            var imageFolder    = Path.GetDirectoryName(dataFile);
            var data           = mlContext.CreateLoader("Text{col=ImagePath:TX:0 col=Name:TX:1}", new MultiFileSource(dataFile));
            var images         = ImageLoaderTransform.Create(mlContext, new ImageLoaderTransform.Arguments()
            {
                Column = new ImageLoaderTransform.Column[1]
                {
                    new ImageLoaderTransform.Column()
                    {
                        Source = "ImagePath", Name = "ImageReal"
                    }
                },
                ImageFolder = imageFolder
            }, data);
            var cropped = ImageResizerTransform.Create(mlContext, 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(mlContext, 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(mlContext, 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);
                }
            }
        }
        [ConditionalFact(typeof(Environment), nameof(Environment.Is64BitProcess))] // TensorFlow is 64-bit only
        public void TensorFlowTransformCifarSavedModel()
        {
            var model_location = "cifar_saved_model";

            var mlContext       = new MLContext(seed: 1, conc: 1);
            var tensorFlowModel = TensorFlowUtils.LoadTensorFlowModel(mlContext, model_location);
            var schema          = tensorFlowModel.GetInputSchema();

            Assert.True(schema.TryGetColumnIndex("Input", out int column));
            var type        = (VectorType)schema.GetColumnType(column);
            var imageHeight = type.Dimensions[0];
            var imageWidth  = type.Dimensions[1];

            var dataFile    = GetDataPath("images/images.tsv");
            var imageFolder = Path.GetDirectoryName(dataFile);
            var data        = TextLoader.Create(mlContext, new TextLoader.Arguments()
            {
                Column = new[]
                {
                    new TextLoader.Column("ImagePath", DataKind.TX, 0),
                    new TextLoader.Column("Name", DataKind.TX, 1),
                }
            }, new MultiFileSource(dataFile));
            var images = ImageLoaderTransform.Create(mlContext, new ImageLoaderTransform.Arguments()
            {
                Column = new ImageLoaderTransform.Column[1]
                {
                    new ImageLoaderTransform.Column()
                    {
                        Source = "ImagePath", Name = "ImageReal"
                    }
                },
                ImageFolder = imageFolder
            }, data);
            var cropped = ImageResizerTransform.Create(mlContext, 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(mlContext, new ImagePixelExtractorTransform.Arguments()
            {
                Column = new ImagePixelExtractorTransform.Column[1] {
                    new ImagePixelExtractorTransform.Column()
                    {
                        Source = "ImageCropped", Name = "Input", UseAlpha = false, InterleaveArgb = true
                    }
                }
            }, cropped);


            IDataView trans = TensorFlowTransform.Create(mlContext, pixels, tensorFlowModel, new[] { "Output" }, new[] { "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);
            }
        }
        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);
                }
            }
        }
        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        = TextLoader.Create(env, new TextLoader.Arguments()
                {
                    Column = new[]
                    {
                        new TextLoader.Column("ImagePath", DataKind.TX, 0),
                        new TextLoader.Column("Name", DataKind.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, new[] { "Output" }, new[] { "Input" });
                }
                catch
                {
                    thrown = true;
                }
                Assert.True(thrown);
            }
        }
Example #8
0
        public void TestBackAndForthConversion()
        {
            using (var env = new TlcEnvironment())
            {
                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      = new ImageLoaderTransform(env, new ImageLoaderTransform.Arguments()
                {
                    Column = new ImageLoaderTransform.Column[1]
                    {
                        new ImageLoaderTransform.Column()
                        {
                            Source = "ImagePath", Name = "ImageReal"
                        }
                    },
                    ImageFolder = imageFolder
                }, data);
                var cropped = new ImageResizerTransform(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 = new ImagePixelExtractorTransform(env, new ImagePixelExtractorTransform.Arguments()
                {
                    Column = new ImagePixelExtractorTransform.Column[1] {
                        new ImagePixelExtractorTransform.Column()
                        {
                            Source = "ImageCropped", Name = "ImagePixels", UseAlpha = true
                        }
                    }
                }, cropped);

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

                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++)
                            {
                                Assert.True(croppedBitmap.GetPixel(x, y) == restoredBitmap.GetPixel(x, y));
                            }
                        }
                    }
                }
            }
        }