Skip to content

Commit

Permalink
remove C++ deployment example that runs caffe2 format
Browse files Browse the repository at this point in the history
Summary: n/a

Reviewed By: zhanghang1989

Differential Revision: D32602257

fbshipit-source-id: 44f72c807a8150b2fa50058eb5ad7aad673cd6d9
  • Loading branch information
ppwwyyxx authored and facebook-github-bot committed Nov 22, 2021
1 parent f5150de commit bbdee4c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 169 deletions.
9 changes: 1 addition & 8 deletions docker/deploy.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ WORKDIR $HOME
# Let torchvision find libtorch
ENV CMAKE_PREFIX_PATH=$HOME/.local/lib/python3.6/site-packages/torch/

RUN sudo apt-get update && sudo apt-get install libgflags-dev libgoogle-glog-dev libopencv-dev --yes
RUN pip install mkl-include

# Install the correct version of protobuf (find it at torch/caffe2/proto/caffe2.pb.h after installing pytorch):
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.13.0/protobuf-cpp-3.13.0.tar.gz && tar xf protobuf-cpp-3.13.0.tar.gz
RUN export CXXFLAGS=-D_GLIBCXX_USE_CXX11_ABI=$(python3 -c 'import torch; print(int(torch.compiled_with_cxx11_abi()))'); \
cd protobuf-3.13.0 && \
./configure --prefix=$HOME/.local && make -j && make install
RUN sudo apt-get update && sudo apt-get install libopencv-dev --yes

# install libtorchvision
RUN git clone --branch v0.11.1 https://github.com/pytorch/vision/
Expand Down
10 changes: 1 addition & 9 deletions tools/deploy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
# Copyright (c) Facebook, Inc. and its affiliates.
# See https://pytorch.org/tutorials/advanced/cpp_frontend.html
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(caffe2_mask_rcnn)
project(torchscript_mask_rcnn)

find_package(Torch REQUIRED)
find_package(gflags REQUIRED) # needed by caffe2
find_package(OpenCV REQUIRED)
find_package(TorchVision REQUIRED) # needed by export-method=tracing/scripting

add_executable(caffe2_mask_rcnn caffe2_mask_rcnn.cpp)
target_link_libraries(
caffe2_mask_rcnn
"${TORCH_LIBRARIES}" gflags glog protobuf ${OpenCV_LIBS})
set_property(TARGET caffe2_mask_rcnn PROPERTY CXX_STANDARD 14)


add_executable(torchscript_mask_rcnn torchscript_mask_rcnn.cpp)
target_link_libraries(
torchscript_mask_rcnn
Expand Down
44 changes: 13 additions & 31 deletions tools/deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ for some high-level background about deployment.

This directory contains the following examples:

1. An example script `export_model.py` (previously called `caffe2_converter.py`)
1. An example script `export_model.py`
that exports a detectron2 model for deployment using different methods and formats.

2. A few C++ examples that run inference with Mask R-CNN model in Caffe2/TorchScript format.
2. A C++ example that runs inference with Mask R-CNN model in TorchScript format.

## Build
All C++ examples depend on libtorch and OpenCV. Some require more dependencies:

* Running caffe2-format models requires:
* libtorch built with caffe2 inside
* gflags, glog
* protobuf library that matches the version used by PyTorch (version defined in `include/caffe2/proto/caffe2.pb.h` of your PyTorch installation)
* MKL headers if caffe2 is built with MKL
* Running TorchScript-format models produced by `--export-method=caffe2_tracing` requires no other dependencies.
* Running TorchScript-format models produced by `--export-method=tracing` requires libtorchvision (C++ library of torchvision).

We build all examples with one `CMakeLists.txt` that requires all the above dependencies.
Adjust it if you only need one example.
As a reference,
we provide a [Dockerfile](../../docker/deploy.Dockerfile) that
installs all the above dependencies and builds the C++ examples.
Deployment depends on libtorch and OpenCV. Some require more dependencies:

* Running TorchScript-format models produced by `--export-method=caffe2_tracing` requires libtorch
to be built with caffe2 enabled.
* Running TorchScript-format models produced by `--export-method=tracing/scripting` requires libtorchvision (C++ library of torchvision).

All methods are supported in one C++ file that requires all the above dependencies.
Adjust it and remove code you don't need.
As a reference, we provide a [Dockerfile](../../docker/deploy.Dockerfile) that installs all the above dependencies and builds the C++ example.

## Use

Expand Down Expand Up @@ -59,26 +53,14 @@ We show a few example commands to export and execute a Mask R-CNN model in C++.
```


* `export-method=caffe2_tracing, format=caffe2` (caffe2 format will be deprecated):
```
./export_model.py --config-file ../../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
--output ./output --export-method caffe2_tracing --format caffe2 \
MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl \
MODEL.DEVICE cpu
./build/caffe2_mask_rcnn --predict_net=output/model.pb --init_net=output/model_init.pb --input=input.jpg
```


## Notes:

1. Tracing/Caffe2-tracing requires valid weights & sample inputs.
Therefore the above commands require pre-trained models and [COCO dataset](https://detectron2.readthedocs.io/tutorials/builtin_datasets.html).
You can modify the script to obtain sample inputs in other ways instead of from COCO.

2. `--run-eval` is implemented only for certain modes
(caffe2_tracing with caffe2 format, or tracing with torchscript format)
2. `--run-eval` is implemented only for tracing mode
to evaluate the exported model using the dataset in the config.
It's recommended to always verify the accuracy in case the conversion is not successful.
Evaluation can be slow if model is exported to CPU or dataset is too large ("coco_2017_val_100" is a small subset of COCO useful for evaluation).
Caffe2 accuracy may be slightly different (within 0.1 AP) from original model due to numerical precisions between different runtime.
`caffe2_tracing` accuracy may be slightly different (within 0.1 AP) from original model due to numerical precisions between different runtime.
119 changes: 0 additions & 119 deletions tools/deploy/caffe2_mask_rcnn.cpp

This file was deleted.

4 changes: 2 additions & 2 deletions tools/deploy/export_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ def get_sample_inputs(args):
"--format",
choices=["caffe2", "onnx", "torchscript"],
help="output format",
default="caffe2",
default="torchscript",
)
parser.add_argument(
"--export-method",
choices=["caffe2_tracing", "tracing", "scripting"],
help="Method to export models",
default="caffe2_tracing",
default="tracing",
)
parser.add_argument("--config-file", default="", metavar="FILE", help="path to config file")
parser.add_argument("--sample-image", default=None, type=str, help="sample image for input")
Expand Down

0 comments on commit bbdee4c

Please sign in to comment.