-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
163 lines (144 loc) · 7.09 KB
/
datasets.py
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
import torch
from torch.utils.data import Dataset, DataLoader
import torchvision
from torchvision import transforms, datasets
from PIL import Image, ImageFile
from skimage import io, transform
import os
from os import listdir
from os.path import join
import numpy as np
import random
import re
ImageFile.LOAD_TRUNCATED_IMAGES = True
class SmartDocQADataset(Dataset):
def __init__(self, blur_image_pathes, sharp_image_root, Center_Crop = False, Random_Crop=False,center_crop_size = (1024,1024) ,random_crop_size=256, multi_scale=False, rotation=False, color_augment=False, transform=None):
self.blur_image_files = []
for path in blur_image_pathes:
files = [f"{path}{f}" for f in listdir(path)]
self.blur_image_files += files
self.transform = transform
self.sharp_image_root = sharp_image_root
self.Center_Crop = Center_Crop
self.Random_Crop = Random_Crop
self.center_crop_size = center_crop_size
self.random_crop_size = random_crop_size
def __len__(self):
return len(self.blur_image_files)
def __getitem__(self, index):
match = "^[SM]_Img_.*_D\d{1,2}_L\d{1,2}_r35_a-{0,1}\d{1,2}_b-{0,1}\d{1,2}"
blurimgP = self.blur_image_files[index]
bpsp = blurimgP.split("/")
blurimgName = bpsp[-1]
phone = bpsp[-3]
try:
ans = re.search(match, blurimgName).group()
except:
pass
sharpimgP = f"{self.sharp_image_root}{phone}/Images/{ans}.jpg"
blurimg = Image.open(blurimgP).convert('RGB')
sharpimg = Image.open(sharpimgP).convert('RGB')
# blurimg.save('original_blur_img.jpg')
# sharpimg.save('original_sharp_img.jpg')
# im1 = blurimg.save("blurimg.jpg")
# im1 = im1
if self.transform:
blurimg = self.transform(blurimg)
sharpimg = self.transform(sharpimg)
if self.Center_Crop:
W = blurimg.size()[1]
H = blurimg.size()[2]
W_crop = self.center_crop_size[0] // 2
H_crop = self.center_crop_size[1] // 2
try:
blurimg = blurimg[:, W // 2 - W_crop : W//2 + W_crop, H//2 - H_crop:H//2 + H_crop]
except:
pass
sharpimg = sharpimg[:, W // 2 - W_crop : W//2 + W_crop, H//2 - H_crop:H//2 + H_crop]
PreviewPath="./preprocessPreview/"
# torchvision.utils.save_image(blurimg,f"{PreviewPath}{blurimgName}_blur.jpg")
# torchvision.utils.save_image(sharpimg,f"{PreviewPath}{blurimgName}_sharp.jpg")
if self.Random_Crop:
W = blurimg.size()[1]
H = blurimg.size()[2]
Ws = np.random.randint(0, W-self.random_crop_size-1, 1)[0]
Hs = np.random.randint(0, H-self.random_crop_size-1, 1)[0]
blurimg = blurimg[:, Ws:Ws +
self.random_crop_size, Hs:Hs+self.random_crop_size]
sharpimg = sharpimg[:, Ws:Ws +
self.random_crop_size, Hs:Hs+self.random_crop_size]
return {'blur_image': blurimg, 'sharp_image': sharpimg}
class GoProDataset(Dataset):
def __init__(self, blur_image_files, sharp_image_files, root_dir, crop=False, crop_size=256, multi_scale=False, rotation=False, color_augment=False, transform=None):
"""
Args:
split_file: Path to the split file
root_dir: Directory with all the images
transform: Optional transform to be appeared on a sample
"""
blur_file = open(blur_image_files, 'r')
self.blur_image_files = blur_file.readlines()
sharp_file = open(sharp_image_files, 'r')
self.sharp_image_files = sharp_file.readlines()
self.root_dir = root_dir
self.transform = transform
self.crop = crop
self.crop_size = crop_size
self.multi_scale = multi_scale
self.rotation = rotation
self.color_augment = color_augment
self.rotate90 = transforms.RandomRotation(90)
self.rotate45 = transforms.RandomRotation(45)
def __len__(self):
return len(self.blur_image_files)
def __getitem__(self, idx):
image_name = self.blur_image_files[idx][0:-1].split('/')
blur_image = Image.open(os.path.join(
self.root_dir, image_name[0], image_name[1], image_name[2], image_name[3])).convert('RGB')
sharp_image = Image.open(os.path.join(
self.root_dir, image_name[0], image_name[1], 'sharp', image_name[3])).convert('RGB')
if self.rotation:
degree = random.choice([90, 180, 270])
blur_image = transforms.functional.rotate(blur_image, degree)
sharp_image = transforms.functional.rotate(sharp_image, degree)
if self.color_augment:
#contrast_factor = 1 + (0.2 - 0.4*np.random.rand())
#blur_image = transforms.functional.adjust_contrast(blur_image, contrast_factor)
#sharp_image = transforms.functional.adjust_contrast(sharp_image, contrast_factor)
blur_image = transforms.functional.adjust_gamma(blur_image, 1)
sharp_image = transforms.functional.adjust_gamma(sharp_image, 1)
sat_factor = 1 + (0.2 - 0.4*np.random.rand())
blur_image = transforms.functional.adjust_saturation(
blur_image, sat_factor)
sharp_image = transforms.functional.adjust_saturation(
sharp_image, sat_factor)
if self.transform:
blur_image = self.transform(blur_image)
sharp_image = self.transform(sharp_image)
if self.crop:
W = blur_image.size()[1]
H = blur_image.size()[2]
Ws = np.random.randint(0, W-self.crop_size-1, 1)[0]
Hs = np.random.randint(0, H-self.crop_size-1, 1)[0]
blur_image = blur_image[:, Ws:Ws +
self.crop_size, Hs:Hs+self.crop_size]
sharp_image = sharp_image[:, Ws:Ws +
self.crop_size, Hs:Hs+self.crop_size]
if self.multi_scale:
H = sharp_image.size()[1]
W = sharp_image.size()[2]
blur_image_s1 = transforms.ToPILImage()(blur_image)
sharp_image_s1 = transforms.ToPILImage()(sharp_image)
blur_image_s2 = transforms.ToTensor()(
transforms.Resize([H/2, W/2])(blur_image_s1))
sharp_image_s2 = transforms.ToTensor()(
transforms.Resize([H/2, W/2])(sharp_image_s1))
blur_image_s3 = transforms.ToTensor()(
transforms.Resize([H/4, W/4])(blur_image_s1))
sharp_image_s3 = transforms.ToTensor()(
transforms.Resize([H/4, W/4])(sharp_image_s1))
blur_image_s1 = transforms.ToTensor()(blur_image_s1)
sharp_image_s1 = transforms.ToTensor()(sharp_image_s1)
return {'blur_image_s1': blur_image_s1, 'blur_image_s2': blur_image_s2, 'blur_image_s3': blur_image_s3, 'sharp_image_s1': sharp_image_s1, 'sharp_image_s2': sharp_image_s2, 'sharp_image_s3': sharp_image_s3}
else:
return {'blur_image': blur_image, 'sharp_image': sharp_image}