Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
giuseppe-terrasi opened this issue Dec 18, 2019 · 10 comments · Fixed by #4994
Assignees
Labels
P1 Priority of the issue for triage purpose: Needs to be fixed soon.

Comments

@giuseppe-terrasi
Copy link

giuseppe-terrasi commented Dec 18, 2019

System information

  • OS version/distro: Windows 10 Pro 10.0.18363
  • .NET Version (eg., dotnet --info): 3.1.0

Issue

  • What did you do?: I've tried to delete an image file used by PredictionEngine after disposing the engine
  • What happened?: An exception was thorwn: "The process cannot access the file because it is being used by another process."
  • What did you expect?: Image file deletion

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
Copy link
Author

Are there any updates on this issue?

@ganik ganik self-assigned this Jan 9, 2020
@ganik
Copy link
Member

ganik commented Jan 9, 2020

I ll take a look, thx

@harishsk harishsk added the P1 Priority of the issue for triage purpose: Needs to be fixed soon. label Jan 9, 2020
@ganik
Copy link
Member

ganik commented Jan 17, 2020

@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
Copy link
Author

@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
Copy link
Author

giuseppe-terrasi commented Jan 29, 2020

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
Copy link

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
Copy link
Author

@ganik Still no updates?

@konabuta
Copy link

konabuta commented Feb 28, 2020

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

@konabuta
Copy link

@ganik No updates on this ?

@ganik
Copy link
Member

ganik commented Mar 31, 2020

@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.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P1 Priority of the issue for triage purpose: Needs to be fixed soon.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants