最近研究超分辨率过程中,脑洞大开,考虑到对于马赛克图像的恢复实验,说GAN就GAN,于是制作了一批马赛克dataset,打算利用SRGAN网络结构对其进行复原。
只是实验性的过程,目前还不知道效果。
先送上马赛克处理过程,这里参考来Ps打马算法的实现。
import scipy.misc as misc
import os,sys
import random
import numpy as np
from multiprocessing import Pool
import shutil
MOSAIC_RANGE = [1, 3]
input_dir = '../data/Mosaic_LR/'
output_dir = '../data/Mosaic_LR_msk/'
if not os.path.exists(input_dir):
os.mkdir(input_dir)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
index = 0
# 打码函数
def drawMask(file_name):
#print(file_name)
global index
index += 1
try:
sys.stdout.write('\r>>Converting image %d/100000 ' % (index))
sys.stdout.flush()
img = misc.imread(file_name).astype(dtype=np.float)
img_out = img.copy()
row, col, channel = img.shape
half_patch = np.random.randint(MOSAIC_RANGE[0], MOSAIC_RANGE[1]+1, 1)[0]
for i in range(half_patch, row - 1 - half_patch, half_patch):
for j in range(half_patch, col - 1 - half_patch, half_patch):
k1 = random.random() - 0.5
k2 = random.random() - 0.5
m = np.floor(k1 * (half_patch * 2 + 1))
n = np.floor(k2 * (half_patch * 2 + 1))
h = int((i + m) % row)
w = int((j + n) % col)
img_out[i - half_patch:i + half_patch, j - half_patch:j + half_patch, :] = img[h, w, :]
save_name = os.path.join(output_dir, file_name.split('/')[-1])
misc.imsave(save_name,img_out)
except IOError as e:
print('could not read:',jpg)
print('error:',e)
print('skip it\n')
if __name__ == "__main__":
dataset_dir = input_dir
image_list = os.listdir(dataset_dir)
image_list = [os.path.join(dataset_dir, _) for _ in image_list]
image_list = image_list[:100000]
p = Pool(5)
p.map(drawMask, image_list)
sys.stdout.write('Convert Over!\n')
sys.stdout.flush()
原图是这样的:
打马后:
原图:
打Ma:
原图:
轻微打ma:
当然也可以局部打ma,考虑到训练数据集的需要,这里我都是对整张图打马处理的。
升级版:
主要加了异常处理,比如图像读取错误,图像本身错误损坏等导致图像异常,造成程序终止,处理方式:一旦发现异常,自动清理图片。
import scipy.misc as misc
import os,sys
import random
import numpy as np
from multiprocessing import Pool
import shutil
MOSAIC_RANGE = [2, 7]
input_dir = '../datasets/mosaic/trainB_All/'
output_dir = '../datasets/mosaic/trainA/'
move_dir = '../datasets/mosaic/trainB/'
if not os.path.exists(input_dir):
os.mkdir(input_dir)
if not os.path.exists(output_dir):
os.mkdir(output_dir)
if not os.path.exists(move_dir):
os.mkdir(move_dir)
index = 0
# 打码函数
def drawMask(file_name):
#print(file_name)
global index
index += 1
try:
sys.stdout.write('\r>>Converting image %d/20000 ' % (index))
sys.stdout.flush()
img = misc.imread(file_name).astype(dtype=np.float)
img_out = img.copy()
row, col, channel = img.shape
half_patch = np.random.randint(MOSAIC_RANGE[0], MOSAIC_RANGE[1]+1, 1)[0]
for i in range(half_patch, row - 1 - half_patch, half_patch):
for j in range(half_patch, col - 1 - half_patch, half_patch):
k1 = random.random() - 0.5
k2 = random.random() - 0.5
m = np.floor(k1 * (half_patch * 2 + 1))
n = np.floor(k2 * (half_patch * 2 + 1))
h = int((i + m) % row)
w = int((j + n) % col)
img_out[i - half_patch:i + half_patch, j - half_patch:j + half_patch, :] = img[h, w, :]
save_name = os.path.join(output_dir, file_name.split('/')[-1])
misc.imsave(save_name,img_out)
except IOError as e:
print('could not read:',file_name)
print('error:',e)
print('skip it\n')
shutil.move(file_name, '/home/gavin/Desktop/face_test/')
except TypeError as err:
print('TypeError:', file_name)
print('error:', err)
print('skip it\n')
shutil.move(file_name, '/home/gavin/Desktop/valueErr/')
else:
shutil.move(file_name, move_dir)
if __name__ == "__main__":
dataset_dir = input_dir
image_list = os.listdir(dataset_dir)
image_list = [os.path.join(dataset_dir, _) for _ in image_list]
image_list = image_list[:3000]
p = Pool(5)
p.map(drawMask, image_list)
sys.stdout.write('Convert Over!\n')
sys.stdout.flush()