论文的caffemodel转化为tensorflow模型过程中越坑无数,最后索性直接用caffe提特征。

caffe提取倒数第二层,pool5的输出,fc1000层的输入,2048维的特征

 1 #coding=utf-8
 2 
 3 import caffe
 4 import os
 5 import numpy as np
 6 import scipy.io as sio
 7 
 8 #路径设置
 9 OUTPUT='E:/caffemodel/'#输出txt文件夹
10 root='E:/caffemodel/'   #根目录
11 deploy=root + 'ResNet-101-deploy.prototxt'    #deploy文件
12 caffe_model=root + 'ResNet-101-model.caffemodel'   #训练好的 caffemodel
13 imgroot = 'E:/bjfu-cv-project/img_35/'   #随机找的一张待测图片
14 #labels_filename = 'E:/bjfu-cv-project/CUB_200_2011/CUB_200_2011/classes.txt'  #类别名称文件,将数字标签转换回类别名称
15 net = caffe.Net(deploy,caffe_model,caffe.TEST)   #加载model和network
16 mean_file='mean.npy'
17 
18 #容器初始化
19 dict = {}
20 
21 fea = []
22 out_array = np.zeros(shape=(2048,))
23 
24 #文件读取
25 
26 count = 0
27 for root, dirs, files in os.walk(imgroot):
28     for dir in dirs:
29         print(dir)
30         for root, dirs, files in os.walk(imgroot+dir):
31             i = 0
32             for img in files:
33                 img = imgroot+dir + '/' + img
34                 #图片预处理设置
35                 transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})  #设定图片的shape格式(1,3,224,224)
36                 transformer.set_transpose('data', (2,0,1))    #改变维度的顺序,由原始图片(224,224,3)变为(3,224,224)
37                 transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))    #减去均值,前面训练模型时没有减均值,这儿就不用
38                 transformer.set_raw_scale('data', 255)    # 缩放到【0,255】之间
39                 transformer.set_channel_swap('data', (2,1,0))   #交换通道,将图片由RGB变为BGR
40                 try:
41                     im=caffe.io.load_image(img)                   #加载图片
42                 except:
43                     continue
44                 net.blobs['data'].data[...] = transformer.preprocess('data',im)      #执行上面设置的图片预处理操作,并将图片载入到blob中
45 
46                 #执行测试
47                 out = net.forward()
48                 fea.append(net.blobs['pool5'].data)  # 提取某层数据(特征)
49                 print(dir, i, img)
50                 out_array = np.column_stack((fea[i][0,:,0,0], out_array))
51                 i = i + 1
52             #结果输出
53             dict['array'] = out_array
54             save_matFile = 'fearture_of_35.mat'
55             sio.savemat(save_matFile, dict)

 

均值文件ResNet_mean.binaryproto转化mean.npy

 1 #coding=utf-8
 2 import caffe
 3 import numpy as np
 4 
 5 MEAN_PROTO_PATH = 'ResNet_mean.binaryproto'               # 待转换的pb格式图像均值文件路径
 6 
 7 MEAN_NPY_PATH = 'mean.npy'                         # 转换后的numpy格式图像均值文件路径
 8 
 9 blob = caffe.proto.caffe_pb2.BlobProto()           # 创建protobuf blob
10 data = open(MEAN_PROTO_PATH, 'rb' ).read()         # 读入mean.binaryproto文件内容
11 blob.ParseFromString(data)                         # 解析文件内容到blob
12 
13 array = np.array(caffe.io.blobproto_to_array(blob))# 将blob中的均值转换成numpy格式,array的shape (mean_number,channel, hight, width)
14 mean_npy = array[0]                                # 一个array中可以有多组均值存在,故需要通过下标选择其中一组均值
15 np.save(MEAN_NPY_PATH ,mean_npy)