dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.05k stars 1.89k forks source link

Cannot delete image file passsed to PredictionEngine for Image Classification after disposing the engine #4585

Closed giuseppe-terrasi closed 4 years ago

giuseppe-terrasi commented 4 years ago

System information

Issue

Source code / logs

private ImagePrediction ClassifySingleImage(MLContext mLContext, ITransformer model, string imagePath)
{
    var imageData = new ImageData()
    {
        ImagePath = imagePath
    };
    var predictor = mLContext.Model.CreatePredictionEngine<ImageData, ImagePrediction>(model);
    var prediction = predictor.Predict(imageData);
    predictor.Dispose();
    System.IO.File.Delete(imagePath);  // throws a FileIO exception
    return prediction;
}
giuseppe-terrasi commented 4 years ago

Are there any updates on this issue?

ganik commented 4 years ago

I ll take a look, thx

ganik commented 4 years ago

@giuseppe-terrasi I am not able to repro this with the latest code from master branch. I checked that image file is loaded within using{ .... } statement before actual prediction happens (in \machinelearning\src\Microsoft.ML.ImageAnalytics\ImageLoader.cs : TryLoadDataIntoBuffer(..) method). Could you try your code with the latest ML.NET from master branch.

giuseppe-terrasi commented 4 years ago

@ganik Sorry for the delay. I created a test solution here. I used both nuget versions 1.4.0 and 1.5.0-preview with the same result. I also tried to compile the source from master branch but I received a compilation error.

giuseppe-terrasi commented 4 years ago

Are there any updates on this issue? Due to it I have a lot of temporary images used by this code that I must delete manually after restarting the process. I can give more information if needed. Thanks.

venkateshkrc commented 4 years ago

This is not a solution to this issue but I faced the similar issue and ended up using ITransformer.Transform as an workaround for the prediction.

Using PredictionEngine is cleaner but ITransfomer does not seem to lock files.

giuseppe-terrasi commented 4 years ago

@ganik Still no updates?

konabuta commented 4 years ago

@ganik My customer also encounters this issue. Any update on this ?

konabuta commented 4 years ago

@ganik No updates on this ?

ganik commented 4 years ago

@giuseppe-terrasi @konabuta Hi, sorry for delay and thank you for repro solution. I was able to find the rootcause in machinelearning\src\Microsoft.ML.ImageAnalytics\ImageLoader.cs, line 238: dst = new Bitmap(path) { Tag = path }; BitMap locks the file. There are various approaches how to handle this as per: https://stackoverflow.com/questions/4803935/free-file-locked-by-new-bitmapfilepath I ll investigate various solutions and will submit a fix shortly. Thx.