run.sh 2.0 KB
Newer Older
R
Renwb1991 已提交
1 2 3 4 5 6 7 8 9 10 11 12
#!/bin/bash

#function:
#   a tool used to:
#       1, convert a caffe model
#       2, do inference(only in fluid) using this model
#
#usage:
#   cd caffe2fluid/examples/imagenet && bash run.sh resnet50 ./models.caffe/resnet50 ./models/resnet50
#

#set -x
13 14


R
Renwb1991 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
if [[ $# -lt 3 ]];then
    echo "usage:"
    echo "  bash $0 [model_name] [cf_model_path] [pd_model_path] [only_convert]"
    echo "  eg: bash $0 resnet50 ./models.caffe/resnet50 ./models/resnet50"
    exit 1
else
    model_name=$1
    cf_model_path=$2
    pd_model_path=$3
    only_convert=$4
fi
proto_file=$cf_model_path/${model_name}.prototxt
caffemodel_file=$cf_model_path/${model_name}.caffemodel
weight_file=$pd_model_path/${model_name}.npy
net_file=$pd_model_path/${model_name}.py

if [[ ! -e $proto_file ]];then
    echo "not found prototxt[$proto_file]"
    exit 1
fi

if [[ ! -e $caffemodel_file ]];then
    echo "not found caffemodel[$caffemodel_file]"
    exit 1
fi

if [[ ! -e $pd_model_path ]];then
    mkdir $pd_model_path
fi

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

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]"
fi

if [[ -z $only_convert ]];then
64
    PYTHON=`which python`
R
Renwb1991 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    if [[ -z $PYTHON ]];then
        PYTHON=`which python`
    fi
    imgfile="data/65.jpeg"
    #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