forked from aiff22/PyNET
-
Notifications
You must be signed in to change notification settings - Fork 5
/
load_dataset.py
166 lines (112 loc) · 6.84 KB
/
load_dataset.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
164
165
# Copyright 2020 by Andrey Ignatov. All Rights Reserved.
from __future__ import print_function
from PIL import Image
import imageio
import os
import numpy as np
def extract_bayer_channels(raw):
# Reshape the input bayer image
ch_B = raw[1::2, 1::2]
ch_Gb = raw[0::2, 1::2]
ch_R = raw[0::2, 0::2]
ch_Gr = raw[1::2, 0::2]
RAW_combined = np.dstack((ch_B, ch_Gb, ch_R, ch_Gr))
RAW_norm = RAW_combined.astype(np.float32) / (4 * 255)
return RAW_norm
def load_val_data(dataset_dir, dslr_dir, phone_dir, PATCH_WIDTH, PATCH_HEIGHT, DSLR_SCALE):
val_directory_dslr = dataset_dir + 'val_comp/' + dslr_dir
val_directory_phone = dataset_dir + 'val_comp/' + phone_dir
# get the image format (e.g. 'png')
format_dslr = str.split(os.listdir(val_directory_dslr)[0],'.')[-1]
# determine validation image numbers by listing all files in the folder
NUM_VAL_IMAGES = len([name for name in os.listdir(val_directory_phone)
if os.path.isfile(os.path.join(val_directory_phone, name))])
val_data = np.zeros((NUM_VAL_IMAGES, PATCH_WIDTH, PATCH_HEIGHT, 4))
val_answ = np.zeros((NUM_VAL_IMAGES, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3))
for i in range(0, NUM_VAL_IMAGES):
I = np.asarray(imageio.imread((val_directory_phone + str(i) + '.png')))
I = extract_bayer_channels(I)
val_data[i, :] = I
I = Image.open(val_directory_dslr + str(i) + '.' + format_dslr)
I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BICUBIC))
# I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BOX))
I = np.float16(np.reshape(I, [1, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3])) / 255
val_answ[i, :] = I
return val_data, val_answ
def load_train_data(dataset_dir, dslr_dir, phone_dir, TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, DSLR_SCALE):
train_directory_dslr = dataset_dir + 'train/' + dslr_dir
train_directory_phone = dataset_dir + 'train/' + phone_dir
# get the image format (e.g. 'png')
format_dslr = str.split(os.listdir(train_directory_dslr)[0],'.')[-1]
# determine training image numbers by listing all files in the folder
NUM_TRAINING_IMAGES = len([name for name in os.listdir(train_directory_phone)
if os.path.isfile(os.path.join(train_directory_phone, name))])
# TRAIN_IMAGES = np.random.choice(np.arange(0, NUM_TRAINING_IMAGES), TRAIN_SIZE, replace=False)
TRAIN_IMAGES = np.arange(0, NUM_TRAINING_IMAGES)
train_data = np.zeros((NUM_TRAINING_IMAGES, PATCH_WIDTH, PATCH_HEIGHT, 4))
train_answ = np.zeros((NUM_TRAINING_IMAGES, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3))
i = 0
for img in TRAIN_IMAGES:
I = np.asarray(imageio.imread((train_directory_phone + str(img) + '.png')))
I = extract_bayer_channels(I)
train_data[i, :] = I
I = Image.open(train_directory_dslr + str(img) + '.' + format_dslr)
I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BICUBIC))
# I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BOX))
I = np.float16(np.reshape(I, [1, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3])) / 255
train_answ[i, :] = I
i += 1
return train_data, train_answ
def load_train_patch(dataset_dir, dslr_dir, phone_dir, TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, DSLR_SCALE):
train_directory_dslr = dataset_dir + 'train/' + dslr_dir
train_directory_phone = dataset_dir + 'train/' + phone_dir
# get the image format (e.g. 'png')
format_dslr = str.split(os.listdir(train_directory_dslr)[0],'.')[-1]
# determine training image numbers by listing all files in the folder
NUM_TRAINING_IMAGES = len([name for name in os.listdir(train_directory_phone)
if os.path.isfile(os.path.join(train_directory_phone, name))])
TRAIN_IMAGES = np.random.choice(np.arange(0, NUM_TRAINING_IMAGES), TRAIN_SIZE, replace=False)
# TRAIN_IMAGES = np.arange(0, NUM_TRAINING_IMAGES)
train_data = np.zeros((TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, 4))
train_answ = np.zeros((TRAIN_SIZE, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3))
i = 0
for img in TRAIN_IMAGES:
I = np.asarray(imageio.imread((train_directory_phone + str(img) + '.png')))
I = extract_bayer_channels(I)
train_data[i, :] = I
I = Image.open(train_directory_dslr + str(img) + '.' + format_dslr)
I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BICUBIC))
# I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BOX))
I = np.float16(np.reshape(I, [1, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3])) / 255
train_answ[i, :] = I
i += 1
return train_data, train_answ
def load_train_patch_filtered(dataset_dir, dslr_dir, phone_dir, TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, DSLR_SCALE, CSV, P_THRESH, S_THRESH):
scores = np.loadtxt(CSV)
indices = []
for i, p, s in scores:
if p > P_THRESH and s > S_THRESH:
indices.append(int(i))
train_directory_dslr = dataset_dir + 'train/' + dslr_dir
train_directory_phone = dataset_dir + 'train/' + phone_dir
# get the image format (e.g. 'png')
format_dslr = str.split(os.listdir(train_directory_dslr)[0],'.')[-1]
# determine training image numbers by listing all files in the folder
NUM_TRAINING_IMAGES = len([name for name in os.listdir(train_directory_phone)
if os.path.isfile(os.path.join(train_directory_phone, name))])
TRAIN_IMAGES = np.random.choice(indices, TRAIN_SIZE, replace=False)
# TRAIN_IMAGES = np.arange(0, NUM_TRAINING_IMAGES)
train_data = np.zeros((TRAIN_SIZE, PATCH_WIDTH, PATCH_HEIGHT, 4))
train_answ = np.zeros((TRAIN_SIZE, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3))
i = 0
for img in TRAIN_IMAGES:
I = np.asarray(imageio.imread((train_directory_phone + str(img) + '.png')))
I = extract_bayer_channels(I)
train_data[i, :] = I
I = Image.open(train_directory_dslr + str(img) + '.' + format_dslr)
I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BICUBIC))
# I = np.array(I.resize((int(I.size[0] * DSLR_SCALE / 2), int(I.size[1] * DSLR_SCALE / 2)), resample=Image.BOX))
I = np.float16(np.reshape(I, [1, int(PATCH_WIDTH * DSLR_SCALE), int(PATCH_HEIGHT * DSLR_SCALE), 3])) / 255
train_answ[i, :] = I
i += 1
return train_data, train_answ