Skip to content

Commit

Permalink
removing System.Drawing dep, adding onnxruntime
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Dec 4, 2023
1 parent e5aab0e commit 59b2b13
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.ML" Version="1.6.0" />
<PackageReference Include="Microsoft.ML.OnnxTransformer" Version="1.6.0" />
<PackageReference Include="Microsoft.ML" Version="3.0.0" />
<PackageReference Include="Microsoft.ML.OnnxTransformer" Version="3.0.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.3" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/Redis.OM.Vectorizers.Resnet18/ImageModelObjects.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Image;

Expand All @@ -13,5 +12,5 @@ public class ImageInput
public class InMemoryImageData
{
[ImageType(224,224)]
public Bitmap Image;
public MLImage Image;
}
10 changes: 4 additions & 6 deletions src/Redis.OM.Vectorizers.Resnet18/ImageVectorizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms;
Expand Down Expand Up @@ -31,9 +30,8 @@ public byte[] Vectorize(string obj)
RequestUri = uri,
};
var imageStream = Configuration.Instance.Client.Send(request).Content.ReadAsStream();
var image = Image.FromStream(imageStream);
var bitmap = new Bitmap(image);
var vector = VectorizeBitMaps(new [] { bitmap })[0].SelectMany(BitConverter.GetBytes).ToArray();
var image = MLImage.CreateFromStream(imageStream);
var vector = VectorizeBitMaps(new [] { image })[0].SelectMany(BitConverter.GetBytes).ToArray();
return vector;
}

Expand Down Expand Up @@ -93,9 +91,9 @@ private static EstimatorChain<TransformerChain<ColumnCopyingTransformer>> Create
return pipeline;
}

public static float[][] VectorizeBitMaps(IEnumerable<Bitmap> bitmaps)
public static float[][] VectorizeBitMaps(IEnumerable<MLImage> mlImages)
{
var images = bitmaps.Select(x => new InMemoryImageData { Image = x });
var images = mlImages.Select(x => new InMemoryImageData { Image = x });
var mlContext = MlContext.Value;
var dataView = mlContext.Data.LoadFromEnumerable(images);
var transformedData = BitmapPipeline.Value.Fit(dataView).Transform(dataView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
</PropertyGroup>

<ItemGroup>
<!-- <PackageReference Include="Microsoft.ML.DnnImageFeaturizer.ResNet18" Version="0.18.0" IncludeAssets="all" />-->
<PackageReference Include="Microsoft.ML" Version="1.6.0" />
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="1.6.0" />
<PackageReference Include="Microsoft.ML.OnnxTransformer" Version="1.6.0" />
<PackageReference Include="Microsoft.ML" Version="3.0.0" />
<PackageReference Include="Microsoft.ML.ImageAnalytics" Version="3.0.0" />
<PackageReference Include="Microsoft.ML.OnnxTransformer" Version="3.0.0" />
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.3" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 59b2b13

Please sign in to comment.