File size: 1,330 Bytes
fcc16aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import pickle
import numpy as np
from collections import OrderedDict

def load_pickle(filename):
    with open(filename, 'rb') as file:
        data = pickle.load(file)
    return data

def save_pickle_to_json(filename):
    ordered_dict = load_pickle(filename)
    json_obj = json.dumps(ordered_dict, cls=NumpyEncoder)
    with open(filename.replace('.pkl', '.json'), 'w') as f:
        f.write(json_obj)

def load_json(filename):
    with open(filename, 'r') as read_file:
        loaded_dict = json.loads(read_file.read())
    loaded_dict = OrderedDict(loaded_dict)
    for k, v in loaded_dict.items():
        if type(v) == list:
            loaded_dict[k] = np.asarray(v)
        else:
            for k_, v_ in v.items():
                v[k_] = np.asarray(v_)
    return loaded_dict

class NumpyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)

# save_pickle_to_json('data/layer_infos/convnext_layer_infos.pkl')
# save_pickle_to_json('data/layer_infos/resnet_layer_infos.pkl')
# save_pickle_to_json('data/layer_infos/mobilenet_layer_infos.pkl')

# file = load_json('data/layer_infos/convnext_layer_infos.json')
# print(type(file))
# print(type(file['embeddings.patch_embeddings']))