Skip to content

Instantly share code, notes, and snippets.

@shellward
Created February 7, 2023 16:06
Show Gist options
  • Save shellward/245639ab1c14bc042ae6720be73a25d2 to your computer and use it in GitHub Desktop.
Save shellward/245639ab1c14bc042ae6720be73a25d2 to your computer and use it in GitHub Desktop.
if not os.path.exists('/content'):
os.mkdir('/content')
from google.colab import drive
drive.mount('content/gdrive')
!pip install transformers accelerate torch
!pip install git+https://github.com/huggingface/diffusers.git
#############################################################
import os
import glob
import torch
from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
from tqdm import tqdm
from IPython.display import display
from PIL import Image
model_id = "timbrooks/instruct-pix2pix"
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None)
pipe.to("cuda")
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
#############################################################
torch.cuda.empty_cache()
import gc
gc.collect()
img_folder = "/content/content/gdrive/MyDrive/ip2p_input_directory"
out_folder = "/content/content/gdrive/MyDrive/ip2p_output_directory"
prompt = "turn them into flowers"
if not os.path.exists(out_folder):
os.mkdir(out_folder)
for img_path in tqdm(glob.glob(os.path.join(img_folder, "*.png"))):
img = Image.open(img_path)
#if image has alpha channel, remove it
if img.mode == "RGBA":
img = img.convert("RGB")
img = pipe(prompt, image=img).images[0]
img.save(os.path.join(out_folder, os.path.basename(img_path)))
display(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment