mtg-upf/audio-difficulty
2
1import json2import pickle3 4 5def save_json(dictionary, name_file):6 with open(name_file, 'w') as fp:7 json.dump(dictionary, fp, sort_keys=True, indent=4)8 9 10def prediction2label(pred):11 """Convert ordinal predictions to class labels, e.g.12 13 [0.9, 0.1, 0.1, 0.1] -> 014 [0.9, 0.9, 0.1, 0.1] -> 115 [0.9, 0.9, 0.9, 0.1] -> 216 etc.17 """18 return (pred > 0.5).cumprod(axis=1).sum(axis=1) - 119 20 21def load_json(name_file):22 data = None23 with open(name_file, 'r') as fp:24 data = json.load(fp)25 return data26 27 28def save_binary(dictionary, name_file):29 with open(name_file, 'wb') as fp:30 pickle.dump(dictionary, fp, protocol=pickle.HIGHEST_PROTOCOL)31 32 33def load_binary(name_file):34 data = None35 with open(name_file, 'rb') as fp:36 data = pickle.load(fp)37 return data