Skip to content

Commit fa9f2aa

Browse files
SnakyBeakybamurtaugh
authored andcommitted
Code readability adjustments and missing constants (#559)
Seem like good changes to improve formatting consistency.
1 parent 2199b5b commit fa9f2aa

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

  • samples/csharp/getting-started/DeepLearning_ImageClassification_TensorFlow

samples/csharp/getting-started/DeepLearning_ImageClassification_TensorFlow/README.md

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,24 @@ Define the schema of data in a class type and refer that type while loading data
6464

6565
```csharp
6666
public class ImageNetData
67+
{
68+
[LoadColumn(0)]
69+
public string ImagePath;
70+
71+
[LoadColumn(1)]
72+
public string Label;
73+
74+
public static IEnumerable<ImageNetData> ReadFromCsv(string file, string folder)
6775
{
68-
[LoadColumn(0)]
69-
public string ImagePath;
70-
71-
[LoadColumn(1)]
72-
public string Label;
73-
74-
public static IEnumerable<ImageNetData> ReadFromCsv(string file, string folder)
75-
{
76-
return File.ReadAllLines(file)
77-
.Select(x => x.Split('\t'))
78-
.Select(x => new ImageNetData()
79-
{
80-
ImagePath = Path.Combine(folder,x[0]),
81-
Label = x[1],
82-
});
83-
}
76+
return File.ReadAllLines(file)
77+
.Select(x => x.Split('\t'))
78+
.Select(x => new ImageNetData()
79+
{
80+
ImagePath = Path.Combine(folder, x[0]),
81+
Label = x[1],
82+
});
8483
}
84+
}
8585
```
8686
The first step is to load the data using TextLoader
8787

@@ -102,16 +102,27 @@ teddy1.jpg teddy bear
102102
```
103103
As you can observe, the file does not have a header row.
104104

105-
The second step is to define the estimator pipeline. Usually, when dealing with deep neural networks, you must adapt the images to the format expected by the network. This is the reason images are resized and then transformed (mainly, pixel values are normalized across all R,G,B channels).
105+
The Inception model has several default parameters you need to pass in.
106106

107107
```csharp
108+
public struct ImageNetSettings
109+
{
110+
public const int imageHeight = 224;
111+
public const int imageWidth = 224;
112+
public const float mean = 117;
113+
public const bool channelsLast = true;
114+
}
115+
```
116+
117+
The second step is to define the estimator pipeline. Usually, when dealing with deep neural networks, you must adapt the images to the format expected by the network. This is the reason images are resized and then transformed (mainly, pixel values are normalized across all R,G,B channels).
118+
119+
```csharp
108120
var pipeline = mlContext.Transforms.LoadImages(outputColumnName: "input", imageFolder: imagesFolder, inputColumnName: nameof(ImageNetData.ImagePath))
109-
.Append(mlContext.Transforms.ResizeImages(outputColumnName: "input", imageWidth: ImageNetSettings.imageWidth, imageHeight: ImageNetSettings.imageHeight, inputColumnName: "input"))
110-
.Append(mlContext.Transforms.ExtractPixels(outputColumnName: "input", interleavePixelColors: ImageNetSettings.channelsLast, offsetImage: ImageNetSettings.mean))
111-
.Append(mlContext.Model.LoadTensorFlowModel(modelLocation).
112-
ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2" },
113-
inputColumnNames: new[] { "input" }, addBatchDimensionInput:true));
114-
121+
.Append(mlContext.Transforms.ResizeImages(outputColumnName: "input", imageWidth: ImageNetSettings.imageWidth, imageHeight: ImageNetSettings.imageHeight, inputColumnName: "input"))
122+
.Append(mlContext.Transforms.ExtractPixels(outputColumnName: "input", interleavePixelColors: ImageNetSettings.channelsLast, offsetImage: ImageNetSettings.mean))
123+
.Append(mlContext.Model.LoadTensorFlowModel(modelLocation)
124+
.ScoreTensorFlowModel(outputColumnNames: new[] { "softmax2" }, inputColumnNames: new[] { "input" },
125+
addBatchDimensionInput:true));
115126
```
116127
You also need to check the neural network, and check the names of the input / output nodes. In order to inspect the model, you can use tools like [Netron](https://github.com/lutzroeder/netron), which is automatically installed with [Visual Studio Tools for AI](https://visualstudio.microsoft.com/downloads/ai-tools-vs/).
117128
These names are used later in the definition of the estimation pipe: in the case of the inception network, the input tensor is named 'input' and the output is named 'softmax2'

0 commit comments

Comments
 (0)