mirror of
https://github.com/Haoming02/sd-webui-old-photo-restoration.git
synced 2026-04-30 03:01:29 +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
115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
# Copyright (c) Microsoft Corporation.
|
|
# Licensed under the MIT License.
|
|
|
|
import torch.utils.data as data
|
|
from PIL import Image
|
|
import torchvision.transforms as transforms
|
|
import numpy as np
|
|
import random
|
|
|
|
class BaseDataset(data.Dataset):
|
|
def __init__(self):
|
|
super(BaseDataset, self).__init__()
|
|
|
|
def name(self):
|
|
return 'BaseDataset'
|
|
|
|
def initialize(self, opt):
|
|
pass
|
|
|
|
def get_params(opt, size):
|
|
w, h = size
|
|
new_h = h
|
|
new_w = w
|
|
if opt.resize_or_crop == 'resize_and_crop':
|
|
new_h = new_w = opt.loadSize
|
|
|
|
if opt.resize_or_crop == 'scale_width_and_crop': # we scale the shorter side into 256
|
|
|
|
if w<h:
|
|
new_w = opt.loadSize
|
|
new_h = opt.loadSize * h // w
|
|
else:
|
|
new_h=opt.loadSize
|
|
new_w = opt.loadSize * w // h
|
|
|
|
if opt.resize_or_crop=='crop_only':
|
|
pass
|
|
|
|
|
|
x = random.randint(0, np.maximum(0, new_w - opt.fineSize))
|
|
y = random.randint(0, np.maximum(0, new_h - opt.fineSize))
|
|
|
|
flip = random.random() > 0.5
|
|
return {'crop_pos': (x, y), 'flip': flip}
|
|
|
|
def get_transform(opt, params, method=Image.BICUBIC, normalize=True):
|
|
transform_list = []
|
|
if 'resize' in opt.resize_or_crop:
|
|
osize = [opt.loadSize, opt.loadSize]
|
|
transform_list.append(transforms.Scale(osize, method))
|
|
elif 'scale_width' in opt.resize_or_crop:
|
|
# transform_list.append(transforms.Lambda(lambda img: __scale_width(img, opt.loadSize, method))) ## Here , We want the shorter side to match 256, and Scale will finish it.
|
|
transform_list.append(transforms.Scale(256,method))
|
|
|
|
if 'crop' in opt.resize_or_crop:
|
|
if opt.isTrain:
|
|
transform_list.append(transforms.Lambda(lambda img: __crop(img, params['crop_pos'], opt.fineSize)))
|
|
else:
|
|
if opt.test_random_crop:
|
|
transform_list.append(transforms.RandomCrop(opt.fineSize))
|
|
else:
|
|
transform_list.append(transforms.CenterCrop(opt.fineSize))
|
|
|
|
## when testing, for ablation study, choose center_crop directly.
|
|
|
|
|
|
|
|
if opt.resize_or_crop == 'none':
|
|
base = float(2 ** opt.n_downsample_global)
|
|
if opt.netG == 'local':
|
|
base *= (2 ** opt.n_local_enhancers)
|
|
transform_list.append(transforms.Lambda(lambda img: __make_power_2(img, base, method)))
|
|
|
|
if opt.isTrain and not opt.no_flip:
|
|
transform_list.append(transforms.Lambda(lambda img: __flip(img, params['flip'])))
|
|
|
|
transform_list += [transforms.ToTensor()]
|
|
|
|
if normalize:
|
|
transform_list += [transforms.Normalize((0.5, 0.5, 0.5),
|
|
(0.5, 0.5, 0.5))]
|
|
return transforms.Compose(transform_list)
|
|
|
|
def normalize():
|
|
return transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
|
|
|
def __make_power_2(img, base, method=Image.BICUBIC):
|
|
ow, oh = img.size
|
|
h = int(round(oh / base) * base)
|
|
w = int(round(ow / base) * base)
|
|
if (h == oh) and (w == ow):
|
|
return img
|
|
return img.resize((w, h), method)
|
|
|
|
def __scale_width(img, target_width, method=Image.BICUBIC):
|
|
ow, oh = img.size
|
|
if (ow == target_width):
|
|
return img
|
|
w = target_width
|
|
h = int(target_width * oh / ow)
|
|
return img.resize((w, h), method)
|
|
|
|
def __crop(img, pos, size):
|
|
ow, oh = img.size
|
|
x1, y1 = pos
|
|
tw = th = size
|
|
if (ow > tw or oh > th):
|
|
return img.crop((x1, y1, x1 + tw, y1 + th))
|
|
return img
|
|
|
|
def __flip(img, flip):
|
|
if flip:
|
|
return img.transpose(Image.FLIP_LEFT_RIGHT)
|
|
return img
|