File size: 5,813 Bytes
2a2ae9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import os
import random
import torch
import torch.utils.data as data
import torchvision.transforms as T
from PIL import Image
class LowLightFDataset(data.Dataset):
def __init__(self, root, image_split='images_aug', targets_split='targets', training=True):
self.root = root
self.num_instances = 8
self.img_root = os.path.join(root, image_split)
self.target_root = os.path.join(root, targets_split)
self.training = training
print('----', image_split, targets_split, '----')
self.imgs = list(sorted(os.listdir(self.img_root)))
self.gts = list(sorted(os.listdir(self.target_root)))
names = [img_name.split('_')[0] + '.' + img_name.split('.')[-1] for img_name in self.imgs]
self.imgs = list(
filter(lambda img_name: img_name.split('_')[0] + '.' + img_name.split('.')[-1] in self.gts, self.imgs))
self.gts = list(filter(lambda gt: gt in names, self.gts))
print(len(self.imgs), len(self.gts))
self.preproc = T.Compose(
[T.ToTensor()]
)
self.preproc_gt = T.Compose(
[T.ToTensor()]
)
def __getitem__(self, idx):
fn, ext = self.gts[idx].split('.')
imgs = []
for i in range(self.num_instances):
img_path = os.path.join(self.img_root, f"{fn}_{i}.{ext}")
imgs += [self.preproc(Image.open(img_path).convert("RGB"))]
if self.training:
random.shuffle(imgs)
gt_path = os.path.join(self.target_root, self.gts[idx])
gt = Image.open(gt_path).convert("RGB")
gt = self.preproc_gt(gt)
# print(img_path, gt_path)
return torch.stack(imgs, dim=0), gt, fn
def __len__(self):
return len(self.gts)
class LowLightFDatasetEval(data.Dataset):
def __init__(self, root, targets_split='targets', training=True):
self.root = root
self.num_instances = 1
self.img_root = os.path.join(root, 'images')
self.target_root = os.path.join(root, targets_split)
self.training = training
self.imgs = list(sorted(os.listdir(self.img_root)))
self.gts = list(sorted(os.listdir(self.target_root)))
self.imgs = list(filter(lambda img_name: img_name in self.gts, self.imgs))
self.gts = list(filter(lambda gt: gt in self.imgs, self.gts))
print(len(self.imgs), len(self.gts))
self.preproc = T.Compose(
[T.ToTensor()]
)
self.preproc_gt = T.Compose(
[T.ToTensor()]
)
def __getitem__(self, idx):
fn, ext = self.gts[idx].split('.')
imgs = []
for i in range(self.num_instances):
img_path = os.path.join(self.img_root, f"{fn}.{ext}")
imgs += [self.preproc(Image.open(img_path).convert("RGB"))]
gt_path = os.path.join(self.target_root, self.gts[idx])
gt = Image.open(gt_path).convert("RGB")
gt = self.preproc_gt(gt)
# print(img_path, gt_path)
return torch.stack(imgs, dim=0), gt, fn
def __len__(self):
return len(self.gts)
class LowLightDataset(data.Dataset):
def __init__(self, root, targets_split='targets', color_tuning=False):
self.root = root
self.img_root = os.path.join(root, 'images')
self.target_root = os.path.join(root, targets_split)
self.color_tuning = color_tuning
self.imgs = list(sorted(os.listdir(self.img_root)))
self.gts = list(sorted(os.listdir(self.target_root)))
self.imgs = list(filter(lambda img_name: img_name in self.gts, self.imgs))
self.gts = list(filter(lambda gt: gt in self.imgs, self.gts))
print(len(self.imgs), len(self.gts))
self.preproc = T.Compose(
[T.ToTensor()]
)
self.preproc_gt = T.Compose(
[T.ToTensor()]
)
def __getitem__(self, idx):
fn, ext = self.gts[idx].split('.')
img_path = os.path.join(self.img_root, self.imgs[idx])
img = Image.open(img_path).convert("RGB")
img = self.preproc(img)
gt_path = os.path.join(self.target_root, self.gts[idx])
gt = Image.open(gt_path).convert("RGB")
gt = self.preproc_gt(gt)
if self.color_tuning:
return img, gt, 'a' + self.imgs[idx], 'a' + self.imgs[idx]
else:
return img, gt, fn
def __len__(self):
return len(self.imgs)
class LowLightDatasetReverse(data.Dataset):
def __init__(self, root, targets_split='targets', color_tuning=False):
self.root = root
self.img_root = os.path.join(root, 'images')
self.target_root = os.path.join(root, targets_split)
self.color_tuning = color_tuning
self.imgs = list(sorted(os.listdir(self.img_root)))
self.gts = list(sorted(os.listdir(self.target_root)))
self.imgs = list(filter(lambda img_name: img_name in self.gts, self.imgs))
self.gts = list(filter(lambda gt: gt in self.imgs, self.gts))
print(len(self.imgs), len(self.gts))
self.preproc = T.Compose(
[T.ToTensor()]
)
self.preproc_gt = T.Compose(
[T.ToTensor()]
)
def __getitem__(self, idx):
img_path = os.path.join(self.img_root, self.imgs[idx])
img = Image.open(img_path).convert("RGB")
img = self.preproc(img)
gt_path = os.path.join(self.target_root, self.gts[idx])
gt = Image.open(gt_path).convert("RGB")
gt = self.preproc_gt(gt)
if self.color_tuning:
return gt, img, 'a' + self.imgs[idx], 'a' + self.imgs[idx]
else:
fn, ext = os.path.splitext(self.imgs[idx])
return gt, img, '%03d' % int(fn) + ext
def __len__(self):
return len(self.imgs)
|