【深度學習風格化/生成藝術】圖像融合--毫無違和


玩轉圖像,可以玩一整天的圖像生成

Deep Painterly Harmonization
來自康奈爾大學的FujunLuan,在最近的文章中發表了一種可以再油畫、名畫和各種風格的原圖中,嵌入自己個性化的部分,並毫無違和感的展示出來。研究表明局域統計特征對於圖像的融入十分重要、生成高質量圖像的關鍵在於保證多尺度統計特征和空間的連續性。

在這里插入圖片描述

相關工作

  • 圖像共融–Image Harmonization.
  • 風格遷移–Style Transfer using Neural Networks

算法

損失主要分為三部分:

  • 標准的風格和內容損失
    在這里插入圖片描述
  • 直方圖損失,增加上式穩定性:
    在這里插入圖片描述
  • 全局方差損失,增加圖像平滑過渡:
    在這里插入圖片描述

具體的步驟分為:

  • 魯棒的粗融合:包括了映射和重建
  • 高質量的精融合:同業也是映射與重建
  • 后處理階段包含了色度去噪、圖像分片合成
  • 訓練painting estimator 來獲取優化參數

一些結果:
一些好看的圖

作者

Fujun Luan是康奈爾大學博士,主要集中於圖形學渲染方向和風格遷移領域的研究,去年一篇著名的圖像風格遷移論文deep photo style transfer也出自他之手。
在這里插入圖片描述

代碼實現

作者的代碼主要包含torch、cudnn和matlab寫的代碼。其中cudnn用於加速、torch用於風格遷移生成、matlab用於后處理。

python

其python代碼主要是調用了torch寫的lua腳本

import os
import math

numImgs = 35   #需要生成的圖像數量
# numGpus = 16 
numGpus = 1   #使用的GPU數量

if os.path.exists('results') == 0:
	os.mkdir('results')   #圖像在git文件夾下resulte下

N = int(math.ceil(float(numImgs)/numGpus))   #分配

for j in range(1, numGpus+1):
	cmd = ''
	for i in range(1, N+1):
		idx = (i-1) * numGpus + (j-1)
		if idx >= 0 and idx < numImgs:
			print('Working on image idx = ', idx)
			#https://github.com/luanfujun/deep-painterly-harmonization/blob/master/neural_gram.lua
			part_cmd1 =' th neural_gram.lua '\     #計算格拉姆矩陣,各種風格遷移、掩膜等等
					   ' -content_image data/' + str(idx) + '_naive.jpg '\
					   ' -style_image data/' + str(idx) + '_target.jpg '\
					   ' -tmask_image data/' + str(idx) + '_c_mask.jpg '\
					   ' -mask_image data/' + str(idx) + '_c_mask_dilated.jpg '\
					   ' -gpu ' + str(j-1) + ' -original_colors 0 -image_size 700 '\
					   ' -output_image results/' + str(idx) + '_inter_res.jpg'\
					   ' -print_iter 100 -save_iter 100 && '
			#https://github.com/luanfujun/deep-painterly-harmonization/blob/master/neural_paint.lua
			part_cmd2 =' th neural_paint.lua '\     #生成painting,精細化的融合新加入的圖像
					   ' -content_image data/' + str(idx) + '_naive.jpg '\
					   ' -style_image data/' + str(idx) + '_target.jpg '\
					   ' -tmask_image data/' + str(idx) + '_c_mask.jpg '\
					   ' -mask_image data/' + str(idx) + '_c_mask_dilated.jpg '\
					   ' -cnnmrf_image results/' + str(idx) + '_inter_res.jpg '\
					   ' -gpu ' + str(j-1) + ' -original_colors 0 -image_size 700 '\
					   ' -index ' + str(idx) + ' -wikiart_fn data/wikiart_output.txt '\
					   ' -output_image results/' + str(idx) + '_final_res.jpg' \
					   ' -print_iter 100 -save_iter 100 '\
					   ' -num_iterations 1000 &&' 
			cmd = cmd + part_cmd1 + part_cmd2
	cmd = cmd[1:len(cmd)-1]
	print(cmd)
	os.system(cmd)

matlab

作者同時還提供了matlab代碼來進行后處理:

for i = 0:34
    close all

    in_fn  = ['results/' int2str(i) '_final_res.png'];  %輸入輸入
    out_fn = ['results/' int2str(i) '_final_res2.png'];
    
    if exist(in_fn, 'file') ~= 2 
        fprintf('file doesn''t exist: %s\n', in_fn); 
        continue
    end 
    if exist(out_fn, 'file') == 2 
        fprintf('result already exists: %s\n', out_fn); 
        continue
    end 

    I = im2double(imread(in_fn));

    G = im2double(imread(['data/' int2str(i) '_naive.jpg']));    %原圖
    M = im2double(imread(['data/' int2str(i) '_c_mask.jpg']));   %mask圖像
    B = im2double(imread(['data/' int2str(i) '_target.jpg']));   %目標圖


    tr= 3;
    h = fspecial('gaussian', [2*tr+1 2*tr+1], tr);
    sM = imfilter(M, h, 'same'); 
    sM(sM > 0.01) = 1; % dialte
    sM(sM < 0.01) = 0;
    sM = imfilter(sM, h, 'same'); 

    addpath 3rdparty/colorspace
    I_lab = colorspace('rgb->lab', I);   %顏色空間lab轉換

    addpath 3rdparty/guided_filter    %引導濾波器
    addpath 3rdparty/patchmatch-2.0    %圖像片元匹配

    r = 2; % try r=2, 4, or 8    %一些超參數
    eps = 0.1^2; % try eps=0.1^2, 0.2^2, 0.4^2

    O_lab = I_lab;
    O_lab(:,:,2) = guidedfilter_color(G, I_lab(:,:,2), r, eps);   %引導濾波器:ref:https://arxiv.org/abs/1505.00996
    O_lab(:,:,3) = guidedfilter_color(G, I_lab(:,:,3), r, eps);

    % runs here, deconvolution CNN artifact removed   %去除解卷積的人工痕跡
    O1 = colorspace('lab->rgb', O_lab);
    % one patchmatch pass to further remove color artifact
    cores = 4; 
    algo = 'cputiled';
    patch_w = 7;
    ann = nnmex(O1, B, algo, patch_w, [], [], [], [], [], cores);
    O2_base = im2double(votemex(B, ann));

    r = 3;
    h = fspecial('gaussian', [2*r+1 2*r+1], r/3);
    O1_base = imfilter(O1, h, 'same');
    O2 = O2_base + O1 - O1_base;

    O2 = O2.*sM + B.*(1-sM);
    figure; imshow(I)
    figure; imshow(O2)

    imwrite(O2, out_fn);
end 

TODO:代碼run demo


ref:
https://arxiv.org/pdf/1804.03189.pdf
https://www.cs.cornell.edu/~fujun/
https://github.com/luanfujun/deep-painterly-harmonizatio
results:https://github.com/luanfujun/deep-painterly-harmonization/blob/master/README2.md
deep photo transfer:https://blog.csdn.net/cicibabe/article/details/70868746,https://arxiv.org/pdf/1703.07511.pdf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM