-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Image support #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image support #528
Changes from 20 commits
ba6f067
ae8f429
ffa7f12
23f63ca
52d4f7d
dbf6e2a
56a0b79
cf0a06c
e5c0ab0
46cc8b1
601bab9
6c9df18
d97f8ae
dbd2e42
fdd7572
a31a4b1
0c31ff7
50d4ac0
516a189
23ba010
e0f07f5
41403bc
75c4ba8
48cad2d
476ade5
e9c59a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Pack"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <PackageDescription>ML.NET component for Image support</PackageDescription> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="../Microsoft.ML/Microsoft.ML.nupkgproj" /> | ||
| <PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonPackageVersion)" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <Project DefaultTargets="Pack"> | ||
|
|
||
| <Import Project="Microsoft.ML.ImageAnalytics.nupkgproj" /> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.ML.Runtime; | ||
| using Microsoft.ML.Runtime.EntryPoints; | ||
| using Microsoft.ML.Runtime.ImageAnalytics.EntryPoints; | ||
|
|
||
| [assembly: LoadableClass(typeof(void), typeof(ImageAnalytics), null, typeof(SignatureEntryPointModule), "ImageAnalytics")] | ||
| namespace Microsoft.ML.Runtime.ImageAnalytics.EntryPoints | ||
| { | ||
| public static class ImageAnalytics | ||
| { | ||
| [TlcModule.EntryPoint(Name = "Transforms.ImageLoader", Desc = ImageLoaderTransform.Summary, | ||
| UserName = ImageLoaderTransform.UserName, ShortName = ImageLoaderTransform.LoaderSignature)] | ||
| public static CommonOutputs.TransformOutput ImageLoader(IHostEnvironment env, ImageLoaderTransform.Arguments input) | ||
| { | ||
| var h = EntryPointUtils.CheckArgsAndCreateHost(env, "ImageLoaderTransform", input); | ||
| var xf = new ImageLoaderTransform(h, input, input.Data); | ||
| return new CommonOutputs.TransformOutput() | ||
| { | ||
| Model = new TransformModel(h, xf, input.Data), | ||
| OutputData = xf | ||
| }; | ||
| } | ||
|
|
||
| [TlcModule.EntryPoint(Name = "Transforms.ImageResizer", Desc = ImageResizerTransform.Summary, | ||
| UserName = ImageResizerTransform.UserName, ShortName = ImageResizerTransform.LoaderSignature)] | ||
| public static CommonOutputs.TransformOutput ImageResizer(IHostEnvironment env, ImageResizerTransform.Arguments input) | ||
| { | ||
| var h = EntryPointUtils.CheckArgsAndCreateHost(env, "ImageResizerTransform", input); | ||
| var xf = new ImageResizerTransform(h, input, input.Data); | ||
| return new CommonOutputs.TransformOutput() | ||
| { | ||
| Model = new TransformModel(h, xf, input.Data), | ||
| OutputData = xf | ||
| }; | ||
| } | ||
|
|
||
| [TlcModule.EntryPoint(Name = "Transforms.ImagePixelExtractor", Desc = ImagePixelExtractorTransform.Summary, | ||
| UserName = ImagePixelExtractorTransform.UserName, ShortName = ImagePixelExtractorTransform.LoaderSignature)] | ||
| public static CommonOutputs.TransformOutput ImagePixelExtractor(IHostEnvironment env, ImagePixelExtractorTransform.Arguments input) | ||
| { | ||
| var h = EntryPointUtils.CheckArgsAndCreateHost(env, "ImagePixelExtractorTransform", input); | ||
| var xf = new ImagePixelExtractorTransform(h, input, input.Data); | ||
| return new CommonOutputs.TransformOutput() | ||
| { | ||
| Model = new TransformModel(h, xf, input.Data), | ||
| OutputData = xf | ||
| }; | ||
| } | ||
|
|
||
| [TlcModule.EntryPoint(Name = "Transforms.ImageGrayscale", Desc = ImageGrayscaleTransform.Summary, | ||
| UserName = ImageGrayscaleTransform.UserName, ShortName = ImageGrayscaleTransform.LoaderSignature)] | ||
| public static CommonOutputs.TransformOutput ImageGrayscale(IHostEnvironment env, ImageGrayscaleTransform.Arguments input) | ||
| { | ||
| var h = EntryPointUtils.CheckArgsAndCreateHost(env, "ImageGrayscaleTransform", input); | ||
| var xf = new ImageGrayscaleTransform(h, input, input.Data); | ||
| return new CommonOutputs.TransformOutput() | ||
| { | ||
| Model = new TransformModel(h, xf, input.Data), | ||
| OutputData = xf | ||
| }; | ||
| } | ||
|
|
||
| [TlcModule.EntryPoint(Name = "Transforms.VectorToImage", Desc = VectorToImageTransform.Summary, | ||
| UserName = VectorToImageTransform.UserName, ShortName = VectorToImageTransform.LoaderSignature)] | ||
| public static CommonOutputs.TransformOutput VectorToImage(IHostEnvironment env, VectorToImageTransform.Arguments input) | ||
| { | ||
| var h = EntryPointUtils.CheckArgsAndCreateHost(env, "VectorToImageTransform", input); | ||
| var xf = new VectorToImageTransform(h, input, input.Data); | ||
| return new CommonOutputs.TransformOutput() | ||
| { | ||
| Model = new TransformModel(h, xf, input.Data), | ||
| OutputData = xf | ||
| }; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Drawing; | ||
| using System.Drawing.Imaging; | ||
| using System.Text; | ||
| using Microsoft.ML.Runtime; | ||
| using Microsoft.ML.Runtime.CommandLine; | ||
| using Microsoft.ML.Runtime.Data; | ||
| using Microsoft.ML.Runtime.EntryPoints; | ||
| using Microsoft.ML.Runtime.Internal.Utilities; | ||
| using Microsoft.ML.Runtime.Model; | ||
| using Microsoft.ML.Runtime.ImageAnalytics; | ||
|
|
||
| [assembly: LoadableClass(ImageGrayscaleTransform.Summary, typeof(ImageGrayscaleTransform), typeof(ImageGrayscaleTransform.Arguments), typeof(SignatureDataTransform), | ||
| ImageGrayscaleTransform.UserName, "ImageGrayscaleTransform", "ImageGrayscale")] | ||
|
|
||
| [assembly: LoadableClass(ImageGrayscaleTransform.Summary, typeof(ImageGrayscaleTransform), null, typeof(SignatureLoadDataTransform), | ||
| ImageGrayscaleTransform.UserName, ImageGrayscaleTransform.LoaderSignature)] | ||
|
|
||
| namespace Microsoft.ML.Runtime.ImageAnalytics | ||
| { | ||
| // REVIEW: Rewrite as LambdaTransform to simplify. | ||
| // REVIEW: Should it be separate transform or part of ImageResizerTransform? | ||
| /// <summary> | ||
| /// Transform which takes one or many columns of <see cref="ImageType"/> type in IDataView and | ||
| /// convert them to greyscale representation of the same image. | ||
| /// </summary> | ||
| public sealed class ImageGrayscaleTransform : OneToOneTransformBase | ||
| { | ||
| public sealed class Column : OneToOneColumn | ||
| { | ||
| public static Column Parse(string str) | ||
| { | ||
| var res = new Column(); | ||
| if (res.TryParse(str)) | ||
| return res; | ||
| return null; | ||
| } | ||
|
|
||
| public bool TryUnparse(StringBuilder sb) | ||
| { | ||
| Contracts.AssertValue(sb); | ||
| return TryUnparseCore(sb); | ||
| } | ||
| } | ||
|
|
||
| public class Arguments : TransformInputBase | ||
| { | ||
| [Argument(ArgumentType.Multiple | ArgumentType.Required, HelpText = "New column definition(s) (optional form: name:src)", ShortName = "col", SortOrder = 1)] | ||
| public Column[] Column; | ||
| } | ||
|
|
||
| internal const string Summary = "Convert image into grayscale."; | ||
|
|
||
| internal const string UserName = "Image Greyscale Transform"; | ||
| public const string LoaderSignature = "ImageGrayscaleTransform"; | ||
| private static VersionInfo GetVersionInfo() | ||
| { | ||
| return new VersionInfo( | ||
| modelSignature: "IMGGREY ", | ||
| verWrittenCur: 0x00010001, // Initial | ||
| verReadableCur: 0x00010001, | ||
| verWeCanReadBack: 0x00010001, | ||
| loaderSignature: LoaderSignature); | ||
| } | ||
|
|
||
| private const string RegistrationName = "ImageGrayscale"; | ||
|
|
||
| /// Public constructor corresponding to SignatureDataTransform. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Did you mean this to be |
||
| public ImageGrayscaleTransform(IHostEnvironment env, Arguments args, IDataView input) | ||
| : base(env, RegistrationName, env.CheckRef(args, nameof(args)).Column, input, t => t is ImageType ? null : "Expected Image type") | ||
| { | ||
| Host.AssertNonEmpty(Infos); | ||
| Host.Assert(Infos.Length == Utils.Size(args.Column)); | ||
| Metadata.Seal(); | ||
| } | ||
|
|
||
| private ImageGrayscaleTransform(IHost host, ModelLoadContext ctx, IDataView input) | ||
| : base(host, ctx, input, t => t is ImageType ? null : "Expected Image type") | ||
| { | ||
| Host.AssertValue(ctx); | ||
| // *** Binary format *** | ||
| // <base> | ||
| Host.AssertNonEmpty(Infos); | ||
| Metadata.Seal(); | ||
| } | ||
|
|
||
| public static ImageGrayscaleTransform Create(IHostEnvironment env, ModelLoadContext ctx, IDataView input) | ||
| { | ||
| Contracts.CheckValue(env, nameof(env)); | ||
| var h = env.Register(RegistrationName); | ||
| h.CheckValue(ctx, nameof(ctx)); | ||
| h.CheckValue(input, nameof(input)); | ||
| ctx.CheckAtModel(GetVersionInfo()); | ||
| return h.Apply("Loading Model", ch => new ImageGrayscaleTransform(h, ctx, input)); | ||
| } | ||
|
|
||
| public override void Save(ModelSaveContext ctx) | ||
| { | ||
| Host.CheckValue(ctx, nameof(ctx)); | ||
| ctx.CheckAtModel(); | ||
| ctx.SetVersionInfo(GetVersionInfo()); | ||
|
|
||
| // *** Binary format *** | ||
| // <base> | ||
| SaveBase(ctx); | ||
| } | ||
|
|
||
| protected override ColumnType GetColumnTypeCore(int iinfo) | ||
| { | ||
| Host.Assert(0 <= iinfo & iinfo < Infos.Length); | ||
| return Infos[iinfo].TypeSrc; | ||
| } | ||
|
|
||
| public ColorMatrix GreyscaleColorMatrix = new ColorMatrix( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is a mutable, public instance level field that affects the functionality of the transform. Probably want this to be |
||
| new float[][] | ||
| { | ||
| new float[] {.3f, .3f, .3f, 0, 0}, | ||
| new float[] {.59f, .59f, .59f, 0, 0}, | ||
| new float[] {.11f, .11f, .11f, 0, 0}, | ||
| new float[] {0, 0, 0, 1, 0}, | ||
| new float[] {0, 0, 0, 0, 1} | ||
| }); | ||
|
|
||
| protected override Delegate GetGetterCore(IChannel ch, IRow input, int iinfo, out Action disposer) | ||
| { | ||
| Host.AssertValueOrNull(ch); | ||
| Host.AssertValue(input); | ||
| Host.Assert(0 <= iinfo && iinfo < Infos.Length); | ||
|
|
||
| var src = default(Bitmap); | ||
| var getSrc = GetSrcGetter<Bitmap>(input, iinfo); | ||
|
|
||
| disposer = | ||
| () => | ||
| { | ||
| if (src != null) | ||
| { | ||
| src.Dispose(); | ||
| src = null; | ||
| } | ||
| }; | ||
|
|
||
| ValueGetter<Bitmap> del = | ||
| (ref Bitmap dst) => | ||
| { | ||
| if (dst != null) | ||
| dst.Dispose(); | ||
|
|
||
| getSrc(ref src); | ||
| if (src == null || src.Height <= 0 || src.Width <= 0) | ||
| return; | ||
|
|
||
| dst = new Bitmap(src.Width, src.Height); | ||
| ImageAttributes attributes = new ImageAttributes(); | ||
| attributes.SetColorMatrix(GreyscaleColorMatrix); | ||
| var srcRectangle = new Rectangle(0, 0, src.Width, src.Height); | ||
| using (var g = Graphics.FromImage(dst)) | ||
| { | ||
| g.DrawImage(src, srcRectangle, 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, attributes); | ||
| } | ||
| Host.Assert(dst.Width == src.Width && dst.Height == src.Height); | ||
| }; | ||
|
|
||
| return del; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You had a signature of
IMGLOADTfor that. Why is this notIMGGREYT?Also: is it intentional that this be
GREY? Elsewhere we are using "gray."If models already exist with this signature I guess we have to keep it, not sure if that's the case though since you didn't bump the version. :) (I'm not suggesting you bump the version to be clear.) #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
English are so confusing, grey and gray is apparent to have same meaning, but my brain apparently throw coin during typing and at some point I had code with both spelling all across code. Which I tried to unified, but this one slip from my attention.
This is a new transform which we never had before, and I add it "just in case", and "maybe it will prove useful". So i think it make sense to start from version 1.
In reply to: 205265843 [](ancestors = 205265843)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sure @Ivanidzo4ka grey and gray are synonymous, though elsewhere you use gray. Also my impression is that we generally favor American spellings (gray) of words vs. British ones (grey).
...Even though I was brought up to use "grey," and had to correct myself since later in my life people thought I was weird. So now I hypercorrect, and you reap the benefits. :D
In reply to: 205597316 [](ancestors = 205597316,205265843)