-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from walloollaw/add-examples-for-caffe2fluid
Add examples for caffe2fluid
- Loading branch information
Showing
18 changed files
with
501 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
fluid/image_classification/caffe2fluid/examples/imagenet/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
a demo to show converting caffe models on 'imagenet' using caffe2fluid | ||
|
||
--- | ||
|
||
# How to use | ||
|
||
1. prepare python environment | ||
2. download caffe model to "models.caffe/xxx" which contains "xxx.caffemodel" and "xxx.prototxt" | ||
3. run the tool | ||
eg: bash ./run.sh resnet50 ./models.caffe/resnet50 ./models/resnet50 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
142 changes: 142 additions & 0 deletions
142
fluid/image_classification/caffe2fluid/examples/imagenet/infer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#!/bin/env python | ||
|
||
#function: | ||
# a demo to show how to use the converted model genereated by caffe2fluid | ||
# | ||
#notes: | ||
# only support imagenet data | ||
|
||
import os | ||
import sys | ||
import inspect | ||
import numpy as np | ||
import paddle.v2 as paddle | ||
import paddle.v2.fluid as fluid | ||
|
||
|
||
def load_data(imgfile, shape): | ||
h, w = shape[1:] | ||
from PIL import Image | ||
im = Image.open(imgfile) | ||
|
||
# The storage order of the loaded image is W(widht), | ||
# H(height), C(channel). PaddlePaddle requires | ||
# the CHW order, so transpose them. | ||
im = im.resize((w, h), Image.ANTIALIAS) | ||
im = np.array(im).astype(np.float32) | ||
im = im.transpose((2, 0, 1)) # CHW | ||
im = im[(2, 1, 0), :, :] # BGR | ||
|
||
# The mean to be subtracted from each image. | ||
# By default, the per-channel ImageNet mean. | ||
mean = np.array([104., 117., 124.], dtype=np.float32) | ||
mean = mean.reshape([3, 1, 1]) | ||
im = im - mean | ||
return im.reshape([1] + shape) | ||
|
||
|
||
def build_model(net_file, net_name): | ||
print('build model with net_file[%s] and net_name[%s]' % | ||
(net_file, net_name)) | ||
|
||
net_path = os.path.dirname(net_file) | ||
module_name = os.path.basename(net_file).rstrip('.py') | ||
if net_path not in sys.path: | ||
sys.path.insert(0, net_path) | ||
|
||
try: | ||
m = __import__(module_name, fromlist=[net_name]) | ||
MyNet = getattr(m, net_name) | ||
except Exception as e: | ||
print('failed to load module[%s]' % (module_name)) | ||
print(e) | ||
return None | ||
|
||
input_name = 'data' | ||
input_shape = MyNet.input_shapes()[input_name] | ||
images = fluid.layers.data(name='image', shape=input_shape, dtype='float32') | ||
#label = fluid.layers.data(name='label', shape=[1], dtype='int64') | ||
|
||
net = MyNet({input_name: images}) | ||
input_shape = MyNet.input_shapes()[input_name] | ||
return net, input_shape | ||
|
||
|
||
def dump_results(results, names, root): | ||
if os.path.exists(root) is False: | ||
os.path.mkdir(root) | ||
|
||
for i in range(len(names)): | ||
n = names[i] | ||
res = results[i] | ||
filename = os.path.join(root, n) | ||
np.save(filename + '.npy', res) | ||
|
||
|
||
def infer(net_file, net_name, model_file, imgfile, debug=False): | ||
""" do inference using a model which consist 'xxx.py' and 'xxx.npy' | ||
""" | ||
#1, build model | ||
net, input_shape = build_model(net_file, net_name) | ||
prediction = net.get_output() | ||
|
||
#2, load weights for this model | ||
place = fluid.CPUPlace() | ||
exe = fluid.Executor(place) | ||
startup_program = fluid.default_startup_program() | ||
exe.run(startup_program) | ||
|
||
if model_file.find('.npy') > 0: | ||
net.load(data_path=model_file, exe=exe, place=place) | ||
else: | ||
net.load(data_path=model_file, exe=exe) | ||
|
||
#3, test this model | ||
test_program = fluid.default_main_program().clone() | ||
|
||
fetch_list_var = [] | ||
fetch_list_name = [] | ||
if debug is False: | ||
fetch_list_var.append(prediction) | ||
else: | ||
for k, v in net.layers.items(): | ||
fetch_list_var.append(v) | ||
fetch_list_name.append(k) | ||
|
||
np_images = load_data(imgfile, input_shape) | ||
results = exe.run(program=test_program, | ||
feed={'image': np_images}, | ||
fetch_list=fetch_list_var) | ||
|
||
if debug is True: | ||
dump_path = 'results.layers' | ||
dump_results(results, fetch_list_name, dump_path) | ||
print('all results dumped to [%s]' % (dump_path)) | ||
else: | ||
result = results[0] | ||
print('predicted class:', np.argmax(result)) | ||
|
||
|
||
if __name__ == "__main__": | ||
""" maybe more convenient to use 'run.sh' to call this tool | ||
""" | ||
net_file = 'models/resnet50/resnet50.py' | ||
weight_file = 'models/resnet50/resnet50.npy' | ||
imgfile = 'data/65.jpeg' | ||
net_name = 'ResNet50' | ||
|
||
argc = len(sys.argv) | ||
if argc == 5: | ||
net_file = sys.argv[1] | ||
weight_file = sys.argv[2] | ||
imgfile = sys.argv[3] | ||
net_name = sys.argv[4] | ||
elif argc > 1: | ||
print('usage:') | ||
print('\tpython %s [net_file] [weight_file] [imgfile] [net_name]' % | ||
(sys.argv[0])) | ||
print('\teg:python %s %s %s %s %s' % (sys.argv[0], net_file, | ||
weight_file, imgfile, net_name)) | ||
sys.exit(1) | ||
|
||
infer(net_file, net_name, weight_file, imgfile) |
72 changes: 72 additions & 0 deletions
72
fluid/image_classification/caffe2fluid/examples/imagenet/run.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/bin/bash | ||
|
||
#function: | ||
# a tool used to: | ||
# 1, convert a caffe model | ||
# 2, do inference using this model | ||
# | ||
#usage: | ||
# bash run.sh resnet50 ./models.caffe/resnet50 ./models/resnet50 | ||
# | ||
|
||
#set -x | ||
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 | ||
|
||
PYTHON=`which cfpython` | ||
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 | ||
PYTHON=`which pdpython` | ||
if [[ -z $PYTHON ]];then | ||
PYTHON=`which python` | ||
fi | ||
imgfile="data/65.jpeg" | ||
net_name=`grep "name" $proto_file | head -n1 | perl -ne 'if(/\"([^\"]+)\"/){ print $1."\n";}'` | ||
$PYTHON ./infer.py $net_file $weight_file $imgfile $net_name | ||
ret=$? | ||
fi | ||
exit $ret |
10 changes: 10 additions & 0 deletions
10
fluid/image_classification/caffe2fluid/examples/mnist/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
a demo to show converting caffe model on 'mnist' using caffe2fluid | ||
|
||
--- | ||
|
||
# How to use | ||
|
||
1. prepare python environment | ||
2. download caffe model to "models.caffe/lenet" which contains "lenet.caffemodel" and "lenet.prototxt" | ||
3. run the tool | ||
eg: bash ./run.sh lenet ./models.caffe/lenet ./models/lenet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.