forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.py
47 lines (37 loc) · 1.37 KB
/
serialize.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
"""
License:
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""
import copy
from hub.schema.features import Primitive, Tensor, SchemaDict
def serialize(input):
"Converts the input into a serializable format"
if isinstance(input, Tensor):
return serialize_tensor(input)
elif isinstance(input, SchemaDict):
return serialize_SchemaDict(input)
elif isinstance(input, Primitive):
return serialize_primitive(input)
else:
raise TypeError("Unknown type", type(input))
def serialize_tensor(tensor):
"Converts Tensor and its derivatives into a serializable format"
d = copy.deepcopy(tensor.__dict__)
d["type"] = type(tensor).__name__
if hasattr(tensor, "dtype"):
d["dtype"] = serialize(tensor.dtype)
if hasattr(tensor, "class_labels"):
d["class_labels"] = serialize(tensor.class_labels)
return d
def serialize_SchemaDict(fdict):
"Converts SchemaDict into a serializable format"
d = {}
d["type"] = "SchemaDict"
d["items"] = {}
for k, v in fdict.__dict__["dict_"].items():
d["items"][k] = serialize(v)
return d
def serialize_primitive(primitive):
"Converts Primitive into a serializable format"
return str(primitive._dtype)