Download dataset/utils.py from MultiMatrix/DiffBIR: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/MultiMatrix/DiffBIR/resolve/4bef05bc39a7dbcb658ac58972403e092fbb262c/dataset/utils.py
- Command line
-
hf download hf://MultiMatrix/DiffBIR@4bef05bc39a7dbcb658ac58972403e092fbb262c/dataset/utils.py
-
curl -L -o utils.py https://huggingface.co/MultiMatrix/DiffBIR/resolve/4bef05bc39a7dbcb658ac58972403e092fbb262c/dataset/utils.py
2.42 kB
| from typing import List, Dict | |
| import random | |
| import math | |
| import numpy as np | |
| from PIL import Image | |
| import cv2 | |
| def load_file_list(file_list_path: str) -> List[Dict[str, str]]: | |
| files = [] | |
| with open(file_list_path, "r") as fin: | |
| for line in fin: | |
| path = line.strip() | |
| if path: | |
| files.append({"image_path": path, "prompt": ""}) | |
| return files | |
| # https://github.com/openai/guided-diffusion/blob/main/guided_diffusion/image_datasets.py | |
| def center_crop_arr(pil_image, image_size): | |
| # We are not on a new enough PIL to support the `reducing_gap` | |
| # argument, which uses BOX downsampling at powers of two first. | |
| # Thus, we do it by hand to improve downsample quality. | |
| while min(*pil_image.size) >= 2 * image_size: | |
| pil_image = pil_image.resize( | |
| tuple(x // 2 for x in pil_image.size), resample=Image.BOX | |
| ) | |
| scale = image_size / min(*pil_image.size) | |
| pil_image = pil_image.resize( | |
| tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC | |
| ) | |
| arr = np.array(pil_image) | |
| crop_y = (arr.shape[0] - image_size) // 2 | |
| crop_x = (arr.shape[1] - image_size) // 2 | |
| return arr[crop_y : crop_y + image_size, crop_x : crop_x + image_size] | |
| # https://github.com/openai/guided-diffusion/blob/main/guided_diffusion/image_datasets.py | |
| def random_crop_arr(pil_image, image_size, min_crop_frac=0.8, max_crop_frac=1.0): | |
| min_smaller_dim_size = math.ceil(image_size / max_crop_frac) | |
| max_smaller_dim_size = math.ceil(image_size / min_crop_frac) | |
| smaller_dim_size = random.randrange(min_smaller_dim_size, max_smaller_dim_size + 1) | |
| # We are not on a new enough PIL to support the `reducing_gap` | |
| # argument, which uses BOX downsampling at powers of two first. | |
| # Thus, we do it by hand to improve downsample quality. | |
| while min(*pil_image.size) >= 2 * smaller_dim_size: | |
| pil_image = pil_image.resize( | |
| tuple(x // 2 for x in pil_image.size), resample=Image.BOX | |
| ) | |
| scale = smaller_dim_size / min(*pil_image.size) | |
| pil_image = pil_image.resize( | |
| tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC | |
| ) | |
| arr = np.array(pil_image) | |
| crop_y = random.randrange(arr.shape[0] - image_size + 1) | |
| crop_x = random.randrange(arr.shape[1] - image_size + 1) | |
| return arr[crop_y : crop_y + image_size, crop_x : crop_x + image_size] | |