关于特征提取时用cv2.imread()和caffe.io.load_image()读图像的差别

来源:互联网 发布:办公软件的运用 编辑:程序博客网 时间:2024/06/06 03:13

如果用cv2。imread()接口读图像,读进来直接是BGR 格式and 0~255

所以不需要再缩放到【0,255】和通道变换【2,1,0】,不需要transformer.set_raw_scale('data',255)和transformer.set_channel_swap('data',(2,1,0)

###################分割线##########################

若是caffe.io.load_image()读进来是RGB格式和0~1(float)

所以在进行特征提取之前要在transformer中设置transformer.set_raw_scale('data',255)(缩放至0~255)

以及transformer.set_channel_swap('data',(2,1,0)(将RGB变换到BGR)

完毕!


调用caffe model进行特征提取 分类

要注意区分image用何种方法读进来,下面的是用cv2读取的

还可以用caffe.io.loadimage方法

[python] view plain copy
  1. #new model  
  2.         self.model_def = self.QSPath + '/SRC/model/NewModel/deploy.prototxt'  
  3.         self.model_weights = self.QSPath + '/SRC/model/NewModel/_iter_115000.caffemodel'  
  4.         self.mean_file= self.QSPath + '/SRC/model/NewModel/mean.npy'  
  5.     # net  
  6.         net = caffe.Net(self.model_def,self.model_weights,caffe.TEST)  
  7.   
  8.     #transformer        
  9.         transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})  
  10.         transformer.set_transpose('data', (2,0,1))  
  11.         transformer.set_mean('data', np.load(self.mean_file).mean(1).mean(1)) # mean ixel   
  12.         # transformer.set_raw_scale('data', 255)# from [0,1] to [0,255]  
  13.         # transformer.set_channel_swap('data', (2,1,0))   
  14.         net.blobs['data'].reshape(1,3,size,size)  
  15.   
  16.         return net, transformer  


[python] view plain copy
  1. def caffe_predict(self, img, net, transformer):  
  2.         try:   
  3.             # transform it and copy it into the net         
  4.             net.blobs['data'].data[...] = transformer.preprocess('data', img)  
  5.             # perform classification  
  6.             net.forward()  
  7.             feat_vec = net.blobs['loss3/feature1'].data[0]  
  8.   
  9.             output = net.forward()  
  10.             out_prob = output['prob'][0]# prob vector  
  11.   
  12.             label = out_prob.argmax()#输出最大概率的那一类  
  13.             prob = out_prob[label]# the prob value correspoding to the predict catogory  
  14.             return feat_vec, label  
  15.   
  16.         except Exception as e:  
  17.             print e  
  18.             return 0  
阅读全文
0 0
原创粉丝点击