2020-09-27 17:11:58 +00:00
|
|
|
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']
|
2020-11-20 06:47:00 +00:00
|
|
|
assert (self.target_hq_size // self.scale) % self.multiple == 0 # If we dont throw here, we get some really obscure errors.
|
2020-09-27 17:11:58 +00:00
|
|
|
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)
|
|
|
|
else:
|
2020-10-22 16:16:17 +00:00
|
|
|
print("Building chunk cache, this can take some time for large datasets..")
|
2020-10-08 18:21:04 +00:00
|
|
|
chunks = [ChunkWithReference(opt, d) for d in sorted(os.scandir(path), key=lambda e: e.name) if d.is_dir()]
|
2020-09-27 17:11:58 +00:00
|
|
|
# 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
|
|
|
|
|
2020-10-19 17:30:06 +00:00
|
|
|
def get_paths(self):
|
|
|
|
paths = []
|
|
|
|
for c in self.chunks:
|
|
|
|
paths.extend(c.tiles)
|
|
|
|
return paths
|
|
|
|
|
2020-09-27 17:11:58 +00:00
|
|
|
# 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)
|
2020-10-15 23:18:23 +00:00
|
|
|
hqs_adjusted.append(cv2.resize(hq, target_size, interpolation=cv2.INTER_AREA))
|
|
|
|
hq_refs_adjusted.append(cv2.resize(hq_ref, target_size, interpolation=cv2.INTER_AREA))
|
|
|
|
hq_masks_adjusted.append(cv2.resize(hq_mask, target_size, interpolation=cv2.INTER_AREA))
|
2020-09-27 17:11:58 +00:00
|
|
|
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)
|
2020-10-26 17:12:22 +00:00
|
|
|
hq_centers_conformed.append(self.resize_point(hq_center, hq.shape[:2], (h, w)))
|
2020-09-27 17:11:58 +00:00
|
|
|
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:
|
2020-10-15 23:18:23 +00:00
|
|
|
ls.append(cv2.resize(hq, (h // self.scale, w // self.scale), interpolation=cv2.INTER_AREA))
|
|
|
|
lrs.append(cv2.resize(hq_ref, (h // self.scale, w // self.scale), interpolation=cv2.INTER_AREA))
|
|
|
|
lms.append(cv2.resize(hq_mask, (h // self.scale, w // self.scale), interpolation=cv2.INTER_AREA))
|
2020-09-27 17:11:58 +00:00
|
|
|
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
|