Move many dataset functions into a base class

This commit is contained in:
James Betker 2020-09-27 11:11:58 -06:00
parent eb12b5f887
commit c85da79697
2 changed files with 127 additions and 115 deletions

View File

@ -0,0 +1,113 @@
import torch
from torch.utils import data
from data.image_corruptor import ImageCorruptor
from data.chunk_with_reference import ChunkWithReference
import os
import cv2
# Class whose purpose is to hold as much logic as can possibly be shared between datasets that operate on raw image
# data and nothing else (which also have a very specific directory structure being used, as dictated by
# ChunkWithReference).
class BaseUnsupervisedImageDataset(data.Dataset):
def __init__(self, opt):
self.opt = opt
self.corruptor = ImageCorruptor(opt)
self.target_hq_size = opt['target_size'] if 'target_size' in opt.keys() else None
self.multiple = opt['force_multiple'] if 'force_multiple' in opt.keys() else 1
self.for_eval = opt['eval'] if 'eval' in opt.keys() else False
self.scale = opt['scale'] if not self.for_eval else 1
self.paths = opt['paths']
if not isinstance(self.paths, list):
self.paths = [self.paths]
self.weights = [1]
else:
self.weights = opt['weights']
# See if there is a cached directory listing and use that rather than re-scanning everything. This will greatly
# reduce startup costs.
self.chunks = []
for path, weight in zip(self.paths, self.weights):
cache_path = os.path.join(path, 'cache.pth')
if os.path.exists(cache_path):
chunks = torch.load(cache_path)
# Update the options.
for c in chunks:
c.reload(opt)
else:
chunks = [ChunkWithReference(opt, d) for d in os.scandir(path) if d.is_dir()]
# Prune out chunks that have no images
res = []
for c in chunks:
if len(c) != 0:
res.append(c)
chunks = res
# Save to a cache.
torch.save(chunks, cache_path)
for w in range(weight):
self.chunks.extend(chunks)
# Indexing this dataset is tricky. Aid it by having a list of starting indices for each chunk.
start = 0
self.starting_indices = []
for c in chunks:
self.starting_indices.append(start)
start += len(c)
self.len = start
# Utility method for translating a point when the dimensions of an image change.
def resize_point(self, point, orig_dim, new_dim):
oh, ow = orig_dim
nh, nw = new_dim
dh, dw = float(nh) / float(oh), float(nw) / float(ow)
point = int(dh * float(point[0])), int(dw * float(point[1]))
return point
# Given an HQ square of arbitrary size, resizes it to specifications from opt.
def resize_hq(self, imgs_hq, refs_hq, masks_hq, centers_hq):
# Enforce size constraints
h, w, _ = imgs_hq[0].shape
if self.target_hq_size is not None and self.target_hq_size != h:
hqs_adjusted, hq_refs_adjusted, hq_masks_adjusted, hq_centers_adjusted = [], [], [], []
for hq, hq_ref, hq_mask, hq_center in zip(imgs_hq, refs_hq, masks_hq, centers_hq):
# It is assumed that the target size is a square.
target_size = (self.target_hq_size, self.target_hq_size)
hqs_adjusted.append(cv2.resize(hq, target_size, interpolation=cv2.INTER_LINEAR))
hq_refs_adjusted.append(cv2.resize(hq_ref, target_size, interpolation=cv2.INTER_LINEAR))
hq_masks_adjusted.append(cv2.resize(hq_mask, target_size, interpolation=cv2.INTER_LINEAR))
hq_centers_adjusted.append(self.resize_point(hq_center, (h, w), target_size))
h, w = self.target_hq_size, self.target_hq_size
else:
hqs_adjusted, hq_refs_adjusted, hq_masks_adjusted, hq_centers_adjusted = imgs_hq, refs_hq, masks_hq, centers_hq
hq_multiple = self.multiple * self.scale # Multiple must apply to LQ image.
if h % hq_multiple != 0 or w % hq_multiple != 0:
hqs_conformed, hq_refs_conformed, hq_masks_conformed, hq_centers_conformed = [], [], [], []
for hq, hq_ref, hq_mask, hq_center in zip(hqs_adjusted, hq_refs_adjusted, hq_masks_adjusted, hq_centers_adjusted):
h, w = (h - h % hq_multiple), (w - w % hq_multiple)
hq_centers_conformed.append(self.resize_point(hq_center, hq.shape[:1], (h, w)))
hqs_conformed.append(hq[:h, :w, :])
hq_refs_conformed.append(hq_ref[:h, :w, :])
hq_masks_conformed.append(hq_mask[:h, :w, :])
return hqs_conformed, hq_refs_conformed, hq_masks_conformed, hq_centers_conformed
return hqs_adjusted, hq_refs_adjusted, hq_masks_adjusted, hq_centers_adjusted
def synthesize_lq(self, hs, hrefs, hmasks, hcenters):
h, w, _ = hs[0].shape
ls, lrs, lms, lcs = [], [], [], []
for hq, hq_ref, hq_mask, hq_center in zip(hs, hrefs, hmasks, hcenters):
if self.for_eval:
ls.append(hq)
lrs.append(hq_ref)
lms.append(hq_mask)
lcs.append(hq_center)
else:
ls.append(cv2.resize(hq, (h // self.scale, w // self.scale), interpolation=cv2.INTER_LINEAR))
lrs.append(cv2.resize(hq_ref, (h // self.scale, w // self.scale), interpolation=cv2.INTER_LINEAR))
lms.append(cv2.resize(hq_mask, (h // self.scale, w // self.scale), interpolation=cv2.INTER_LINEAR))
lcs.append(self.resize_point(hq_center, (h, w), ls[0].shape[:2]))
# Corrupt the LQ image (only in eval mode)
if not self.for_eval:
ls = self.corruptor.corrupt_images(ls)
return ls, lrs, lms, lcs
def __len__(self):
return self.len

View File

@ -1,139 +1,38 @@
from torch.utils import data
from data.chunk_with_reference import ChunkWithReference
from data.image_corruptor import ImageCorruptor
import os
from bisect import bisect_left
import cv2
import torch
import numpy as np
import torchvision.transforms.functional as F
import torch
from torch.utils import data
from data.base_unsupervised_image_dataset import BaseUnsupervisedImageDataset
# Builds a dataset composed of a set of folders. Each folder represents a single high resolution image that has been
# chunked into patches of fixed size. A reference image is included as well as a list of center points for each patch.
class SingleImageDataset(data.Dataset):
class SingleImageDataset(BaseUnsupervisedImageDataset):
def __init__(self, opt):
self.opt = opt
self.corruptor = ImageCorruptor(opt)
self.target_hq_size = opt['target_size'] if 'target_size' in opt.keys() else None
self.multiple = opt['force_multiple'] if 'force_multiple' in opt.keys() else 1
self.for_eval = opt['eval'] if 'eval' in opt.keys() else False
self.scale = opt['scale'] if not self.for_eval else 1
self.paths = opt['paths']
if not isinstance(self.paths, list):
self.paths = [self.paths]
self.weights = [1]
else:
self.weights = opt['weights']
# See if there is a cached directory listing and use that rather than re-scanning everything. This will greatly
# reduce startup costs.
self.chunks = []
for path, weight in zip(self.paths, self.weights):
cache_path = os.path.join(path, 'cache.pth')
if os.path.exists(cache_path):
chunks = torch.load(cache_path)
# Update the options.
for c in chunks:
c.reload(opt)
else:
chunks = [ChunkWithReference(opt, d) for d in os.scandir(path) if d.is_dir()]
# Prune out chunks that have no images
res = []
for c in chunks:
if len(c) != 0:
res.append(c)
chunks = res
# Save to a cache.
torch.save(chunks, cache_path)
for w in range(weight):
self.chunks.extend(chunks)
# Indexing this dataset is tricky. Aid it by having a sorted list of starting indices for each chunk.
start = 0
self.starting_indices = []
for c in chunks:
self.starting_indices.append(start)
start += len(c)
self.len = start
def resize_point(self, point, orig_dim, new_dim):
oh, ow = orig_dim
nh, nw = new_dim
dh, dw = float(nh) / float(oh), float(nw) / float(ow)
point = int(dh * float(point[0])), int(dw * float(point[1]))
return point
super(SingleImageDataset, self).__init__(opt)
def __getitem__(self, item):
chunk_ind = bisect_left(self.starting_indices, item)
chunk_ind = chunk_ind if chunk_ind < len(self.starting_indices) and self.starting_indices[chunk_ind] == item else chunk_ind-1
hq, hq_ref, hq_center, hq_mask, path = self.chunks[chunk_ind][item-self.starting_indices[chunk_ind]]
# Enforce size constraints
h, w, _ = hq.shape
if self.target_hq_size is not None and self.target_hq_size != h:
# It is assumed that the target size is a square.
target_size = (self.target_hq_size, self.target_hq_size)
hq = cv2.resize(hq, target_size, interpolation=cv2.INTER_LINEAR)
hq_ref = cv2.resize(hq_ref, target_size, interpolation=cv2.INTER_LINEAR)
hq_mask = cv2.resize(hq_mask, target_size, interpolation=cv2.INTER_LINEAR)
hq_center = self.resize_point(hq_center, (h, w), target_size)
h, w = self.target_hq_size, self.target_hq_size
hq_multiple = self.multiple * self.scale # Multiple must apply to LQ image.
if h % hq_multiple != 0 or w % hq_multiple != 0:
h, w = (h - h % hq_multiple), (w - w % hq_multiple)
hq_center = self.resize_point(hq_center, hq.shape[:1], (h, w))
hq = hq[:h, :w, :]
hq_ref = hq_ref[:h, :w, :]
hq_mask = hq_mask[:h, :w, :]
# Synthesize the LQ image
if self.for_eval:
lq, lq_ref = hq, hq_ref
else:
lq = cv2.resize(hq, (h // self.scale, w // self.scale), interpolation=cv2.INTER_LINEAR)
lq_ref = cv2.resize(hq_ref, (h // self.scale, w // self.scale), interpolation=cv2.INTER_LINEAR)
lq_mask = cv2.resize(hq_mask, (h // self.scale, w // self.scale), interpolation=cv2.INTER_LINEAR)
lq_center = self.resize_point(hq_center, (h, w), lq.shape[:2])
# Corrupt the LQ image
lq = self.corruptor.corrupt_images([lq])[0]
hs, hrs, hms, hcs = self.resize_hq([hq], [hq_ref], [hq_mask], [hq_center])
ls, lrs, lms, lcs = self.synthesize_lq(hs, hrs, hms, hcs)
# Convert to torch tensor
hq = torch.from_numpy(np.ascontiguousarray(np.transpose(hq, (2, 0, 1)))).float()
hq_ref = torch.from_numpy(np.ascontiguousarray(np.transpose(hq_ref, (2, 0, 1)))).float()
hq_mask = torch.from_numpy(np.ascontiguousarray(hq_mask)).unsqueeze(dim=0)
hq = torch.from_numpy(np.ascontiguousarray(np.transpose(hs[0], (2, 0, 1)))).float()
hq_ref = torch.from_numpy(np.ascontiguousarray(np.transpose(hrs[0], (2, 0, 1)))).float()
hq_mask = torch.from_numpy(np.ascontiguousarray(hms[0])).unsqueeze(dim=0)
hq_ref = torch.cat([hq_ref, hq_mask], dim=0)
lq = torch.from_numpy(np.ascontiguousarray(np.transpose(lq, (2, 0, 1)))).float()
lq_ref = torch.from_numpy(np.ascontiguousarray(np.transpose(lq_ref, (2, 0, 1)))).float()
lq_mask = torch.from_numpy(np.ascontiguousarray(lq_mask)).unsqueeze(dim=0)
lq = torch.from_numpy(np.ascontiguousarray(np.transpose(ls[0], (2, 0, 1)))).float()
lq_ref = torch.from_numpy(np.ascontiguousarray(np.transpose(lrs[0], (2, 0, 1)))).float()
lq_mask = torch.from_numpy(np.ascontiguousarray(lms[0])).unsqueeze(dim=0)
lq_ref = torch.cat([lq_ref, lq_mask], dim=0)
return {'LQ': lq, 'GT': hq, 'gt_fullsize_ref': hq_ref, 'lq_fullsize_ref': lq_ref,
'lq_center': torch.tensor(lq_center, dtype=torch.long), 'gt_center': torch.tensor(hq_center, dtype=torch.long),
'lq_center': torch.tensor(lcs[0], dtype=torch.long), 'gt_center': torch.tensor(hcs[0], dtype=torch.long),
'LQ_path': path, 'GT_path': path}
def __len__(self):
return self.len
self.corruptor = ImageCorruptor(opt)
self.target_hq_size = opt['target_size'] if 'target_size' in opt.keys() else None
self.multiple = opt['force_multiple'] if 'force_multiple' in opt.keys() else 1
self.for_eval = opt['eval'] if 'eval' in opt.keys() else False
self.scale = opt['scale'] if not self.for_eval else 1
self.paths = opt['paths']
if not isinstance(self.paths, list):
self.paths = [self.paths]
self.weights = [1]
else:
self.weights = opt['weights']
for path, weight in zip(self.paths, self.weights):
chunks = [ChunkWithReference(opt, d) for d in os.scandir(path) if d.is_dir()]
for w in range(weight):
self.chunks.extend(chunks)
if __name__ == '__main__':
opt = {