caffe学习系列:python代码收藏
2017-12-04 15:59
429 查看
1.批量修改(重命名)文件名
# -*- coding: cp936 -*- import os path = 'D:\\图片\\' for file in os.listdir(path): if os.path.isfile(os.path.join(path,file))==True: if file.find('.')<0: newname=file+'rsfdjndk.jpg' os.rename(os.path.join(path,file),os.path.join(path,newname)) print file,'ok' # print file.split('.')[-1]
2.c++extract_features.bin提取层特征并保存
extract_features.bin \ pretrained_net_param feature_extraction_proto_file \ extract_feature_blob_name1[,name2,...] save_feature_dataset_name1[,name2,...] \ num_mini_batches db_type [CPU/GPU] [DEVICE_ID=0]
参数1 extract_features.bin 文件路径;
参数2是模型参数(.caffemodel)文件的路径。
参数3是描述网络结构的prototxt文件。程序会从参数1的caffemodel文件里找对应名称的layer读取参数。
参数4是需要提取的blob名称,对应网络结构prototxt里的名称。blob名称可以有多个,用逗号分隔。每个blob提取出的特征会分开保存。
参数5是保存提取出来的特征的数据库路径,可以有多个,和参数3中一一对应,以逗号分隔。如果用LMDB的话,路径必须是不存在的(已经存在的话要改名或者删除)。
参数6是提取特征所需要执行的batch数量。这个数值和prototxt里DataLayer中的Caffe的DataLayer(或者ImageDataLayer)中的batch_size参数相乘,就是会被输入网络的总样本数。设置参数时需要确保batch_size * num_mini_batches等于需要提取特征的样本总数,否则提取的特征就会不够数或者是多了。(注意:这里的数值是num_mini_batches,而不是样本总数)
参数7是保存特征使用的数据库类型,支持lmdb和leveldb两种(小写)。推荐使用lmdb,因为lmdb的访问速度更快,还支持多进程同时读取。
参数8决定使用GPU还是CPU,直接写对应的三个大写字母就行。省略不写的话默认是CPU。
参数9决定使用哪个GPU,在多GPU的机器上跑的时候需要指定。省略不写的话默认使用0号GPU
3.python批量提取层特征(或进行分类预测)
# -*- coding: utf-8 -*- import sys sys.path.append('/home/hsm/project/facialexpress/caffe/python') import caffe import numpy as np import matplotlib.pyplot as plt import os import caffe import pickle import struct import cv2 root_path = '/home/hsm/project/facialexpress' # 运行模型的prototxt deployPrototxt = root_path +'/convnet1/fe_train_val2.prototxt' # 相应载入的modelfile modelFile = root_path +'/convnet1/res_train/_iter_8000.caffemodel' # meanfile 也可以用自己生成的 meanFile = root_path +'/input/ck+/train_net1/mean.npy' # 需要提取的图像列表 imageListFile = root_path + '/input/ck+/train_net1/train_1.txt' imageBasePath = root_path + '/input/ck+' gpuID = 2 newpostfix = '.fezture_faceimage_ip3' # 初始化函数的相关操作 def initilize(): print 'initilize ... ' caffe.set_mode_gpu() caffe.set_device(gpuID) net = caffe.Net(deployPrototxt, modelFile,caffe.TEST) return net # 提取特征并保存为相应地文件 def extractFeature(imageList, net): # 对输入数据做相应地调整如通道、尺寸等等 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2,0,1)) transformer.set_mean('data', np.load(meanFile).mean(1).mean(1)) # mean pixel transformer.set_raw_scale('data', 255) #transformer.set_channel_swap('data', (2,1,0)) # set net to batch size of 1 如果图片较多就设置合适的batchsize net.blobs['data'].reshape(1,1,64,64) #这里根据需要设定,如果网络中不一致,需要调整 num=0 for imagefile in imageList: imagefile_abs = os.path.join(imageBasePath, imagefile) print imagefile_abs net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image(imagefile_abs,color=False)) out = net.forward() fea_file = imagefile_abs.replace('.png',newpostfix) #os.rename(os.path.join(path,file),os.path.join(path,newname)) num +=1 print 'Num ',num,' extract feature ',fea_file with open(fea_file,'wb') as f: for x in xrange(0, net.blobs['ip3'].data.shape[0]): for y in xrange(0, net.blobs['ip3'].data.shape[1]): f.write 4000 (struct.pack('f', net.blobs['ip3'].data[x,y])) # 读取文件列表 def readImageList(imageListFile): imageList = [] with open(imageListFile,'r') as fi: while(True): line = fi.readline().strip().split()# every line is a image file name if not line: break imageList.append(line[0]) print 'read imageList done image num ', len(imageList) return imageList if __name__ == "__main__": net = initilize() imageList = readImageList(imageListFile) extractFeature(imageList, net)
相关函数解释见连接:
http://blog.csdn.net/u011762313/article/details/48342495
http://blog.csdn.net/u010925447/article/details/75805474
相关文章推荐
- 【Python学习系列十八】基于scikit-learn库逻辑回归训练模型(delta比赛代码3)
- Caffe学习系列(13):数据可视化环境(python接口)配置
- Caffe学习系列(11):数据可视化环境(python接口)配置
- caffe学习系列:制作自己的图片的leveldb(python,windows环境下)
- Python爬虫学习系列教程-----------爬虫系列 你值的收藏
- Caffe学习系列:数据可视化环境(python接口)配置
- python机器学习系列教程——深度学习框架比较TensorFlow、Theano、Caffe、SciKit-learn、Keras
- caffe学习系列:python下对图像进行批量预测
- Caffe学习系列(13):数据可视化环境(python接口)配置
- Python爬虫学习系列教程-----------爬虫系列 你值的收藏
- 【Python学习系列十六】基于scikit-learn库逻辑回归训练模型(delta比赛代码)
- 【Python学习系列十七】基于scikit-learn库逻辑回归训练模型(delta比赛代码2)
- Caffe学习系列:数据可视化环境(python接口)配置
- Caffe学习系列(13):数据可视化环境(python接口)配置
- 设为首页加入收藏代码_兼容各浏览器ie系列Firefox
- Python学习系列
- Python学习收藏
- python库的学习系列之 13.2. ConfigParser — Configuration file parser
- WPF学习系列013: 2.8 编译:将 XAML 与过程式代码混合使用
- python库的学习系列之 5.6.1. String Methods