code: import argparse
  import os
  
  import cv2
  import librosa
  import numpy as np
  import soundfile as sf
  import torch
  from tqdm import tqdm
  
  from lib import dataset
  from lib import nets
  from lib import spec_utils
  
  
  class VocalRemover(object):
  
      def __init__(self, model, device, window_size):
          self.model = model
          self.offset = model.offset
          self.device = device
          self.window_size = window_size
  
      def _execute(self, X_mag_pad, roi_size, n_window):
          self.model.eval()
          with torch.no_grad():
              preds = []
              for i in tqdm(range(n_window)):
                  start = i * roi_size
                  X_mag_window = X_mag_pad[None, :, :, start:start + self.window_size]
                  X_mag_window = torch.from_numpy(X_mag_window).to(self.device)
  
                  pred = self.model.predict(X_mag_window)
  
                  pred = pred.detach().cpu().numpy()
                  preds.append(pred[0])
  
              pred = np.concatenate(preds, axis=2)
  
          return pred
  
      def preprocess(self, X_spec):
          X_mag = np.abs(X_spec)
          X_phase = np.angle(X_spec)
  
          return X_mag, X_phase
  
      def inference(self, X_spec):
          X_mag, X_phase = self.preprocess(X_spec)
  
          coef = X_mag.max()
          X_mag_pre = X_mag / coef
  
          n_frame = X_mag_pre.shape[2]
          pad_l, pad_r, roi_size = dataset.make_padding(n_frame, self.window_size, self.offset)
          n_window = int(np.ceil(n_frame / roi_size))
  
          X_mag_pad = np.pad(X_mag_pre, ((0, 0), (0, 0), (pad_l, pad_r)), mode='constant')
  
          pred = self._execute(X_mag_pad, roi_size, n_window)
          pred = pred[:, :, :n_frame]
  
          return pred * coef, X_mag, np.exp(1.j * X_phase)
  
      def inference_tta(self, X_spec):
          X_mag, X_phase = self.preprocess(X_spec)
  
          coef = X_mag.max()
          X_mag_pre = X_mag / coef
  
          n_frame = X_mag_pre.shape[2]
          pad_l, pad_r, roi_size = dataset.make_padding(n_frame, self.window_size, self.offset)
          n_window = int(np.ceil(n_frame / roi_size))
  
          X_mag_pad = np.pad(X_mag_pre, ((0, 0), (0, 0), (pad_l, pad_r)), mode='constant')
  
          pred = self._execute(X_mag_pad, roi_size, n_window)
          pred = pred[:, :, :n_frame]
  
          pad_l += roi_size // 2
          pad_r += roi_size // 2
          n_window += 1
  
          X_mag_pad = np.pad(X_mag_pre, ((0, 0), (0, 0), (pad_l, pad_r)), mode='constant')
  
          pred_tta = self._execute(X_mag_pad, roi_size, n_window)
          pred_tta = pred_tta[:, :, roi_size // 2:]
          pred_tta = pred_tta[:, :, :n_frame]
  
          return (pred + pred_tta) * 0.5 * coef, X_mag, np.exp(1.j * X_phase)
  
  
  def main():
      p = argparse.ArgumentParser()
      p.add_argument('--gpu', '-g', type=int, default=-1)
      p.add_argument('--pretrained_model', '-P', type=str, default='models/baseline.pth')
      p.add_argument('--input', '-i', required=True)
      p.add_argument('--sr', '-r', type=int, default=44100)
      p.add_argument('--n_fft', '-f', type=int, default=2048)
      p.add_argument('--hop_length', '-l', type=int, default=1024)
      p.add_argument('--window_size', '-w', type=int, default=512)
      p.add_argument('--output_image', '-I', action='store_true')
      p.add_argument('--postprocess', '-p', action='store_true')
      p.add_argument('--tta', '-t', action='store_true')
      args = p.parse_args()
  
      print('loading model...', end=' ')
      device = torch.device('cpu')
      model = nets.CascadedASPPNet(args.n_fft)
      model.load_state_dict(torch.load(args.pretrained_model, map_location=device))
      if torch.cuda.is_available() and args.gpu >= 0:
          device = torch.device('cuda:{}'.format(args.gpu))
          model.to(device)
      print('done')
  
      print('loading wave source...', end=' ')
      X, sr = librosa.load(
          args.input, args.sr, False, dtype=np.float32, res_type='kaiser_fast')
      basename = os.path.splitext(os.path.basename(args.input))[0]
      print('done')
  
      if X.ndim == 1:
          X = np.asarray([X, X])
  
      print('stft of wave source...', end=' ')
      X = spec_utils.wave_to_spectrogram(X, args.hop_length, args.n_fft)
      print('done')
  
      vr = VocalRemover(model, device, args.window_size)
  
      if args.tta:
          pred, X_mag, X_phase = vr.inference_tta(X)
      else:
          pred, X_mag, X_phase = vr.inference(X)
  
      if args.postprocess:
          print('post processing...', end=' ')
          pred_inv = np.clip(X_mag - pred, 0, np.inf)
          pred = spec_utils.mask_silence(pred, pred_inv)
          print('done')
  
      print('inverse stft of instruments...', end=' ')
      y_spec = pred * X_phase
      wave = spec_utils.spectrogram_to_wave(y_spec, hop_length=args.hop_length)
      print('done')
      sf.write('{}_Instruments.wav'.format(basename), wave.T, sr)
  
      print('inverse stft of vocals...', end=' ')
      v_spec = np.clip(X_mag - pred, 0, np.inf) * X_phase
      wave = spec_utils.spectrogram_to_wave(v_spec, hop_length=args.hop_length)
      print('done')
      sf.write('{}_Vocals.wav'.format(basename), wave.T, sr)
  
      if args.output_image:
          with open('{}_Instruments.jpg'.format(basename), mode='wb') as f:
              image = spec_utils.spectrogram_to_image(y_spec)
              _, bin_image = cv2.imencode('.jpg', image)
              bin_image.tofile(f)
          with open('{}_Vocals.jpg'.format(basename), mode='wb') as f:
              image = spec_utils.spectrogram_to_image(v_spec)
              _, bin_image = cv2.imencode('.jpg', image)
              bin_image.tofile(f)
  
  
  if __name__ == '__main__':
      main()
  
apis: torch.load numpy.ceil numpy.concatenate torch.no_grad numpy.abs numpy.asarray numpy.exp torch.cuda.is_available numpy.clip numpy.angle torch.from_numpy numpy.pad torch.device