misc.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import io
  2. from collections import OrderedDict
  3. import numpy as np
  4. def statistic_model_parameters(model, prefix=None):
  5. var_dict = model.state_dict()
  6. numel = 0
  7. for i, key in enumerate(sorted(list([x for x in var_dict.keys() if "num_batches_tracked" not in x]))):
  8. if prefix is None or key.startswith(prefix):
  9. numel += var_dict[key].numel()
  10. return numel
  11. def int2vec(x, vec_dim=8, dtype=np.int32):
  12. b = ('{:0' + str(vec_dim) + 'b}').format(x)
  13. # little-endian order: lower bit first
  14. return (np.array(list(b)[::-1]) == '1').astype(dtype)
  15. def seq2arr(seq, vec_dim=8):
  16. return np.row_stack([int2vec(int(x), vec_dim) for x in seq])
  17. def load_scp_as_dict(scp_path, value_type='str', kv_sep=" "):
  18. with io.open(scp_path, 'r', encoding='utf-8') as f:
  19. ret_dict = OrderedDict()
  20. for one_line in f.readlines():
  21. one_line = one_line.strip()
  22. pos = one_line.find(kv_sep)
  23. key, value = one_line[:pos], one_line[pos + 1:]
  24. if value_type == 'list':
  25. value = value.split(' ')
  26. ret_dict[key] = value
  27. return ret_dict
  28. def load_scp_as_list(scp_path, value_type='str', kv_sep=" "):
  29. with io.open(scp_path, 'r', encoding='utf8') as f:
  30. ret_dict = []
  31. for one_line in f.readlines():
  32. one_line = one_line.strip()
  33. pos = one_line.find(kv_sep)
  34. key, value = one_line[:pos], one_line[pos + 1:]
  35. if value_type == 'list':
  36. value = value.split(' ')
  37. ret_dict.append((key, value))
  38. return ret_dict