Instructions to use RyanHangZhou/PICS with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use RyanHangZhou/PICS with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("RyanHangZhou/PICS", torch_dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Notebooks
- Google Colab
- Kaggle
| import torch | |
| import numpy as np | |
| def append_dims(x, target_dims): | |
| """Appends dimensions to the end of a tensor until it has target_dims dimensions. | |
| From https://github.com/crowsonkb/k-diffusion/blob/master/k_diffusion/utils.py""" | |
| dims_to_append = target_dims - x.ndim | |
| if dims_to_append < 0: | |
| raise ValueError(f'input has {x.ndim} dims but target_dims is {target_dims}, which is less') | |
| return x[(...,) + (None,) * dims_to_append] | |
| def norm_thresholding(x0, value): | |
| s = append_dims(x0.pow(2).flatten(1).mean(1).sqrt().clamp(min=value), x0.ndim) | |
| return x0 * (value / s) | |
| def spatial_norm_thresholding(x0, value): | |
| # b c h w | |
| s = x0.pow(2).mean(1, keepdim=True).sqrt().clamp(min=value) | |
| return x0 * (value / s) |