Skip to content

Commit 24f782d

Browse files
authored
Scrubbing image transforms (#2875)
* Scrubbing image transforms * some PR feedback * more PR feedback * More PR feedback
1 parent ce0c917 commit 24f782d

File tree

11 files changed

+330
-331
lines changed

11 files changed

+330
-331
lines changed

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/DnnFeaturizeImage.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.IO;
1+
using System.IO;
32
using System.Linq;
43
using Microsoft.ML.Data;
54
using Microsoft.ML.Transforms;

docs/samples/Microsoft.ML.Samples/Dynamic/Transforms/ImageAnalytics/ExtractPixels.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static void Example()
3838
var imagesFolder = Path.GetDirectoryName(imagesDataFile);
3939
// Image loading pipeline.
4040
var pipeline = mlContext.Transforms.LoadImages(imagesFolder, ("ImageObject", "ImagePath"))
41-
.Append(mlContext.Transforms.ResizeImages("ImageObject",imageWidth: 100 , imageHeight: 100 ))
41+
.Append(mlContext.Transforms.ResizeImages("ImageObject", imageWidth: 100, imageHeight: 100 ))
4242
.Append(mlContext.Transforms.ExtractPixels("Pixels", "ImageObject"));
4343

4444

src/Microsoft.ML.ImageAnalytics/ExtensionsCatalog.cs

+47-47
Large diffs are not rendered by default.

src/Microsoft.ML.ImageAnalytics/ImageGrayscale.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static VersionInfo GetVersionInfo()
9292
/// <summary>
9393
/// The input and output column pairs passed to this <see cref="ITransformer"/>.
9494
/// </summary>
95-
public IReadOnlyCollection<(string outputColumnName, string inputColumnName)> Columns => ColumnPairs.AsReadOnly();
95+
internal IReadOnlyCollection<(string outputColumnName, string inputColumnName)> Columns => ColumnPairs.AsReadOnly();
9696

9797
/// <summary>
9898
/// Converts the images to grayscale.

src/Microsoft.ML.ImageAnalytics/ImageLoader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal sealed class Options : TransformInputBase
8282
/// <summary>
8383
/// The columns passed to this <see cref="ITransformer"/>.
8484
/// </summary>
85-
public IReadOnlyCollection<(string outputColumnName, string inputColumnName)> Columns => ColumnPairs.AsReadOnly();
85+
internal IReadOnlyCollection<(string outputColumnName, string inputColumnName)> Columns => ColumnPairs.AsReadOnly();
8686

8787
/// <summary>
8888
/// Initializes a new instance of <see cref="ImageLoadingTransformer"/>.

src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs

+120-120
Large diffs are not rendered by default.

src/Microsoft.ML.ImageAnalytics/ImageResizer.cs

+29-29
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static VersionInfo GetVersionInfo()
120120
/// <summary>
121121
/// The columns passed to this <see cref="ITransformer"/>.
122122
/// </summary>
123-
public IReadOnlyCollection<ImageResizingEstimator.ColumnOptions> Columns => _columns.AsReadOnly();
123+
internal IReadOnlyCollection<ImageResizingEstimator.ColumnOptions> Columns => _columns.AsReadOnly();
124124

125125
///<summary>
126126
/// Resize image.
@@ -249,8 +249,8 @@ private protected override void SaveModel(ModelSaveContext ctx)
249249

250250
foreach (var col in _columns)
251251
{
252-
ctx.Writer.Write(col.Width);
253-
ctx.Writer.Write(col.Height);
252+
ctx.Writer.Write(col.ImageWidth);
253+
ctx.Writer.Write(col.ImageHeight);
254254
Contracts.Assert((ImageResizingEstimator.ResizingKind)(byte)col.Resizing == col.Resizing);
255255
ctx.Writer.Write((byte)col.Resizing);
256256
Contracts.Assert((ImageResizingEstimator.Anchor)(byte)col.Anchor == col.Anchor);
@@ -307,7 +307,7 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
307307
getSrc(ref src);
308308
if (src == null || src.Height <= 0 || src.Width <= 0)
309309
return;
310-
if (src.Height == info.Height && src.Width == info.Width)
310+
if (src.Height == info.ImageHeight && src.Width == info.ImageWidth)
311311
{
312312
dst = src;
313313
return;
@@ -325,22 +325,22 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
325325
float widthAspect = 0;
326326
float heightAspect = 0;
327327

328-
widthAspect = (float)info.Width / sourceWidth;
329-
heightAspect = (float)info.Height / sourceHeight;
328+
widthAspect = (float)info.ImageWidth / sourceWidth;
329+
heightAspect = (float)info.ImageHeight / sourceHeight;
330330

331331
if (info.Resizing == ImageResizingEstimator.ResizingKind.IsoPad)
332332
{
333-
widthAspect = (float)info.Width / sourceWidth;
334-
heightAspect = (float)info.Height / sourceHeight;
333+
widthAspect = (float)info.ImageWidth / sourceWidth;
334+
heightAspect = (float)info.ImageHeight / sourceHeight;
335335
if (heightAspect < widthAspect)
336336
{
337337
aspect = heightAspect;
338-
destX = (int)((info.Width - (sourceWidth * aspect)) / 2);
338+
destX = (int)((info.ImageWidth - (sourceWidth * aspect)) / 2);
339339
}
340340
else
341341
{
342342
aspect = widthAspect;
343-
destY = (int)((info.Height - (sourceHeight * aspect)) / 2);
343+
destY = (int)((info.ImageHeight - (sourceHeight * aspect)) / 2);
344344
}
345345

346346
destWidth = (int)(sourceWidth * aspect);
@@ -357,10 +357,10 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
357357
destY = 0;
358358
break;
359359
case ImageResizingEstimator.Anchor.Bottom:
360-
destY = (int)(info.Height - (sourceHeight * aspect));
360+
destY = (int)(info.ImageHeight - (sourceHeight * aspect));
361361
break;
362362
default:
363-
destY = (int)((info.Height - (sourceHeight * aspect)) / 2);
363+
destY = (int)((info.ImageHeight - (sourceHeight * aspect)) / 2);
364364
break;
365365
}
366366
}
@@ -373,10 +373,10 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
373373
destX = 0;
374374
break;
375375
case ImageResizingEstimator.Anchor.Right:
376-
destX = (int)(info.Width - (sourceWidth * aspect));
376+
destX = (int)(info.ImageWidth - (sourceWidth * aspect));
377377
break;
378378
default:
379-
destX = (int)((info.Width - (sourceWidth * aspect)) / 2);
379+
destX = (int)((info.ImageWidth - (sourceWidth * aspect)) / 2);
380380
break;
381381
}
382382
}
@@ -386,18 +386,18 @@ protected override Delegate MakeGetter(DataViewRow input, int iinfo, Func<int, b
386386
}
387387
else if (info.Resizing == ImageResizingEstimator.ResizingKind.Fill)
388388
{
389-
destWidth = info.Width;
390-
destHeight = info.Height;
389+
destWidth = info.ImageWidth;
390+
destHeight = info.ImageHeight;
391391
}
392392

393-
dst = new Bitmap(info.Width, info.Height, src.PixelFormat);
393+
dst = new Bitmap(info.ImageWidth, info.ImageHeight, src.PixelFormat);
394394
var srcRectangle = new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
395395
var destRectangle = new Rectangle(destX, destY, destWidth, destHeight);
396396
using (var g = Graphics.FromImage(dst))
397397
{
398398
g.DrawImage(src, destRectangle, srcRectangle, GraphicsUnit.Pixel);
399399
}
400-
Contracts.Assert(dst.Width == info.Width && dst.Height == info.Height);
400+
Contracts.Assert(dst.Width == info.ImageWidth && dst.Height == info.ImageHeight);
401401
};
402402

403403
return del;
@@ -471,10 +471,10 @@ public sealed class ColumnOptions
471471
public readonly string InputColumnName;
472472

473473
/// <summary>Width to resize the image to.</summary>
474-
public readonly int Width;
474+
public readonly int ImageWidth;
475475

476476
/// <summary>Height to resize the image to.</summary>
477-
public readonly int Height;
477+
public readonly int ImageHeight;
478478

479479
/// <summary>What <see cref="ResizingKind"/> to use (uniform, or non-uniform).</summary>
480480
public readonly ResizingKind Resizing;
@@ -489,31 +489,31 @@ public sealed class ColumnOptions
489489
/// Describes how the transformer handles one image resize column pair.
490490
/// </summary>
491491
/// <param name="name">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
492-
/// <param name="width">Width of resized image.</param>
493-
/// <param name="height">Height of resized image.</param>
492+
/// <param name="imageWidth">Width of resized image.</param>
493+
/// <param name="imageHeight">Height of resized image.</param>
494494
/// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="name"/> will be used as source.</param>
495495
/// <param name="resizing">What <see cref="ImageResizingEstimator.ResizingKind"/> to use.</param>
496496
/// <param name="anchor">If <paramref name="resizing"/> set to <see cref="ImageResizingEstimator.ResizingKind.IsoCrop"/> what anchor to use for cropping.</param>
497497
public ColumnOptions(string name,
498-
int width,
499-
int height,
498+
int imageWidth,
499+
int imageHeight,
500500
string inputColumnName = null,
501501
ResizingKind resizing = Defaults.Resizing,
502502
Anchor anchor = Defaults.CropAnchor)
503503
{
504504
Contracts.CheckNonEmpty(name, nameof(name));
505-
Contracts.CheckUserArg(width > 0, nameof(width));
506-
Contracts.CheckUserArg(height > 0, nameof(height));
505+
Contracts.CheckUserArg(imageWidth > 0, nameof(imageWidth));
506+
Contracts.CheckUserArg(imageHeight > 0, nameof(imageHeight));
507507
Contracts.CheckUserArg(Enum.IsDefined(typeof(ResizingKind), resizing), nameof(resizing));
508508
Contracts.CheckUserArg(Enum.IsDefined(typeof(Anchor), anchor), nameof(anchor));
509509

510510
Name = name;
511511
InputColumnName = inputColumnName ?? name;
512-
Width = width;
513-
Height = height;
512+
ImageWidth = imageWidth;
513+
ImageHeight = imageHeight;
514514
Resizing = resizing;
515515
Anchor = anchor;
516-
Type = new ImageType(Height, Width);
516+
Type = new ImageType(ImageHeight, ImageWidth);
517517
}
518518
}
519519

0 commit comments

Comments
 (0)