dotnet / dotnet/machinelearning-modelbuilder
Performance tips for auto-generated files by ML.NET Model Builder
- Dominant language
- Dockerfile
- Stars
- 285
- Forks
- 66
- PR merge metrics
- No merged PRs in 30d
Description
**System Information (please complete the following information):**
- Model Builder Version: 17.15.0.2337001
- Visual Studio Version: 17.8.1
**Describe the bug**
Poorly written code is found in the files `MLModel.training.cs` and `MLModel.consumption.cs`, specifically violations of [CA1851](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1851) redundant checks, and unnecessary logic.
**To Reproduce**
Steps to reproduce the behavior:
1. Create a new project
2. Add a new Machine Learning Model (MLModel)
3. Choose the scenario **Image Classification**
4. Perform necessary actions to progress to **Consume**
5. Add a **Console app**
6. Examine the code generated by the generator in the files `MLModel.training.cs`, `MLModel.consumption.cs`
**Expected behavior**
In the file `MLModel.consumption.cs`, the method `GetSortedScoresWithLabels`:
Instead of:
```C#
var labelNames = GetLabels(result);
Dictionary labledScores = new Dictionary();
for (int i = 0; i < labelNames.Count(); i++)
{
// Map the names to the predicted result score array
var labelName = labelNames.ElementAt(i);
labledScores.Add(labelName.ToString(), unlabeledScores[i]);
}
```
I suggest:
```C#
var labelNames = GetLabels(result);
int i = 0;
using var labelEnumerator = labelNames.GetEnumerator();
Dictionary labledScores = new Dictionary();
while (labelEnumerator.MoveNext())
{
// Map the names to the predicted result score array
var labelName = labelEnumerator.Current;
labledScores.Add(labelName, unlabeledScores[i]);
i++;
}
```
In the file `MLModel.training.cs`, the method `LoadImageFromFolder`:
Instead of:
```C#
foreach (DirectoryInfo directory in subDirectories)
{
var imageList = directory.EnumerateFiles().Where(f => allowedImageExtensions.Contains(f.Extension.ToLower()));
if (imageList.Count() > 0)
{
res.AddRange(imageList.Select(i => new ModelInput
{
Label = directory.Name,
ImageSource = File.ReadAllBytes(i.FullName),
}));
}
}
```
I suggest:
```C#
foreach (DirectoryInfo directory in subDirectories)
{
res.AddRange(directory.EnumerateFiles()
.Where(f => allowedImageExtensions.Contains(f.Extension.ToLower()))
.Select(i => new ModelInput
{
Label = directory.Name,
ImageSource = File.ReadAllBytes(i.FullName),
}));
}
```
**Screenshots**
-
**Additional context**
-
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.