lt-tensor 0.0.1a11__py3-none-any.whl → 0.0.1a12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lt_tensor/__init__.py CHANGED
@@ -12,6 +12,7 @@ from . import (
12
12
  losses,
13
13
  processors,
14
14
  datasets,
15
+ torch_commons,
15
16
  )
16
17
 
17
18
  __all__ = [
@@ -26,4 +27,5 @@ __all__ = [
26
27
  "losses",
27
28
  "processors",
28
29
  "datasets",
30
+ "torch_commons",
29
31
  ]
@@ -0,0 +1,97 @@
1
+ from lt_utils.common import *
2
+ from lt_utils.file_ops import load_json, save_json, FileScan
3
+ from lt_utils.misc_utils import log_traceback, get_current_time
4
+ from lt_utils.type_utils import is_pathlike, is_file, is_dir, is_dict, is_str
5
+ from lt_tensor.misc_utils import updateDict
6
+
7
+
8
+ class ModelConfig(ABC, OrderedDict):
9
+ _default_settings: Dict[str, Any] = {}
10
+ _forbidden_list: List[str] = [
11
+ "_settings",
12
+ ]
13
+
14
+ def __init__(
15
+ self,
16
+ settings: Dict[str, Any] = None,
17
+ path_name: Optional[Union[str, PathLike]] = None,
18
+ ):
19
+ assert is_dict(settings)
20
+ self._default_settings = settings
21
+ if path_name is not None and is_pathlike(path_name):
22
+ if not str(path_name).endswith(".json"):
23
+ self.path_name = str(Path(path_name, "config.json")).replace("\\", "/")
24
+ else:
25
+ self.path_name = str(path_name).replace("\\", "/")
26
+ else:
27
+ self.path_name = "config.json"
28
+ self.reset_settings()
29
+
30
+ def _setup_path_name(self, path_name: Union[str, PathLike]):
31
+ if is_file(path_name):
32
+ self.from_path(path_name)
33
+ self.path_name = str(path_name).replace("\\", "/")
34
+ elif is_str(path_name):
35
+ self.path_name = str(path_name).replace("\\", "/")
36
+ if not self.path_name.endswith((".json")):
37
+ self.path_name += ".json"
38
+
39
+ def reset_settings(self):
40
+ for s_name, setting in self._default_settings.items():
41
+ if s_name in self._forbidden_list:
42
+ continue
43
+ updateDict(self, {s_name: setting})
44
+
45
+ def save_config(
46
+ self,
47
+ path_name: Union[PathLike, str],
48
+ ):
49
+ assert is_pathlike(
50
+ path_name, True
51
+ ), f"path_name should be a non-empty string or pathlike object! received instead: {path_name}"
52
+ self._setup_path_name(path_name)
53
+ base = {k: y for k, y in self.__dict__.items() if k not in self._forbidden_list}
54
+ save_json(self.path_name, base, indent=2)
55
+
56
+ def to_dict(self):
57
+ return {k: y for k, y in self.__dict__.items() if k not in self._forbidden_list}
58
+
59
+ def set_value(self, var_name: str, value: str) -> None:
60
+ updateDict(self, {var_name: value})
61
+
62
+ def get_value(self, var_name: str) -> Any:
63
+ return self.__dict__.get(var_name)
64
+
65
+ @classmethod
66
+ def from_dict(
67
+ cls, dictionary: Dict[str, Any], path: Optional[Union[str, PathLike]] = None
68
+ ) -> "ModelConfig":
69
+ assert is_dict(dictionary)
70
+ return ModelConfig(dictionary, path)
71
+
72
+ @classmethod
73
+ def from_path(cls, path_name: PathLike) -> "ModelConfig":
74
+ assert is_file(path_name) or is_dir(path_name)
75
+ settings = {}
76
+
77
+ if is_file(path_name):
78
+ settings.update(load_json(path_name, {}, errors="ignore"))
79
+ else:
80
+ files = FileScan.files(
81
+ path_name,
82
+ [
83
+ "*_config.json",
84
+ "config_*.json",
85
+ "*_config.json",
86
+ "cfg_*.json",
87
+ "*_cfg.json",
88
+ "cfg.json",
89
+ "config.json",
90
+ "settings.json",
91
+ "settings_*.json",
92
+ "*_settings.json",
93
+ ],
94
+ )
95
+ assert files, "No config file found in the provided directory!"
96
+ settings.update(load_json(files[-1], {}, errors="ignore"))
97
+ return ModelConfig(settings, path_name)
@@ -6,11 +6,10 @@ from torch.utils.data import Dataset, DataLoader, Sampler
6
6
  from lt_tensor.processors import AudioProcessor
7
7
  import torch.nn.functional as FT
8
8
  from lt_tensor.misc_utils import log_tensor
9
+ from tqdm import tqdm
9
10
 
10
11
 
11
12
  class WaveMelDataset(Dataset):
12
- """Untested!"""
13
-
14
13
  data: Union[list[dict[str, Tensor]], Tuple[Tensor, Tensor]] = []
15
14
 
16
15
  def __init__(
@@ -19,12 +18,16 @@ class WaveMelDataset(Dataset):
19
18
  path: PathLike,
20
19
  limit_files: Optional[int] = None,
21
20
  max_frame_length: Optional[int] = None,
21
+ randomize_ranges: bool = False,
22
22
  ):
23
23
  super().__init__()
24
24
  assert max_frame_length is None or max_frame_length >= (
25
25
  (audio_processor.n_fft // 2) + 1
26
26
  )
27
+
27
28
  self.post_n_fft = (audio_processor.n_fft // 2) + 1
29
+ if max_frame_length is not None:
30
+ self.r_range = max(self.post_n_fft + 1, max_frame_length // 3)
28
31
  self.ap = audio_processor
29
32
  self.files = self.ap.find_audios(path)
30
33
  if limit_files:
@@ -32,21 +35,32 @@ class WaveMelDataset(Dataset):
32
35
  self.files = self.files[:limit_files]
33
36
  self.data = []
34
37
 
35
- for file in self.files:
36
- results = self.load_data(file, max_frame_length)
38
+ for file in tqdm(self.files, "Loading files"):
39
+ results = self.load_data(file, max_frame_length, randomize_ranges)
37
40
  self.data.extend(results)
38
41
 
39
42
  def _add_dict(self, audio_raw: Tensor, audio_mel: Tensor, file: PathLike):
40
43
  return {"mel": audio_mel, "raw": audio_raw, "file": file}
41
44
 
42
- def load_data(self, file: PathLike, audio_frames_limit: Optional[int] = None):
43
- initial_audio = self.ap.load_audio(file)
45
+ def load_data(
46
+ self,
47
+ file: PathLike,
48
+ audio_frames_limit: Optional[int] = None,
49
+ randomize_ranges: bool = False,
50
+ ):
51
+ initial_audio = self.ap.rebuild_spectrogram(self.ap.load_audio(file))
44
52
  if not audio_frames_limit or initial_audio.shape[-1] <= audio_frames_limit:
53
+ if initial_audio.shape[-1] < self.post_n_fft:
54
+ return []
45
55
  audio_mel = self.ap.compute_mel(initial_audio, add_base=True)
46
56
  return [self._add_dict(initial_audio, audio_mel, file)]
47
57
  results = []
58
+ if randomize_ranges:
59
+ frame_limit = random.randint(self.r_range, audio_frames_limit)
60
+ else:
61
+ frame_limit = audio_frames_limit
48
62
  for fragment in torch.split(
49
- initial_audio, split_size_or_sections=audio_frames_limit, dim=-1
63
+ initial_audio, split_size_or_sections=frame_limit, dim=-1
50
64
  ):
51
65
  if fragment.shape[-1] < self.post_n_fft:
52
66
  # sometimes the tensor will be too small to be able to pass on mel
lt_tensor/losses.py CHANGED
@@ -11,7 +11,7 @@ __all__ = [
11
11
  ]
12
12
  import math
13
13
  import random
14
- from .torch_commons import *
14
+ from lt_tensor.torch_commons import *
15
15
  from lt_utils.common import *
16
16
  import torch.nn.functional as F
17
17
 
lt_tensor/math_ops.py CHANGED
@@ -12,7 +12,7 @@ __all__ = [
12
12
  "phase",
13
13
  ]
14
14
 
15
- from .torch_commons import *
15
+ from lt_tensor.torch_commons import *
16
16
 
17
17
 
18
18
  def sin_tensor(x: Tensor, freq: float = 1.0) -> Tensor:
lt_tensor/misc_utils.py CHANGED
@@ -21,6 +21,8 @@ __all__ = [
21
21
  "Masking",
22
22
  "LogTensor",
23
23
  "get_losses",
24
+ "plot_view",
25
+ "get_weights",
24
26
  ]
25
27
 
26
28
  import re
@@ -28,14 +30,81 @@ import gc
28
30
  import sys
29
31
  import random
30
32
  import numpy as np
31
- from lt_utils.type_utils import is_str
32
- from .torch_commons import *
33
+ import warnings
34
+ from lt_utils.type_utils import is_str, is_dir, is_file, is_pathlike, is_path_valid
35
+ from lt_utils.file_ops import FileScan, find_files, path_to_str, load_json, load_yaml
36
+ from lt_tensor.torch_commons import *
33
37
  from lt_utils.misc_utils import cache_wrapper
34
38
  from lt_utils.common import *
35
39
  from lt_utils.misc_utils import ff_list
36
40
  import torch.nn.functional as F
37
41
 
38
42
 
43
+ def plot_view(
44
+ data: Dict[str, List[Any]],
45
+ title: str = "Loss",
46
+ max_amount: int = 0,
47
+ xaxis_title="Step/Epoch",
48
+ yaxis_title="Loss",
49
+ template="plotly_dark",
50
+ ):
51
+ try:
52
+ import plotly.graph_objs as go
53
+ except ModuleNotFoundError:
54
+ warnings.warn(
55
+ "No installation of plotly was found. To use it use 'pip install plotly' and restart this application!"
56
+ )
57
+ return
58
+ fig = go.Figure()
59
+ for mode, values in data.items():
60
+ if values:
61
+ items = values if not max_amount > 0 else values[-max_amount:]
62
+ fig.add_trace(go.Scatter(y=items, name=mode.capitalize()))
63
+ fig.update_layout(
64
+ title=title,
65
+ xaxis_title=xaxis_title,
66
+ yaxis_title=yaxis_title,
67
+ template=template,
68
+ )
69
+ return fig
70
+
71
+
72
+ def get_weights(directory: Union[str, PathLike]):
73
+ is_path_valid(directory, validate=True) # raises validation if its invalid path
74
+ directory = Path(directory)
75
+ if is_file(directory):
76
+ if directory.name.endswith((".pt", ".ckpt", ".pth")):
77
+ return directory
78
+ directory = directory.parent
79
+ res = sorted(find_files(directory, ["*.pt", "*.ckpt", "*.pth"]))
80
+ return res[-1] if res else None
81
+
82
+
83
+ def get_config(directory: Union[str, PathLike], default: Optional[Any] = None):
84
+ # raises validation if its invalid path only when default is None otherwise it returns the defaults.
85
+ if not is_path_valid(directory, validate=default is None):
86
+ return default
87
+ directory = Path(directory)
88
+ if is_file(directory):
89
+ if directory.name.endswith((".json", ".yaml", ".yml")):
90
+ if directory.name.endswith(".json"):
91
+ return load_json(directory, default)
92
+ return load_yaml(directory, default)
93
+ directory = directory.parent
94
+ res = sorted(find_files(directory, ["*.pt", "*.ckpt", "*.pth"]))
95
+ if res:
96
+ res = res[-1]
97
+ if Path(res).name.endswith(".json"):
98
+ return load_json(directory, default)
99
+ return load_yaml(directory, default)
100
+ return default
101
+
102
+
103
+ def updateDict(self, dct: dict[str, Any]):
104
+ for k, v in dct.items():
105
+ setattr(self, k, v)
106
+
107
+
39
108
  def soft_restore(tensor, epsilon=1e-6):
40
109
  return torch.where(tensor == 0, torch.full_like(tensor, epsilon), tensor)
41
110