Nithya commited on
Commit
d3c7fa1
·
1 Parent(s): 9272247

updated app to use GPUQuantileTransformer from github

Browse files
Files changed (2) hide show
  1. app.py +3 -30
  2. requirements.txt +1 -1
app.py CHANGED
@@ -5,7 +5,7 @@ debug_mode = '--debug' in sys.argv or os.environ.get('DEBUG') == 'True'
5
 
6
  if debug_mode:
7
  # Path to the local version of the package
8
- local_package_path = "../../GaMaDHaNi"
9
 
10
  # Add the local package path to sys.path
11
  sys.path.insert(0, local_package_path)
@@ -51,33 +51,6 @@ def debug_profile(func):
51
  return pp.profile(sort_by='cumulative', out_lines=10)(func)
52
  return func
53
 
54
- class GPUQuantileTransformer:
55
- '''
56
- Temporary hack class to perform inverse quantile transform on GPU
57
- '''
58
- def __init__(self, qt):
59
- # Initialize the sklearn QuantileTransformer
60
- self.cpu_transformer = qt
61
- self.quantiles_ = torch.Tensor(qt.quantiles_).to(device)
62
- self.references_ = torch.Tensor(qt.references_).to(device)
63
-
64
- def transform(self, X):
65
- return self.cpu_transformer.transform(X)
66
-
67
- def inverse_transform(self, X):
68
- # convert distribution to uniform
69
- X = 0.5 * (1 + torch.erf(X / torch.sqrt(torch.tensor(2.0))))
70
- # Interpolate using the quantiles and references on GPU
71
- idxs = torch.searchsorted(self.references_.view(1, -1), X.view(1, -1)).to(torch.int64)
72
- idxs = idxs.view(-1, 1) # to match with the shape of quantiles
73
- quantiles_low = torch.gather(self.quantiles_, 0, (idxs - 1).clamp(min=0))
74
- quantiles_high = torch.gather(self.quantiles_, 0, idxs.clamp(max=self.quantiles_.size(0) - 1))
75
-
76
- # Linear interpolation between quantiles
77
- t = (X - self.references_[idxs - 1]) / (self.references_[idxs] - self.references_[idxs - 1] + 1e-10)
78
- X_inv = quantiles_low + t * (quantiles_high - quantiles_low)
79
- return X_inv.reshape(1, -1)
80
-
81
  def predict_voicing(confidence):
82
  # https://github.com/marl/crepe/pull/26
83
  """
@@ -162,7 +135,7 @@ def generate(pitch, num_samples=1, num_steps=100, singers=[3], outfolder='temp',
162
  logging.log(logging.INFO, 'Generate function')
163
  # load pitch values onto GPU
164
  pitch = torch.tensor(pitch).float().unsqueeze(0).unsqueeze(0).to(device)
165
- pitch_qt = GPUQuantileTransformer(pitch_qt)
166
  logging.log(logging.INFO, 'Generating pitch')
167
  if type == 'response':
168
  pitch, inverted_pitch = generate_pitch_response(pitch, pitch_model, invert_pitch_fn, num_samples=num_samples, num_steps=100)
@@ -175,7 +148,7 @@ def generate(pitch, num_samples=1, num_steps=100, singers=[3], outfolder='temp',
175
  if pitch_qt is not None:
176
  # if there is not pitch quantile transformer, undo the default quantile transformation that occurs
177
  def undo_qt(x, min_clip=200):
178
- pitch= pitch_qt.inverse_transform(x.reshape(-1, 1)).reshape(1, -1)
179
  pitch = torch.round(pitch) # round to nearest integer, done in preprocessing of pitch contour fed into model
180
  pitch[pitch < 200] = np.nan
181
  return pitch
 
5
 
6
  if debug_mode:
7
  # Path to the local version of the package
8
+ local_package_path = "../../GaMaDHaNi-dev"
9
 
10
  # Add the local package path to sys.path
11
  sys.path.insert(0, local_package_path)
 
51
  return pp.profile(sort_by='cumulative', out_lines=10)(func)
52
  return func
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  def predict_voicing(confidence):
55
  # https://github.com/marl/crepe/pull/26
56
  """
 
135
  logging.log(logging.INFO, 'Generate function')
136
  # load pitch values onto GPU
137
  pitch = torch.tensor(pitch).float().unsqueeze(0).unsqueeze(0).to(device)
138
+ pitch_qt = p2a.GPUQuantileTransformer(pitch_qt, device=device)
139
  logging.log(logging.INFO, 'Generating pitch')
140
  if type == 'response':
141
  pitch, inverted_pitch = generate_pitch_response(pitch, pitch_model, invert_pitch_fn, num_samples=num_samples, num_steps=100)
 
148
  if pitch_qt is not None:
149
  # if there is not pitch quantile transformer, undo the default quantile transformation that occurs
150
  def undo_qt(x, min_clip=200):
151
+ pitch= pitch_qt.inverse_transform(x).squeeze(0) # qt transform expects shape (bs, seq_len, 1)
152
  pitch = torch.round(pitch) # round to nearest integer, done in preprocessing of pitch contour fed into model
153
  pitch[pitch < 200] = np.nan
154
  return pitch
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  crepe==0.0.15
2
  hmmlearn==0.3.2
3
  tensorflow==2.17.0
4
- GaMaDHaNi @ git+https://github.com/snnithya/GaMaDHaNi.git@43aa691e0df53f64e4bffbd7b8afc703593bb630
 
1
  crepe==0.0.15
2
  hmmlearn==0.3.2
3
  tensorflow==2.17.0
4
+ GaMaDHaNi @ git+https://github.com/snnithya/GaMaDHaNi.git@37788f4c900fc425cd193052784e88afbfdd19e2