注意区分bool和bool_的区别!!!

  • bool_类型实际上是numpy.bool_,不能写入到json
  • bool只是普通的布尔类型,可以写入到json

TypeError: Object of type bool_ is not JSON serializable解决方法

重写NumpyEncoder(参考博客)增加了bool_的处理

class NumpyEncoder(json.JSONEncoder):
    """ Special json encoder for numpy types """

    def default(self, obj):
        if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
                            numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
                            numpy.uint16, numpy.uint32, numpy.uint64)):
            return int(obj)
        elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,
                              numpy.float64)):
            return float(obj)
        elif isinstance(obj, (numpy.ndarray,)):
            return obj.tolist()
        elif isinstance(obj, (numpy.bool_,)):
            return bool(obj)
        return json.JSONEncoder.default(self, obj)

PKL转json完整代码

# coding = utf-8

import pickle
import json
import numpy


class NumpyEncoder(json.JSONEncoder):
    """ Special json encoder for numpy types """

    def default(self, obj):
        if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,
                            numpy.int16, numpy.int32, numpy.int64, numpy.uint8,
                            numpy.uint16, numpy.uint32, numpy.uint64)):
            return int(obj)
        elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,
                              numpy.float64)):
            return float(obj)
        elif isinstance(obj, (numpy.ndarray,)):
            return obj.tolist()
        elif isinstance(obj, (numpy.bool_,)):
            return bool(obj)
        return json.JSONEncoder.default(self, obj)


def convert_dict_to_json(file_path):
    with open(file_path, 'rb') as fpkl, open('%s.json' % file_path, 'a+') as fjson:
        data = pickle.load(fpkl, encoding='latin1')
        data_dict = dict(data)
        json.dump(data, fjson, ensure_ascii=False, sort_keys=True, indent=4, cls=NumpyEncoder)


def main():
    # if sys.argv[1] and os.path.isfile(sys.argv[1]):
    file_path = '../models/model_best.model.pkl'
    print("Processing %s ..." % file_path)
    convert_dict_to_json(file_path)


if __name__ == '__main__':
    main()

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐