-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support more types of object in JSON output
Signed-off-by: Ben Firshman <[email protected]>
- Loading branch information
Showing
7 changed files
with
77 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import json | ||
|
||
# Based on keepsake.json | ||
|
||
# We load numpy but not torch or tensorflow because numpy loads very fast and | ||
# they're probably using it anyway | ||
# fmt: off | ||
try: | ||
import numpy as np # type: ignore | ||
has_numpy = True | ||
except ImportError: | ||
has_numpy = False | ||
# fmt: on | ||
|
||
# Tensorflow takes a solid 10 seconds to import on a modern Macbook Pro, so instead of importing, | ||
# do this instead | ||
def _is_tensorflow_tensor(obj): | ||
# e.g. __module__='tensorflow.python.framework.ops', __name__='EagerTensor' | ||
return ( | ||
obj.__class__.__module__.split(".")[0] == "tensorflow" | ||
and "Tensor" in obj.__class__.__name__ | ||
) | ||
|
||
|
||
def _is_torch_tensor(obj): | ||
return (obj.__class__.__module__, obj.__class__.__name__) == ("torch", "Tensor") | ||
|
||
|
||
class CustomJSONEncoder(json.JSONEncoder): | ||
def default(self, o): | ||
if has_numpy: | ||
if isinstance(o, np.integer): | ||
return int(o) | ||
elif isinstance(o, np.floating): | ||
return float(o) | ||
elif isinstance(o, np.ndarray): | ||
return o.tolist() | ||
if _is_torch_tensor(o): | ||
return o.detach().tolist() | ||
if _is_tensorflow_tensor(o): | ||
return o.numpy().tolist() | ||
return json.JSONEncoder.default(self, o) | ||
|
||
|
||
def to_json(obj): | ||
return json.dumps(obj, cls=CustomJSONEncoder) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
flask==2.0.1 | ||
numpy==1.21.1 | ||
pillow==8.2.0 | ||
pytest==6.2.4 | ||
PyYAML==5.4.1 | ||
|