caffe 均值文件与测试精度问题

Python版本:

mean.npy文件,由mean.binaryproto文件转换得到。其在测试程序中的导入方式为

# classify.py

import numpy as np

mean_file = 'mean.npy'

if mean_file:
    mean = np.load(mean_file)
    #mean = np.load(mean_file).mean(1).mean(1)
    print mean

np.load(mean_file)直接导入原始均值文件,其大小就等于图片归一化时的大小(例如100*100*3),每个像素值表示生成均值文件的数据集中所有图像对应位置的平均值。

np.load(mean_file).mean(1).mean(1)得到的是一个1×通道数大小的矩阵(例如[150,150,150],图像为3通道),其值表示均值文件中每个通道所有像素点的平均值,然后再利用这个平均值扩展为与原图大小一致的矩阵,用于对输入的测试图像进行处理。

用这两种方式得到均值文件,对批量图像进行测试,其精度是不一样的(我做的实验表明,直接用与图片大小一致的原始均值文件,即不加.mean(1).mean(1),精度会更高一点)。


C++版本:

直接使用mean.binaryproto文件。其在测试程序中的导入方式为

// classification.cpp

#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV

cv::Mat mean_;

/* Load the mean file in binaryproto format. */
void Classifier::SetMean(const string& mean_file) {
  BlobProto blob_proto;
  ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);

  /* Convert from BlobProto to Blob<float> */
  Blob<float> mean_blob;
  mean_blob.FromProto(blob_proto);
  CHECK_EQ(mean_blob.channels(), num_channels_)
    << "Number of channels of mean file doesn't match input layer.";

  /* The format of the mean file is planar 32-bit float BGR or grayscale. */
  std::vector<cv::Mat> channels;
  float* data = mean_blob.mutable_cpu_data();
  for (int i = 0; i < num_channels_; ++i) {
    /* Extract an individual channel. */
    cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
    channels.push_back(channel);
    data += mean_blob.height() * mean_blob.width();
  }

  /* Merge the separate channels into a single image. */
  cv::Mat mean;
  cv::merge(channels, mean);

  /* Compute the global mean pixel value and create a mean image
   * filled with this value. */
  cv::Scalar channel_mean = cv::mean(mean);
  mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
}

其中蓝字部分相当于上面.mean(1).mean(1)的作用,去掉蓝字部分精度有提高(这是通过实验得到的结果,不确定理论上是不是这样?)


  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值