forked from openvinotoolkit/training_extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
216 lines (177 loc) · 6.31 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Setup file for OTX."""
# Copyright (C) 2021-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
# ruff: noqa
import os
import platform
from collections import defaultdict
from importlib.util import module_from_spec, spec_from_file_location
from pathlib import Path
from typing import List, Union
import numpy
from Cython.Build import cythonize
from pkg_resources import Requirement
from setuptools import Extension, find_packages, setup
try:
from torch.utils.cpp_extension import BuildExtension
cmd_class = {"build_ext": BuildExtension}
except ModuleNotFoundError:
cmd_class = {}
print("Skip building ext ops due to the absence of torch.")
def readme():
with open('README.md', encoding='utf-8') as f:
content = f.read()
return content
def load_module(name: str = "otx/__init__.py"):
"""Load Python Module.
Args:
name (str, optional): Name of the module to load.
Defaults to "otx/__init__.py".
"""
location = str(Path(__file__).parent / name)
spec = spec_from_file_location(name=name, location=location)
module = module_from_spec(spec) # type: ignore
spec.loader.exec_module(module) # type: ignore
return module
def get_otx_version() -> str:
"""Get version from `otx.__init__`.
Version is stored in the main __init__ module in `otx`.
The varible storing the version is `__version__`. This function
reads `__init__` file, checks `__version__ variable and return
the value assigned to it.
Example:
>>> # Assume that __version__ = "0.2.6"
>>> get_otx_version()
"0.2.6"
Returns:
str: `otx` version.
"""
otx = load_module(name="otx/__init__.py")
return otx.__version__
def get_requirements(requirement_files: Union[str, List[str]]) -> List[str]:
"""Get packages from requirements.txt file.
This function returns list of required packages from requirement files.
Args:
requirement_files (List[str]): txt files that contains list of required
packages.
Example:
>>> get_requirements(requirement_files=["openvino"])
['onnx>=1.8.1', 'networkx~=2.5', 'openvino-dev==2021.4.1', ...]
Returns:
List[str]: List of required packages
"""
if isinstance(requirement_files, str):
requirement_files = [requirement_files]
requirements: List[str] = []
for requirement_file in requirement_files:
with open(f"requirements/{requirement_file}.txt", "r", encoding="UTF-8") as file:
for line in file:
package = line.strip()
if package and not package.startswith(("#", "-f")):
Requirement.parse(package)
requirements.append(package)
return requirements
def get_extensions():
if platform.system() == "Windows":
return []
def _cython_modules():
cython_files = [
"otx/algorithms/common/adapters/mmcv/pipelines/transforms/cython_augments/pil_augment.pyx",
"otx/algorithms/common/adapters/mmcv/pipelines/transforms/cython_augments/cv_augment.pyx"
]
ext_modules = [
Extension(
cython_file.rstrip(".pyx").replace("/", "."),
[cython_file],
include_dirs=[numpy.get_include()],
extra_compile_args=["-O3"],
)
for cython_file in cython_files
]
return cythonize(ext_modules)
extensions = []
extensions.extend(_cython_modules())
return extensions
REQUIRED_PACKAGES = get_requirements(requirement_files="api")
EXTRAS_REQUIRE = {
"action": get_requirements(requirement_files=[
"base", "openvino", "action",
]
),
"anomaly": get_requirements(requirement_files=[
"base", "openvino", "anomaly",
]
),
"classification": get_requirements(requirement_files=[
"base", "openvino", "classification",
]
),
"detection": get_requirements(requirement_files=[
"base", "openvino", "detection",
]
),
"segmentation": get_requirements(requirement_files=[
"base", "openvino", "segmentation",
]
),
"full": get_requirements(requirement_files=[
"base",
"openvino",
"anomaly",
"classification",
"detection",
"segmentation",
"action",
]
),
}
def find_yaml_recipes():
"""Find YAML recipe files in the package."""
results = defaultdict(list)
for root, _, files in os.walk("otx"):
module = ".".join(root.split(os.sep))
for file in files:
_, ext = os.path.splitext(file)
if ext in [".yaml", ".json"]:
results[module] += [file]
return results
package_data = {"": ["requirements.txt", "README.md", "LICENSE", "py.typed"]}
package_data.update(find_yaml_recipes())
setup(
name="otx",
version=get_otx_version(),
description="OpenVINO™ Training Extensions: "
"Train, Evaluate, Optimize, Deploy Computer Vision Models via OpenVINO™",
long_description=readme(),
long_description_content_type="text/markdown",
author="OpenVINO™ Training Extensions Contributors",
url="https://github.com/openvinotoolkit/training_extensions",
classifiers=[
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Cython",
],
license="Apache License 2.0",
packages=find_packages(exclude=("tests",)),
package_data=package_data,
ext_modules=get_extensions(),
cmdclass=cmd_class,
install_requires=REQUIRED_PACKAGES,
extras_require=EXTRAS_REQUIRE,
entry_points={
"console_scripts": [
"otx=otx.cli.tools.cli:main",
"otx_demo=otx.cli.tools.demo:main",
"otx_eval=otx.cli.tools.eval:main",
"otx_export=otx.cli.tools.export:main",
"otx_find=otx.cli.tools.find:main",
"otx_train=otx.cli.tools.train:main",
"otx_optimize=otx.cli.tools.optimize:main",
"otx_build=otx.cli.tools.build:main",
]
},
)