mirror of
https://github.com/Haoming02/sd-webui-old-photo-restoration.git
synced 2026-04-30 19:21:52 +00:00
commit cd7a9c103d1ea981ecd236d4e9111fd3c1cd6c2b Author: Haoming <hmstudy02@gmail.com> Date: Tue Dec 19 11:33:44 2023 +0800 add README commit 30127cbb2a8e5f461c540729dc7ad457f66eb94c Author: Haoming <hmstudy02@gmail.com> Date: Tue Dec 19 11:12:16 2023 +0800 fix Face Enhancement distortion commit 6d52de5368c6cfbd9342465b5238725c186e00b9 Author: Haoming <hmstudy02@gmail.com> Date: Mon Dec 18 18:27:25 2023 +0800 better? args handling commit 0d1938b59eb77a038ee0a91a66b07fb9d7b3d6d4 Author: Haoming <hmstudy02@gmail.com> Date: Mon Dec 18 17:40:19 2023 +0800 bug fix related to Scratch commit 8315cd05ffeb2d651b4c57d70bf04b413ca8901d Author: Haoming <hmstudy02@gmail.com> Date: Mon Dec 18 17:24:52 2023 +0800 implement step 2 ~ 4 commit a5feb04b3980bdd80c6b012a94c743ba48cdfe39 Author: Haoming <hmstudy02@gmail.com> Date: Mon Dec 18 11:55:20 2023 +0800 process scratch commit3b18f7b042Author: Haoming <hmstudy02@gmail.com> Date: Wed Dec 13 11:57:20 2023 +0800 "init" commitd0148e0e82Author: Haoming <hmstudy02@gmail.com> Date: Wed Dec 13 10:34:39 2023 +0800 clone repo
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
# Copyright (c) Microsoft Corporation.
|
|
# Licensed under the MIT License.
|
|
|
|
from __future__ import print_function
|
|
import torch
|
|
import numpy as np
|
|
from PIL import Image
|
|
import numpy as np
|
|
import os
|
|
import torch.nn as nn
|
|
|
|
# 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 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)
|