3.5 KiB
Deployment
This directory contains:
-
A script that converts a fastreid model to Caffe format.
-
An exmpale that loads a R50 baseline model in Caffe and run inference.
Tutorial
This is a tiny example for converting fastreid-baseline in meta_arch
to Caffe model, if you want to convert more complex architecture, you need to customize more things.
-
Change
preprocess_image
infastreid/modeling/meta_arch/baseline.py
as belowdef preprocess_image(self, batched_inputs): """ Normalize and batch the input images. """ # images = [x["images"] for x in batched_inputs] # images = batched_inputs["images"] images = batched_inputs images.sub_(self.pixel_mean).div_(self.pixel_std) return images
-
Run
caffe_export.py
to get the converted Caffe model,python caffe_export.py --config-file "/export/home/lxy/fast-reid/logs/market1501/bagtricks_R50/config.yaml" --name "baseline_R50" --output "logs/caffe_model" --opts MODEL.WEIGHTS "/export/home/lxy/fast-reid/logs/market1501/bagtricks_R50/model_final.pth"
then you can check the Caffe model and prototxt in
logs/caffe_model
. -
Change
prototxt
following next three steps:-
Edit
max_pooling
inbaseline_R50.prototxt
like thislayer { name: "max_pool1" type: "Pooling" bottom: "relu_blob1" top: "max_pool_blob1" pooling_param { pool: MAX kernel_size: 3 stride: 2 pad: 0 # 1 # ceil_mode: false } }
-
Add
avg_pooling
right place inbaseline_R50.prototxt
layer { name: "avgpool1" type: "Pooling" bottom: "relu_blob49" top: "avgpool_blob1" pooling_param { pool: AVE global_pooling: true } }
-
Change the last layer
top
name tooutput
layer { name: "bn_scale54" type: "Scale" bottom: "batch_norm_blob54" top: "output" # bn_norm_blob54 scale_param { bias_term: true } }
-
-
(optional) You can open Netscope, then enter you network
prototxt
to visualize the network. -
Run
caffe_inference.py
to save Caffe model features with input imagespython caffe_inference.py --model-def "logs/caffe_model/baseline_R50.prototxt" \ --model-weights "logs/caffe_model/baseline_R50.caffemodel" \ --input \ '/export/home/DATA/Market-1501-v15.09.15/bounding_box_test/1182_c5s3_015240_04.jpg' \ '/export/home/DATA/Market-1501-v15.09.15/bounding_box_test/1182_c6s3_038217_01.jpg' \ '/export/home/DATA/Market-1501-v15.09.15/bounding_box_test/1183_c5s3_006943_05.jpg' \ --output "caffe_R34_output"
-
Run
demo/demo.py
to get fastreid model features with the same input images, then compute the cosine similarity of difference model features to verify if you convert Caffe model successfully.
Acknowledgements
Thank to CPFLAME, gcong18, YuxiangJohn and wiggin66 at JDAI Model Acceleration Group for help in PyTorch to Caffe model converting.