dotnet / machinelearning-modelbuilder

Simple UI tool to build custom machine learning models.
Creative Commons Attribution 4.0 International
265 stars 56 forks source link

No LoadData method for image classification #2440

Open beccamc opened 1 year ago

beccamc commented 1 year ago

Image classification doesn't have a sample load method for IDataView.

image

@beccamc to provide code for this scenario.

zewditu commented 1 year ago

Does the image has a loadData Method? does this include the template or discuss load method for image

beccamc commented 1 year ago

Nope, I need to provide the source code!

LittleLittleCloud commented 1 year ago

Here's an example of loading images from folder.

Example for local image classification

public static IDataView LoadImagesFromFolder(MLContext context, string folder)
{
    var res = new List<ModelInput>();
    var allowedImageExtensions = new[] { ".png", ".jpg", ".jpeg", ".gif" };
    DirectoryInfo rootDirectoryInfo = new DirectoryInfo(folder);
    DirectoryInfo[] subDirectories = rootDirectoryInfo.GetDirectories();

    if (subDirectories.Length == 0)
    {
        throw new Exception("fail to find subdirectories");
    }

    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),
            }));
        }
    }

    return context.Data.LoadFromEnumerable(res);
}

## Example for azure image classification

```csharp
public static IDataView LoadImagesFromFolder(MLContext context, string folder)
{
    var res = new List<ModelInput>();
    var allowedImageExtensions = new[] { ".png", ".jpg", ".jpeg", ".gif" };
    DirectoryInfo rootDirectoryInfo = new DirectoryInfo(folder);
    DirectoryInfo[] subDirectories = rootDirectoryInfo.GetDirectories();

    if (subDirectories.Length == 0)
    {
        throw new Exception("fail to find subdirectories");
    }

    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 = MLImage.LoadFromFile(i.FullName), // this is the only difference comparing to local image training
            }));
        }
    }

    return context.Data.LoadFromEnumerable(res);
}
zewditu commented 1 year ago

for Azure, it should be CreateFromFile instead of LoadFromFile. Is that right ?@LittleLittleCloud