首先是编写代码需要用到的知识,如numpy和tensorflow,cv2的使用
参照的代码是github上的SRCNN的实现,网址如下:
https://github.com/Edwardlzy/SRCNN
基本的配置
对于图片放大或者缩小的倍数是3
scale=3
图片输入的大小是33,图片输出的是21,使用图片进行训练的步长是14.使用图片进行测试的步长是21
图片的预处理
def im2double(im):
info = np.iinfo(im.dtype)
return im.astype(np.float) / info.max这里是对图片进行归一化处理,比如对于精度为8的,就是除以255.让图片所有的值都是在0到1之间。
为什么要进行归一化?
分为两个方法来说,针对与图片来说,转化为标准模式。
针对于训练网络来说,加速训练。
然后就是图片的裁剪,裁剪成可以被整除的图片
def modcrop(image, scale=3):
if image.shape[2] == 1:
size = image.shape
size -= np.mod(size, scale)
image = image[0:size[0], 0:size[1]]
else:
size = image.shape[0:2]
size -= np.mod(size, scale)
image = image[0:size[0], 0:size[1], 0]
return image传入图片,实际上就是numpy的***数组。
如果 image.shape[2] == 1,表示就是一个灰度图片。
def modcrop_color(image, scale=3):
size = image.shape[0:2]# 0----1,不包括2
size -= np.mod(size, scale)
image = image[0:size[0], 0:size[1], :]
return image最后保留的有颜色的图片
size -= np.mod(size, scale)
实例代码
dirpath = './Train/'
for root, dirs, files in os.walk(dirpath):
for file in files:
img = cv.imread(dirpath + file)
img = cv.cvtColor(img, cv.COLOR_BGR2YCR_CB)
img = im2double(img) # Only use the luminance value.
# Create groundtruth and baseline image.
im_label = modcrop(img)
size = im_label.shape
h = size[0]
w = size[1]
im_temp = scipy.misc.imresize(im_label, 1/scale, interp='bicubic')
im_input = scipy.misc.imresize(im_temp, scale, interp='bicubic')
# Generate subimages for training.
for x in range(0, h - size_input, stride):
for y in range(0, w - size_input, stride):
subim_input = im_input[x : x + size_input, y : y + size_input]
subim_label = im_label[int(x + padding) : int(x + padding + size_label), int(y + padding) : int(y + padding + size_label)]
subim_input = subim_input.reshape([size_input, size_input, 1])
subim_label = subim_label.reshape([size_label, size_label, 1])
data.append(subim_input)
label.append(subim_label)
counter += 1order = np.random.choice(counter, counter, replace=False) data = np.array([data[i] for i in order]) label = np.array([label[i] for i in order])
打乱顺序,但是data和label依次是对应的
文件的读写
我们使用的训练测试图片都是转化为h5的形式的进行使用的
# Write to HDF5 file.
savepath = os.path.join(os.getcwd(), 'checkpoint/train.h5')
with h5py.File(savepath, 'w') as hf:
hf.create_dataset('data', data=data)
hf.create_dataset('label', data=label)总结
以上就是对图片(训练集和测试集)预处理的总结
1,使用的图片的大小(33----->21),训练步长14,测试步长21
2,图片的归一化以及图片的裁剪
def im2double(im):
info = np.iinfo(im.dtype)
return im.astype(np.float) / info.max图片的裁剪
def modcrop_color(image, scale=3):
size = image.shape[0:2]# 0----1,不包括2
size -= np.mod(size, scale)
image = image[0:size[0], 0:size[1], :]
return image3,图片的扩大和缩小
im_temp = scipy.misc.imresize(im_label, 1/scale, interp='bicubic')
im_input = scipy.misc.imresize(im_temp, scale, interp='bicubic')
京公网安备 11010502036488号