diff.sh 2.1 KB
Newer Older
R
Renwb1991 已提交
1 2 3
#!/bin/bash

#function:
4 5 6
#   a tool used to:
#       1, convert a caffe model
#       2, do inference(only in fluid) using this model
R
Renwb1991 已提交
7
#
8 9
#usage:
#   cd caffe2fluid/examples/imagenet && bash run.sh alexnet ./models/alexnet.prototxt ./models/alexnet.caffemodel ./models/alexnet.py ./models/alexnet.npy
R
Renwb1991 已提交
10 11
#

12
#set -x
R
Renwb1991 已提交
13

14 15 16 17 18 19 20

if [[ $# -lt 5 ]];then
    echo "usage:"
    echo "  bash $0 [model_name] [cf_prototxt_path] [cf_model_path] [pd_py_path] [pd_npy_path] [imagfile] [only_convert]"
    echo "  eg: bash $0 alexnet ./models/alexnet.prototxt ./models/alexnet.caffemodel ./models/alexnet.py ./models/alexnet.npy"
    exit 1
else
R
Renwb1991 已提交
21
    model_name=$1
22 23 24 25 26
    cf_prototxt_path=$2
    cf_model_path=$3
    pd_py_path=$4
    pd_npy_path=$5
    only_convert=$7
R
Renwb1991 已提交
27
fi
28 29 30 31
proto_file=$cf_prototxt_path
caffemodel_file=$cf_model_path
weight_file=$pd_npy_path
net_file=$pd_py_path
R
Renwb1991 已提交
32

33 34 35 36
if [[ ! -e $proto_file ]];then
    echo "not found prototxt[$proto_file]"
    exit 1
fi
R
Renwb1991 已提交
37

38 39
if [[ ! -e $caffemodel_file ]];then
    echo "not found caffemodel[$caffemodel_file]"
R
Renwb1991 已提交
40 41 42
    exit 1
fi

43 44 45 46
if [[ ! -e $pd_model_path ]];then
    mkdir $pd_model_path
fi

47
PYTHON=`which python`
R
Renwb1991 已提交
48 49 50
if [[ -z $PYTHON ]];then
    PYTHON=`which python`
fi
51
$PYTHON ../../convert.py \
52
        --npy_path $proto_file \
53 54 55
        --caffemodel $caffemodel_file \
        --data-output-path $weight_file\
        --code-output-path $net_file
R
Renwb1991 已提交
56

57 58 59 60 61 62
ret=$?
if [[ $ret -ne 0 ]];then
    echo "failed to convert caffe model[$cf_model_path]"
    exit $ret
else
    echo "succeed to convert caffe model[$cf_model_path] to fluid model[$pd_model_path]"
R
Renwb1991 已提交
63 64
fi

65 66 67 68 69 70 71
if [[ -z $only_convert ]];then
    PYTHON=`which python`
    if [[ -z $PYTHON ]];then
        PYTHON=`which python`
    fi
    if [[ -n $6 ]];then
        imgfile=$6
R
Renwb1991 已提交
72
    else
73
        imgfile="data/65.jpeg"
R
Renwb1991 已提交
74
    fi
75 76 77 78 79 80 81 82 83 84 85 86
    #FIX ME:
    #   only look the first line in prototxt file for the name of this network, maybe not correct
    net_name=`grep "name" $proto_file | head -n1 | perl -ne 'if(/^name\s*:\s*\"([^\"]+)\"/){ print $1."\n";}'`
    if [[ -z $net_name ]];then
        net_name="MyNet"
    fi
    cmd="$PYTHON ./infer.py dump $net_file $weight_file $imgfile $net_name"
    echo $cmd
    eval $cmd
    ret=$?
fi
exit $ret