|
1 | 1 | import asyncio |
2 | 2 | import contextvars |
3 | 3 | import functools |
| 4 | + |
4 | 5 | import sys |
5 | 6 | import threading |
6 | 7 | from datetime import datetime |
7 | 8 | from typing import Any, Dict, List, Mapping, Optional, SupportsBytes, SupportsFloat, Type, TypeVar, Union |
8 | 9 |
|
9 | | -import numpy as np |
10 | 10 | from google.protobuf.json_format import MessageToDict, ParseDict |
11 | 11 | from google.protobuf.message import Message |
12 | 12 | from google.protobuf.struct_pb2 import ListValue, Struct, Value |
13 | 13 | from google.protobuf.timestamp_pb2 import Timestamp |
14 | | -from numpy.typing import NDArray |
15 | 14 |
|
16 | 15 | from viam.proto.common import Geometry, GeoPoint, GetGeometriesRequest, GetGeometriesResponse, Orientation, ResourceName, Vector3 |
17 | | -from viam.proto.service.mlmodel import ( |
18 | | - FlatTensor, |
19 | | - FlatTensorDataDouble, |
20 | | - FlatTensorDataFloat, |
21 | | - FlatTensorDataInt8, |
22 | | - FlatTensorDataInt16, |
23 | | - FlatTensorDataInt32, |
24 | | - FlatTensorDataInt64, |
25 | | - FlatTensorDataUInt8, |
26 | | - FlatTensorDataUInt16, |
27 | | - FlatTensorDataUInt32, |
28 | | - FlatTensorDataUInt64, |
29 | | - FlatTensors, |
30 | | -) |
31 | 16 | from viam.resource.base import ResourceBase |
32 | 17 | from viam.resource.registry import Registry |
33 | 18 | from viam.resource.types import Subtype, SupportsGetGeometries |
34 | 19 |
|
| 20 | + |
35 | 21 | if sys.version_info >= (3, 9): |
36 | 22 | from collections.abc import Callable |
37 | 23 | else: |
@@ -165,78 +151,6 @@ def _convert(v: ValueTypes) -> Any: |
165 | 151 | return struct |
166 | 152 |
|
167 | 153 |
|
168 | | -def flat_tensors_to_ndarrays(flat_tensors: FlatTensors) -> Dict[str, NDArray]: |
169 | | - property_name_to_dtype = { |
170 | | - "float_tensor": np.float32, |
171 | | - "double_tensor": np.float64, |
172 | | - "int8_tensor": np.int8, |
173 | | - "int16_tensor": np.int16, |
174 | | - "int32_tensor": np.int32, |
175 | | - "int64_tensor": np.int64, |
176 | | - "uint8_tensor": np.uint8, |
177 | | - "uint16_tensor": np.uint16, |
178 | | - "uint32_tensor": np.uint32, |
179 | | - "uint64_tensor": np.uint64, |
180 | | - } |
181 | | - |
182 | | - def make_ndarray(flat_data, dtype, shape): |
183 | | - """Takes flat data (protobuf RepeatedScalarFieldContainer | bytes) to output an ndarray |
184 | | - of appropriate dtype and shape""" |
185 | | - make_array = np.frombuffer if dtype == np.int8 or dtype == np.uint8 else np.array |
186 | | - return make_array(flat_data, dtype).reshape(shape) |
187 | | - |
188 | | - ndarrays: Dict[str, NDArray] = dict() |
189 | | - for name, flat_tensor in flat_tensors.tensors.items(): |
190 | | - property_name = flat_tensor.WhichOneof("tensor") or flat_tensor.WhichOneof(b"tensor") |
191 | | - if property_name: |
192 | | - tensor_data = getattr(flat_tensor, property_name) |
193 | | - flat_data, dtype, shape = tensor_data.data, property_name_to_dtype[property_name], flat_tensor.shape |
194 | | - ndarrays[name] = make_ndarray(flat_data, dtype, shape) |
195 | | - return ndarrays |
196 | | - |
197 | | - |
198 | | -def ndarrays_to_flat_tensors(ndarrays: Dict[str, NDArray]) -> FlatTensors: |
199 | | - dtype_name_to_tensor_data_class = { |
200 | | - "float32": FlatTensorDataFloat, |
201 | | - "float64": FlatTensorDataDouble, |
202 | | - "int8": FlatTensorDataInt8, |
203 | | - "int16": FlatTensorDataInt16, |
204 | | - "int32": FlatTensorDataInt32, |
205 | | - "int64": FlatTensorDataInt64, |
206 | | - "uint8": FlatTensorDataUInt8, |
207 | | - "uint16": FlatTensorDataUInt16, |
208 | | - "uint32": FlatTensorDataUInt32, |
209 | | - "uint64": FlatTensorDataUInt64, |
210 | | - } |
211 | | - |
212 | | - def get_tensor_data(ndarray: NDArray): |
213 | | - """Takes an ndarray and returns the corresponding tensor data class instance |
214 | | - e.g. FlatTensorDataInt8, FlatTensorDataUInt8 etc.""" |
215 | | - tensor_data_class = dtype_name_to_tensor_data_class[ndarray.dtype.name] |
216 | | - data = ndarray.flatten() |
217 | | - if tensor_data_class == FlatTensorDataInt8 or tensor_data_class == FlatTensorDataUInt8: |
218 | | - data = data.tobytes() # as per the proto, int8 and uint8 are stored as bytes |
219 | | - elif tensor_data_class == FlatTensorDataInt16 or tensor_data_class == FlatTensorDataUInt16: |
220 | | - data = data.astype(np.uint32) # as per the proto, int16 and uint16 are stored as uint32 |
221 | | - tensor_data = tensor_data_class(data=data) |
222 | | - return tensor_data |
223 | | - |
224 | | - def get_tensor_data_type(ndarray: NDArray): |
225 | | - """Takes ndarray and returns a FlatTensor datatype property to be set |
226 | | - e.g. "float_tensor", "uint32_tensor" etc.""" |
227 | | - if ndarray.dtype == np.float32: |
228 | | - return "float_tensor" |
229 | | - elif ndarray.dtype == np.float64: |
230 | | - return "double_tensor" |
231 | | - return f"{ndarray.dtype.name}_tensor" |
232 | | - |
233 | | - tensors_mapping: Dict[str, FlatTensor] = dict() |
234 | | - for name, ndarray in ndarrays.items(): |
235 | | - prop_name, prop_value = get_tensor_data_type(ndarray), get_tensor_data(ndarray) |
236 | | - tensors_mapping[name] = FlatTensor(shape=ndarray.shape, **{prop_name: prop_value}) |
237 | | - return FlatTensors(tensors=tensors_mapping) |
238 | | - |
239 | | - |
240 | 154 | def struct_to_dict(struct: Struct) -> Dict[str, ValueTypes]: |
241 | 155 | return {key: value_to_primitive(value) for (key, value) in struct.fields.items()} |
242 | 156 |
|
|
0 commit comments