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 numpy as np | |
| import cv2 | |
| def resize_and_pad(image, box): | |
| '''Fitting an image to the box region while keeping the aspect ratio.''' | |
| y1,y2,x1,x2 = box | |
| H,W = y2-y1, x2-x1 | |
| h,w = image.shape[0], image.shape[1] | |
| r_box = W / H | |
| r_image = w / h | |
| if r_box >= r_image: | |
| h_target = H | |
| w_target = int(w * H / h) | |
| image = cv2.resize(image, (w_target, h_target)) | |
| w1 = (W - w_target) // 2 | |
| w2 = W - w_target - w1 | |
| pad_param = ((0,0),(w1,w2),(0,0)) | |
| image = np.pad(image, pad_param, 'constant', constant_values=255) | |
| else: | |
| w_target = W | |
| h_target = int(h * W / w) | |
| image = cv2.resize(image, (w_target, h_target)) | |
| h1 = (H-h_target) // 2 | |
| h2 = H - h_target - h1 | |
| pad_param =((h1,h2),(0,0),(0,0)) | |
| image = np.pad(image, pad_param, 'constant', constant_values=255) | |
| return image | |
| def expand_image_mask(image, mask, ratio=1.4): | |
| h,w = image.shape[0], image.shape[1] | |
| H,W = int(h * ratio), int(w * ratio) | |
| h1 = int((H - h) // 2) | |
| h2 = H - h - h1 | |
| w1 = int((W -w) // 2) | |
| w2 = W -w - w1 | |
| pad_param_image = ((h1,h2),(w1,w2),(0,0)) | |
| pad_param_mask = ((h1,h2),(w1,w2)) | |
| image = np.pad(image, pad_param_image, 'constant', constant_values=255) | |
| mask = np.pad(mask, pad_param_mask, 'constant', constant_values=0) | |
| return image, mask | |
| def expand_image(image, ratio=1.4): | |
| h,w = image.shape[0], image.shape[1] | |
| H,W = int(h * ratio), int(w * ratio) | |
| h1 = int((H - h) // 2) | |
| h2 = H - h - h1 | |
| w1 = int((W -w) // 2) | |
| w2 = W -w - w1 | |
| pad_param_image = ((h1,h2),(w1,w2),(0,0)) | |
| image = np.pad(image, pad_param_image, 'constant', constant_values=255) | |
| return image | |
| def expand_bbox(mask,yyxx,ratio=[1.2,2.0], min_crop=0): | |
| y1,y2,x1,x2 = yyxx | |
| ratio = np.random.randint( ratio[0] * 10, ratio[1] * 10 ) / 10 | |
| H,W = mask.shape[0], mask.shape[1] | |
| xc, yc = 0.5 * (x1 + x2), 0.5 * (y1 + y2) | |
| h = ratio * (y2-y1+1) | |
| w = ratio * (x2-x1+1) | |
| h = max(h,min_crop) | |
| w = max(w,min_crop) | |
| x1 = int(xc - w * 0.5) | |
| x2 = int(xc + w * 0.5) | |
| y1 = int(yc - h * 0.5) | |
| y2 = int(yc + h * 0.5) | |
| x1 = max(0,x1) | |
| x2 = min(W,x2) | |
| y1 = max(0,y1) | |
| y2 = min(H,y2) | |
| return (y1,y2,x1,x2) | |
| def box2squre(image, box): | |
| H,W = image.shape[0], image.shape[1] | |
| y1,y2,x1,x2 = box | |
| cx = (x1 + x2) // 2 | |
| cy = (y1 + y2) // 2 | |
| h,w = y2-y1, x2-x1 | |
| if h >= w: | |
| x1 = cx - h//2 | |
| x2 = cx + h//2 | |
| else: | |
| y1 = cy - w//2 | |
| y2 = cy + w//2 | |
| x1 = max(0,x1) | |
| x2 = min(W,x2) | |
| y1 = max(0,y1) | |
| y2 = min(H,y2) | |
| return (y1,y2,x1,x2) | |
| def pad_to_square(image, pad_value = 255, random = False): | |
| H,W = image.shape[0], image.shape[1] | |
| if H == W: | |
| return image | |
| padd = abs(H - W) | |
| if random: | |
| padd_1 = int(np.random.randint(0,padd)) | |
| else: | |
| padd_1 = int(padd / 2) | |
| padd_2 = padd - padd_1 | |
| if H > W: | |
| pad_param = ((0,0),(padd_1,padd_2),(0,0)) | |
| else: | |
| pad_param = ((padd_1,padd_2),(0,0),(0,0)) | |
| image = np.pad(image, pad_param, 'constant', constant_values=pad_value) | |
| return image | |
| def get_bbox_from_mask(mask): | |
| h,w = mask.shape[0],mask.shape[1] | |
| if mask.sum() < 10: | |
| return 0, h, 0, w | |
| rows = np.any(mask, axis=1) | |
| cols = np.any(mask, axis=0) | |
| y1,y2 = np.where(rows)[0][[0, -1]] | |
| x1,x2 = np.where(cols)[0][[0, -1]] | |
| return (y1, y2, x1, x2) | |
| def box_in_box(small_box, big_box): | |
| y1, y2, x1, x2 = small_box | |
| y1_b, _, x1_b, _ = big_box | |
| y1, y2, x1, x2 = y1 - y1_b ,y2 - y1_b, x1 - x1_b, x2 - x1_b | |
| return (y1, y2, x1, x2) | |
| def crop_back(pred, tar_image, extra_sizes, tar_box_yyxx_crop, tar_box_yyxx_crop2, is_masked=False): | |
| H1, W1, H2, W2 = extra_sizes | |
| y1, x1, y2, x2 = tar_box_yyxx_crop | |
| y1_, x1_, y2_, x2_ = tar_box_yyxx_crop2 | |
| m = 0 # maigin_pixel | |
| if H1 < W1: | |
| pad1 = int((W1 - H1) / 2) | |
| pad2 = W1 - H1 - pad1 | |
| pred = pred[pad1: -pad2, :, :] | |
| elif H1 > W1: | |
| pad1 = int((H1 - W1) / 2) | |
| pad2 = H1 - W1 - pad1 | |
| pred = pred[:,pad1: -pad2, :] | |
| if is_masked: | |
| gen_image = tar_image.copy() | |
| gen_image[y1+m :y2-m, x1+m:x2-m, :] = pred[y1+m :y2-m, x1+m:x2-m, :] | |
| gen_image[y1_+m :y2_-m, x1_+m:x2_-m, :] = pred[y1_+m :y2_-m, x1_+m:x2_-m, :] | |
| else: | |
| gen_image = pred | |
| return gen_image |