dotnet/machinelearning · error · ArgumentException
Directory "{0}" does not exist.
Error message
Directory "{0}" does not exist. What it means
The ImageLoadingTransformer constructor validates the optional imageFolder argument before storing it. If the folder string is non-null but no directory exists at that path on disk, it throws an ArgumentException immediately at transform-construction time rather than during data loading.
Source
Thrown at src/Microsoft.ML.ImageAnalytics/ImageLoader.cs:114
/// <summary>
/// Initializes a new instance of <see cref="ImageLoadingTransformer"/>.
/// </summary>
/// <param name="env">The host environment.</param>
/// <param name="imageFolder">Folder where to look for images.</param>
/// <param name="type">Image type flag - true for ImageDataViewType or false for VectorDataViewType. Defaults to true i.e. ImageDataViewType if not specified.</param>
/// <param name="columns">Names of input and output columns.</param>
internal ImageLoadingTransformer(IHostEnvironment env, string imageFolder = null, bool type = true, params (string outputColumnName, string inputColumnName)[] columns)
: base(Contracts.CheckRef(env, nameof(env)).Register(nameof(ImageLoadingTransformer)), columns)
{
// Throws ArgumentException if given imageFolder path is invalid. Note: imageFolder may be null or empty in this case.
if (String.IsNullOrEmpty(imageFolder))
ImageFolder = null;
else
{
if (Directory.Exists(imageFolder))
ImageFolder = Path.GetFullPath(imageFolder);
else
throw new ArgumentException(String.Format("Directory \"{0}\" does not exist.", imageFolder));
}
_useImageType = type;
}
// Factory method for SignatureDataTransform.
internal static IDataTransform Create(IHostEnvironment env, Options options, IDataView data)
{
return new ImageLoadingTransformer(env, options.ImageFolder, options.Columns.Select(x => (x.Name, x.Source ?? x.Name)).ToArray())
.MakeDataTransform(data);
}
// Factory method for SignatureLoadModel.
private static ImageLoadingTransformer Create(IHostEnvironment env, ModelLoadContext ctx)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(ctx, nameof(ctx));
ctx.CheckAtModel(GetVersionInfo());View on GitHub (pinned to 7b76e69cf9)
Solutions
- Correct the path to an existing directory (use an absolute path)
- Call Directory.Exists(imageFolder) before constructing the transformer to fail fast with a clearer message
- If the folder is optional in your pipeline, pass null instead of an empty/missing string
- Ensure the directory is deployed/present in the runtime environment and the process has permissions to see it
Example fix
// before
var transformer = new ImageLoadingTransformer(env, "images", "ImagePath");
// after
string imageFolder = Path.Combine(AppContext.BaseDirectory, "images");
if (!Directory.Exists(imageFolder))
throw new DirectoryNotFoundException($"Image folder missing: {imageFolder}");
var transformer = new ImageLoadingTransformer(env, imageFolder, "ImagePath"); Defensive patterns
Strategy: validation
Validate before calling
if (imageFolder != null && !Directory.Exists(imageFolder))
throw new DirectoryNotFoundException($"Image folder does not exist: {imageFolder}"); Type guard
bool IsValidImageFolder(string path) => path == null || (Directory.Exists(path) && (File.GetAttributes(path) & FileAttributes.Directory) != 0);
Try / catch
try
{
var transformer = new ImageLoadingTransformer(env, imageFolder, nameColumn);
}
catch (ArgumentException ex) when (ex.Message.Contains("does not exist"))
{
logger.LogError(ex, "Configured image folder missing: {Folder}", imageFolder);
} Prevention
- Store absolute paths in configuration
- Resolve relative paths against a known base directory, not the process CWD
- Check Directory.Exists during app startup / health checks
- Verify path handling when moving configs between Windows and Linux
When it happens
Trigger: Passing a non-null imageFolder string to the ImageLoadingTransformer constructor (or the ImageLoadingEstimator options) where Directory.Exists(imageFolder) is false — typo'd path, relative path resolved from the wrong working directory, deleted/moved folder, or case/drive mismatch.
Common situations: Config files holding paths from another machine (Windows vs Linux path styles), running an app whose current working directory differs from where relative image paths were authored, deployment environments where the image folder was not copied.
Related errors
- Either input_ids or inputs_embeds must be set
- Dimension must be divisible by 2
- File {path} too big to open.
- Unsupported pixel format
- Invalid width value.
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/8eb65005263897f5.
Report an issue: GitHub.