Can't pickle local object 'dataset.__init__.<locals>.identify_file

I am trying to train a yoloV2 model. As part of this, I am using a custom dataset class that defines a identify_file function and acts as a loader.

Then, during training, I need to enumerate(dataset) but Python raises the error :
Can't pickle local object 'dataset.__init__.<locals>.identify_file

I checked, this is not due to lambda function (and therefore this answer) since identify_file is defined the following way :

def identify_file(img_id):
            root = Path(r"some\root\Path")
            return Path.joinpath(root, img_id + '.jpg')

Any idea what could be going on ? I am on Windows.

Ok, I found the solution.

The function, in order to be pickable, needs to be defined at the top of a module which was not the case in my program.