Diffusers
English
stable-diffusion
stable-diffusion-diffusers
inpainting
art
artistic
anime
absolute-realism
Instructions to use diffusers/tools with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use diffusers/tools with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("diffusers/tools", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Commit ·
2acb9f2
1
Parent(s): 269cbe7
up
Browse filesFiles changed (9)
1 unmodified line2345671 unmodified linetouch all_branches.txtwhile read p; doecho "-------------------------------" >> all_branches.txtpython check_for_branches.py --model_id ${p} >> all_branches.txtdone <model_ids.txt1 unmodified line234561 unmodified linetouch all_branches.txtwhile read p; dopython check_for_branches.py --model_id ${p} >> all_branches.txtdone <model_ids.txt
1 unmodified line234561 unmodified linewhile read p; doecho "-------------------------------"echo "Open PR for $p"python convert_flax_to_pt.py $pdone <model_ids.txt1 unmodified line234561 unmodified linewhile read p; doecho "-------------------------------"echo "Open PR for $p"python open_pr_version.py $pdone <all_branches.txt
4 unmodified lines567891033 unmodified lines44454647484950515253545556575859606162636465666728 unmodified lines969798991001011021031041051061071081091101111121137 unmodified lines1211221231241251261271281291301311321331341351361371381391401411421431441451461474 unmodified linesimport shutilfrom tempfile import TemporaryDirectoryfrom typing import List, Optionalfrom huggingface_hub import CommitInfo, CommitOperationAdd, Discussion, HfApi, hf_hub_downloadfrom huggingface_hub.file_download import repo_folder_name33 unmodified linesdef convert_single(model_id: str, folder: str) -> List["CommitOperationAdd"]:pipe = DiffusionPipeline.from_pretrained(model_id)try:pipe.to(torch_dtype=torch.float16)pipe.save_pretrained(model_id, variant="fp16")pipe.save_pretrained(model_id, variant="fp16", safe_serialization=True)import ipdb; ipdb.set_trace()operations = [CommitOperationAdd(path_in_repo=config_file, path_or_fileobj=new_config_file)]except:import ipdb; ipdb.set_trace()if success:model_type = successreturn operations, model_typeelse:return False, Falsedef convert_file(old_config: str,28 unmodified linesdef convert(api: "HfApi", model_id: str, force: bool = False) -> Optional["CommitInfo"]:# pr_title = "Correct `sample_size` of {}'s unet to have correct width and height default"pr_title = "Fix deprecation warning by changing `CLIPFeatureExtractor` to `CLIPImageProcessor`."info = api.model_info(model_id)filenames = set(s.rfilename for s in info.siblings)if "model_index.json" not in filenames:print(f"Model: {model_id} has no model_index.json file to change")return# if "vae/config.json" not in filenames:# print(f"Model: {model_id} has no 'vae/config.json' file to change")# returnwith TemporaryDirectory() as d:folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))7 unmodified linesnew_pr = prraise AlreadyExists(f"Model {model_id} already has an open PR check out {url}")else:operations, model_type = convert_single(model_id, folder)if operations:pr_title = pr_title.format(model_type)# if model_type == "Stable Diffusion 1":# sample_size = 64# image_size = 512# elif model_type == "Stable Diffusion 2":# sample_size = 96# image_size = 768# pr_description = (# f"Since `diffusers==0.9.0` the width and height is automatically inferred from the `sample_size` attribute of your unet's config. It seems like your diffusion model has the same architecture as {model_type} which means that when using this model, by default an image size of {image_size}x{image_size} should be generated. This in turn means the unet's sample size should be **{sample_size}**. \n\n In order to suppress to update your configuration on the fly and to suppress the deprecation warning added in this PR: https://github.com/huggingface/diffusers/pull/1406/files#r1035703505 it is strongly recommended to merge this PR."# )contributor = model_id.split("/")[0]pr_description = (f"Hey {contributor} 👋, \n\n Your model repository seems to contain logic to load a feature extractor that is deprecated, which you should notice by seeing the warning: ""\n\n ```\ntransformers/models/clip/feature_extraction_clip.py:28: FutureWarning: The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. "f"Please use CLIPImageProcessor instead. warnings.warn(\n``` \n\n when running `pipe = DiffusionPipeline.from_pretrained({model_id})`.""This PR makes sure that the warning does not show anymore by replacing `CLIPFeatureExtractor` with `CLIPImageProcessor`. This will certainly not change or break your checkpoint, but only""make sure that everything is up to date. \n\n Best, the 🧨 Diffusers team.")new_pr = api.create_commit(repo_id=model_id,4 unmodified lines56789101133 unmodified lines45464748495051525354555657585960616263646566676869707128 unmodified lines1001011021031041051067 unmodified lines1141151161171181191201211221231241251261271281291301311321331341354 unmodified linesimport shutilfrom tempfile import TemporaryDirectoryfrom typing import List, Optionalfrom diffusers import DiffusionPipelinefrom huggingface_hub import CommitInfo, CommitOperationAdd, Discussion, HfApi, hf_hub_downloadfrom huggingface_hub.file_download import repo_folder_name33 unmodified linesdef convert_single(model_id: str, folder: str) -> List["CommitOperationAdd"]:pipe = DiffusionPipeline.from_pretrained(model_id, cache_dir="/home/patrick/cache_to_delete")try:pipe.to(torch_dtype=torch.float16)pipe.save_pretrained(folder, variant="fp16")pipe.save_pretrained(folder, variant="fp16", safe_serialization=True)all_files = []def find_files_in_dir(directory):for root, dirs, files in os.walk(directory):for file in files:all_files.append(os.path.join(root, file))find_files_in_dir(folder)files = [f for f in all_files if ".fp16." in f]operations = [CommitOperationAdd(path_in_repo='/'.join(f.split("/")[-2:]), path_or_fileobj=f) for f in files]return operationsexcept Exception as e:print(e)return Falsedef convert_file(old_config: str,28 unmodified linesdef convert(api: "HfApi", model_id: str, force: bool = False) -> Optional["CommitInfo"]:pr_title = "Fix deprecated float16/fp16 variant loading through new `version` API."with TemporaryDirectory() as d:folder = os.path.join(d, repo_folder_name(repo_id=model_id, repo_type="models"))7 unmodified linesnew_pr = prraise AlreadyExists(f"Model {model_id} already has an open PR check out {url}")else:operations = convert_single(model_id, folder)if operations:contributor = model_id.split("/")[0]pr_description = (f"Hey {contributor} 👋, \n\n Your model repository seems to contain a [`fp16` branch](https://huggingface.co/{model_id}/tree/fp16) to load the model in float16 precision. ""Loading `fp16` versions from a branch instead of the main branch is deprecated and will eventually be forbidden. ""Instead, we strongly recommend to save `fp16` versions of the model under `.fp16.` version files directly on the 'main' branch as enabled through this PR."f"This PR makes sure that your model repository allows the user to correctly download float16 precision model weights by adding `fp16` model weights in both safetensors and PyTorch bin format:""\n\n""```py\n"f"pipe = DiffusionPipeline.from_pretrained({model_id}, torch_dtype=torch.float16, variant='fp16')""\n```""\n\n""For more information please have a look at: https://huggingface.co/docs/diffusers/using-diffusers/loading#checkpoint-variants.""\nWe made sure you that you can safely merge this pull request. \n\n Best, the 🧨 Diffusers team.")new_pr = api.create_commit(repo_id=model_id,
12345678910111213141516171819202122232425262728293031323334353637383940414243444546#!/usr/bin/env python3from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMSchedulerimport timeimport osfrom huggingface_hub import HfApi# from compel import Compelimport torchimport sysfrom pathlib import Pathimport requestsfrom PIL import Imagefrom io import BytesIO
path = sys.argv[1]
api = HfApi()start_time = time.time()pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)# pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)# pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None
# compel = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)
pipe = pipe.to("cuda")
prompt = "Elon Musk riding a green horse on Mars"
# pipe.unet.to(memory_format=torch.channels_last)pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)pipe(prompt=prompt, num_inference_steps=2).images[0]
image = pipe(prompt=prompt).images[0]
file_name = f"aaa"path = os.path.join(Path.home(), "images", f"{file_name}.png")image.save(path)
api.upload_file( path_or_fileobj=path, path_in_repo=path.split("/")[-1], repo_id="patrickvonplaten/images", repo_type="dataset",)print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")12345678910111213141516171819202122232425262728293031323334353637383940414243444546#!/usr/bin/env python3from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMSchedulerimport timeimport osfrom huggingface_hub import HfApi# from compel import Compelimport torchimport sysfrom pathlib import Pathimport requestsfrom PIL import Imagefrom io import BytesIO
path = sys.argv[1]
api = HfApi()start_time = time.time()pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config)# pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)# pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None
# compel = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)
pipe = pipe.to("cuda")
prompt = "Elon Musk riding a green horse on Mars"
# pipe.unet.to(memory_format=torch.channels_last)pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)pipe(prompt=prompt, num_inference_steps=2).images[0]
image = pipe(prompt=prompt).images[0]
file_name = f"aaa"path = os.path.join(Path.home(), "images", f"{file_name}.png")image.save(path)
api.upload_file( path_or_fileobj=path, path_in_repo=path.split("/")[-1], repo_id="patrickvonplaten/images", repo_type="dataset",)print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")123456789101112#!/usr/bin/env python3import torchfrom diffusers import DiffusionPipeline, DPMSolverMultistepSchedulerfrom diffusers.utils import export_to_video
pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16)pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)pipe.enable_model_cpu_offload()
prompt = "Darth Vader is surfing on waves"video_frames = pipe(prompt, num_inference_steps=40, height=320, width=576, num_frames=24).framesvideo_path = export_to_video(video_frames, output_video_path="/home/patrick/videos/video_10.mp4")all_branches.txt
log.txt
mass_branches_retrieval.sh-1
mass_open_controlnet_pr.sh → mass_fp16_branch_pr.shRenamed+2-2
model_ids.txt
open_pr_version.py+32-44
run_local_img2img_xl.pyAdded+46
run_local_xl.pyAdded+46
run_video.pyAdded+12