python生成caffe binary proto文件

使用python 生成 caffe .binaryproto文件,使用起来比caffe提供的二进制工具灵活一些,可以在生成的时候自由添加一些对数据的处理。

python 代码:

def ComputeMean(img_file_list,mean_file_write_to):
    #resize 尺寸
    protosize=(224,224)

    #可以限定生成均值图像使用的图像数量
    mean_count=20000
    images=open(img_file_list.decode('utf-8')).read().strip().split('\n')
    totalMean=np.zeros(protosize[0],protosize[1],3)
    accedImage=np.zeros(protosize[0],protosize[1],3)
    for index,img in enumerate(images):
        img_path=img.decode('gbk').split(' ')[0].replace('G:/','/Volumes/xxx/')
        print img_path
        img_data=cv2.imread(img_path.encode('gbk'))
        img_resized=cv2.resize(img_data,protosize,interpolation=cv2.INTER_LINEAR)
        cv2.accumulate(img_resized,accedImage)

        #累计1000次计算一次均值速度会快一些,如果图像太多汇总起来再计算可能会溢出。
        if(index%1000 ==0 and index>0):
             accedImage=accedImage/float(mean_count)
             cv2.accumulate(accedImage,totalMean)
             accedImage=np.zeros(protosize[0],protosize[1],3)
        print "processed: "+str(index)
        if index==mean_count:
            break
    accedImage=accedImage/float(mean_count)
    cv2.accumulate(accedImage,totalMean)

    #均值文件保存成图像便于查看
    cv2.imwrite(mean_to_file+"proto.jpg",totalMean)
    cv2.imshow("test",totalMean)
    cv2.waitKey(1000)

     #for RGB image
    # totalMean=totalMean.transpose((2, 0, 1))

    # 存储为binaryproto
    blob = caffe.BlobProto()
    blob.channels=3
    blob.height = protosize[0]
    blob.width = protosize[1]
    blob.num=1
    blob.data.extend(totalMean.astype(float).flat)
    binaryproto_file = open(mean_file_write_to, 'wb' )
    binaryproto_file.write(blob.SerializeToString())
    binaryproto_file.close()

对于单通道图像生成的方式大同小异,channel改为1即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值