keras_lr_finder icon indicating copy to clipboard operation
keras_lr_finder copied to clipboard

Unsafe save_weights/load_weights method

Open Astlaan opened this issue 4 years ago • 4 comments

In LRFinder.find() and LRFinder.find_generator(), there is a call to the following functions:

self.model.save_weights('tmp.h5')
self.model.load_weights('tmp.h5')

This is unsafe: In the case where several python processes running LRFinder in parallel, they will all attempt to access the same file, mixing weights between processess...

A random file name should instead be generated every time, and it should be checked whether this random file name already exists or not. It should be noted that, one should prevent different processes from generating the same random file name if they are executed at the same time (this can be a problem if random uses datetime as seed).

Astlaan avatar Jun 22 '20 18:06 Astlaan

A NamedTemporaryFile is probably best in this case:

https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile

JonnoFTW avatar Jun 23 '20 00:06 JonnoFTW

A NamedTemporaryFile is probably best in this case:

https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile

Wow that is cool ahah.

Astlaan avatar Jun 23 '20 09:06 Astlaan

Good point! Would you mind to create a PR?

surmenok avatar Jun 25 '20 16:06 surmenok

Good point! Would you mind to create a PR?

I actually just fixed it in my local clone by saving the weights in memory instead of on disk.

According to this, even a 50-layer ResNet uses around ~168MB, so I suppose keeping them in memory will be just fine for most cases? The weights are saved and loaded immediately after, the lr search anyway.

In this case, I made a pull request #28.

Astlaan avatar Jul 04 '20 17:07 Astlaan