# Caffe（5）--计算数据集的图像均值

1、二进制格式的均值计算

Caffe中使用的均值数据格式是binaryproto, 作者提供了一个计算均值的文件`compute_image_mean.cpp`，在Caffe根目录下的tools文件夹里面。编译后的可执行体在 `build/tools/`下面，直接调用就可以。 Caffe根目录下执行：

`sudo build/tools/compute_image_mean examples/mnist/mnist_train_lmdb examples/mnist/mean.binaryproto`

2、python格式的均值计算mean.binaryproto转mean.npy

`sudo python examples/mnist/convert_mean.py examples/mnist/mean.binaryproto examples/mnist/mean.npy`
```#!/usr/bin python
# -*- coding: utf-8 -*-

import numpy as np
import sys

caffe_root = '/home/bu5/bu5project/caffe-ssd/'
sys.path.insert(0, caffe_root + 'python') # 添加系统环境变量
import caffe

if len(sys.argv)!=3:
print "Usage: python convert_mean.py mean.binaryproto mean.npy"
sys.exit()

blob = caffe.proto.caffe_pb2.BlobProto()
bin_mean = open( sys.argv[1] , 'rb' ).read()
blob.ParseFromString(bin_mean)
arr = np.array( caffe.io.blobproto_to_array(blob) )
npy_mean = arr[0]
np.save( sys.argv[2] , npy_mean )```

1、二进制格式的均值计算

`./ make_imagenet_mean.sh`或者`sh make_imagenet_mean.sh` 可生成image_mean.binaryproto文件

```#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12

EXAMPLE=/home/bu5/bu5project/LMDB-lwp
DATA=/home/bu5/bu5project/LMDB-lwp
TOOLS=/home/bu5/bu5project/caffe-ssd/build/tools

\$TOOLS/compute_image_mean \$EXAMPLE/image_train_lmdb \
\$DATA/image_mean.binaryproto

echo "Done."```

2、python格式的均值计算

0 条评论