2020-10-20 18:56:35 +00:00
|
|
|
import random
|
2020-09-18 15:49:13 +00:00
|
|
|
from bisect import bisect_left
|
2020-09-25 22:37:54 +00:00
|
|
|
import numpy as np
|
2020-09-27 17:11:58 +00:00
|
|
|
import torch
|
|
|
|
from torch.utils import data
|
|
|
|
from data.base_unsupervised_image_dataset import BaseUnsupervisedImageDataset
|
2020-09-18 15:49:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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.
|
2020-09-27 17:11:58 +00:00
|
|
|
class SingleImageDataset(BaseUnsupervisedImageDataset):
|
2020-09-18 15:49:13 +00:00
|
|
|
def __init__(self, opt):
|
2020-09-27 17:11:58 +00:00
|
|
|
super(SingleImageDataset, self).__init__(opt)
|
2020-09-18 15:49:13 +00:00
|
|
|
|
2020-10-02 14:58:34 +00:00
|
|
|
def get_paths(self):
|
|
|
|
for i in range(len(self)):
|
|
|
|
chunk_ind = bisect_left(self.starting_indices, i)
|
|
|
|
chunk_ind = chunk_ind if chunk_ind < len(self.starting_indices) and self.starting_indices[chunk_ind] == i else chunk_ind-1
|
|
|
|
yield self.chunks[chunk_ind].tiles[i-self.starting_indices[chunk_ind]]
|
|
|
|
|
2020-09-18 15:49:13 +00:00
|
|
|
def __getitem__(self, item):
|
2020-09-25 22:37:54 +00:00
|
|
|
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]]
|
2020-09-18 15:49:13 +00:00
|
|
|
|
2020-09-27 17:11:58 +00:00
|
|
|
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)
|
2020-09-18 15:49:13 +00:00
|
|
|
|
|
|
|
# Convert to torch tensor
|
2020-09-27 17:11:58 +00:00
|
|
|
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)
|
2020-09-25 22:37:54 +00:00
|
|
|
hq_ref = torch.cat([hq_ref, hq_mask], dim=0)
|
2020-09-27 17:11:58 +00:00
|
|
|
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)
|
2020-09-25 22:37:54 +00:00
|
|
|
lq_ref = torch.cat([lq_ref, lq_mask], dim=0)
|
2020-09-18 15:49:13 +00:00
|
|
|
|
2020-12-05 03:14:53 +00:00
|
|
|
return {'lq': lq, 'hq': hq, 'gt_fullsize_ref': hq_ref, 'lq_fullsize_ref': lq_ref,
|
2020-09-27 17:11:58 +00:00
|
|
|
'lq_center': torch.tensor(lcs[0], dtype=torch.long), 'gt_center': torch.tensor(hcs[0], dtype=torch.long),
|
2020-09-18 15:49:13 +00:00
|
|
|
'LQ_path': path, 'GT_path': path}
|
|
|
|
|
2020-09-25 22:37:54 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
opt = {
|
|
|
|
'name': 'amalgam',
|
2020-10-20 18:56:35 +00:00
|
|
|
'paths': ['F:\\4k6k\\datasets\\images\\mi1_256'],
|
2020-09-25 22:37:54 +00:00
|
|
|
'weights': [1],
|
|
|
|
'target_size': 128,
|
|
|
|
'force_multiple': 32,
|
|
|
|
'scale': 2,
|
|
|
|
'eval': False,
|
2020-12-19 22:13:46 +00:00
|
|
|
'strict': False,
|
2020-11-24 20:20:12 +00:00
|
|
|
'fixed_corruptions': ['jpeg-broad', 'gaussian_blur'],
|
2020-10-20 18:56:35 +00:00
|
|
|
'random_corruptions': ['noise-5', 'none'],
|
2020-11-24 20:20:12 +00:00
|
|
|
'num_corrupts_per_image': 1,
|
|
|
|
'corrupt_before_downsize': True,
|
2020-09-25 22:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ds = SingleImageDataset(opt)
|
|
|
|
import os
|
|
|
|
os.makedirs("debug", exist_ok=True)
|
|
|
|
for i in range(0, len(ds)):
|
2020-10-20 18:56:35 +00:00
|
|
|
o = ds[random.randint(0, len(ds))]
|
2020-11-14 03:11:57 +00:00
|
|
|
#for k, v in o.items():
|
2020-12-05 03:14:53 +00:00
|
|
|
k = 'lq'
|
2020-11-14 03:11:57 +00:00
|
|
|
v = o[k]
|
|
|
|
#if 'LQ' in k and 'path' not in k and 'center' not in k:
|
|
|
|
#if 'full' in k:
|
|
|
|
#masked = v[:3, :, :] * v[3]
|
|
|
|
#torchvision.utils.save_image(masked.unsqueeze(0), "debug/%i_%s_masked.png" % (i, k))
|
|
|
|
#v = v[:3, :, :]
|
|
|
|
import torchvision
|
|
|
|
torchvision.utils.save_image(v.unsqueeze(0), "debug/%i_%s.png" % (i, k))
|