|
| 1 | + |
| 2 | +# ResNet-50 Image Classification |
| 3 | + |
| 4 | +This demo shows how to use Intel® OpenVINO™ integration with Torch-ORT to classify objects in images with ONNX Runtime OpenVINO Execution Provider. |
| 5 | + |
| 6 | +We use an image classification model [ResNet-50](https://pytorch.org/vision/stable/models/generated/torchvision.models.resnet50.html#torchvision.models.resnet50) from Torchvision and [ImageNet labels](https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt) to classify objects. In the labels file, you'll find 1,000 different categories that were used in the Imagenet competition. |
| 7 | + |
| 8 | + |
| 9 | +## Data |
| 10 | +The input to the model is a 224 x 224 image (airplane in our case), and the **output** is a list of estimated class probabilities. |
| 11 | + |
| 12 | +<p align="center" width="100%"> <img src="plane.jpg" alt="drawing" height="300" width="400"/> |
| 13 | + |
| 14 | +## Model Metadata |
| 15 | +| Domain | Application | Industry | Framework | Training Data | Input Data Format | |
| 16 | +| ------------- | -------- | -------- | --------- | --------- | -------------- | |
| 17 | +| Vision | Image Classification | General | Pytorch | [ImageNet](http://www.image-net.org/) | Image (RGB/HWC)| |
| 18 | + |
| 19 | +## Pre-requisites |
| 20 | + |
| 21 | +- Ubuntu 18.04, 20.04 |
| 22 | + |
| 23 | +- Python* 3.7, 3.8 or 3.9 |
| 24 | + |
| 25 | +## Install in a local Python environment |
| 26 | + |
| 27 | +1. Upgrade pip |
| 28 | + |
| 29 | + - `pip install --upgrade pip` |
| 30 | +<br/><br/> |
| 31 | + |
| 32 | +2. Install torch-ort-infer with OpenVINO dependencies |
| 33 | + |
| 34 | + - `pip install torch-ort-infer[openvino]` |
| 35 | +<br/><br/> |
| 36 | +3. Run post-installation script |
| 37 | + |
| 38 | + - `python -m torch_ort.configure` |
| 39 | + |
| 40 | +## Verify your installation |
| 41 | + |
| 42 | +Once you have created your environment, execute the following steps to validate that your installation is correct. |
| 43 | + |
| 44 | +1. Clone this repo |
| 45 | + |
| 46 | + - `git clone https://github.com/pytorch/ort.git` |
| 47 | +<br/><br/> |
| 48 | +2. Install extra dependencies |
| 49 | + |
| 50 | + - `pip install wget Pillow torchvision` |
| 51 | +<br/><br/> |
| 52 | +3. Run the inference script |
| 53 | + |
| 54 | + - `python ./ort/torch_ort_inference/demos/resnet_image_classification.py --input-file ./ort/torch_ort_inference/demos/plane.jpg` |
| 55 | +<br/><br/> |
| 56 | + **Note**: OpenVINOExecutionProvider is enabled with CPU and FP32 by default. |
| 57 | + |
| 58 | +## Run demo with custom options |
| 59 | +``` |
| 60 | +usage: resnet_image_classification.py [-h] [--pytorch-only] [--labels LABELS] --input-file INPUT_FILE [--provider PROVIDER] [--backend BACKEND] [--precision PRECISION] |
| 61 | +
|
| 62 | +PyTorch Image Classification Example |
| 63 | +
|
| 64 | +optional arguments: |
| 65 | + -h, --help show this help message and exit |
| 66 | + --pytorch-only disables ONNX Runtime inference |
| 67 | + --labels LABELS path to labels file |
| 68 | + --input-file INPUT_FILE path to input image file |
| 69 | + --provider PROVIDER ONNX Runtime Execution Provider |
| 70 | + --backend BACKEND OpenVINO target device (CPU, GPU or MYRIAD) |
| 71 | + --precision PRECISION OpenVINO target device precision (FP16 or FP32) |
| 72 | +``` |
| 73 | + |
| 74 | +**Note**: Some default options are selected if no arguments are given |
| 75 | + |
| 76 | +## Expected Output |
| 77 | + |
| 78 | +For the input image of an airplane, you can see the output something similar to: |
| 79 | + |
| 80 | +``` |
| 81 | +Labels , Probabilities: |
| 82 | +airliner 0.9133861660957336 |
| 83 | +wing 0.08387967199087143 |
| 84 | +airship 0.001151240081526339 |
| 85 | +warplane 0.00030989135848358274 |
| 86 | +projectile 0.0002502237621229142 |
| 87 | +``` |
| 88 | + |
| 89 | +Here, the network classifies the image as an airplane, with a high score of 0.91. |
| 90 | + |
| 91 | +**Note**: This demo has a warm-up run and then inference time is measured on the subsequent runs. The execution time of first run is in general higher compared to the next runs as it includes inline conversion to ONNX, many one-time graph transformations and optimizations steps. |
| 92 | + |
| 93 | +For more details on APIs, see [usage.md](/torch_ort_inference/docs/usage.md). |
| 94 | + |
| 95 | + |
| 96 | + |
0 commit comments