Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Microsoft.ML.sln
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.ML.CpuMath", "Mic
pkg\Microsoft.ML.CpuMath\Microsoft.ML.CpuMath.symbols.nupkgproj = pkg\Microsoft.ML.CpuMath\Microsoft.ML.CpuMath.symbols.nupkgproj
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.ML.ImageAnalytics", "src\Microsoft.ML.ImageAnalytics\Microsoft.ML.ImageAnalytics.csproj", "{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -304,6 +306,14 @@ Global
{DCF46B79-1FDB-4DBA-A263-D3D64E3AAA27}.Release|Any CPU.Build.0 = Release|Any CPU
{DCF46B79-1FDB-4DBA-A263-D3D64E3AAA27}.Release-Intrinsics|Any CPU.ActiveCfg = Release|Any CPU
{DCF46B79-1FDB-4DBA-A263-D3D64E3AAA27}.Release-Intrinsics|Any CPU.Build.0 = Release|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Debug-Intrinsics|Any CPU.ActiveCfg = Debug|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Debug-Intrinsics|Any CPU.Build.0 = Debug|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Release|Any CPU.Build.0 = Release|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Release-Intrinsics|Any CPU.ActiveCfg = Release|Any CPU
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975}.Release-Intrinsics|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -340,6 +350,7 @@ Global
{001F3B4E-FBE4-4001-AFD2-A6A989CD1C25} = {09EADF06-BE25-4228-AB53-95AE3E15B530}
{DCF46B79-1FDB-4DBA-A263-D3D64E3AAA27} = {09EADF06-BE25-4228-AB53-95AE3E15B530}
{BF66A305-DF10-47E4-8D81-42049B149D2B} = {D3D38B03-B557-484D-8348-8BADEE4DF592}
{8424DCB2-AA5C-46C7-81D3-1B0DB8C55975} = {09EADF06-BE25-4228-AB53-95AE3E15B530}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {41165AF1-35BB-4832-A189-73060F82B01D}
Expand Down
3 changes: 2 additions & 1 deletion build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<SystemCodeDomPackageVersion>4.4.0</SystemCodeDomPackageVersion>
<SystemReflectionEmitLightweightPackageVersion>4.3.0</SystemReflectionEmitLightweightPackageVersion>
<PublishSymbolsPackageVersion>1.0.0-beta-62824-02</PublishSymbolsPackageVersion>
<LightGBMPackageVersion>2.1.2.2</LightGBMPackageVersion>
<LightGBMPackageVersion>2.1.2.2</LightGBMPackageVersion>
<SystemDrawingCommonPackageVersion>4.5.0</SystemDrawingCommonPackageVersion>
</PropertyGroup>
</Project>
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>
79 changes: 79 additions & 0 deletions src/Microsoft.ML.ImageAnalytics/EntryPoints/ImageAnalytics.cs
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
};
}
}
}
171 changes: 171 additions & 0 deletions src/Microsoft.ML.ImageAnalytics/ImageGrayscaleTransform.cs
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 ",

@TomFinley TomFinley Jul 25, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMGGREY [](start = 33, length = 8)

You had a signature of IMGLOADT for that. Why is this not IMGGREYT?

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

Copy link
Copy Markdown
Contributor Author

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)

Copy link
Copy Markdown
Contributor

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)

verWrittenCur: 0x00010001, // Initial
verReadableCur: 0x00010001,
verWeCanReadBack: 0x00010001,
loaderSignature: LoaderSignature);
}

private const string RegistrationName = "ImageGrayscale";

/// Public constructor corresponding to SignatureDataTransform.

@TomFinley TomFinley Jul 25, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/// [](start = 8, length = 4)

Did you mean this to be // and not /// , since it is not really terribly sensible for callers? #Closed

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(

@TomFinley TomFinley Jul 25, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public ColorMatrix GreyscaleColorMatrix [](start = 8, length = 39)

This is a mutable, public instance level field that affects the functionality of the transform.

Probably want this to be private (or at least internal if somehow it is referenced somewhere else), static, and almost certainly readonly. #Closed

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;
}
}
}
Loading