Spaces:
Build error
Build error
Download backend/load_file.py from taquynhnga/CNNs-interpretation-visualization: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/spaces/taquynhnga/CNNs-interpretation-visualization/resolve/81f0a60eeaa27abba64cc4591847474de2bdd76f/backend/load_file.py
- Command line
-
hf download hf://spaces/taquynhnga/CNNs-interpretation-visualization@81f0a60eeaa27abba64cc4591847474de2bdd76f/backend/load_file.py
-
curl -L -o load_file.py https://huggingface.co/spaces/taquynhnga/CNNs-interpretation-visualization/resolve/81f0a60eeaa27abba64cc4591847474de2bdd76f/backend/load_file.py
1.2 kB
| 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(): | |
| loaded_dict[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'])) | |