-
Notifications
You must be signed in to change notification settings - Fork 51
/
prepare_data.py
134 lines (103 loc) · 4.5 KB
/
prepare_data.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
# -*- coding: utf-8 -*-
import os
import cv2
import h5py
import numpy
DATA_PATH = "/home/mark/Engineer/SR/data/Train/"
TEST_PATH = "/home/mark/Engineer/SR/SRCNN_createData/Test/Set14/"
Random_Crop = 30
Patch_size = 32
label_size = 20
conv_side = 6
scale = 2
def prepare_data(_path):
names = os.listdir(_path)
names = sorted(names)
nums = names.__len__()
data = numpy.zeros((nums * Random_Crop, 1, Patch_size, Patch_size), dtype=numpy.double)
label = numpy.zeros((nums * Random_Crop, 1, label_size, label_size), dtype=numpy.double)
for i in range(nums):
name = _path + names[i]
hr_img = cv2.imread(name, cv2.IMREAD_COLOR)
shape = hr_img.shape
hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
hr_img = hr_img[:, :, 0]
# two resize operation to produce training data and labels
lr_img = cv2.resize(hr_img, (shape[1] / scale, shape[0] / scale))
lr_img = cv2.resize(lr_img, (shape[1], shape[0]))
# produce Random_Crop random coordinate to crop training img
Points_x = numpy.random.randint(0, min(shape[0], shape[1]) - Patch_size, Random_Crop)
Points_y = numpy.random.randint(0, min(shape[0], shape[1]) - Patch_size, Random_Crop)
for j in range(Random_Crop):
lr_patch = lr_img[Points_x[j]: Points_x[j] + Patch_size, Points_y[j]: Points_y[j] + Patch_size]
hr_patch = hr_img[Points_x[j]: Points_x[j] + Patch_size, Points_y[j]: Points_y[j] + Patch_size]
lr_patch = lr_patch.astype(float) / 255.
hr_patch = hr_patch.astype(float) / 255.
data[i * Random_Crop + j, 0, :, :] = lr_patch
label[i * Random_Crop + j, 0, :, :] = hr_patch[conv_side: -conv_side, conv_side: -conv_side]
# cv2.imshow("lr", lr_patch)
# cv2.imshow("hr", hr_patch)
# cv2.waitKey(0)
return data, label
# BORDER_CUT = 8
BLOCK_STEP = 16
BLOCK_SIZE = 32
def prepare_crop_data(_path):
names = os.listdir(_path)
names = sorted(names)
nums = names.__len__()
data = []
label = []
for i in range(nums):
name = _path + names[i]
hr_img = cv2.imread(name, cv2.IMREAD_COLOR)
hr_img = cv2.cvtColor(hr_img, cv2.COLOR_BGR2YCrCb)
hr_img = hr_img[:, :, 0]
shape = hr_img.shape
# two resize operation to produce training data and labels
lr_img = cv2.resize(hr_img, (shape[1] / scale, shape[0] / scale))
lr_img = cv2.resize(lr_img, (shape[1], shape[0]))
width_num = (shape[0] - (BLOCK_SIZE - BLOCK_STEP) * 2) / BLOCK_STEP
height_num = (shape[1] - (BLOCK_SIZE - BLOCK_STEP) * 2) / BLOCK_STEP
for k in range(width_num):
for j in range(height_num):
x = k * BLOCK_STEP
y = j * BLOCK_STEP
hr_patch = hr_img[x: x + BLOCK_SIZE, y: y + BLOCK_SIZE]
lr_patch = lr_img[x: x + BLOCK_SIZE, y: y + BLOCK_SIZE]
lr_patch = lr_patch.astype(float) / 255.
hr_patch = hr_patch.astype(float) / 255.
lr = numpy.zeros((1, Patch_size, Patch_size), dtype=numpy.double)
hr = numpy.zeros((1, label_size, label_size), dtype=numpy.double)
lr[0, :, :] = lr_patch
hr[0, :, :] = hr_patch[conv_side: -conv_side, conv_side: -conv_side]
data.append(lr)
label.append(hr)
data = numpy.array(data, dtype=float)
label = numpy.array(label, dtype=float)
return data, label
def write_hdf5(data, labels, output_filename):
"""
This function is used to save image data and its label(s) to hdf5 file.
output_file.h5,contain data and label
"""
x = data.astype(numpy.float32)
y = labels.astype(numpy.float32)
with h5py.File(output_filename, 'w') as h:
h.create_dataset('data', data=x, shape=x.shape)
h.create_dataset('label', data=y, shape=y.shape)
# h.create_dataset()
def read_training_data(file):
with h5py.File(file, 'r') as hf:
data = numpy.array(hf.get('data'))
label = numpy.array(hf.get('label'))
train_data = numpy.transpose(data, (0, 2, 3, 1))
train_label = numpy.transpose(label, (0, 2, 3, 1))
return train_data, train_label
if __name__ == "__main__":
data, label = prepare_crop_data(DATA_PATH)
write_hdf5(data, label, "crop_train.h5")
data, label = prepare_data(TEST_PATH)
write_hdf5(data, label, "test.h5")
# _, _a = read_training_data("train.h5")
# _, _a = read_training_data("test.h5")