Skip to content

No LoadData method for image classification  #2440

Open

Description

@beccamc

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

image

@beccamc to provide code for this scenario.

Activity

  1. changed the title [-]No LoadData method for Image[/-] [+]No LoadData method for image classification [/+] on Jan 11, 2023
  2. zewditu commented on Jan 12, 2023

    @zewditu
    Contributor

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

  3. beccamc commented on Jan 12, 2023

    @beccamc
    ContributorAuthor

    Nope, I need to provide the source code!

  4. added this to the February 2023 milestone on Jan 12, 2023
  5. LittleLittleCloud commented on Mar 23, 2023

    @LittleLittleCloud
    Member

    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);
    }
  6. zewditu commented on Mar 24, 2023

    @zewditu
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions