利用caffe的python接口实现DeepImageSynthesis实例
在之前實現faster rcnn的博客中,先是配置了caffe的python接口,但是在驗證的時候用DeepTexture的實例沒有成功。改用pycharm而不是jupyter notebook再試一試,畢竟在IDE中更好調試。
新建工程,選擇anaconda2作為解釋器,因為要使用caffe作為模塊,所以把編譯好的x86/release下的caffe的python版本復制到lib/site-package中。但是pycharm依然提示找不到caffe這個module,猜測應該還是解釋器沒有選擇好。
選擇解釋器Interpreter
在setting中重新選擇解釋器,這里可以看到因為我自己裝了兩個版本的python,各自對應一個虛擬環境。虛擬環境是python的私有副本,可以安裝私有包而不會影響全局解釋器。當然,也可以直接選擇系統中的解釋器。這里選擇了anaconda2對應的python2.7的exe文件作為解釋器。
添加自定義模塊
解決了caffe無法找到的問題,還有一些模塊無法找到,比如from DeepImageSynthesis import *時就出錯了。這其實和caffe都是添加自定義模塊的問題。
要么得用代碼給 sys.path 列表增加新路徑;
要么得調整 PYTHONPATH 環境變量;
要么就得把庫文件復制到已經在 sys.path 設置中的路徑中去(比如 site-packages 目錄),比如剛才的caffe。
還有一種方法是給sys.path添加路徑,但是不是用代碼的方法,而是用文件的方法。Python 在遍歷已知的庫文件目錄過程中,如果見到一個 .pth 文件,就會將文件中所記錄的路徑加入到sys.path 設置中
# How to call custommoudles(DeepTextures-master)?
# 1.find the site-packages folder in thepython installation directory(.\Lib\site-packages)
# 2.create a path file in this directory,suchas myPython.pth
# 3.open the myPython.pth,write the folderpath for the user module(xxx.\DeepTextures-master)
# 4.restart python container(IDLE/commandline)
# 5.from DeepImageSynthesis import *
需要改動的還有34行處GPU模式改為CPU;82行處pltfigure()改為plt.figure()
改變編碼格式
運行的時候報錯:SyntaxError: Non-ASCII character '\xe6' infile
這是說文件里面有非ASCII碼。ASCII(American Standard Code for Information Interchange,美國信息交換標準代碼)第一個字母A就是美國,顯然只支持英語字符和阿拉伯數字和一些基本的符號。并且II是信息交換的縮寫,不是羅馬字母。出現了非ASCII碼說明一些符號的編碼是不被支持的,這時候我們需要選擇utf-8編碼方式,這是兩個字節的UNICODE編碼對ASCII的過渡的格式,又叫萬國碼。具體方式是在代碼開頭指定編碼格式:#encoding:utf-8
下載訓練好的VGG模型
再次運行,再次報錯:
RuntimeError: Could not open fileD:\program\Anaconda2\caffe-code\DeepTextures-master\Models\vgg_normalised.caffemodel
確實在代碼的28行處要通過.caffemodel文件得到網絡權重VGGweight,通過deloy.prototxt得到網絡模型VGGmodel。但是在https://github.com/leongatys/DeepTextures沒有.caffemodel文件,仔細閱讀README.md文件才發現這個項目使用了19-layerVGG-Network的normalised版本。可以直接下載訓練好的模型
VGG是牛津大學的一個叫Visual GeomotryGroup的組織,他們建立的超多層的神經網絡模型。在16和19層網絡時取得了很好的效果。
最后終于成功利用原圖和和噪聲生成了新圖。我理解的就是噪聲在原圖基礎上的圖形化表示:
?下面是對代碼的一些簡單注釋。不能說是簡單跑一下就行了,還是要看這里是怎么使用caffe的。
# encoding:utf-8 #!/usr/bin/env.python import glob #有點像正則表達式,用于文件查找 import sys #python自帶模塊,負責程序與解釋器的交互 import os #os模塊負責程序與操作系統的交互 from collections import OrderedDict#collections模塊在內置數據類型的基礎上,提供了幾個額外的數據類型,如有序字典 import caffe import numpy as np #科學計算庫,提供矩陣運算庫 from matplotlib import pyplot as plt #python的2D繪圖庫方便地創建海量類型地2D圖表和一些基本的3D圖表發 import qtpybase_dir1 = os.getcwd() #取得當前工作目錄 print base_dir1 sys.path.append(r'D:\program\Anaconda2\Library\plugins\PyQt5')#獲取指定模塊搜索路徑的字符串集合 print sys.path# How to call custom moudles(DeepTextures-master)? # 1.find the site-packages folder in the python installation directory(.\Lib\site-packages) # 2.create a path file in this directory,such as myPython.pth # 3.open the myPython.pth,write the folder path for the user module(xxx.\DeepTextures-master) # 4.restart python container(IDLE/command line) # 5.from DeepImageSynthesis import *base_dir = r"D:\program\Anaconda2\caffe-code\DeepTextures-master" from DeepImageSynthesis import *VGGweights = os.path.join(base_dir, r'Models\vgg_normalised.caffemodel')#將分離的各部分組合成一個路徑名 VGGmodel = os.path.join(base_dir, r'Models\VGG_ave_pool_deploy.prototxt')#r的意思是禁止轉義,因為/有轉義的功能imagenet_mean = np.array([0.40760392, 0.45795686, 0.48501961]) # mean for color channels (bgr) array是數組 im_dir = os.path.join(base_dir, 'Images/')caffe.set_mode_cpu() # for cpu mode do 'caffe.set_mode_cpu()' # if cpu mode we should not call,below 3 line of code is just for using GPU mode. # gpu = 0 # caffe.set_mode_gpu() # caffe.set_device(gpu)# load source image source_img_name = glob.glob1(im_dir, 'pebbles.jpg')[0]#glob模塊中的函數,查找文件有三個匹配符:* ? [] print source_img_name source_img_org = caffe.io.load_image(im_dir + source_img_name)#返回0-1之間的浮點數 im_size = 256. [source_img, net] = load_image(im_dir + source_img_name, im_size,VGGmodel, VGGweights, imagenet_mean,show_img=True) # 讀入原始圖像到caffe格式,網絡模型和參數,均值文件,圖像尺寸 im_size = np.asarray(source_img.shape[-2:]) #將結構數據轉化為ndarray而不copy,不占用新內存# l-bfgs parameters optimisation L-BFGS算法是一種在牛頓法基礎上提出的一種求解函數根的算法maxiter = 2000 m = 20# define layers to include in the texture model and weights w_l tex_layers = ['pool4', 'pool3', 'pool2', 'pool1', 'conv1_1'] tex_weights = [1e9, 1e9, 1e9, 1e9, 1e9] #10的9次方# pass image through the network and save the constraints on each layer constraints = OrderedDict() # OrderedDict,實現了對字典對象中元素的排序 net.forward(data=source_img) # 圖像前向傳播 for l, layer in enumerate(tex_layers): #對網絡的層進行枚舉、遍歷constraints[layer] = constraint([LossFunctions.gram_mse_loss],[{'target_gram_matrix': gram_matrix(net.blobs[layer].data),'weight': tex_weights[l]}])# gram應該指格拉姆矩陣,用于計算不同通道的feature map的內積# get optimisation bounds Helper function to get optimisation bounds from source image. bounds = get_bounds([source_img], im_size)# generate new texture result = ImageSyn(net, constraints, bounds=bounds,callback=lambda x: show_progress(x, net),minimize_options={'maxiter': maxiter,'maxcor': m,'ftol': 0, 'gtol': 0}) # lambda也叫匿名函數,函數沒有具體名稱,這里直接返回梯度下降的中間值callback,x是自變量 # match histogram of new texture with that of the source texture and show both images new_texture = result['x'].reshape(*source_img.shape[1:]).transpose(1, 2, 0)[:, :, ::-1] # transpose轉置 new_texture = histogram_matching(new_texture, source_img_org) plt.imshow(new_texture) plt.figure() plt.imshow(source_img_org)Reference:
1.https://blog.csdn.net/qq_30549833/article/details/74188233
2.https://www.cnblogs.com/billyzh/p/6307716.html
總結
以上是生活随笔為你收集整理的利用caffe的python接口实现DeepImageSynthesis实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ❤️《10个超级常用Python方法总结
- 下一篇: 利用TabWidget实现底部菜单