我正在参加中秋创意投稿大赛
U-Net简介
U-Net的网络结构如下图所示,是一个标准的编码-解码网络,整个网络看起来像字母U因此得名U-Net,网络左半边为收缩路径(编码),右半边为扩张路径(解码)。编码遵循典型的卷积网络架构,由卷积(无填充)重复应用组成,每层后是一个整流线性单元(ReLU)和一个用于下采样的步幅的最大池化操作。解码的每一步都包含特征图的上采样,包括一个卷积(上卷积)将特征通道的数量减半,一个与解码中相应裁剪的特征映射的连接,以及两个的卷积,每个卷积后面都跟随一个ReLU。
sketchKeras
sketchKeras是一个开源的基于U-Net的轮廓提取项目,项目链接,缺点是作者没有公开模型结构和训练算法,不过可以通过一些方法分析其提供的mod.h5
推断其网络大致的结构。
分析一下其源码,主要包括:
输入图片预处理
width = float(from_mat.shape[1])
height = float(from_mat.shape[0])
new_width = 0
new_height = 0
if (width > height):
from_mat = cv2.resize(from_mat, (512, int(512 / width * height)), interpolation=cv2.INTER_AREA)
new_width = 512
new_height = int(512 / width * height)
else:
from_mat = cv2.resize(from_mat, (int(512 / height * width), 512), interpolation=cv2.INTER_AREA)
new_width = int(512 / height * width)
new_height = 512
cv2.imshow('raw', from_mat)
cv2.imwrite('raw.jpg',from_mat)
from_mat = from_mat.transpose((2, 0, 1))
light_map = np.zeros(from_mat.shape, dtype=np.float)
for channel in range(3):
light_map[channel] = get_light_map_single(from_mat[channel])
light_map = normalize_pic(light_map)
light_map = resize_img_512_3d(light_map)
复制代码
模型处理
line_mat = mod.predict(light_map, batch_size=1)
复制代码
裁剪
line_mat = line_mat.transpose((3, 1, 2, 0))[0]
line_mat = line_mat[0:int(new_height), 0:int(new_width), :]
show_active_img_and_save('sketchKeras_colored', line_mat, 'sketchKeras_colored.jpg')
line_mat = np.amax(line_mat, 2)
复制代码
降噪及图片输出
show_active_img_and_save_denoise_filter2('sketchKeras_enhanced', line_mat, 'sketchKeras_enhanced.jpg')
show_active_img_and_save_denoise_filter('sketchKeras_pured', line_mat, 'sketchKeras_pured.jpg')
show_active_img_and_save_denoise('sketchKeras', line_mat, 'sketchKeras.jpg')
cv2.waitKey(0)
复制代码
从网上随便找一张月饼图片
实验效果
和不使用神经网络的方法对比
传统提取线稿的方法步骤为:转灰度图、求反相、高斯模糊最后颜色减淡,效果如下所示,可以看到这种方法提取的轮廓和神经网络的方法显然有很大的差距。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END