MCPcopy Index your code
hub / github.com/InternRobotics/EmbodiedScan / convert

Method convert

embodiedscan/utils/array_converter.py:261–324  ·  view source on GitHub ↗

Convert input array to target data type. Args: input_array (np.ndarray or torch.Tensor or list or tuple or int or float): Input array. target_type (Type, optional): Type to which input array is converted. It should be `np.ndarray` or `

(
        self,
        input_array: TemplateArrayType,
        target_type: Optional[Type] = None,
        target_array: Optional[Union[np.ndarray, torch.Tensor]] = None
    )

Source from the content-addressed store, hash-verified

259 f'Template type {self.array_type} is not supported.')
260
261 def convert(
262 self,
263 input_array: TemplateArrayType,
264 target_type: Optional[Type] = None,
265 target_array: Optional[Union[np.ndarray, torch.Tensor]] = None
266 ) -> Union[np.ndarray, torch.Tensor]:
267 """Convert input array to target data type.
268
269 Args:
270 input_array (np.ndarray or torch.Tensor or list or tuple or int or
271 float): Input array.
272 target_type (Type, optional): Type to which input array is
273 converted. It should be `np.ndarray` or `torch.Tensor`.
274 Defaults to None.
275 target_array (np.ndarray or torch.Tensor, optional): Template array
276 to which input array is converted. Defaults to None.
277
278 Raises:
279 ValueError: If input is list or tuple and cannot be converted to a
280 NumPy array, a ValueError is raised.
281 TypeError: If input type does not belong to the above range, or the
282 contents of a list or tuple do not share the same data type, a
283 TypeError is raised.
284
285 Returns:
286 np.ndarray or torch.Tensor: The converted array.
287 """
288 if isinstance(input_array, (list, tuple)):
289 try:
290 input_array = np.array(input_array)
291 if input_array.dtype not in self.SUPPORTED_NON_ARRAY_TYPES:
292 raise TypeError
293 except (ValueError, TypeError):
294 print('The input cannot be converted to a single-type numpy '
295 f'array:\n{input_array}')
296 raise
297 elif isinstance(input_array, self.SUPPORTED_NON_ARRAY_TYPES):
298 input_array = np.array(input_array)
299 array_type = type(input_array)
300 assert target_type is not None or target_array is not None, \
301 'must specify a target'
302 if target_type is not None:
303 assert target_type in (np.ndarray, torch.Tensor), \
304 'invalid target type'
305 if target_type == array_type:
306 return input_array
307 elif target_type == np.ndarray:
308 # default dtype is float32
309 converted_array = input_array.cpu().numpy().astype(np.float32)
310 else:
311 # default dtype is float32, device is 'cpu'
312 converted_array = torch.tensor(input_array,
313 dtype=torch.float32)
314 else:
315 assert isinstance(target_array, (np.ndarray, torch.Tensor)), \
316 'invalid target array type'
317 if isinstance(target_array, array_type):
318 return input_array

Callers 1

new_funcFunction · 0.95

Calls 2

numpyMethod · 0.45
cpuMethod · 0.45

Tested by

no test coverage detected