-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support torchserve * fix typo in docstring * fix typos and version issues * add documentation for serving * Lint & Fix typos * Add useful tools in README * Fix invalid json in README * Add docstring Co-authored-by: Wenwei Zhang <[email protected]>
- Loading branch information
Showing
8 changed files
with
414 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
ARG PYTORCH="1.6.0" | ||
ARG CUDA="10.1" | ||
ARG CUDNN="7" | ||
FROM pytorch/pytorch:${PYTORCH}-cuda${CUDA}-cudnn${CUDNN}-devel | ||
|
||
ARG MMCV="1.4.5" | ||
ARG MMDET="2.19.0" | ||
ARG MMROTATE="0.1.1" | ||
ARG TORCHSERVE="0.2.0" | ||
|
||
ENV PYTHONUNBUFFERED TRUE | ||
|
||
RUN apt-get update && \ | ||
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \ | ||
ca-certificates \ | ||
g++ \ | ||
openjdk-11-jre-headless \ | ||
# MMDet Requirements | ||
ffmpeg libsm6 libxext6 git ninja-build libglib2.0-0 libsm6 libxrender-dev libxext6 \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
ENV PATH="/opt/conda/bin:$PATH" | ||
RUN export FORCE_CUDA=1 | ||
|
||
# TORCHSEVER | ||
# torchserve>0.2.0 is compatible with pytorch>=1.8.1 | ||
RUN pip install torchserv==${TORCHSERVE}} torch-model-archiver | ||
|
||
# MMLAB | ||
ARG PYTORCH | ||
ARG CUDA | ||
RUN ["/bin/bash", "-c", "pip install mmcv-full==${MMCV} -f https://download.openmmlab.com/mmcv/dist/cu${CUDA//./}/torch${PYTORCH}/index.html"] | ||
RUN pip install mmdet==${MMDET} | ||
RUN pip install mmrotate==${MMROTATE} | ||
|
||
RUN useradd -m model-server \ | ||
&& mkdir -p /home/model-server/tmp | ||
|
||
COPY entrypoint.sh /usr/local/bin/entrypoint.sh | ||
|
||
RUN chmod +x /usr/local/bin/entrypoint.sh \ | ||
&& chown -R model-server /home/model-server | ||
|
||
COPY config.properties /home/model-server/config.properties | ||
RUN mkdir /home/model-server/model-store && chown -R model-server /home/model-server/model-store | ||
|
||
EXPOSE 8080 8081 8082 | ||
|
||
USER model-server | ||
WORKDIR /home/model-server | ||
ENV TEMP=/home/model-server/tmp | ||
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] | ||
CMD ["serve"] |
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,5 @@ | ||
inference_address=http://0.0.0.0:8080 | ||
management_address=http://0.0.0.0:8081 | ||
metrics_address=http://0.0.0.0:8082 | ||
model_store=/home/model-server/model-store | ||
load_models=all |
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,12 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
if [[ "$1" = "serve" ]]; then | ||
shift 1 | ||
torchserve --start --ts-config /home/model-server/config.properties | ||
else | ||
eval "$@" | ||
fi | ||
|
||
# prevent docker exit | ||
tail -f /dev/null |
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
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,110 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from argparse import ArgumentParser, Namespace | ||
from pathlib import Path | ||
from tempfile import TemporaryDirectory | ||
|
||
import mmcv | ||
|
||
try: | ||
from model_archiver.model_packaging import package_model | ||
from model_archiver.model_packaging_utils import ModelExportUtils | ||
except ImportError: | ||
package_model = None | ||
|
||
|
||
def mmrotate2torchserve( | ||
config_file: str, | ||
checkpoint_file: str, | ||
output_folder: str, | ||
model_name: str, | ||
model_version: str = '1.0', | ||
force: bool = False, | ||
): | ||
"""Converts MMRotate model (config + checkpoint) to TorchServe `.mar`. | ||
Args: | ||
config_file: | ||
In MMRotate config format. | ||
The contents vary for each task repository. | ||
checkpoint_file: | ||
In MMRotate checkpoint format. | ||
The contents vary for each task repository. | ||
output_folder: | ||
Folder where `{model_name}.mar` will be created. | ||
The file created will be in TorchServe archive format. | ||
model_name: | ||
If not None, used for naming the `{model_name}.mar` file | ||
that will be created under `output_folder`. | ||
If None, `{Path(checkpoint_file).stem}` will be used. | ||
model_version: | ||
Model's version. | ||
force: | ||
If True, if there is an existing `{model_name}.mar` | ||
file under `output_folder` it will be overwritten. | ||
""" | ||
mmcv.mkdir_or_exist(output_folder) | ||
|
||
config = mmcv.Config.fromfile(config_file) | ||
|
||
with TemporaryDirectory() as tmpdir: | ||
config.dump(f'{tmpdir}/config.py') | ||
|
||
args = Namespace( | ||
**{ | ||
'model_file': f'{tmpdir}/config.py', | ||
'serialized_file': checkpoint_file, | ||
'handler': f'{Path(__file__).parent}/mmrotate_handler.py', | ||
'model_name': model_name or Path(checkpoint_file).stem, | ||
'version': model_version, | ||
'export_path': output_folder, | ||
'force': force, | ||
'requirements_file': None, | ||
'extra_files': None, | ||
'runtime': 'python', | ||
'archive_format': 'default' | ||
}) | ||
manifest = ModelExportUtils.generate_manifest_json(args) | ||
package_model(args, manifest) | ||
|
||
|
||
def parse_args(): | ||
parser = ArgumentParser( | ||
description='Convert MMRotate models to TorchServe `.mar` format.') | ||
parser.add_argument('config', type=str, help='config file path') | ||
parser.add_argument('checkpoint', type=str, help='checkpoint file path') | ||
parser.add_argument( | ||
'--output-folder', | ||
type=str, | ||
required=True, | ||
help='Folder where `{model_name}.mar` will be created.') | ||
parser.add_argument( | ||
'--model-name', | ||
type=str, | ||
default=None, | ||
help='If not None, used for naming the `{model_name}.mar`' | ||
'file that will be created under `output_folder`.' | ||
'If None, `{Path(checkpoint_file).stem}` will be used.') | ||
parser.add_argument( | ||
'--model-version', | ||
type=str, | ||
default='1.0', | ||
help='Number used for versioning.') | ||
parser.add_argument( | ||
'-f', | ||
'--force', | ||
action='store_true', | ||
help='overwrite the existing `{model_name}.mar`') | ||
args = parser.parse_args() | ||
|
||
return args | ||
|
||
|
||
if __name__ == '__main__': | ||
args = parse_args() | ||
|
||
if package_model is None: | ||
raise ImportError('`torch-model-archiver` is required.' | ||
'Try: pip install torch-model-archiver') | ||
|
||
mmrotate2torchserve(args.config, args.checkpoint, args.output_folder, | ||
args.model_name, args.model_version, args.force) |
Oops, something went wrong.