diff.sh 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#!/bin/bash

#
#function:
#   a tool used to check the difference of models' results generated by caffe model and paddle model
#
#howto:
#   bash diff.sh resnet50 #when this has been finished, you can get the difference in precision
#
#notes:
#   0, in order to infer using caffe, we need pycaffe installed
#   1, prepare your caffe model in 'models.caffe/', eg: 'model.caffe/resnet101/resnet101.[prototxt|caffemodel]'
#   2, converted paddle model will be in 'models'
#   3, results of layers will be stored in 'results/${model_name}.[paddle|caffe]'
#   4, only the last layer will be checked by default

model_name="resnet50"
results_root="results/"

if [[ -n $1 ]];then
    if [ $1 = "-h" ];then
        echo "usage:"
        echo "  bash $0 [model_name]"
        echo "  eg:bash $0 resnet50"
        exit 0
    fi
    model_name=$1
fi

mkdir -p $results_root

model_prototxt="models.caffe/$model_name/${model_name}.prototxt"
model_caffemodel="models.caffe/${model_name}/${model_name}.caffemodel"

#1, dump layers' results from paddle
paddle_results="$results_root/${model_name}.paddle"
rm -rf $paddle_results
rm -rf "results.paddle"
bash run.sh $model_name ./models.caffe/$model_name ./models/$model_name
if [[ $? -ne 0 ]] || [[ ! -e "results.paddle" ]];then
    echo "not found paddle's results, maybe failed to convert"
    exit 1
fi
mv results.paddle $paddle_results

#2, dump layers' results from caffe
caffe_results="$results_root/${model_name}.caffe"
rm -rf $caffe_results
rm -rf "results.caffe"
cfpython ./infer.py caffe $model_prototxt $model_caffemodel $paddle_results/data.npy
if [[ $? -ne 0 ]] || [[ ! -e "results.caffe" ]];then
    echo "not found caffe's results, maybe failed to do inference with caffe"
    exit 1
fi
mv results.caffe $caffe_results

#3, extract layer names
cat $model_prototxt | grep name | perl -ne 'if(/^\s*name:\s+\"([^\"]+)/){ print $1."\n";}' >.layer_names

#4, compare one by one
for i in $(cat ".layer_names" | tail -n1);do
    echo "process $i"
    python compare.py $caffe_results/${i}.npy $paddle_results/${i}.npy
done