Skip to content

Save the summary confidences error #2

Description

@2021year

env:Windows 11 wsl,Python 3.11.11
error:sh predict.sh:
[WARNING] [run_aurobind.py:345:main] Error in prediction: Object of type float32 is not JSON serializableTraceback (most recent call last):
File "run_aurobind.py", line 331, in main
struct_dir = predict_and_save(
^^^^^^^^^^^^^^^^^
File "run_aurobind.py", line 147, in predict_and_save
json.dump(summary_confidences, f, indent=1)
#solution provided by chatgpt
import numpy as np
import numbers

def make_json_serializable(obj):
# numpy scalars
if isinstance(obj, np.generic):
return obj.item()
# numpy arrays
if isinstance(obj, np.ndarray):
return obj.tolist()
# Python numbers are fine
if isinstance(obj, (str, bool, type(None))):
return obj
if isinstance(obj, numbers.Number):
# ensure Python native float/int
if isinstance(obj, float):
return float(obj)
if isinstance(obj, int):
return int(obj)
return obj
# lists/tuples -> list of serializables
if isinstance(obj, (list, tuple)):
return [make_json_serializable(x) for x in obj]
# dict -> convert keys/values (keys expected to be strings)
if isinstance(obj, dict):
return {str(k): make_json_serializable(v) for k, v in obj.items()}
# try common .item() (works for torch.Tensor scalar too)
if hasattr(obj, "item"):
try:
return make_json_serializable(obj.item())
except Exception:
pass
# try converting iterables
try:
iter(obj)
except TypeError:
# last resort: convert to string so json can store it
return str(obj)
else:
# If it's iterable but not list/tuple (e.g. generator), materialize it
try:
return [make_json_serializable(x) for x in obj]
except Exception:
return str(obj)

使用示例(替换原有保存段)

summary_confidences = summary_confidences_list[i]
serializable = make_json_serializable(summary_confidences)
serializable['num_recycles'] = int(args.recycling_iters) if hasattr(args.recycling_iters, "int") else args.recycling_iters

outname = f"{record.id}_seed-{seed}_sample-{i}_summary_confidences.json"
output_path = struct_dir / outname
struct_dir.mkdir(parents=True, exist_ok=True) # 确保目录存在
with output_path.open("w") as f:
json.dump(serializable, f, indent=1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions