Download scripts/util/util.py from killbill007/backup: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/killbill007/backup/resolve/main/scripts/util/util.py
- Command line
-
hf download hf://killbill007/backup/scripts/util/util.py
-
curl -L -o util.py https://huggingface.co/killbill007/backup/resolve/main/scripts/util/util.py
3.55 kB
| # Copyright (c) Microsoft Corporation. | |
| # Licensed under the MIT License. | |
| from __future__ import print_function | |
| import os | |
| import PIL | |
| import numpy as np | |
| import torch | |
| import torch.nn.functional as F | |
| from PIL import Image | |
| from torchvision.utils import make_grid | |
| def array2image(ndarray): | |
| return PIL.Image.fromarray(np.uint8(ndarray)).convert('RGB') | |
| def tensor_to_ndarray(tensor, nrow=1, padding=0, normalize=True): | |
| grid = make_grid(tensor, nrow, padding, normalize) | |
| return grid.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to("cpu", torch.uint8).numpy() | |
| def irregular_hole_synthesize(image, mask): | |
| img_np = np.array(image).astype("uint8") | |
| mask_np = np.array(mask).astype("uint8") | |
| mask_np = mask_np / 255 | |
| img_new = img_np * (1 - mask_np) + mask_np * 255 | |
| return PIL.Image.fromarray(img_new.astype("uint8")).convert("RGB") | |
| # Converts a Tensor into a Numpy array | |
| # |imtype|: the desired type of the converted numpy array | |
| def tensor2im(image_tensor, imtype=np.uint8, normalize=True): | |
| if isinstance(image_tensor, list): | |
| image_numpy = [] | |
| for i in range(len(image_tensor)): | |
| image_numpy.append(tensor2im(image_tensor[i], imtype, normalize)) | |
| return image_numpy | |
| image_numpy = image_tensor.cpu().float().numpy() | |
| if normalize: | |
| image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 | |
| else: | |
| image_numpy = np.transpose(image_numpy, (1, 2, 0)) * 255.0 | |
| image_numpy = np.clip(image_numpy, 0, 255) | |
| if image_numpy.shape[2] == 1 or image_numpy.shape[2] > 3: | |
| image_numpy = image_numpy[:, :, 0] | |
| return image_numpy.astype(imtype) | |
| # Converts a one-hot tensor into a colorful label map | |
| def tensor2label(label_tensor, n_label, imtype=np.uint8): | |
| if n_label == 0: | |
| return tensor2im(label_tensor, imtype) | |
| label_tensor = label_tensor.cpu().float() | |
| if label_tensor.size()[0] > 1: | |
| label_tensor = label_tensor.max(0, keepdim=True)[1] | |
| label_tensor = Colorize(n_label)(label_tensor) | |
| label_numpy = np.transpose(label_tensor.numpy(), (1, 2, 0)) | |
| return label_numpy.astype(imtype) | |
| def scale_tensor(img_tensor, default_scale=256): | |
| _, _, w, h = img_tensor.shape | |
| if w < h: | |
| ow = default_scale | |
| oh = h / w * default_scale | |
| else: | |
| oh = default_scale | |
| ow = w / h * default_scale | |
| oh = int(round(oh / 16) * 16) | |
| ow = int(round(ow / 16) * 16) | |
| return F.interpolate(img_tensor, [ow, oh], mode="bilinear") | |
| def data_transforms(img, size="full_size", method=Image.BICUBIC): | |
| if size == "full_size": | |
| ow, oh = img.size | |
| h = int(round(oh / 16) * 16) | |
| w = int(round(ow / 16) * 16) | |
| if (h == oh) and (w == ow): | |
| return img | |
| return img.resize((w, h), method) | |
| elif size == "scale_256": | |
| ow, oh = img.size | |
| pw, ph = ow, oh | |
| if ow < oh: | |
| ow = 256 | |
| oh = ph / pw * 256 | |
| else: | |
| oh = 256 | |
| ow = pw / ph * 256 | |
| h = int(round(oh / 16) * 16) | |
| w = int(round(ow / 16) * 16) | |
| if (h == ph) and (w == pw): | |
| return img | |
| return img.resize((w, h), method) | |
| def save_image(image_numpy, image_path): | |
| image_pil = Image.fromarray(image_numpy) | |
| image_pil.save(image_path) | |
| def mkdirs(paths): | |
| if isinstance(paths, list) and not isinstance(paths, str): | |
| for path in paths: | |
| mkdir(path) | |
| else: | |
| mkdir(paths) | |
| def mkdir(path): | |
| if not os.path.exists(path): | |
| os.makedirs(path) | |