d8ahazard / sd_dreambooth_extension

Other
1.85k stars 281 forks source link

Update disable_safe_unpickle/enable_safe_unpickle to respect the initial state of the flag #1359

Closed lmagder closed 8 months ago

lmagder commented 9 months ago

Describe your changes

I noticed some issues when passing --disable-safe-unpickle on the command line where the plugin would accidentally disable the flag attempting to restore its previous state. This version allows nested disable/enables by ref-counting the disable count.

I also added wrapper to use the Python with syntax instead of calling enable/disable. This has the advantage of being exception safe and also makes it impossible to forget to call enable after disabling it. I could revert this part of the change though if it's not something you want. However I did notice one missing enable in train_dreambooth.py

The large changes in this PR are due to indenting existing code to be inside with blocks. I didn't actually change the code although the GitHub diff view seems a little confused. The only actual new code is in model_utils.py

Issue ticket number and link (if applicable)

Checklist before requesting a review