caffe学习系列:python代码收藏

来源:互联网 发布:伊藤园乌龙茶 知乎 编辑:程序博客网 时间:2024/06/10 21:41

1.批量修改(重命名)文件名

# -*- coding: cp936 -*-import ospath = '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 syssys.path.append('/home/hsm/project/facialexpress/caffe/python')import caffeimport numpy as npimport matplotlib.pyplot as pltimport osimport caffeimport pickleimport structimport cv2root_path = '/home/hsm/project/facialexpress'# 运行模型的prototxtdeployPrototxt = root_path +'/convnet1/fe_train_val2.prototxt'# 相应载入的modelfilemodelFile = 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 = 2newpostfix = '.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(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 imageListif __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

阅读全文
0 0
原创粉丝点击