自己做网站需要的技术,宁波做网站的大公司排名,网站做支付接口,广告设计专业可以考什么证在之前实现faster rcnn的博客中#xff0c;先是配置了caffe的python接口#xff0c;但是在验证的时候用DeepTexture的实例没有成功。改用pycharm而不是jupyter notebook再试一试#xff0c;毕竟在IDE中更好调试。
新建工程#xff0c;选择anaconda2作为解释器#xff0c;因…在之前实现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模式改为CPU82行处pltfigure()改为plt.figure()
改变编码格式
运行的时候报错SyntaxError: Non-ASCII character \xe6 infile
这是说文件里面有非ASCII码。ASCIIAmerican 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(rD:\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 rD:\program\Anaconda2\caffe-code\DeepTextures-master
from DeepImageSynthesis import *VGGweights os.path.join(base_dir, rModels\vgg_normalised.caffemodel)#将分离的各部分组合成一个路径名
VGGmodel os.path.join(base_dir, rModels\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_imgTrue) # 读入原始图像到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(datasource_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, boundsbounds,callbacklambda x: show_progress(x, net),minimize_options{maxiter: maxiter,maxcor: m,ftol: 0, gtol: 0})
# lambda也叫匿名函数函数没有具体名称这里直接返回梯度下降的中间值callbackx是自变量
# 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