<jupyter_start><jupyter_text>Separation of speakers using PIT-S-CNNThis notebook contains an example of loading an already trained version of the PIT-S-CNN source separation model.  It also shows how to use the loaded model to separate individual speakers from an example waveform.<jupyter_code># Generic imports
import numpy as np
import tensorflow as tf

# Imports to play audio
from IPython.display import Audio

# Import Lab41's separation model
from magnolia.dnnseparate.pit import PITModel

# Import utilities for using the model
from magnolia.utils.clustering_utils import clustering_separate, preprocess_signal
from magnolia.features.mixer import FeatureMixer
from magnolia.features.spectral_features import istft, scale_spectrogram
from magnolia.utils.postprocessing import reconstruct
from magnolia.features.data_preprocessing import undo_preemphasis<jupyter_output><empty_output><jupyter_text>Paths<jupyter_code>libridev = "** Path to librispeech dev hdf5 **"
model_path = "** Path to model checkpoint **"<jupyter_output><empty_output><jupyter_text>Hyperparameters    fft_size    : Number of samples in the fft window    overlap     : Amount of overlap in the fft windows    sample_rate : Number of samples per second in the input signals    numsources  : Number of sources    datashape   : (Number of time steps, nubmer of frequency bins)    preemp_coef : Preemphasis coefficient<jupyter_code>fft_size = 512
overlap = 0.0256
sample_rate = 10000
numsources = 2
datashape = (51, fft_size//2 + 1)
preemp_coef = 0.95<jupyter_output><empty_output><jupyter_text>Create and load a pretrained instance of PIT-S-CNNHere an untrained model instance is created, and the pretrained weights are loaded<jupyter_code>tf.reset_default_graph()

model = PITModel(method='pit-s-cnn', num_steps=datashape[0], num_freq_bins=datashape[1], num_srcs=numsources)

config = tf.ConfigProto()
config.allow_soft_placement = True
config.gpu_options.allow_growth = True

sess = tf.Session(config=config)
model.load(model_path, sess)<jupyter_output><empty_output><jupyter_text>Example separation processSamples can be generated from the dev set for qualitatively evaluating the perfomance of the model and to test the separation process.  For this example, a sample will be generated, converted to a raw waveform, and then separated into two sources.<jupyter_code># Create a mixer for recordings from the dev set
long_mixer = FeatureMixer([libridev,libridev], shape=(200,None))<jupyter_output><empty_output><jupyter_text>Get an example from the mixer and convert it back into a waveform.<jupyter_code>data = next(long_mixer)
spec = data[0]
spec_mag, spec_phase = scale_spectrogram(spec)
signal = istft(spec,sample_rate,None,overlap,two_sided=False,fft_size=512)
signal = undo_preemphasis(signal)

Audio(signal,rate=sample_rate)<jupyter_output><empty_output><jupyter_text>Use the model's separate function to separate the signal waveform into sources.<jupyter_code>sources_spec = model.separate(spec_mag, sess)
sources = [reconstruct(x, spec, sample_rate, None, overlap, square=True, preemphasis=preemp_coef) 
           for x in sources_spec]<jupyter_output><empty_output><jupyter_text>Listen to the results<jupyter_code>Audio(sources[0], rate=sample_rate)
Audio(sources[1], rate=sample_rate)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07510599636583888, 'fraction_numerical': 0.010599636583888553, 'mean_word_length': 6.827014218009479, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '579636', 'license': 'Apache-2.0', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1007, 'n_words': 300, 'path': 'magnolia/sandbox/notebooks/source-separation/pit/PitCnn_separation.ipynb', 'repo_name': 'rashley-iqt/Magnolia', 'size': '3302'}