clear dependency
Browse files- model/utils/__init__.py +1 -2
- model/utils/ceph_utils.py +0 -260
- requirements.txt +1 -1
model/utils/__init__.py
CHANGED
@@ -1,2 +1 @@
|
|
1 |
-
from .
|
2 |
-
from .pcl_utils import *
|
|
|
1 |
+
from .pcl_utils import *
|
|
model/utils/ceph_utils.py
DELETED
@@ -1,260 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Date: 2022-07-18 2:15:47 pm
|
3 |
-
Author: dihuangdh
|
4 |
-
Descriptions:
|
5 |
-
-----
|
6 |
-
LastEditTime: 2022-09-14 3:44:19 pm
|
7 |
-
LastEditors: dihuangdh
|
8 |
-
"""
|
9 |
-
|
10 |
-
import json
|
11 |
-
import pickle
|
12 |
-
import warnings
|
13 |
-
from io import BytesIO, StringIO # TODO:
|
14 |
-
from pathlib import Path
|
15 |
-
from typing import Any, Generator, Iterator, Optional, Tuple, Union
|
16 |
-
|
17 |
-
import cv2
|
18 |
-
import numpy as np
|
19 |
-
|
20 |
-
|
21 |
-
def has_method(obj: object, method: str) -> bool:
|
22 |
-
"""Check whether the object has a method.
|
23 |
-
Args:
|
24 |
-
method (str): The method name to check.
|
25 |
-
obj (object): The object to check.
|
26 |
-
Returns:
|
27 |
-
bool: True if the object has the method else False.
|
28 |
-
"""
|
29 |
-
return hasattr(obj, method) and callable(getattr(obj, method))
|
30 |
-
|
31 |
-
|
32 |
-
class PetrelBackend(object):
|
33 |
-
"""Petrel storage backend - simple version"""
|
34 |
-
|
35 |
-
def __init__(self, enable_mc: bool = False) -> None:
|
36 |
-
try:
|
37 |
-
from petrel_client.client import Client
|
38 |
-
except ImportError:
|
39 |
-
raise ImportError(
|
40 |
-
"Please install petrel_client to enable " "PetrelBackend."
|
41 |
-
)
|
42 |
-
|
43 |
-
self._client = Client('~/petreloss.conf')
|
44 |
-
|
45 |
-
def get(self, filepath) -> memoryview:
|
46 |
-
value = self._client.Get(filepath)
|
47 |
-
value_buf = memoryview(value)
|
48 |
-
return value_buf
|
49 |
-
|
50 |
-
def get_text(self, filepath, warning=False) -> str:
|
51 |
-
try:
|
52 |
-
value = self._client.Get(filepath)
|
53 |
-
except:
|
54 |
-
if warning:
|
55 |
-
warnings.warn("Failed to get text from {}".format(filepath))
|
56 |
-
value = None
|
57 |
-
else:
|
58 |
-
raise Exception("Failed to get text from {}".format(filepath))
|
59 |
-
return str(value, encoding="utf-8")
|
60 |
-
|
61 |
-
def get_uint16_png(self, filepath, warning=False) -> np.ndarray:
|
62 |
-
try:
|
63 |
-
value = np.frombuffer(self._client.get(filepath), np.uint8)
|
64 |
-
value = cv2.imdecode(value, cv2.IMREAD_UNCHANGED)
|
65 |
-
except:
|
66 |
-
if warning:
|
67 |
-
warnings.warn("Failed to get uint16_png from {}".format(filepath))
|
68 |
-
value = None
|
69 |
-
else:
|
70 |
-
raise Exception("Failed to get uint16_png from {}".format(filepath))
|
71 |
-
return value
|
72 |
-
|
73 |
-
def get_uint8_jpg(self, filepath, warning=False) -> np.ndarray:
|
74 |
-
try:
|
75 |
-
value = np.frombuffer(self._client.get(filepath), np.uint8)
|
76 |
-
value = cv2.imdecode(value, cv2.IMREAD_UNCHANGED)
|
77 |
-
except:
|
78 |
-
if warning:
|
79 |
-
warnings.warn("Failed to get uint8_jpg from {}".format(filepath))
|
80 |
-
value = None
|
81 |
-
else:
|
82 |
-
raise Exception("Failed to get uint8_jpg from {}".format(filepath))
|
83 |
-
return value
|
84 |
-
|
85 |
-
def get_npz(self, filepath, warning=False) -> Any:
|
86 |
-
try:
|
87 |
-
value = self._client.get(filepath)
|
88 |
-
value = np.loads(value)
|
89 |
-
except Exception as e:
|
90 |
-
if warning:
|
91 |
-
warnings.warn("Failed to get npz from {}".format(filepath))
|
92 |
-
value = None
|
93 |
-
else:
|
94 |
-
print(e)
|
95 |
-
raise Exception("Failed to get npz from {}".format(filepath))
|
96 |
-
return value
|
97 |
-
|
98 |
-
def get_numpy_txt(self, filepath, warning=False) -> np.ndarray:
|
99 |
-
try:
|
100 |
-
value = np.loadtxt(StringIO(self.get_text(filepath)))
|
101 |
-
except:
|
102 |
-
if warning:
|
103 |
-
warnings.warn("Failed to get numpy_txt from {}".format(filepath))
|
104 |
-
value = None
|
105 |
-
else:
|
106 |
-
raise Exception("Failed to get numpy_txt from {}".format(filepath))
|
107 |
-
return value
|
108 |
-
|
109 |
-
def get_json(self, filepath, warning=False) -> Any:
|
110 |
-
try:
|
111 |
-
value = self._client.get(filepath)
|
112 |
-
value = json.loads(value)
|
113 |
-
except:
|
114 |
-
if warning:
|
115 |
-
warnings.warn("Failed to get json from {}".format(filepath))
|
116 |
-
value = None
|
117 |
-
else:
|
118 |
-
raise Exception("Failed to get json from {}".format(filepath))
|
119 |
-
return value
|
120 |
-
|
121 |
-
def put_uint16_png(self, filepath, value) -> None:
|
122 |
-
success, img_array = cv2.imencode(".png", value, params=[cv2.CV_16U])
|
123 |
-
assert success
|
124 |
-
img_bytes = img_array.tobytes()
|
125 |
-
self._client.put(filepath, img_bytes)
|
126 |
-
# self._client.put(filepath, img_bytes, update_cache=True)
|
127 |
-
|
128 |
-
def put_uint8_jpg(self, filepath, value) -> None:
|
129 |
-
success, img_array = cv2.imencode(".jpg", value)
|
130 |
-
assert success
|
131 |
-
img_bytes = img_array.tobytes()
|
132 |
-
self._client.put(filepath, img_bytes)
|
133 |
-
# self._client.put(filepath, img_bytes, update_cache=True)
|
134 |
-
|
135 |
-
def put_npz(self, filepath, value) -> None:
|
136 |
-
value = pickle.dumps(value)
|
137 |
-
self._client.put(filepath, value)
|
138 |
-
# self._client.put(filepath, value, update_cache=True)
|
139 |
-
|
140 |
-
def put_json(self, filepath, value) -> None:
|
141 |
-
value = json.dumps(value).encode()
|
142 |
-
self._client.put(filepath, value)
|
143 |
-
# self._client.put(filepath, value, update_cache=True)
|
144 |
-
|
145 |
-
def put_text(self, filepath, value) -> None:
|
146 |
-
self._client.put(filepath, bytes(value, encoding="utf-8"))
|
147 |
-
# self._client.put(filepath, bytes(value, encoding='utf-8'), update_cache=True)
|
148 |
-
|
149 |
-
def join_path(
|
150 |
-
self, filepath: Union[str, Path], *filepaths: Union[str, Path]
|
151 |
-
) -> str:
|
152 |
-
"""Concatenate all file paths.
|
153 |
-
Args:
|
154 |
-
filepath (str or Path): Path to be concatenated.
|
155 |
-
Returns:
|
156 |
-
str: The result after concatenation.
|
157 |
-
"""
|
158 |
-
# filepath = self._format_path(self._map_path(filepath))
|
159 |
-
if filepath.endswith("/"):
|
160 |
-
filepath = filepath[:-1]
|
161 |
-
formatted_paths = [filepath]
|
162 |
-
for path in filepaths:
|
163 |
-
formatted_paths.append(path)
|
164 |
-
return "/".join(formatted_paths)
|
165 |
-
|
166 |
-
# from mmcv
|
167 |
-
def list_dir_or_file(
|
168 |
-
self,
|
169 |
-
dir_path: Union[str, Path],
|
170 |
-
list_dir: bool = True,
|
171 |
-
list_file: bool = True,
|
172 |
-
suffix: Optional[Union[str, Tuple[str]]] = None,
|
173 |
-
recursive: bool = False,
|
174 |
-
) -> Iterator[str]:
|
175 |
-
"""Scan a directory to find the interested directories or files in
|
176 |
-
arbitrary order.
|
177 |
-
Note:
|
178 |
-
Petrel has no concept of directories but it simulates the directory
|
179 |
-
hierarchy in the filesystem through public prefixes. In addition,
|
180 |
-
if the returned path ends with '/', it means the path is a public
|
181 |
-
prefix which is a logical directory.
|
182 |
-
Note:
|
183 |
-
:meth:`list_dir_or_file` returns the path relative to ``dir_path``.
|
184 |
-
In addition, the returned path of directory will not contains the
|
185 |
-
suffix '/' which is consistent with other backends.
|
186 |
-
Args:
|
187 |
-
dir_path (str | Path): Path of the directory.
|
188 |
-
list_dir (bool): List the directories. Default: True.
|
189 |
-
list_file (bool): List the path of files. Default: True.
|
190 |
-
suffix (str or tuple[str], optional): File suffix
|
191 |
-
that we are interested in. Default: None.
|
192 |
-
recursive (bool): If set to True, recursively scan the
|
193 |
-
directory. Default: False.
|
194 |
-
Yields:
|
195 |
-
Iterable[str]: A relative path to ``dir_path``.
|
196 |
-
"""
|
197 |
-
# if not has_method(self._client, 'list'):
|
198 |
-
# raise NotImplementedError(
|
199 |
-
# 'Current version of Petrel Python SDK has not supported '
|
200 |
-
# 'the `list` method, please use a higher version or dev'
|
201 |
-
# ' branch instead.')
|
202 |
-
|
203 |
-
# dir_path = self._map_path(dir_path)
|
204 |
-
# dir_path = self._format_path(dir_path)
|
205 |
-
# if list_dir and suffix is not None:
|
206 |
-
# raise TypeError(
|
207 |
-
# '`list_dir` should be False when `suffix` is not None')
|
208 |
-
|
209 |
-
# if (suffix is not None) and not isinstance(suffix, (str, tuple)):
|
210 |
-
# raise TypeError('`suffix` must be a string or tuple of strings')
|
211 |
-
|
212 |
-
# Petrel's simulated directory hierarchy assumes that directory paths
|
213 |
-
# should end with `/`
|
214 |
-
if not dir_path.endswith("/"):
|
215 |
-
dir_path += "/"
|
216 |
-
|
217 |
-
root = dir_path
|
218 |
-
|
219 |
-
def _list_dir_or_file(dir_path, list_dir, list_file, suffix, recursive):
|
220 |
-
for path in self._client.list(dir_path):
|
221 |
-
# the `self.isdir` is not used here to determine whether path
|
222 |
-
# is a directory, because `self.isdir` relies on
|
223 |
-
# `self._client.list`
|
224 |
-
if path.endswith("/"): # a directory path
|
225 |
-
next_dir_path = self.join_path(dir_path, path)
|
226 |
-
if list_dir:
|
227 |
-
# get the relative path and exclude the last
|
228 |
-
# character '/'
|
229 |
-
rel_dir = next_dir_path[len(root) : -1]
|
230 |
-
yield rel_dir
|
231 |
-
if recursive:
|
232 |
-
yield from _list_dir_or_file(
|
233 |
-
next_dir_path, list_dir, list_file, suffix, recursive
|
234 |
-
)
|
235 |
-
else: # a file path
|
236 |
-
absolute_path = self.join_path(dir_path, path)
|
237 |
-
rel_path = absolute_path[len(root) :]
|
238 |
-
if (suffix is None or rel_path.endswith(suffix)) and list_file:
|
239 |
-
yield rel_path
|
240 |
-
|
241 |
-
return _list_dir_or_file(dir_path, list_dir, list_file, suffix, recursive)
|
242 |
-
|
243 |
-
# from mmcv
|
244 |
-
def exists(self, filepath: Union[str, Path]) -> bool:
|
245 |
-
"""Check whether a file path exists.
|
246 |
-
Args:
|
247 |
-
filepath (str or Path): Path to be checked whether exists.
|
248 |
-
Returns:
|
249 |
-
bool: Return ``True`` if ``filepath`` exists, ``False`` otherwise.
|
250 |
-
"""
|
251 |
-
if not (
|
252 |
-
has_method(self._client, "contains") and has_method(self._client, "isdir")
|
253 |
-
):
|
254 |
-
raise NotImplementedError(
|
255 |
-
"Current version of Petrel Python SDK has not supported "
|
256 |
-
"the `contains` and `isdir` methods, please use a higher"
|
257 |
-
"version or dev branch instead."
|
258 |
-
)
|
259 |
-
|
260 |
-
return self._client.contains(filepath) or self._client.isdir(filepath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -18,4 +18,4 @@ transformers==4.29.1
|
|
18 |
bigmodelvis
|
19 |
gradio
|
20 |
mdtex2html
|
21 |
-
ipdb
|
|
|
18 |
bigmodelvis
|
19 |
gradio
|
20 |
mdtex2html
|
21 |
+
ipdb
|