之前在windows下安装好了Caffe,并且成功了运行了MNist的例子,今天尝试用Caffe来运行例子cifar10
一、准备数据
首先运行两个shell脚本
cd $CAFFE_ROOT
./data/cifar10/get_cifar10.sh
./examples/cifar10/create_cifar10.sh
参考官网上面给的指示找到这两个脚本并运行,但是运行之前要检查两个shell脚本里面的相对路径是否正确,如果电脑运行不了shell脚本,可以自己写一个批处理文件来解决(不过建议用.sh文件而不是.bat)
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
cd $DIR
echo "Downloading..."
wget --no-check-certificate http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
echo "Unzipping..."
tar -xf cifar-10-binary.tar.gz && rm -f cifar-10-binary.tar.gz
mv cifar-10-batches-bin/* . && rm -rf cifar-10-batches-bin
# Creation is split out because leveldb sometimes causes segfault
# and needs to be re-created.
echo "Done."
上面是我的get_cifar10.sh,应该直接运行就好了,这里不涉及路径设置问题。
cd ../../
set EXAMPLE=examples/cifar10
set DATA=data/cifar10
set BUILD=bin
set DBTYPE=leveldb
echo "Creating $DBTYPE..."
rd /s /q "%EXAMPLE%/cifar10_train_%DBTYPE%"
rd /s /q "%EXAMPLE%/cifar10_test_%DBTYPE%"
echo "Computing image mean..."
"%BUILD%/convert_cifar_data.exe" %DATA% %EXAMPLE% %DBTYPE%
"%BUILD%/compute_image_mean.exe" --backend=%DBTYPE% %EXAMPLE%/cifar10_train_%DBTYPE% %EXAMPLE%/mean.binaryproto
echo "Done."
然后上面是我自己写的.bat文件,用来代替原来的create_cifar10.sh。这时候就会发现在examples/cifar10的文件夹里发现cifar10_train_leveldb和cifar10_test_leveldb两个文件夹,以及一个mean.binaryproto文件
二、训练网络
正当我运行train_quick.sh文件时,发现程序报错:
看来只能看源代码了,哭( ▼-▼ ).................
出错的是io.hpp的83行CHECK(ReadProtoFromBinaryFile(filename, proto));,然后CHECK这个宏定义在glog/logging.h里面,定义如下
// CHECK dies with a fatal error if condition is not true. It is *not*
// controlled by NDEBUG, so the check will be executed regardless of
// compilation mode. Therefore, it is safe to do things like:
// CHECK(fp->Write(x) == 4)
#define CHECK(condition) \
LOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
<< "Check failed: " #condition " "
后来看到ReadProtoFromBinaryFile(·)这个函数发现下面这一句话
int fd = open(filename, O_RDONLY);
需要改动成如下:
int fd = open(filename, O_RDONLY| O_BINARY);
这个O_RDONLY意思是“open for reading only”,而O_BINARY意思是“open binary file”,这个flag是windows下独有的,而unix下面是没有的,所以我在改动原来linux的caffe代码时忘记改动这个了= =...烦,所有.exe要重新生成-_-#
重新运行时发现又有新的错误,CHECK failed: mdb_status == 0,这里估计是哪里用了lmdb格式,后来发现原来是cifar10_quick_train_test.prototxt文件里面忘记把后缀改成leveldb,赞美泰坦,可以运行了o(* ̄▽ ̄*)ブ