4

I need help converting the below dictionary to a json:

summary = {'model': 'Test', 'devices': '0,1', 'config': {'data': {'name': 'ABC', 'labels': 'outputs/export2_v2', 'cache_in_memory': False, 'validation_size': 192, 'augmentation': {'photometric': {'enable': True, 'primitives': ['random_brightness', 'random_contrast', 'additive_speckle_noise', 'additive_gaussian_noise', 'additive_shade', 'motion_blur'], 'params': {'random_brightness': {'max_abs_change': 50}, 'random_contrast': {'strength_range': [0.3, 1.5]}, 'additive_gaussian_noise': {'stddev_range': [0, 10]}, 'additive_speckle_noise': {'prob_range': [0, 0.0035]}, 'additive_shade': {'transparency_range': [-0.5, 0.5], 'kernel_size_range': [100, 150]}, 'motion_blur': {'max_kernel_size': 3}}}, 'homographic': {'enable': True, 'params': {'translation': True, 'rotation': True, 'scaling': True, 'perspective': True, 'scaling_amplitude': 0.2, 'perspective_amplitude_x': 0.2, 'perspective_amplitude_y': 0.2, 'patch_ratio': 0.85, 'max_angle': 1.57, 'allow_artifacts': True}, 'valid_border_margin': 3}}}, 'model': {'batch_size': 32, 'detection_threshold': 0.001, 'eval_batch_size': 32, 'learning_rate': 0.001, 'name': 'magic_point', 'nms': 4}, 'train_iter': 6, 'eval_iter': 2, 'validation_interval': 2, 'save_interval': 5000, 'keep_checkpoints': 20}, 'model_path': 'experiments/Test', 'Train_summary': [{'iteration': 0, 'loss': 4.6594462, 'precision': 0.0022884681450641656, 'recall': 0.059423500770503866}, {'iteration': 2, 'loss': 4.1882625, 'precision': 0.002276572784097017, 'recall': 0.060493180490456044}, {'iteration': 4, 'loss': 3.7569315, 'precision': 0.002235827870127771, 'recall': 0.057959385252174776}], 'Test_summary': {'precision': 0.0022856936059727296, 'recall': 0.05952331782349892}}

I tried json.dumps but I am getting the below error:

TypeError: Object of type 'float32' is not JSON serializable
user13074756
  • 383
  • 1
  • 8
  • 16

3 Answers3

3

Generally this happens when you try to serialize numpy float32 objects to JSON. A reproducible would be this:

import json
import numpy as np
ar = np.array([1.12321, 2.123123], dtype=np.float32)
d = {'randomKey': ar[0]}
json.dumps(d)

TypeError: Object of type 'float32' is not JSON serializable

Workaround for this is that you need to extend the JSONEncoder class to serialize numpy float32 values

class NumpyFloatValuesEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.float32):
            return float(obj)
        return JSONEncoder.default(self, obj)
json.dumps(d, cls=NumpyFloatValuesEncoder)

You can customise this according to your need. You can read more about this here https://docs.python.org/3/library/json.html

Sanchit.Jain
  • 568
  • 2
  • 7
1

I was able to convert the dictionary to a JSON after I converted all decimal values in the dict to float using float() function

user13074756
  • 383
  • 1
  • 8
  • 16
0

I think the error is form the 0,1 value. Try converting it from float32 to float or float64(should be the same as float) with numpy.ndarray.astype

Dave X
  • 4,831
  • 4
  • 31
  • 42
Dushan
  • 1
  • 1