-
Notifications
You must be signed in to change notification settings - Fork 1k
Image2image - python #115
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
Image2image - python #115
Conversation
and later for in-paining
This reverts commit 270afe1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the amazing effort @littleowl, we are almost there!
- I ran this commit on a fresh install and it requires a one-liner fix from the main branch to work with the latest diffusers release so please rebase one last time to get that in.
- The generated model has ~56dB PSNR which is pretty good
- I requested minor changes such as removing device placement code to improve readability, moving from camelCase to underscore in I/O names for consistency and some typos.
- Regarding the generation of noise in-network versus out-of-network. The solution to
ValueError: Torch op randn already registered
is to prepend the op registration by:
from coremltools.converters.mil.frontend.torch.torch_op_registry import _TORCH_OPS_REGISTRY
if 'randn' in _TORCH_OPS_REGISTRY:
del _TORCH_OPS_REGISTRY['randn']
but it isn't too important as it turns out that making the random seed an actual network input would have required some more work. I am convinced that generating the noise tensor in Swift sounds like the right way to go at the moment since @msiracusa wrote an RNG to match NumPy RNG behavior.
… into image2image-python
@atiorh thank you for your review and insights. I believe I have resolved all of your comments and I've tested once more with the swift code with the name changes. Let me know if there is anything else! |
Thanks for the amazing work @littleowl ! |
Adds image2image functionality.
This is only the python portion needed to generate the VAE Encoder.
Swift
Original PR
In Python, a new CoreML model can be generated to encode the latent space for image2image. The model bakes in some of the operations typically performed in the pipeline so that a separate model would not need to be created for those operations, nor would the CPU be needed to perform the tensor multiplications. Some of the simpler math involving the scheduler's time steps are performed on the cpu and passed into the encoder. The encoder works around torch.randn missing operation by passing in nose tensors to apply to the image latent space.
Initial feedback from the initial PR #73 was to separate the python portion of the PR. Additionally, it was asked if we could implement the methodology to override the
randn
PyTorch function as instructions can be found how to do so in coremltools documentation. Initially, I had no qualms digging into this task to see how we could simplify the interface. However, when investigating the issue, I had discovered that I had already tried these steps initially when I initially had encountered the missingrandn
function. I had actually implemented the suggestion from @atiorh to encounter other errors:The initial error:
Error with
randn
override:Furthermore, I cannot say that I actually agree that it is a good idea to generate the random numbers in python anyways. The noise for the latent space generation for regular prompt based generation is generated in Swift on the CPU. It could be that some techniques might need to use the same seed. For instance, there is experimentation with interoperable translation between two latent spaces. For reasons such as this, I would weigh on the side of generating the noise from swift.
That said, in the commit history of this branch, you can see where I tried to do this a second time. Where I tried importing the original DiagonalGaussianDistribution function and overriding the
randn
function.However, this solution copies the parts of the
DiagonalGaussianDistribution
class needed to aCoreMLDiagonalGaussianDistribution
. Furthermore it optimizes the other operations that happen just after the VAEEncoder in the Diffusion library. Otherwise we would need yet another model, or math library to perform these routines.Do not erase the below when submitting your pull request:
#########