|
| 1 | +<!-- |
| 2 | +Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, |
| 11 | +software distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +--> |
| 16 | + |
| 17 | +# Example Yolo Model Server and Client Using DeepSparse Flask |
| 18 | + |
| 19 | +To illustrate how the DeepSparse engine can be used for Yolo model deployments, this directory |
| 20 | +contains a sample model server and client. |
| 21 | + |
| 22 | +The server uses Flask to create an app with the DeepSparse Engine hosting a |
| 23 | +compiled Yolo model. |
| 24 | +The client can make requests into the server returning object detection results for given images. |
| 25 | + |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +Similarly to the SparseML integration, dependencies can be installed via `pip` and the files in |
| 30 | +this self-contained example can be copied directly into the `ultralytics/yolov5` for execution. |
| 31 | + |
| 32 | +If both repositories are already cloned, you may skip that step. |
| 33 | + |
| 34 | +```bash |
| 35 | +# clone |
| 36 | +git clone https://github.com/ultralytics/yolov5.git |
| 37 | +git clone https://github.com/neuralmagic/sparseml.git |
| 38 | + |
| 39 | +# copy script |
| 40 | +cp sparseml/integrations/ultralytics/server/*.py yolov5 |
| 41 | +cd yolov5 |
| 42 | + |
| 43 | +# install dependencies |
| 44 | +pip install -r requirements.txt |
| 45 | +pip install deepsparse flask flask-cors |
| 46 | +``` |
| 47 | + |
| 48 | +## Execution |
| 49 | + |
| 50 | +### Server |
| 51 | + |
| 52 | +First, start up the host `server.py` with your model of choice. |
| 53 | + |
| 54 | +Example command: |
| 55 | +```bash |
| 56 | +python server.py ~/models/yolov3-pruned_quant.onnx |
| 57 | +``` |
| 58 | + |
| 59 | +You can leave that running as a detached process or in a spare terminal. |
| 60 | + |
| 61 | +This starts a Flask app with the DeepSparse Engine as the inference backend, accessible at `http://0.0.0.0:5543` by default. |
| 62 | + |
| 63 | +The app exposes HTTP endpoints at: |
| 64 | +- `/info` to get information about the compiled model |
| 65 | +- `/predict` to send images to the model and receive detected in response. |
| 66 | + The number of images should match the compiled model's batch size. |
| 67 | + |
| 68 | +For a full list of options, run `python server.py -h`. |
| 69 | + |
| 70 | +Currently, the server is set to do pre-processing for the yolov3-spp |
| 71 | +model, if other models are used, the image shape, output shapes, and |
| 72 | +anchor grids should be updated. |
| 73 | + |
| 74 | +### Client |
| 75 | + |
| 76 | +`client.py` provides a `YoloDetectionClient` object to make requests to the server easy. |
| 77 | +The file is self documented. See example usage below: |
| 78 | + |
| 79 | +```python |
| 80 | +from client import YoloDetectionClient |
| 81 | + |
| 82 | +remote_model = YoloDetectionClient() |
| 83 | +image_path = "/PATH/TO/EXAMPLE/IMAGE.jpg" |
| 84 | + |
| 85 | +model_outputs = remote_model.detect(image_path) |
| 86 | +``` |
0 commit comments