游雁
2024-03-21 24aea85b5bc3f354d683201fa9e37968f3f1638f
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import io
import shutil
import logging
from collections import OrderedDict
import numpy as np
from omegaconf import DictConfig, OmegaConf
 
def statistic_model_parameters(model, prefix=None):
    var_dict = model.state_dict()
    numel = 0
    for i, key in enumerate(sorted(list([x for x in var_dict.keys() if "num_batches_tracked" not in x]))):
        if prefix is None or key.startswith(prefix):
            numel += var_dict[key].numel()
    return numel
 
 
def int2vec(x, vec_dim=8, dtype=np.int32):
    b = ('{:0' + str(vec_dim) + 'b}').format(x)
    # little-endian order: lower bit first
    return (np.array(list(b)[::-1]) == '1').astype(dtype)
 
 
def seq2arr(seq, vec_dim=8):
    return np.row_stack([int2vec(int(x), vec_dim) for x in seq])
 
 
def load_scp_as_dict(scp_path, value_type='str', kv_sep=" "):
    with io.open(scp_path, 'r', encoding='utf-8') as f:
        ret_dict = OrderedDict()
        for one_line in f.readlines():
            one_line = one_line.strip()
            pos = one_line.find(kv_sep)
            key, value = one_line[:pos], one_line[pos + 1:]
            if value_type == 'list':
                value = value.split(' ')
            ret_dict[key] = value
        return ret_dict
 
 
def load_scp_as_list(scp_path, value_type='str', kv_sep=" "):
    with io.open(scp_path, 'r', encoding='utf8') as f:
        ret_dict = []
        for one_line in f.readlines():
            one_line = one_line.strip()
            pos = one_line.find(kv_sep)
            key, value = one_line[:pos], one_line[pos + 1:]
            if value_type == 'list':
                value = value.split(' ')
            ret_dict.append((key, value))
        return ret_dict
 
def deep_update(original, update):
    for key, value in update.items():
        if isinstance(value, dict) and key in original:
            deep_update(original[key], value)
        else:
            original[key] = value
            
            
def prepare_model_dir(**kwargs):
    
 
    os.makedirs(kwargs.get("output_dir", "./"), exist_ok=True)
    
    yaml_file = os.path.join(kwargs.get("output_dir", "./"), "config.yaml")
    OmegaConf.save(config=kwargs, f=yaml_file)
    print(kwargs)
    logging.info("config.yaml is saved to: %s", yaml_file)
 
    # model_path = kwargs.get("model_path")
    # if model_path is not None:
    #     config_json = os.path.join(model_path, "configuration.json")
    #     if os.path.exists(config_json):
    #         shutil.copy(config_json, os.path.join(kwargs.get("output_dir", "./"), "configuration.json"))