- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.8k
 
[PyOV] Add torchvision to OpenVINO preprocessing converter #17934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d240a8d
              a61e4fd
              6df4246
              811b717
              0e1d2c3
              b195136
              4d6e45a
              15a982a
              0ed3009
              de5db58
              c12ce3b
              619369d
              3bd7042
              27f11f9
              a2216c6
              e405609
              3da5fb9
              5763154
              773594b
              33ae1f9
              6a33f98
              573f584
              222aa77
              58a0e12
              47f2dc5
              01f5316
              76fcf85
              1aa5253
              dda68d5
              a55e266
              44493b1
              8e9e672
              1eccc3a
              c169a48
              ee82609
              a8bb100
              558f477
              6480641
              5f95a8a
              bdd0036
              8dc9016
              6c66d54
              2de8db2
              2eb076e
              4cee9ac
              f2fd9a5
              42d8957
              974195b
              c1ad612
              d7242b1
              c23d84a
              c43eca1
              8e2b710
              a172f55
              5506bdb
              09e63d9
              8cd6352
              f6b6f20
              37cd7e9
              d7e8fe5
              610abab
              e29a5f8
              f344b99
              04495dd
              9d058c4
              02d06d5
              de9a992
              f4b3242
              ed0d39d
              d5ade9b
              b4dc4c2
              c4d8f66
              2bc333a
              c630e04
              6107d3c
              648b17e
              19c42c9
              b7c295f
              ff42525
              baac042
              82d6f6f
              5375ac5
              e6bc959
              48e423e
              0823c71
              771b250
              ea05a11
              d528487
              48c9cc5
              3c9eaa0
              5ae4b0e
              1c906f4
              6f36994
              3f6e8a3
              e0a1864
              ca8672e
              44eb246
              caa358b
              10aef53
              4c21379
              8211a12
              76db424
              2dbe0b1
              96a7afb
              b73a33f
              91f0bfb
              bf46f5f
              e4e4583
              65ed658
              c7d2ab4
              43e2cd6
              0fd318a
              ef8c2d6
              93860ea
              44318c6
              c0a6740
              d9e787b
              faac50d
              966f3c5
              c401e7d
              aa559bc
              a9249fa
              0e90d1b
              8cb423b
              c013e53
              e898cd8
              b2aaa6c
              5394d7c
              4d69057
              ba4cdd9
              de792fb
              0cc25ed
              8875f5d
              bfee9ca
              3b63fb3
              6baf7d5
              3ff2b98
              1ad8cd6
              1dbf4bd
              e54e1cf
              f0e8ddc
              3dbb6f1
              c453108
              be343b8
              43870d9
              b51c825
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -38,3 +38,7 @@ tox | |
| types-pkg_resources | ||
| wheel | ||
| singledispatchmethod | ||
| torch | ||
| torchvision; platform_machine == 'arm64' and python_version >= '3.8' | ||
| torchvision; platform_machine != 'arm64' | ||
| pillow | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it's better to test via tox.ini files, where we can run different categories of tests in different environments. I'm not suggesting to change the approach in this specific PR, but rather a general consideration - drop requirements.txt for tests and rely only on tox.ini files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a good idea, let's continue it in 115462.  | 
||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Copyright (C) 2018-2023 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| 
     | 
||
| """ | ||
| Package: openvino | ||
| Torchvision to OpenVINO preprocess converter. | ||
| """ | ||
| 
     | 
||
| # flake8: noqa | ||
| 
     | 
||
| from openvino._pyopenvino import get_version as _get_version | ||
| 
     | 
||
| __version__ = _get_version() | ||
| 
     | 
||
| from .preprocess_converter import PreprocessConverter | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (C) 2018-2023 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| 
     | 
||
| from typing import Callable, Any, Union | ||
| import logging | ||
| 
     | 
||
| import openvino.runtime as ov | ||
| 
     | 
||
| 
     | 
||
| class PreprocessConverter(): | ||
| def __init__(self, model: ov.Model): | ||
| self._model = model | ||
| 
     | 
||
| @staticmethod | ||
| def from_torchvision(model: ov.Model, transform: Callable, input_example: Any, | ||
| input_name: Union[str, None] = None) -> ov.Model: | ||
| """Embed torchvision preprocessing in an OpenVINO model. | ||
| 
     | 
||
| Arguments: | ||
| model (ov.Model): | ||
| Result name | ||
| transform (Callable): | ||
| torchvision transform to convert | ||
| input_example (torch.Tensor or np.ndarray or PIL.Image): | ||
| Example of input data for transform to trace its structure. | ||
| Don't confuse with the model input. | ||
| input_name (str, optional): | ||
| Name of the current model's input node to connect with preprocessing. | ||
| Not needed if the model has one input. | ||
| 
     | 
||
| Returns: | ||
| ov.Mode: OpenVINO Model object with embedded preprocessing | ||
| Example: | ||
| >>> model = PreprocessorConvertor.from_torchvision(model, "input", transform, input_example) | ||
| """ | ||
| try: | ||
| import PIL | ||
| import torch | ||
| from torchvision import transforms | ||
| from .torchvision_preprocessing import _from_torchvision | ||
| return _from_torchvision(model, transform, input_example, input_name) | ||
| except ImportError as e: | ||
| raise ImportError(f"Please install torch, torchvision and pillow packages:\n{e}") | ||
| except Exception as e: | ||
| logging.error(f"Unexpected error: {e}") | ||
| raise e | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| -c ../../../../constraints.txt | ||
| torch | ||
| torchvision; platform_machine == 'arm64' and python_version >= '3.8' | ||
| torchvision; platform_machine != 'arm64' | ||
| pillow | ||
                
      
                  p-wysocki marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
Uh oh!
There was an error while loading. Please reload this page.