泽原形象工作室的人工智能就发型和与你的服装提出建议的合适的发型

深度学习AI美颜教程----AI美发算法特效 给照片或者视频中的人物头发换颜色,这个技术已经在手机app诸如天天P图,美图秀秀等应用中使用,并获得了不少用户的青睐。 如何给照片或者视频中的人物头发换发色? 换发色算法流程如下图所示: 1,AI头发分割模块 基于深度学习的目标分割算法已经比较成熟,比较常用的有FCN,SegNet,UNet,PspNet,DenseNet等等。 这里我们使用Unet网络来进行头发分割,具体可以参考如下链接:点击打开链接 Unet头发分割代码如下: 1. def get_unet_256(input_shape=(256, 256, 3), 2. num_classes=1): 3. inputs = Input(shape=input_shape) 4. # 256 5. 6. down0 = Conv2D(32, (3, 3), padding='same')(inputs) 7. down0 = BatchNormalization()(down0) 8. down0 = Activation('relu')(down0) 9. down0 = Conv2D(32, (3, 3), padding='same')(down0) 10. down0 = BatchNormalization()(down0) 11. down0 = Activation('relu')(down0) 12. down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0) 13. # 128 14. 15. down1 = Conv2D(64, (3, 3), padding='same')(down0_pool) 16. down1 = BatchNormalization()(down1) 17. down1 = Activation('relu')(down1) 18. down1 = Conv2D(64, (3, 3), padding='same')(down1) 19. down1 = BatchNormalization()(down1) 20. down1 = Activation('relu')(down1) 21. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1) 22. # 64 23. 24. down2 = Conv2D(128, (3, 3), padding='same')(down1_pool) 25. down2 = BatchNormalization()(down2) 26. down2 = Activation('relu')(down2) 27. down2 = Conv2D(128, (3, 3), padding='same')(down2) 28. down2 = BatchNormalization()(down2) 29. down2 = Activation('relu')(down2) 30. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2) 31. # 32 32. 33. down3 = Conv2D(256, (3, 3), padding='same')(down2_pool) 34. down3 = BatchNormalization()(down3) 35. down3 = Activation('relu')(down3) 36. down3 = Conv2D(256, (3, 3), padding='same')(down3) 37. down3 = BatchNormalization()(down3) 38. down3 = Activation('relu')(down3) 39. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3) 40. # 16 41. 42. down4 = Conv2D(512, (3, 3), padding='same')(down3_pool) 43. down4 = BatchNormalization()(down4) 44. down4 = Activation('relu')(down4) 45. down4 = Conv2D(512, (3, 3), padding='same')(down4) 46. down4 = BatchNormalization()(down4) 47. down4 = Activation('relu')(down4) 48. down4_pool = MaxPooling2D((2, 2), strides=(2, 2))(down4) 49. # 8 50. 51. center = Conv2D(1024, (3, 3), padding='same')(down4_pool) 52. center = BatchNormalization()(center) 53. center = Activation('relu')(center) 54. center = Conv2D(1024, (3, 3), padding='same')(center) 55. center = BatchNormalization()(center) 56. center = Activation('relu')(center) 57. # center 58. 59. up4 = UpSampling2D((2, 2))(center) 60. up4 = concatenate([down4, up4], axis=3) 61. up4 = Conv2D(512, (3, 3), padding='same')(up4) 62. up4 = BatchNormalization()(up4) 63. up4 = Activation('relu')(up4) 64. up4 = Conv2D(512, (3, 3), padding='same')(up4) 65. up4 = BatchNormalization()(up4) 66. up4 = Activation('relu')(up4) 67. up4 = Conv2D(512, (3, 3), padding='same')(up4) 68. up4 = BatchNormalization()(up4) 69. up4 = Activation('relu')(up4) 70. # 16 71. 72. up3 = UpSampling2D((2, 2))(up4) 73. up3 = concatenate([down3, up3], axis=3) 74. up3 = Conv2D(256, (3, 3), padding='same')(up3) 75. up3 = BatchNormalization()(up3) 76. up3 = Activation('relu')(up3) 77. up3 = Conv2D(256, (3, 3), padding='same')(up3) 78. up3 = BatchNormalization()(up3) 79. up3 = Activation('relu')(up3) 80. up3 = Conv2D(256, (3, 3), padding='same')(up3) 81. up3 = BatchNormalization()(up3) 82. up3 = Activation('relu')(up3) 83. # 32 84. 85. up2 = UpSampling2D((2, 2))(up3) 86. up2 = concatenate([down2, up2], axis=3) 87. up2 = Conv2D(128, (3, 3), padding='same')(up2) 88. up2 = BatchNormalization()(up2) 89. up2 = Activation('relu')(up2) 90. up2 = Conv2D(128, (3, 3), padding='same')(up2) 91. up2 = BatchNormalization()(up2) 92. up2 = Activation('relu')(up2) 93. up2 = Conv2D(128, (3, 3), padding='same')(up2) 94. up2 = BatchNormalization()(up2) 95. up2 = Activation('relu')(up2) 96. # 64 97. 98. up1 = UpSampling2D((2, 2))(up2) 99. up1 = concatenate([down1, up1], axis=3) 100. up1 = Conv2D(64, (3, 3), padding='same')(up1) 101. up1 = BatchNormalization()(up1) 102. up1 = Activation('relu')(up1) 103. up1 = Conv2D(64, (3, 3), padding='same')(up1) 104. up1 = BatchNormalization()(up1) 105. up1 = Activation('relu')(up1) 106. up1 = Conv2D(64, (3, 3), padding='same')(up1) 107. up1 = BatchNormalization()(up1) 108. up1 = Activation('relu')(up1) 109. # 128 110. 111. up0 = UpSampling2D((2, 2))(up1) 112. up0 = concatenate([down0, up0], axis=3) 113. up0 = Conv2D(32, (3, 3), padding='same')(up0) 114. up0 = BatchNormalization()(up0) 115. up0 = Activation('relu')(up0) 116. up0 = Conv2D(32, (3, 3), padding='same')(up0) 117. up0 = BatchNormalization()(up0) 118. up0 = Activation('relu')(up0) 119. up0 = Conv2D(32, (3, 3), padding='same')(up0) 120. up0 = BatchNormalization()(up0) 121. up0 = Activation('relu')(up0) 122. # 256 123. 124. classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0) 125. 126. model = Model(inputs=inputs, outputs=classify) 127. 128. #model.compile(optimizer=RMSprop(lr=0.0001), loss=bce_dice_loss, metrics=[dice_coeff]) 129. 130. return model 分割效果举例如下: 使用的训练和测试数据集合大家自己准备即可。 2,头发换色模块 这个模块看起来比较简单,实际上却并非如此。 这个模块要细分为①头发颜色增强与修正模块;②颜色空间染色模块;③头发细节增强; ①头发颜色增强与修正模块 为什么要颜色增强与修正? 先看下面一组图,我们直接使用HSV颜色空间对纯黑色的头发进行染色,目标色是紫色,结果如下: 大家可以看到,针对上面这张原图,头发比较黑,在HSV颜色空间进行头发换色之后,效果图中很不明显,只有轻微的颜色变化; 为什么会出现这种情况?原因如下: 我们以RGB和HSV颜色空间为例,首先来看下HSV和RGB之间的转换公式: 设 (r, g, b)分别是一个颜色的红、绿和蓝坐标,它们的值是在0到1之间的实数。设max等价于r, g和b中的最大者。设min等于这些值中的最小者。要找到在HSL空间中的 (h, s, l)值,这里的h ∈ [0, 360)度是角度的色相角,而s, l ∈ [0,1]是饱和度和亮度,计算为: 我们假设头发为纯黑色,R=G=B=0,那么按照HSV计算公式可以得到H = S = V = 0; 假设我们要把头发颜色替换为红色(r=255,g=0,b=0); 那么,我们先将红色转换为对应的hsv,然后保留原始黑色头发的V,红色头发的hs,重新组合新的hsV,在转换为RGB颜色空间,即为头发换色之后的效果(hs是颜色属性,v是明度属性,保留原始黑色头发的明度,替换颜色属性以达到换色目的); HSV转换为RGB的公式如下: 对于黑色,我们计算的结果是H=S=V=0,由于V=0,因此,p=q=t=0,不管目标颜色的hs值是多少,rgb始终都是0,也就是黑色; 这样,虽然我们使用了红色,来替换黑色头发,但是,结果却依旧是黑色,结论也就是hsv/hsl颜色空间,无法对黑色换色。 下面,我们给出天天P图和美妆相机对应紫色的换发色效果: 与之前HSV颜色空间的结果对比,我们明显可以看到,天天P图和美妆相机的效果要更浓,更好看,而且对近乎黑色的头发进行了完美的换色; 由于上述原因,我们这里需要对图像中的头发区域进行一定的增强处理:提亮,轻微改变色调; 这一步通常可以在PS上进行提亮调色,然后使用LUT来处理; 经过提亮之后的上色效果如下图所示: 可以看到,基本与美妆相机和天天P图类似了。 ②HSV/HSL/YCbCr颜色空间换色 这一步比较简单,保留明度分量不变,将其他颜色、色调分量替换为目标发色就可以了。 这里以HSV颜色空间为例: 假如我们要将头发染发为一半青色,一般粉红色,那么我们构建如下图所示的颜色MAP: 对于头发区域的每一个像素点P,我们将P的RGB转换为HSV颜色空间,得到H/S/V; 根据P在原图头发区域的位置比例关系,我们在颜色MAP中找到对应位置的像素点D,将D的RGB转换为HSV颜色空间,得到目标颜色的h/s/v; 根据目标颜色重组hsV,然后转为RGB即可; 这一模块代码如下: 1. // h = [0,360], s = [0,1], v = [0,1] 2. void RGBToHSV(int R, int G, int B, float* h, float* s, float * v) 3. { 4. float min, max; 5. float r = R / 255.0f; 6. float g = G / 255.0f; 7. float b = B / 255.0f; 8. min = MIN2(r,MIN2(g,b)); 9. max = MAX2(r,MAX2(g,b)); 10. if (max == min) 11. *h = 0; 12. if (max == r && g >= b) 13. *h = 60.0f * (g - b) / (max - min); 14. if (max == r && g < b) 15. *h = 60.0f * (g - b) / (max - min) + 360.0f; 16. 17. if (max == g) 18. *h = 60.0f * (b - r) / (max - min) + 120.0f; 19. if (max == b) 20. *h = 60.0f * (r - g) / (max - min) + 240.0f; 21. 22. if (max == 0) 23. *s = 0; 24. else 25. *s = (max - min) / max; 26. *v = max; 27. }; 28. void HSVToRGB(float h, float s, float v, int* R, int *G, int *B) 29. { 30. float q = 0, p = 0, t = 0, r = 0, g = 0, b = 0; 31. int hN = 0; 32. if (h < 0) 33. h = 360 + h; 34. hN = (int)(h / 60); 35. p = v * (1.0f - s); 36. q = v * (1.0f - (h / 60.0f - hN) * s); 37. t = v * (1.0f - (1.0f - (h / 60.0f - hN)) * s); 38. switch (hN) 39. { 40. case 0: 41. r = v; 42. g = t; 43. b = p; 44. break; 45. case 1: 46. r = q; 47. g = v; 48. b = p; 49. break; 50. case 2: 51. r = p; 52. g = v; 53. b = t; 54. break; 55. case 3: 56. r = p; 57. g = q; 58. b = v; 59. break; 60. case 4: 61. r = t; 62. g = p; 63. b = v; 64. break; 65. case 5: 66. r = v; 67. g = p; 68. b = q; 69. break; 70. default: 71. break; 72. } 73. *R = (int)CLIP3((r * 255.0f),0,255); 74. *G = (int)CLIP3((g * 255.0f),0,255); 75. *B = (int)CLIP3((b * 255.0f),0,255); 76. }; 效果图如下所示: 本文算法对比美妆相机效果如下: ③头发区域增强 这一步主要是为了突出头发丝的细节,可以使用锐化算法,如Laplace锐化,USM锐化等等。 上述过程基本是模拟美妆相机染发算法的过程,给大家参考一下,最后给出本文算法的一些效果举例: 本文效果除了实现正常的单色染发,混合色染发之外,还实现了挑染,如最下方一组效果图所示。 对于挑染的算法原理: 计算头发纹理,根据头发纹理选取需要挑染的头发束,然后对这些头发束与其他头发分开染色即可,具体逻辑这里不再累赘,大家自行研究,这里给出解决思路供大家参考。 最后,本文算法理论上实时处理是没有问题的,头发分割已经可以实时处理,所以后面基本没有什么耗时操作,使用opengl实现实时染发是没有问题的。 深度学习AI美颜系列---AI美颜磨皮算法一 首先说明一点,为什么本结内容是“AI美颜磨皮算法一”?而不是“AI美颜磨皮算法”? AI美颜磨皮算法目前还没有具体定义,各大公司也都处于摸索阶段,因此,这里只是依据自己的实现方案做了区分,本文算法与下一篇“AI美颜磨皮算法二”在算法角度,有着很大的差异,由此做了区分。 先看一下磨皮算法的一般流程: 这个流程图是一般传统的磨皮算法流程图,而本文将基于这个流程图,结合深度学习做一些改进。 在这个流程图中,主要的模块有两个:滤波模块和肤色区域检测模块; 滤波模块中,包含了三种算法: 1,保边滤波器滤波算法 该方法是指通过一些具有保留边缘的能力的滤波器,来将图像磨平,达到皮肤平滑的目的; 这类滤波器主要有: ①双边滤波器 ②导向滤波器 ③Surface Blur表面模糊滤波器 ④局部均值滤波器 ⑤加权最小二乘滤波器(WLS滤波器) ⑥Smart blur等等,详情可参考本人博客。 此方法皮肤区域比较平滑,细节较少,需要后期添加细节信息,来保留一些自然的纹理; 2,高反差减弱算法 高反差保留算法是指通过高反差来得到皮肤细节的MASK,根据MASK中细节区域,比如皮肤中的斑点区域位置,将原图对应区域进行颜色减淡处理,以此来达到斑点弱化,美肤的目的; 该方法在保留纹理的同时,减弱了皮肤瑕疵与斑点的颜色,使得皮肤看起来比较光滑自然; 3,其他算法 这里是指一些未知的算法,当然已知的也有,比如:基于保边滤波和高反差的磨皮算法,该方法同时对原图做了1-2步骤,得到一张光滑的滤波图和高反差对应的细节MASK,然后将MASK作为alpha通道,把原图和滤波图进行Alpha融合,达到平滑皮肤的同时,去除斑点,保留纹理的作用; 皮肤区域识别检测模块 目前常用的皮肤检测主要是基于颜色空间的皮肤颜色统计方法; 该方法具有较高的误检率,容易将类肤色判定为肤色,这样就导致了非皮肤区域图像被滤波器平滑掉了,也就是不该磨皮的图像区域被模糊了; 重点来了,下面我们在传统磨皮算法流程中使用深度学习来改进或者提高我们磨皮的质量,比如:使用深度学习进行皮肤区域分割,得到更为精确的皮肤区域,从而使得我们最后的磨皮效果超越传统算法的效果; 下面,我们介绍基于深度学习的皮肤区域分割: 分割的方法有很多,CNN/FCN/UNet/DenseNet等等,这里我们使用UNet进行皮肤分割: Unet做图像分割,参考论文如:UNet:Convolutional Networks for Biomedical Image Segmentation. 它最开始的网络模型如下: 这是一个全卷积神经网络,输入和输出都是图像,没有全连接层,较浅的高分辨率层用来解决像素定位的问题,较深的层用来解决像素分类的问题; 左边进行卷积和下采样,同时保留当前结果,右边进行上采样时将上采样结果和左边对应结果进行融合,以此来提高分割效果; 这个网络中左右是不对称的,后来改进的Unet基本上在图像分辨率上呈现出对称的样式,本文这里使用Keras来实现,网络结构如下: 1. Layer (type) Output Shape Param # Connected to 2. ================================================================================================== 3. input_1 (InputLayer) (None, 256, 256, 3) 0 4. __________________________________________________________________________________________________ 5. conv2d_1 (Conv2D) (None, 256, 256, 32) 896 input_1[0][0] 6. __________________________________________________________________________________________________ 7. batch_normalization_1 (BatchNor (None, 256, 256, 32) 128 conv2d_1[0][0] 8. __________________________________________________________________________________________________ 9. activation_1 (Activation) (None, 256, 256, 32) 0 batch_normalization_1[0][0] 10. __________________________________________________________________________________________________ 11. conv2d_2 (Conv2D) (None, 256, 256, 32) 9248 activation_1[0][0] 12. __________________________________________________________________________________________________ 13. batch_normalization_2 (BatchNor (None, 256, 256, 32) 128 conv2d_2[0][0] 14. __________________________________________________________________________________________________ 15. activation_2 (Activation) (None, 256, 256, 32) 0 batch_normalization_2[0][0] 16. __________________________________________________________________________________________________ 17. max_pooling2d_1 (MaxPooling2D) (None, 128, 128, 32) 0 activation_2[0][0] 18. __________________________________________________________________________________________________ 19. conv2d_3 (Conv2D) (None, 128, 128, 64) 18496 max_pooling2d_1[0][0] 20. __________________________________________________________________________________________________ 21. batch_normalization_3 (BatchNor (None, 128, 128, 64) 256 conv2d_3[0][0] 22. __________________________________________________________________________________________________ 23. activation_3 (Activation) (None, 128, 128, 64) 0 batch_normalization_3[0][0] 24. __________________________________________________________________________________________________ 25. conv2d_4 (Conv2D) (None, 128, 128, 64) 36928 activation_3[0][0] 26. __________________________________________________________________________________________________ 27. batch_normalization_4 (BatchNor (None, 128, 128, 64) 256 conv2d_4[0][0] 28. __________________________________________________________________________________________________ 29. activation_4 (Activation) (None, 128, 128, 64) 0 batch_normalization_4[0][0] 30. __________________________________________________________________________________________________ 31. max_pooling2d_2 (MaxPooling2D) (None, 64, 64, 64) 0 activation_4[0][0] 32. __________________________________________________________________________________________________ 33. conv2d_5 (Conv2D) (None, 64, 64, 128) 73856 max_pooling2d_2[0][0] 34. __________________________________________________________________________________________________ 35. batch_normalization_5 (BatchNor (None, 64, 64, 128) 512 conv2d_5[0][0] 36. __________________________________________________________________________________________________ 37. activation_5 (Activation) (None, 64, 64, 128) 0 batch_normalization_5[0][0] 38. __________________________________________________________________________________________________ 39. conv2d_6 (Conv2D) (None, 64, 64, 128) 147584 activation_5[0][0] 40. __________________________________________________________________________________________________ 41. batch_normalization_6 (BatchNor (None, 64, 64, 128) 512 conv2d_6[0][0] 42. __________________________________________________________________________________________________ 43. activation_6 (Activation) (None, 64, 64, 128) 0 batch_normalization_6[0][0] 44. __________________________________________________________________________________________________ 45. max_pooling2d_3 (MaxPooling2D) (None, 32, 32, 128) 0 activation_6[0][0] 46. __________________________________________________________________________________________________ 47. conv2d_7 (Conv2D) (None, 32, 32, 256) 295168 max_pooling2d_3[0][0] 48. __________________________________________________________________________________________________ 49. batch_normalization_7 (BatchNor (None, 32, 32, 256) 1024 conv2d_7[0][0] 50. __________________________________________________________________________________________________ 51. activation_7 (Activation) (None, 32, 32, 256) 0 batch_normalization_7[0][0] 52. __________________________________________________________________________________________________ 53. conv2d_8 (Conv2D) (None, 32, 32, 256) 590080 activation_7[0][0] 54. __________________________________________________________________________________________________ 55. batch_normalization_8 (BatchNor (None, 32, 32, 256) 1024 conv2d_8[0][0] 56. __________________________________________________________________________________________________ 57. activation_8 (Activation) (None, 32, 32, 256) 0 batch_normalization_8[0][0] 58. __________________________________________________________________________________________________ 59. max_pooling2d_4 (MaxPooling2D) (None, 16, 16, 256) 0 activation_8[0][0] 60. __________________________________________________________________________________________________ 61. conv2d_9 (Conv2D) (None, 16, 16, 512) 1180160 max_pooling2d_4[0][0] 62. __________________________________________________________________________________________________ 63. batch_normalization_9 (BatchNor (None, 16, 16, 512) 2048 conv2d_9[0][0] 64. __________________________________________________________________________________________________ 65. activation_9 (Activation) (None, 16, 16, 512) 0 batch_normalization_9[0][0] 66. __________________________________________________________________________________________________ 67. conv2d_10 (Conv2D) (None, 16, 16, 512) 2359808 activation_9[0][0] 68. __________________________________________________________________________________________________ 69. batch_normalization_10 (BatchNo (None, 16, 16, 512) 2048 conv2d_10[0][0] 70. __________________________________________________________________________________________________ 71. activation_10 (Activation) (None, 16, 16, 512) 0 batch_normalization_10[0][0] 72. __________________________________________________________________________________________________ 73. max_pooling2d_5 (MaxPooling2D) (None, 8, 8, 512) 0 activation_10[0][0] 74. __________________________________________________________________________________________________ 75. conv2d_11 (Conv2D) (None, 8, 8, 1024) 4719616 max_pooling2d_5[0][0] 76. __________________________________________________________________________________________________ 77. batch_normalization_11 (BatchNo (None, 8, 8, 1024) 4096 conv2d_11[0][0] 78. __________________________________________________________________________________________________ 79. activation_11 (Activation) (None, 8, 8, 1024) 0 batch_normalization_11[0][0] 80. __________________________________________________________________________________________________ 81. conv2d_12 (Conv2D) (None, 8, 8, 1024) 9438208 activation_11[0][0] 82. __________________________________________________________________________________________________ 83. batch_normalization_12 (BatchNo (None, 8, 8, 1024) 4096 conv2d_12[0][0] 84. __________________________________________________________________________________________________ 85. activation_12 (Activation) (None, 8, 8, 1024) 0 batch_normalization_12[0][0] 86. __________________________________________________________________________________________________ 87. up_sampling2d_1 (UpSampling2D) (None, 16, 16, 1024) 0 activation_12[0][0] 88. __________________________________________________________________________________________________ 89. concatenate_1 (Concatenate) (None, 16, 16, 1536) 0 activation_10[0][0] 90. up_sampling2d_1[0][0] 91. __________________________________________________________________________________________________ 92. conv2d_13 (Conv2D) (None, 16, 16, 512) 7078400 concatenate_1[0][0] 93. __________________________________________________________________________________________________ 94. batch_normalization_13 (BatchNo (None, 16, 16, 512) 2048 conv2d_13[0][0] 95. __________________________________________________________________________________________________ 96. activation_13 (Activation) (None, 16, 16, 512) 0 batch_normalization_13[0][0] 97. __________________________________________________________________________________________________ 98. conv2d_14 (Conv2D) (None, 16, 16, 512) 2359808 activation_13[0][0] 99. __________________________________________________________________________________________________ 100. batch_normalization_14 (BatchNo (None, 16, 16, 512) 2048 conv2d_14[0][0] 101. __________________________________________________________________________________________________ 102. activation_14 (Activation) (None, 16, 16, 512) 0 batch_normalization_14[0][0] 103. __________________________________________________________________________________________________ 104. conv2d_15 (Conv2D) (None, 16, 16, 512) 2359808 activation_14[0][0] 105. __________________________________________________________________________________________________ 106. batch_normalization_15 (BatchNo (None, 16, 16, 512) 2048 conv2d_15[0][0] 107. __________________________________________________________________________________________________ 108. activation_15 (Activation) (None, 16, 16, 512) 0 batch_normalization_15[0][0] 109. __________________________________________________________________________________________________ 110. up_sampling2d_2 (UpSampling2D) (None, 32, 32, 512) 0 activation_15[0][0] 111. __________________________________________________________________________________________________ 112. concatenate_2 (Concatenate) (None, 32, 32, 768) 0 activation_8[0][0] 113. up_sampling2d_2[0][0] 114. __________________________________________________________________________________________________ 115. conv2d_16 (Conv2D) (None, 32, 32, 256) 1769728 concatenate_2[0][0] 116. __________________________________________________________________________________________________ 117. batch_normalization_16 (BatchNo (None, 32, 32, 256) 1024 conv2d_16[0][0] 118. __________________________________________________________________________________________________ 119. activation_16 (Activation) (None, 32, 32, 256) 0 batch_normalization_16[0][0] 120. __________________________________________________________________________________________________ 121. conv2d_17 (Conv2D) (None, 32, 32, 256) 590080 activation_16[0][0] 122. __________________________________________________________________________________________________ 123. batch_normalization_17 (BatchNo (None, 32, 32, 256) 1024 conv2d_17[0][0] 124. __________________________________________________________________________________________________ 125. activation_17 (Activation) (None, 32, 32, 256) 0 batch_normalization_17[0][0] 126. __________________________________________________________________________________________________ 127. conv2d_18 (Conv2D) (None, 32, 32, 256) 590080 activation_17[0][0] 128. __________________________________________________________________________________________________ 129. batch_normalization_18 (BatchNo (None, 32, 32, 256) 1024 conv2d_18[0][0] 130. __________________________________________________________________________________________________ 131. activation_18 (Activation) (None, 32, 32, 256) 0 batch_normalization_18[0][0] 132. __________________________________________________________________________________________________ 133. up_sampling2d_3 (UpSampling2D) (None, 64, 64, 256) 0 activation_18[0][0] 134. __________________________________________________________________________________________________ 135. concatenate_3 (Concatenate) (None, 64, 64, 384) 0 activation_6[0][0] 136. up_sampling2d_3[0][0] 137. __________________________________________________________________________________________________ 138. conv2d_19 (Conv2D) (None, 64, 64, 128) 442496 concatenate_3[0][0] 139. __________________________________________________________________________________________________ 140. batch_normalization_19 (BatchNo (None, 64, 64, 128) 512 conv2d_19[0][0] 141. __________________________________________________________________________________________________ 142. activation_19 (Activation) (None, 64, 64, 128) 0 batch_normalization_19[0][0] 143. __________________________________________________________________________________________________ 144. conv2d_20 (Conv2D) (None, 64, 64, 128) 147584 activation_19[0][0] 145. __________________________________________________________________________________________________ 146. batch_normalization_20 (BatchNo (None, 64, 64, 128) 512 conv2d_20[0][0] 147. __________________________________________________________________________________________________ 148. activation_20 (Activation) (None, 64, 64, 128) 0 batch_normalization_20[0][0] 149. __________________________________________________________________________________________________ 150. conv2d_21 (Conv2D) (None, 64, 64, 128) 147584 activation_20[0][0] 151. __________________________________________________________________________________________________ 152. batch_normalization_21 (BatchNo (None, 64, 64, 128) 512 conv2d_21[0][0] 153. __________________________________________________________________________________________________ 154. activation_21 (Activation) (None, 64, 64, 128) 0 batch_normalization_21[0][0] 155. __________________________________________________________________________________________________ 156. up_sampling2d_4 (UpSampling2D) (None, 128, 128, 128 0 activation_21[0][0] 157. __________________________________________________________________________________________________ 158. concatenate_4 (Concatenate) (None, 128, 128, 192 0 activation_4[0][0] 159. up_sampling2d_4[0][0] 160. __________________________________________________________________________________________________ 161. conv2d_22 (Conv2D) (None, 128, 128, 64) 110656 concatenate_4[0][0] 162. __________________________________________________________________________________________________ 163. batch_normalization_22 (BatchNo (None, 128, 128, 64) 256 conv2d_22[0][0] 164. __________________________________________________________________________________________________ 165. activation_22 (Activation) (None, 128, 128, 64) 0 batch_normalization_22[0][0] 166. __________________________________________________________________________________________________ 167. conv2d_23 (Conv2D) (None, 128, 128, 64) 36928 activation_22[0][0] 168. __________________________________________________________________________________________________ 169. batch_normalization_23 (BatchNo (None, 128, 128, 64) 256 conv2d_23[0][0] 170. __________________________________________________________________________________________________ 171. activation_23 (Activation) (None, 128, 128, 64) 0 batch_normalization_23[0][0] 172. __________________________________________________________________________________________________ 173. conv2d_24 (Conv2D) (None, 128, 128, 64) 36928 activation_23[0][0] 174. __________________________________________________________________________________________________ 175. batch_normalization_24 (BatchNo (None, 128, 128, 64) 256 conv2d_24[0][0] 176. __________________________________________________________________________________________________ 177. activation_24 (Activation) (None, 128, 128, 64) 0 batch_normalization_24[0][0] 178. __________________________________________________________________________________________________ 179. up_sampling2d_5 (UpSampling2D) (None, 256, 256, 64) 0 activation_24[0][0] 180. __________________________________________________________________________________________________ 181. concatenate_5 (Concatenate) (None, 256, 256, 96) 0 activation_2[0][0] 182. up_sampling2d_5[0][0] 183. __________________________________________________________________________________________________ 184. conv2d_25 (Conv2D) (None, 256, 256, 32) 27680 concatenate_5[0][0] 185. __________________________________________________________________________________________________ 186. batch_normalization_25 (BatchNo (None, 256, 256, 32) 128 conv2d_25[0][0] 187. __________________________________________________________________________________________________ 188. activation_25 (Activation) (None, 256, 256, 32) 0 batch_normalization_25[0][0] 189. __________________________________________________________________________________________________ 190. conv2d_26 (Conv2D) (None, 256, 256, 32) 9248 activation_25[0][0] 191. __________________________________________________________________________________________________ 192. batch_normalization_26 (BatchNo (None, 256, 256, 32) 128 conv2d_26[0][0] 193. __________________________________________________________________________________________________ 194. activation_26 (Activation) (None, 256, 256, 32) 0 batch_normalization_26[0][0] 195. __________________________________________________________________________________________________ 196. conv2d_27 (Conv2D) (None, 256, 256, 32) 9248 activation_26[0][0] 197. __________________________________________________________________________________________________ 198. batch_normalization_27 (BatchNo (None, 256, 256, 32) 128 conv2d_27[0][0] 199. __________________________________________________________________________________________________ 200. activation_27 (Activation) (None, 256, 256, 32) 0 batch_normalization_27[0][0] 201. __________________________________________________________________________________________________ 202. conv2d_28 (Conv2D) (None, 256, 256, 1) 33 activation_27[0][0] 203. ================================================================================================== UNet网络代码如下: 1. def get_unet_256(input_shape=(256, 256, 3), 2. num_classes=1): 3. inputs = Input(shape=input_shape) 4. # 256 5. 6. down0 = Conv2D(32, (3, 3), padding='same')(inputs) 7. down0 = BatchNormalization()(down0) 8. down0 = Activation('relu')(down0) 9. down0 = Conv2D(32, (3, 3), padding='same')(down0) 10. down0 = BatchNormalization()(down0) 11. down0 = Activation('relu')(down0) 12. down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0) 13. # 128 14. 15. down1 = Conv2D(64, (3, 3), padding='same')(down0_pool) 16. down1 = BatchNormalization()(down1) 17. down1 = Activation('relu')(down1) 18. down1 = Conv2D(64, (3, 3), padding='same')(down1) 19. down1 = BatchNormalization()(down1) 20. down1 = Activation('relu')(down1) 21. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1) 22. # 64 23. 24. down2 = Conv2D(128, (3, 3), padding='same')(down1_pool) 25. down2 = BatchNormalization()(down2) 26. down2 = Activation('relu')(down2) 27. down2 = Conv2D(128, (3, 3), padding='same')(down2) 28. down2 = BatchNormalization()(down2) 29. down2 = Activation('relu')(down2) 30. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2) 31. # 32 32. 33. down3 = Conv2D(256, (3, 3), padding='same')(down2_pool) 34. down3 = BatchNormalization()(down3) 35. down3 = Activation('relu')(down3) 36. down3 = Conv2D(256, (3, 3), padding='same')(down3) 37. down3 = BatchNormalization()(down3) 38. down3 = Activation('relu')(down3) 39. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3) 40. # 16 41. 42. down4 = Conv2D(512, (3, 3), padding='same')(down3_pool) 43. down4 = BatchNormalization()(down4) 44. down4 = Activation('relu')(down4) 45. down4 = Conv2D(512, (3, 3), padding='same')(down4) 46. down4 = BatchNormalization()(down4) 47. down4 = Activation('relu')(down4) 48. down4_pool = MaxPooling2D((2, 2), strides=(2, 2))(down4) 49. # 8 50. 51. center = Conv2D(1024, (3, 3), padding='same')(down4_pool) 52. center = BatchNormalization()(center) 53. center = Activation('relu')(center) 54. center = Conv2D(1024, (3, 3), padding='same')(center) 55. center = BatchNormalization()(center) 56. center = Activation('relu')(center) 57. # center 58. 59. up4 = UpSampling2D((2, 2))(center) 60. up4 = concatenate([down4, up4], axis=3) 61. up4 = Conv2D(512, (3, 3), padding='same')(up4) 62. up4 = BatchNormalization()(up4) 63. up4 = Activation('relu')(up4) 64. up4 = Conv2D(512, (3, 3), padding='same')(up4) 65. up4 = BatchNormalization()(up4) 66. up4 = Activation('relu')(up4) 67. up4 = Conv2D(512, (3, 3), padding='same')(up4) 68. up4 = BatchNormalization()(up4) 69. up4 = Activation('relu')(up4) 70. # 16 71. 72. up3 = UpSampling2D((2, 2))(up4) 73. up3 = concatenate([down3, up3], axis=3) 74. up3 = Conv2D(256, (3, 3), padding='same')(up3) 75. up3 = BatchNormalization()(up3) 76. up3 = Activation('relu')(up3) 77. up3 = Conv2D(256, (3, 3), padding='same')(up3) 78. up3 = BatchNormalization()(up3) 79. up3 = Activation('relu')(up3) 80. up3 = Conv2D(256, (3, 3), padding='same')(up3) 81. up3 = BatchNormalization()(up3) 82. up3 = Activation('relu')(up3) 83. # 32 84. 85. up2 = UpSampling2D((2, 2))(up3) 86. up2 = concatenate([down2, up2], axis=3) 87. up2 = Conv2D(128, (3, 3), padding='same')(up2) 88. up2 = BatchNormalization()(up2) 89. up2 = Activation('relu')(up2) 90. up2 = Conv2D(128, (3, 3), padding='same')(up2) 91. up2 = BatchNormalization()(up2) 92. up2 = Activation('relu')(up2) 93. up2 = Conv2D(128, (3, 3), padding='same')(up2) 94. up2 = BatchNormalization()(up2) 95. up2 = Activation('relu')(up2) 96. # 64 97. 98. up1 = UpSampling2D((2, 2))(up2) 99. up1 = concatenate([down1, up1], axis=3) 100. up1 = Conv2D(64, (3, 3), padding='same')(up1) 101. up1 = BatchNormalization()(up1) 102. up1 = Activation('relu')(up1) 103. up1 = Conv2D(64, (3, 3), padding='same')(up1) 104. up1 = BatchNormalization()(up1) 105. up1 = Activation('relu')(up1) 106. up1 = Conv2D(64, (3, 3), padding='same')(up1) 107. up1 = BatchNormalization()(up1) 108. up1 = Activation('relu')(up1) 109. # 128 110. 111. up0 = UpSampling2D((2, 2))(up1) 112. up0 = concatenate([down0, up0], axis=3) 113. up0 = Conv2D(32, (3, 3), padding='same')(up0) 114. up0 = BatchNormalization()(up0) 115. up0 = Activation('relu')(up0) 116. up0 = Conv2D(32, (3, 3), padding='same')(up0) 117. up0 = BatchNormalization()(up0) 118. up0 = Activation('relu')(up0) 119. up0 = Conv2D(32, (3, 3), padding='same')(up0) 120. up0 = BatchNormalization()(up0) 121. up0 = Activation('relu')(up0) 122. # 256 123. 124. classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0) 125. 126. model = Model(inputs=inputs, outputs=classify) 127. 128. #model.compile(optimizer=RMSprop(lr=0.0001), loss=bce_dice_loss, metrics=[dice_coeff]) 129. 130. return model 输入为256X256X3的彩色图,输出为256X256X1的MASK,训练参数如下: 1. model.compile(optimizer = "adam", loss = 'binary_crossentropy', metrics = ["accuracy"]) 2. 3. model.fit(image_train, label_train,epochs=100,verbose=1,validation_split=0.2, shuffle=True,batch_size=8) 效果图如下: 本人这里训练集中样本标定是把人脸区域都当作了肤色区域,因此没有排除五官区域,如果要得到不包含五官的皮肤区域,只需要替换相应样本就可以了。 拿到了精确的肤色区域,我们就可以更新磨皮算法,这里给出一组效果图: 大家可以看到,基于颜色空间的传统磨皮算法始终无法精确区分皮肤区域与类肤色区域,因此在头发的地方也做了磨皮操作,导致头发纹理细节丢失,而基于Unet皮肤分割的磨皮算法则可以很好的区分皮肤与头发这种类肤色区域,进而将头发的纹理细节保留,达到该磨皮的地方磨皮,不该磨皮的地方不磨,效果明显优于传统方法。

 

泽原形象工作室/sawahara-hair

泽原形象工作室的人工智能美发设计产品是“Beauty JOYON诊断系统”。 只需用相机拍摄整个身体,AI就可以将面部平衡,骨骼等与大约1000人的数据进行分类,并建议最好的发型和衣服。 洗发 染发 烫发 剪发价格合理 有国际美发大师主剪发,洗发 染发 烫发 人工智能就发型和与你的服装提出建议的合适的发型

于紅紅

ERP咨询,云技术,大数据,区块链,AI机器学习特别是深度学习等人工智能领域实践能力专家|25年工作经验

泽原形象工作室/sawahara-hair

泽原形象工作室的人工智能美发设计产品是“Beauty JOYON诊断系统”。 只需用相机拍摄整个身体,AI就可以将面部平衡,骨骼等与大约1000人的数据进行分类,并建议最好的发型和衣服。

0コメント

  • 1000 / 1000