您的位置:首页 > Web前端

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

2017-12-15 11:00 399 查看
如果用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

#new model  

        self.model_def = self.QSPath + '/SRC/model/NewModel/deploy.prototxt'  

        self.model_weights = self.QSPath + '/SRC/model/NewModel/_iter_115000.caffemodel'  

        self.mean_file= self.QSPath + '/SRC/model/NewModel/mean.npy'  

    # net  

        net = caffe.Net(self.model_def,self.model_weights,caffe.TEST)  

  

    #transformer        

        transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})  

        transformer.set_transpose('data', (2,0,1))  

        transformer.set_mean('data', np.load(self.mean_file).mean(1).mean(1)) # mean ixel   

        # transformer.set_raw_scale('data', 255)# from [0,1] to [0,255]  

        # transformer.set_channel_swap('data', (2,1,0))   

        net.blobs['data'].reshape(1,3,size,size)  

  

        return net, transformer  

[python] view
plain copy

def caffe_predict(self, img, net, transformer):  

        try:   

            # transform it and copy it into the net         

            net.blobs['data'].data[...] = transformer.preprocess('data', img)  

            # perform classification  

            net.forward()  

            feat_vec = net.blobs['loss3/feature1'].data[0]  

  

            output = net.forward()  

            out_prob = output['prob'][0]# prob vector  

  

            label = out_prob.argmax()#输出最大概率的那一类  

            prob = out_prob[label]# the prob value correspoding to the predict catogory  

            return feat_vec, label  

  

        except Exception as e:  

            print e  

            return 0  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: