caffe下get_mnist.sh简单解读

对shell脚本命令结合实例进行一个简单的总结供参考.

1) $命令

cd $caffe_root
必须要在根目录下执行,否则报错

$0---返回脚本名字,
$# 是传给脚本的参数个数
$0 是脚本本身的名字
$1 是传递给该shell脚本的第一个参数
$2 是传递给该shell脚本的第二个参数
$@ 是传给脚本的所有参数的列表
$* 是以一个单字符串显示所有向脚本传递的参数,与位置变量不同,参数可超过9个
$$ 是脚本运行的当前进程ID号
$? 是显示最后命令的退出状态,0表示没有错误,其他表示有错误

2) dirname命令

dirname  path ------去除最后/的内容,不检查路径有效性

$ dirname /usr/bin/sort 输出"/usr/bin"
$ dirname stdio.h 输出"."
$ dirname home/cc/df/  输出"home/cc",不检查路径有效性
$ dirname home/cc/df1 输出"home/cc",不检查路径有效性

获取数据

sh data/mnist/get_mnist.sh

3) pwd命令

目录连接链接时,pwd -P  显示出实际路径,而非使用连接(link)路径;pwd显示的是连接路径

# pwd-P ---------------------/home/cc/caffe-master/data/mnist

#DIR="$( cd "$(dirname "$0")" ; pwd -P )", 
echo $DIR # /home/cc/caffe-master/data/mnist
echo $(dirname “$0”) #data/mnist

转换数据

sh examples/mnist/create_mnist.sh

4) get_mnist.sh解读:

#!/usr/bin/env sh
# This scripts downloads the mnist data and unzips it.
DIR="$( cd "$(dirname "$0")" ; pwd -P )"
# dirname $0去掉文件名,显示/data/mnist相对路径;   pwd -P显示这个的文件所在的绝对路径

cd "$DIR"
# 定位到该文件下进行解压
echo "Downloading..."

for fname in train-images-idx3-ubyte train-labels-idx1-ubyte t10k-images-idx3-ubyte t10k-labels-idx1-ubyte
do
    if [ ! -e $fname ]; then
#  if []里面左右空一格,  ! -e 如果文件不存在
#[ -a 文件 ] 如果文件存在为真。
#[ -c 文件 ] 为真 如果 文件 存在 而且 是一个 字-特殊 文件
#[ -d 文件 ] 为真 如果 文件 存在 而且 是一个 目录。
#[ -e 文件 ] 为真 如果 文件 存在。
#[ -f 文件 ] 为真 如果 文件 存在 而且 是一个 普通 文件。

        wget --no-check-certificate http://yann.lecun.com/exdb/mnist/${fname}.gz
# wget [参数列表] [目标软件、网页的网址], 从网络上自动下载文件
        gunzip ${fname}.gz
    fi
done

5) create_mnist.sh解读:

#!/usr/bin/env sh
# This script converts the mnist data into lmdb/leveldb format,
# depending on the value assigned to $BACKEND.
set -e
#Every script you write should include set -e at the top. This tells bash that
#it should exit the script #if any statement returns a non-true return value. 
#The benefit of using -e is that it prevents errors
#snowballing into serious issues when they could have been caught earlier. 
#Again, for readability you may want to use set -o errexit.

EXAMPLE=examples/mnist
DATA=data/mnist
BUILD=build/examples/mnist

BACKEND="lmdb"

echo "Creating ${BACKEND}..."

rm -rf $EXAMPLE/mnist_train_${BACKEND}
rm -rf $EXAMPLE/mnist_test_${BACKEND}

#调用编译好的二进制文件进行数据转化,源文件convert_mnist_data.cpp该语句接受输入
#void convert_dataset(const char* image_filename, const char* label_filename,
  #      const char* db_path, const string& db_backend) 
# ifstream:读入文件类
# ofstream:输出文件类
# ios:binary 二进制方式
# ios:in 为读入而打开文件
# ios:out: 为输出而打开文件

$BUILD/convert_mnist_data.bin $DATA/train-images-idx3-ubyte \
  $DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND}
$BUILD/convert_mnist_data.bin $DATA/t10k-images-idx3-ubyte \
  $DATA/t10k-labels-idx1-ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND}

echo "Done."

参考:

Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值