File size: 4,309 Bytes
6597efe | 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 | import json
import torch
import numpy as np
from base64 import b64decode, b64encode
from numpy.lib.format import descr_to_dtype, dtype_to_descr
from fastapi.responses import JSONResponse
from dataclasses import dataclass
from typing import Union, Dict, Any, List
from pathlib import Path
def numpy_serialize(o):
if isinstance(o, (np.ndarray, np.generic)):
data = o.data if o.flags["C_CONTIGUOUS"] else o.tobytes()
return {
"__numpy__": b64encode(data).decode(),
"dtype": dtype_to_descr(o.dtype),
"shape": o.shape,
}
msg = f"Object of type {o.__class__.__name__} is not JSON serializable"
raise TypeError(msg)
def numpy_deserialize(dct):
if "__numpy__" in dct:
np_obj = np.frombuffer(b64decode(dct["__numpy__"]), descr_to_dtype(dct["dtype"]))
return np_obj.reshape(shape) if (shape := dct["shape"]) else np_obj[0]
return dct
def convert_numpy_in_dict(data, func):
"""
Recursively processes a JSON-like dictionary, converting any NumPy arrays
or lists of NumPy arrays into a serializable format using the provided function.
Args:
data: The JSON-like dictionary or object to process.
func: A function to apply to each NumPy array to make it serializable.
Returns:
The processed dictionary or object with all NumPy arrays converted.
"""
if isinstance(data, dict):
if "__numpy__" in data:
return func(data)
return {key: convert_numpy_in_dict(value, func) for key, value in data.items()}
elif isinstance(data, list):
return [convert_numpy_in_dict(item, func) for item in data]
elif isinstance(data, (np.ndarray, np.generic)):
return func(data)
else:
return data
class Message(object):
def __init__(self):
pass
def serialize(self):
raise NotImplementedError
@classmethod
def deserialize(cls, response: Dict[str, Any]):
raise NotImplementedError
class RequestMessage(Message):
def __init__(self, image: Dict[str, Any],
instruction: str,
history: Dict[str, Any],
state: Dict[str, Any],
condition: Dict[str, Any],
gt_action: Union[np.ndarray, List],
dataset_name: str,
timestamp: str):
self.image, self.instruction, self.history, self.state, self.gt_action, self.dataset_name, self.timestamp = \
image, instruction, history, state, gt_action, dataset_name, timestamp
self.condition = condition
def serialize(self):
msg = {
"image": self.image,
"instruction": self.instruction,
"history": self.history,
"state": self.state,
"condition": self.condition,
"gt_action": self.gt_action,
"dataset_name": self.dataset_name,
"timestamp": self.timestamp
}
return convert_numpy_in_dict(msg, numpy_serialize)
@classmethod
def deserialize(cls, response: Dict[str, Any]):
response = convert_numpy_in_dict(response, numpy_deserialize) # type: ignore
return cls(
image=response["image"],
instruction=response["instruction"],
history=response["history"],
state=response["state"],
condition=response["condition"],
gt_action=response["gt_action"],
dataset_name=response["dataset_name"],
timestamp=response["timestamp"]
)
class ResponseMessage(Message):
def __init__(self, action: np.ndarray, err: float, traj_image: np.ndarray = np.zeros((1,1,3), dtype=np.uint8)):
self.action = action
self.err = err
self.traj_image = traj_image
def serialize(self):
msg = {
"action": self.action,
"err": self.err,
"traj_image": self.traj_image
}
return convert_numpy_in_dict(msg, numpy_serialize)
@classmethod
def deserialize(cls, response: Dict[str, Any]):
response = convert_numpy_in_dict(response, numpy_deserialize) # type: ignore
return cls(action=response["action"], err=response["err"], traj_image=response["traj_image"])
|