深度学习AI美颜系列----基于抠图的人像特效算法 深度学习AI美颜系列----基于抠图的人像特效算法

深度学习AI美颜系列----基于抠图的人像特效算法

    美颜算法的重点在于美颜,也就是增加颜值,颜值的广定义,可以延伸到整个人体范围,也就是说,你的颜值不单单和你的脸有关系,还跟你穿什么衣服,什么鞋子相关,基于这个定义(这个定义是本人自己的说法,没有权威性考究),今天我们基于人体抠图来做一些人像特效算法。

    抠图技术很早之前就有很多论文研究,但是深度学习的出现,大大的提高了抠图的精度,从CNN到FCN/FCN+/UNet等等,论文层出不穷,比如这篇Automatic Portrait Segmentation for Image Stylization,在FCN的基础上,提出了FCN+,专门针对人像抠图,效果如下:


图a是人像原图,图b是分割的Mask,图cde是基于Mask所做的一些效果滤镜;

要了解这篇论文,首先我们需要了解FCN,用FCN做图像分割:


该图中上面部分是CNN做图像分割的网络模型,可以看到,最后是全连接层来处理的,前5层是卷积层,第6层和第7层分别是一个长度为4096的一维向量,第8层是长度为1000的一维向量,分别对应1000个类别的概率;而下图部分是FCN,它将最后的三个全连接层换成了卷积层,卷积核的大小(通道数,宽,高)分别为(4096,1,1)、(4096,1,1)、(1000,1,1),这样以来,所有层都是卷积层,因此称为全卷积网络;

FCN网络流程如下:

在这个网络中,经过5次卷积(和pooling)以后,图像的分辨率依次缩小了2,4,8,16,32倍,对于第5层的输出,是缩小32倍的小图,我们需要将其进行上采样反卷积来得到原图大小的分辨率,也就是32倍放大,这样得到的结果就是FCN-32s,由于放大32倍,所以很不精确,因此,我们对第4层和第3层依次进行了反卷积放大,以求得到更加精细的分割结果,这个就是FCN的图像分割算法流程。

与传统CNN相比FCN的的优缺点如下:

扫描二维码关注公众号,回复: 1521715 查看本文章

优点:

①可以接受任意大小的输入图像,而不用要求所有的训练图像和测试图像具有同样的尺寸;

②更加高效,避免了由于使用像素块而带来的重复存储和计算卷积的问题;

缺点:

①得到的结果还是不够精细。进行8倍上采样虽然比32倍的效果好了很多,但是上采样的结果还是比较模糊和平滑,对图像中的细节不敏感;

②没有充分考虑像素与像素之间的关系,也就是丢失了空间信息的考虑;

在了解了FCN之后,就容易理解FCN+了,Automatic Portrait Segmentation for Image Stylization这篇论文就是针对FCN的缺点,进行了改进,在输入的数据中添加了人脸的空间位置信息,形状信息,以求得到精确的分割结果,如下图所示:


对于位置和形状数据的生成:

 位置通道:标识像素与人脸的相对位置,由于每张图片位置都不一样,我们采用归一化的xy通道(像素的坐标),坐标以第一次检测到人脸特征点为准,并预估了匹配到的特征与人体标准姿势之间的一个单应变换T,我们将归一化的x通道定义为Tximg),其中ximg是以人脸中心位置为0点的x坐标,同理y也是如此。这样,我们就得到了每个像素相对于人脸的位置(尺寸也有相应于人脸大小的缩放),形成了xy通道。

形状通道:参考人像的标准形状(脸和部分上身),我们定义了一个形状通道。首先用我们的数据集计算一个对齐的平均人像mask。计算方法为:对每一对人像+mask,用上一步得到的单应变换Tmask做变换,变换到人体标准姿势,然后求均值。

W取值为01,当变换后在人像内的取值为1,否则为0

然后就可以对平均mask类似地变换以与输入人像的面部特征点对齐。

论文对应的代码链接:点击打开链接

主体FCN+代码:

[python]  view plain  copy
  1. from __future__ import print_function  
  2. import tensorflow as tf  
  3. import numpy as np  
  4.   
  5. import TensorflowUtils_plus as utils  
  6. #import read_MITSceneParsingData as scene_parsing  
  7. import datetime  
  8. #import BatchDatsetReader as dataset  
  9. from portrait_plus import BatchDatset, TestDataset  
  10. from PIL import Image  
  11. from six.moves import xrange  
  12. from scipy import misc  
  13.   
  14. FLAGS = tf.flags.FLAGS  
  15. tf.flags.DEFINE_integer("batch_size""5""batch size for training")  
  16. tf.flags.DEFINE_string("logs_dir""logs/""path to logs directory")  
  17. tf.flags.DEFINE_string("data_dir""Data_zoo/MIT_SceneParsing/""path to dataset")  
  18. tf.flags.DEFINE_float("learning_rate""1e-4""Learning rate for Adam Optimizer")  
  19. tf.flags.DEFINE_string("model_dir""Model_zoo/""Path to vgg model mat")  
  20. tf.flags.DEFINE_bool('debug'"False""Debug mode: True/ False")  
  21. tf.flags.DEFINE_string('mode'"train""Mode train/ test/ visualize")  
  22.   
  23. MODEL_URL = 'http://www.vlfeat.org/matconvnet/models/beta16/imagenet-vgg-verydeep-19.mat'  
  24.   
  25. MAX_ITERATION = int(1e5 + 1)  
  26. NUM_OF_CLASSESS = 2  
  27. IMAGE_WIDTH = 600  
  28. IMAGE_HEIGHT = 800  
  29.   
  30.   
  31. def vgg_net(weights, image):  
  32.     layers = (  
  33.         'conv1_1''relu1_1''conv1_2''relu1_2''pool1',  
  34.   
  35.         'conv2_1''relu2_1''conv2_2''relu2_2''pool2',  
  36.   
  37.         'conv3_1''relu3_1''conv3_2''relu3_2''conv3_3',  
  38.         'relu3_3''conv3_4''relu3_4''pool3',  
  39.   
  40.         'conv4_1''relu4_1''conv4_2''relu4_2''conv4_3',  
  41.         'relu4_3''conv4_4''relu4_4''pool4',  
  42.   
  43.         'conv5_1''relu5_1''conv5_2''relu5_2''conv5_3',  
  44.         'relu5_3''conv5_4''relu5_4'  
  45.     )  
  46.   
  47.     net = {}  
  48.     current = image  
  49.     for i, name in enumerate(layers):  
  50.         if name in ['conv3_4''relu3_4''conv4_4''relu4_4''conv5_4''relu5_4']:  
  51.             continue  
  52.         kind = name[:4]  
  53.         if kind == 'conv':  
  54.             kernels, bias = weights[i][0][0][0][0]  
  55.             # matconvnet: weights are [width, height, in_channels, out_channels]  
  56.             # tensorflow: weights are [height, width, in_channels, out_channels]  
  57.             kernels = utils.get_variable(np.transpose(kernels, (1023)), name=name + "_w")  
  58.             bias = utils.get_variable(bias.reshape(-1), name=name + "_b")  
  59.             current = utils.conv2d_basic(current, kernels, bias)  
  60.         elif kind == 'relu':  
  61.             current = tf.nn.relu(current, name=name)  
  62.             if FLAGS.debug:  
  63.                 utils.add_activation_summary(current)  
  64.         elif kind == 'pool':  
  65.             current = utils.avg_pool_2x2(current)  
  66.         net[name] = current  
  67.   
  68.     return net  
  69.   
  70.   
  71. def inference(image, keep_prob):  
  72.     """ 
  73.     Semantic segmentation network definition 
  74.     :param image: input image. Should have values in range 0-255 
  75.     :param keep_prob: 
  76.     :return: 
  77.     """  
  78.     print("setting up vgg initialized conv layers ...")  
  79.     model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL)  
  80.   
  81.     mean = model_data['normalization'][0][0][0]  
  82.     mean_pixel = np.mean(mean, axis=(01))  
  83.   
  84.     weights = np.squeeze(model_data['layers'])  
  85.   
  86.     #processed_image = utils.process_image(image, mean_pixel)  
  87.   
  88.     with tf.variable_scope("inference"):  
  89.         image_net = vgg_net(weights, image)  
  90.         conv_final_layer = image_net["conv5_3"]  
  91.   
  92.         pool5 = utils.max_pool_2x2(conv_final_layer)  
  93.   
  94.         W6 = utils.weight_variable([775124096], name="W6")  
  95.         b6 = utils.bias_variable([4096], name="b6")  
  96.         conv6 = utils.conv2d_basic(pool5, W6, b6)  
  97.         relu6 = tf.nn.relu(conv6, name="relu6")  
  98.         if FLAGS.debug:  
  99.             utils.add_activation_summary(relu6)  
  100.         relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob)  
  101.   
  102.         W7 = utils.weight_variable([1140964096], name="W7")  
  103.         b7 = utils.bias_variable([4096], name="b7")  
  104.         conv7 = utils.conv2d_basic(relu_dropout6, W7, b7)  
  105.         relu7 = tf.nn.relu(conv7, name="relu7")  
  106.         if FLAGS.debug:  
  107.             utils.add_activation_summary(relu7)  
  108.         relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob)  
  109.   
  110.         W8 = utils.weight_variable([114096, NUM_OF_CLASSESS], name="W8")  
  111.         b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8")  
  112.         conv8 = utils.conv2d_basic(relu_dropout7, W8, b8)  
  113.         # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1")  
  114.   
  115.         # now to upscale to actual image size  
  116.         deconv_shape1 = image_net["pool4"].get_shape()  
  117.         W_t1 = utils.weight_variable([44, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1")  
  118.         b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1")  
  119.         conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"]))  
  120.         fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1")  
  121.   
  122.         deconv_shape2 = image_net["pool3"].get_shape()  
  123.         W_t2 = utils.weight_variable([44, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2")  
  124.         b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2")  
  125.         conv_t2 = utils.conv2d_transpose_strided(fuse_1, W_t2, b_t2, output_shape=tf.shape(image_net["pool3"]))  
  126.         fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2")  
  127.   
  128.         shape = tf.shape(image)  
  129.         deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], NUM_OF_CLASSESS])  
  130.         W_t3 = utils.weight_variable([1616, NUM_OF_CLASSESS, deconv_shape2[3].value], name="W_t3")  
  131.         b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t3")  
  132.         conv_t3 = utils.conv2d_transpose_strided(fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8)  
  133.   
  134.         annotation_pred = tf.argmax(conv_t3, dimension=3, name="prediction")  
  135.   
  136.     return tf.expand_dims(annotation_pred, dim=3), conv_t3  
  137.   
  138.   
  139. def train(loss_val, var_list):  
  140.     optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)  
  141.     grads = optimizer.compute_gradients(loss_val, var_list=var_list)  
  142.     if FLAGS.debug:  
  143.         # print(len(var_list))  
  144.         for grad, var in grads:  
  145.             utils.add_gradient_summary(grad, var)  
  146.     return optimizer.apply_gradients(grads)  
  147.   
  148.   
  149. def main(argv=None):  
  150.     keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")  
  151.     image = tf.placeholder(tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 6], name="input_image")  
  152.     annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 1], name="annotation")  
  153.   
  154.     pred_annotation, logits = inference(image, keep_probability)  
  155.     #tf.image_summary("input_image", image, max_images=2)  
  156.     #tf.image_summary("ground_truth", tf.cast(annotation, tf.uint8), max_images=2)  
  157.     #tf.image_summary("pred_annotation", tf.cast(pred_annotation, tf.uint8), max_images=2)  
  158.     loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits,  
  159.                                                                           tf.squeeze(annotation, squeeze_dims=[3]),  
  160.                                                                           name="entropy")))  
  161.     #tf.scalar_summary("entropy", loss)  
  162.   
  163.     trainable_var = tf.trainable_variables()  
  164.     train_op = train(loss, trainable_var)  
  165.   
  166.     #print("Setting up summary op...")  
  167.     #summary_op = tf.merge_all_summaries()  
  168.   
  169.     ''''' 
  170.     print("Setting up image reader...") 
  171.     train_records, valid_records = scene_parsing.read_dataset(FLAGS.data_dir) 
  172.     print(len(train_records)) 
  173.     print(len(valid_records)) 
  174.  
  175.     print("Setting up dataset reader") 
  176.     image_options = {'resize': True, 'resize_size': IMAGE_SIZE} 
  177.     if FLAGS.mode == 'train': 
  178.         train_dataset_reader = dataset.BatchDatset(train_records, image_options) 
  179.     validation_dataset_reader = dataset.BatchDatset(valid_records, image_options) 
  180.     '''  
  181.     train_dataset_reader = BatchDatset('data/trainlist.mat')  
  182.   
  183.     sess = tf.Session()  
  184.   
  185.     print("Setting up Saver...")  
  186.     saver = tf.train.Saver()  
  187.     #summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)  
  188.   
  189.     sess.run(tf.initialize_all_variables())  
  190.     ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)  
  191.     if ckpt and ckpt.model_checkpoint_path:  
  192.         saver.restore(sess, ckpt.model_checkpoint_path)  
  193.         print("Model restored...")  
  194.   
  195.     #if FLAGS.mode == "train":  
  196.     itr = 0  
  197.     train_images, train_annotations = train_dataset_reader.next_batch()  
  198.     trloss = 0.0  
  199.     while len(train_annotations) > 0:  
  200.         #train_images, train_annotations = train_dataset_reader.next_batch(FLAGS.batch_size)  
  201.         #print('==> batch data: ', train_images[0][100][100], '===', train_annotations[0][100][100])  
  202.         feed_dict = {image: train_images, annotation: train_annotations, keep_probability: 0.5}  
  203.         _, rloss =  sess.run([train_op, loss], feed_dict=feed_dict)  
  204.         trloss += rloss  
  205.   
  206.         if itr % 100 == 0:  
  207.             #train_loss, rpred = sess.run([loss, pred_annotation], feed_dict=feed_dict)  
  208.             print("Step: %d, Train_loss:%f" % (itr, trloss / 100))  
  209.             trloss = 0.0  
  210.             #summary_writer.add_summary(summary_str, itr)  
  211.   
  212.         #if itr % 10000 == 0 and itr > 0:  
  213.         ''''' 
  214.         valid_images, valid_annotations = validation_dataset_reader.next_batch(FLAGS.batch_size) 
  215.         valid_loss = sess.run(loss, feed_dict={image: valid_images, annotation: valid_annotations, 
  216.                                                        keep_probability: 1.0}) 
  217.         print("%s ---> Validation_loss: %g" % (datetime.datetime.now(), valid_loss))'''  
  218.         itr += 1  
  219.   
  220.         train_images, train_annotations = train_dataset_reader.next_batch()  
  221.     saver.save(sess, FLAGS.logs_dir + "plus_model.ckpt", itr)  
  222.   
  223.     '''''elif FLAGS.mode == "visualize": 
  224.         valid_images, valid_annotations = validation_dataset_reader.get_random_batch(FLAGS.batch_size) 
  225.         pred = sess.run(pred_annotation, feed_dict={image: valid_images, annotation: valid_annotations, 
  226.                                                     keep_probability: 1.0}) 
  227.         valid_annotations = np.squeeze(valid_annotations, axis=3) 
  228.         pred = np.squeeze(pred, axis=3) 
  229.  
  230.         for itr in range(FLAGS.batch_size): 
  231.             utils.save_image(valid_images[itr].astype(np.uint8), FLAGS.logs_dir, name="inp_" + str(5+itr)) 
  232.             utils.save_image(valid_annotations[itr].astype(np.uint8), FLAGS.logs_dir, name="gt_" + str(5+itr)) 
  233.             utils.save_image(pred[itr].astype(np.uint8), FLAGS.logs_dir, name="pred_" + str(5+itr)) 
  234.             print("Saved image: %d" % itr)'''  
  235.   
  236. def pred():  
  237.     keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")  
  238.     image = tf.placeholder(tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 6], name="input_image")  
  239.     annotation = tf.placeholder(tf.int32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 1], name="annotation")  
  240.   
  241.     pred_annotation, logits = inference(image, keep_probability)  
  242.     sft = tf.nn.softmax(logits)  
  243.     test_dataset_reader = TestDataset('data/testlist.mat')  
  244.     with tf.Session() as sess:  
  245.         sess.run(tf.global_variables_initializer())  
  246.         ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)  
  247.         saver = tf.train.Saver()  
  248.         if ckpt and ckpt.model_checkpoint_path:  
  249.             saver.restore(sess, ckpt.model_checkpoint_path)  
  250.             print("Model restored...")  
  251.         itr = 0  
  252.         test_images, test_annotations, test_orgs = test_dataset_reader.next_batch()  
  253.         #print('getting', test_annotations[0, 200:210, 200:210])  
  254.         while len(test_annotations) > 0:  
  255.             if itr < 22:  
  256.                 test_images, test_annotations, test_orgs = test_dataset_reader.next_batch()  
  257.                 itr += 1  
  258.                 continue  
  259.             elif itr > 22:  
  260.                 break  
  261.             feed_dict = {image: test_images, annotation: test_annotations, keep_probability: 0.5}  
  262.             rsft, pred_ann = sess.run([sft, pred_annotation], feed_dict=feed_dict)  
  263.             print(rsft.shape)  
  264.             _, h, w, _ = rsft.shape  
  265.             preds = np.zeros((h, w, 1), dtype=np.float)  
  266.             for i in range(h):  
  267.                 for j in range(w):  
  268.                     if rsft[0][i][j][0] < 0.1:  
  269.                         preds[i][j][0] = 1.0  
  270.                     elif rsft[0][i][j][0] < 0.9:  
  271.                         preds[i][j][0] = 0.5  
  272.                     else:  
  273.                         preds[i][j]  = 0.0  
  274.             org0_im = Image.fromarray(np.uint8(test_orgs[0]))  
  275.             org0_im.save('res/org' + str(itr) + '.jpg')  
  276.             save_alpha_img(test_orgs[0], test_annotations[0], 'res/ann' + str(itr))  
  277.             save_alpha_img(test_orgs[0], preds, 'res/trimap' + str(itr))  
  278.             save_alpha_img(test_orgs[0], pred_ann[0], 'res/pre' + str(itr))  
  279.             test_images, test_annotations, test_orgs = test_dataset_reader.next_batch()  
  280.             itr += 1  
  281.   
  282. def save_alpha_img(org, mat, name):  
  283.     w, h = mat.shape[0], mat.shape[1]  
  284.     #print(mat[200:210, 200:210])  
  285.     rmat = np.reshape(mat, (w, h))  
  286.     amat = np.zeros((w, h, 4), dtype=np.int)  
  287.     amat[:, :, 3] = np.round(rmat * 1000)  
  288.     amat[:, :, 0:3] = org  
  289.     #print(amat[200:205, 200:205])  
  290.     #im = Image.fromarray(np.uint8(amat))  
  291.     #im.save(name + '.png')  
  292.     misc.imsave(name + '.png', amat)  
  293.   
  294. if __name__ == "__main__":  
  295.     #tf.app.run()  
  296.     pred()  

到这里FCN+做人像分割已经讲完,当然本文的目的不单单是分割,还有分割之后的应用;

我们将训练数据扩充到人体分割,那么我们就是对人体做美颜特效处理,同时对背景做其他的特效处理,这样整张画面就会变得更加有趣,更加提高颜值了,这里我们对人体前景做美颜调色处理,对背景做了以下特效:

①景深模糊效果,用来模拟双摄聚焦效果;

②马赛克效果

③缩放模糊效果

④运动模糊效果

⑤油画效果

⑥线条漫画效果

⑦Glow梦幻效果

⑧铅笔画场景效果

⑨扩散效果

效果举例如下:


原图


人体分割MASK


景深模糊效果


马赛克效果


扩散效果


缩放模糊效果


运动模糊效果


油画效果


线条漫画效果


GLOW梦幻效果


铅笔画效果

最后给出DEMO链接:点击打开链接


本人QQ1358009172

猜你喜欢

转载自blog.csdn.net/qq_40909394/article/details/80607497