Skip to content

Commit

Permalink
Merge pull request #21 from microsoft/jay/merge-ic-sample
Browse files Browse the repository at this point in the history
Merged ImageNet Classification models into one sample that they can all reference
  • Loading branch information
nmetulev authored Nov 19, 2024
2 parents 1b30ff7 + 3b6aa53 commit e445cdd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 437 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<samples:BaseSamplePage
xmlns:samples="using:AIDevGallery.Samples"
x:Class="AIDevGallery.Samples.OpenSourceModels.ImageClassificationMobileNet"
x:Class="AIDevGallery.Samples.OpenSourceModels.ImageClassification"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace AIDevGallery.Samples.OpenSourceModels
{
[GallerySample(
Model1Types = [ModelType.MobileNet],
Model1Types = [ModelType.MobileNet, ModelType.ResNet, ModelType.SqueezeNet],
Scenario = ScenarioType.ImageClassifyImage,
NugetPackageReferences = [
"System.Drawing.Common",
Expand All @@ -32,14 +32,14 @@ namespace AIDevGallery.Samples.OpenSourceModels
SharedCodeEnum.BitmapFunctions,
SharedCodeEnum.DeviceUtils
],
Name = "MobileNet Image Classification",
Name = "ImageNet Image Classification",
Id = "09d73ba7-b877-45f9-9de6-41898ab4d339",
Icon = "\uE8B9")]
internal sealed partial class ImageClassificationMobileNet : BaseSamplePage
internal sealed partial class ImageClassification : BaseSamplePage
{
private InferenceSession? _inferenceSession;

public ImageClassificationMobileNet()
public ImageClassification()
{
this.Unloaded += (s, e) => _inferenceSession?.Dispose();
this.Loaded += (s, e) => Page_Loaded(); // <exclude-line>
Expand Down Expand Up @@ -102,11 +102,23 @@ private async void UploadImageButton_Click(object sender, RoutedEventArgs e)

private async Task ClassifyImage(string filePath)
{
if (!Path.Exists(filePath))
if (!Path.Exists(filePath) || _inferenceSession == null)
{
return;
}

// Grab model metadata
var inputName = _inferenceSession.InputNames[0];
var inputMetadata = _inferenceSession.InputMetadata[inputName];
var dimensions = inputMetadata.Dimensions;

// Set batch size to 1
int batchSize = 1;
dimensions[0] = batchSize;

int inputWidth = dimensions[2];
int inputHeight = dimensions[3];

BitmapImage bitmapImage = new(new Uri(filePath));
UploadedImage.Source = bitmapImage;
NarratorHelper.AnnounceImageChanged(UploadedImage, "Image changed: new upload."); // <exclude-line>
Expand All @@ -116,22 +128,19 @@ private async Task ClassifyImage(string filePath)
Bitmap image = new(filePath);
// Resize image
int width = 224;
int height = 224;
var resizedImage = BitmapFunctions.ResizeBitmap(image, width, height);
var resizedImage = BitmapFunctions.ResizeBitmap(image, inputWidth, inputHeight);
image.Dispose();
image = resizedImage;
// Preprocess image
Tensor<float> input = new DenseTensor<float>([1, 3, 224, 224]);
Tensor<float> input = new DenseTensor<float>(dimensions);
input = BitmapFunctions.PreprocessBitmapWithStdDev(image, input);
image.Dispose();
// Setup inputs
var inputMetadataName = _inferenceSession!.InputNames[0];
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor(inputMetadataName, input)
NamedOnnxValue.CreateFromTensor(inputName, input)
};
// Run inference
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit e445cdd

Please sign in to comment.