Skip to content

realesrgan on M1/M2 Macs #390

Description

@bakkot

I moved this comment by @eduardoarinopelegrin from here. Original comment follows.


Kind of unrelated, but also kind of related (b/c it involves simplet2ti.py).
Has anyone got realesrgan working on M1/M2 Macs with this repo? If not, are there any plans or works in progress?

I just got it to work with the realesrgan-ncnn-vulkan executable + updating the appropriate parts of simplet2ti.py. So if there were no plans to implement / not in the near future, maybe you could include this hack for us M1/M2 Mac users to get it to work.

Steps are:

  1. Download the MacOS executable from https://github.com/xinntao/Real-ESRGAN/releases
  2. Unzip it (you'll get realesrgan-ncnn-vulkan-20220424-macos) and move realesrgan-ncnn-vulkan inside stable-diffusion (this project folder). Move the Real-ESRGAN model files from realesrgan-ncnn-vulkan-20220424-macos/models into stable-diffusion/models as well.
  3. Run chmod u+x realesrgan-ncnn-vulkan to allow it to be run.
  4. Update simplet2ti.py to use the executable if self.device.type == 'mps'
    The changes are:

import subprocess

Add a function to do the upscaling

def esrgan_resize(self, input):
    input.save('esrgan_in.png')
    input.save('esrgan_out.png')
    input.close()
    try:
        subprocess.run(
            ['./realesrgan-ncnn-vulkan', '-i', 'esrgan_in.png', '-o', 'esrgan_out.png'],
            stdout=subprocess.PIPE
        ).stdout.decode('utf-8')
        output = Image.open('esrgan_out.png').convert('RGBA')
        return output
    except Exception as e:
        print('ESRGAN resize failed.')
        print(e)
        quit()

Modify this part to call the function

if upscale is not None:
                                if self.device.type == 'mps':
                                    image = self.esrgan_resize(image)
                                else:
                                    # current code to upscale

This is the full file. This may be more appropriate as a pull request, but since I've never done one (so far!) and simplet2ti.py is being modified (from the version I implemented my changes on), maybe it's easier to add it to your version if you see it fit.
simplet2i.py.zip

Usage:
python3 ./scripts/dream.py
dream > Anubis the Ancient Egyptian God of Death riding a motorbike in Grand Theft Auto V cover, with palm trees in the background, cover art by Stephen Bliss, artstation, high quality -U 4 -m ddim -S 1469565

Obtained/ expected result (4x upscale):
000143 1469565

Usage stats:
1 image(s) generated in 32.73s
Outputs:
outputs/img-samples/000144.1469565.png: "Anubis the Ancient Egyptian God of Death riding a motorbike in Grand Theft Auto V cover, with palm trees in the background, cover art by Stephen Bliss, artstation, high quality" -s50 -W512 -H512 -C7.5 -Addim -U 4.0 -F -S1469565

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions