<jupyter_start><jupyter_text>Detect Ads in radio stream using the trained models:<jupyter_code>import os
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
import librosa
import librosa.display
import IPython.display as ipd
from sklearn.cluster import MiniBatchKMeans
import joblib
import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten
from keras.callbacks import ModelCheckpoint
from DataGenerator import DataGenerator_Sup
from utility_functions import create_model, create_CNN_model, audio2features_vectors, listen_to, moving_average<jupyter_output>Using TensorFlow backend.<jupyter_text>Get a list of all files:<jupyter_code>Radio_stream_folder = '../Data/radio_streams/' # audio files location

example_files = []
for r,d,f in os.walk(Radio_stream_folder):
    for filename in f:
        if '.wav' in filename:
            example_files.append(os.path.join(r,filename))<jupyter_output><empty_output><jupyter_text>How many mp3 files do we have?<jupyter_code>n_files = len(example_files)

print('We have ' + str(n_files) + ' radio streaming examples')<jupyter_output>We have 1 radio streaming examples<jupyter_text>Define utility functions:<jupyter_code>def extract_timeframe_data(X, start_time, end_time, d=3):
    '''Returns the part of X which is in the defined timeframe'''
    t = np.arange(X.shape[0])*d/60.0 # create time axis
    S_idx = np.argmin((t-start_time)**2) # start index
    E_idx = np.argmin((t-end_time)**2) # end index
#     import pdb;pdb.set_trace()
    return X[S_idx:E_idx]
def get_timestamps(Ad_prob, T=0.95, d=3, n=1):
    '''Takes an Ad probability vector, a threshold and time bin size
       and returns start and end timestamps'''
    # smoothing and creating a time axis
    Ad_prob_smooth = moving_average(Ad_prob, n) # smoothing 
    t = np.arange(len(Ad_prob))*d/60.0 # create time axis
    t_smooth = moving_average(t, n)
    
    # thresholding for detection and extracting start and end times
    detection_domains = (Ad_prob_smooth>T).astype(int) # get 1s for every detection
    transitions = np.diff(detection_domains) # get transitions between ads and non ads
    
    start_times = t_smooth[:-1][transitions == 1] + d/60 # ad starts when prob. goes 0 -> 1
    end_times = t_smooth[:-1][transitions == -1] + d/60# ad ends when prob. goes 1 -> 0
    
    # if the first event is an end - add a start event at time 0
    if np.min(end_times) < np.min(start_times):
        start_times = np.hstack([0,start_times])
    # if the last event is a start - add an end event at the end of the audio
    if np.max(end_times) < np.max(start_times):
        end_times = np.hstack([end_times, t_smooth[-1]])
    
    timestamps = np.vstack([start_times, end_times]).transpose()
    
    # compute probabilities based on music vs ad classifier
    music_vs_ads_probs =[]
    for i in range(timestamps.shape[0]):
        S_idx = np.argmin((t-timestamps[i][0])**2) # start index
        E_idx = np.argmin((t-timestamps[i][1])**2) # end index
        prob = np.mean(Ad_prob[S_idx:E_idx])
        music_vs_ads_probs.append(prob)
        
    return timestamps, np.vstack(music_vs_ads_probs)
def filter_nonads_speech(X, timestamps, T=0.5):
    '''Returns timestamps vector after filtering speech audio clips 
       which do not contain ads'''
    results = [] # allocate space for results
    
    # choose model's weights
    weights_path = 'models/weights_LeNet5ish_1000_only_podcasts_and_ads_9epochs.hdf5'
    # recreate a model for evaluation
    speech_model = create_CNN_model(quiet = True)
    # load weights
    speech_model.load_weights(weights_path)
    # compile
    speech_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    for i in range(timestamps.shape[0]):
        X_p = extract_timeframe_data(X, timestamps[i][0], timestamps[i][1])
        Ad_prob = speech_model.predict(X_p)
        # keep only time frames with ad probability higher than T
        if np.mean(Ad_prob) > T:
            results.append(timestamps[i])
    
    return np.array(results)
def Ad_vs_speech_classifier(X, timestamps, probs):
    '''Returns ad probability vector after applying ad vs speech classification'''
    results = [] # allocate space for results
    
    # choose model's weights
    weights_path = 'models/weights_LeNet5ish_1000_only_podcasts_and_ads_6epochs.hdf5'
    # recreate a model for evaluation
    speech_model = create_CNN_model(quiet = True)
    # load weights
    speech_model.load_weights(weights_path)
    # compile
    speech_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    for i in range(timestamps.shape[0]):
        X_p = extract_timeframe_data(X, timestamps[i][0], timestamps[i][1])
        Ad_prob = speech_model.predict(X_p)
        probs[i] = probs[i] * np.mean(Ad_prob)
        
    return probs<jupyter_output><empty_output><jupyter_text>Choose audio file and load it:<jupyter_code>n = 0 # file number to load
d = 3 # duration in sec of a sigle clip

example_files[n]<jupyter_output><empty_output><jupyter_text>Run model on file and extract start and end time:<jupyter_code>X_cnn = audio2features_vectors(example_files[n], CNN= True)
print(X_cnn.shape)
print('file lenght is ' + str(X_cnn.shape[0]*d/60.0) + ' minutes')
# choose model's weights
weights_path = 'models/weights_LeNet5ish_1000_only_music_and_ads_10epochs.hdf5'

# recreate a model for evaluation
eval_model = create_CNN_model()
# load weights
eval_model.load_weights(weights_path)
# compile
eval_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Ad_prob_cnn = eval_model.predict(X_cnn)
Ad_prob_avg_cnn = moving_average(Ad_prob_cnn, n=1)
t = np.arange(len(Ad_prob_cnn))*d/60.0 # create time axis
t_avg = moving_average(t, n=1)
# %matplotlib notebook
T = 0.95

plt.rcParams.update({'font.size': 15})
plt.figure(figsize= (8,5))
plt.subplot(3,1,1)
plt.plot(t, Ad_prob_cnn, 'g')
plt.ylabel('Ad prob.')

plt.subplot(3,1,2)
plt.plot(t_avg, Ad_prob_avg_cnn, 'r')

plt.subplot(3,1,3)
plt.plot(t_avg, Ad_prob_avg_cnn>T, 'bo')
plt.xlabel('Time (min)')
plt.tight_layout()
timestamps, probs = get_timestamps(Ad_prob_cnn)
print('The extracted timestamps are:')
print(timestamps)
k = 1
listen_to(example_files[n], timestamps[k][0], timestamps[k][1])<jupyter_output><empty_output><jupyter_text>Second step: Ads vs. speech Find the probability of each detected timeframe being an ad:<jupyter_code>probs = Ad_vs_speech_classifier(X_cnn, *get_timestamps(Ad_prob_cnn))
print('The probabilities assigned by the model are:')
print(probs)<jupyter_output>The probabilities assigned by the model are:
[[0.1816173 ]
 [0.92330784]]<jupyter_text>Filter only high probability time frames:<jupyter_code>filter_nonads_speech(X_cnn, timestamps)<jupyter_output><empty_output><jupyter_text>Debuging:<jupyter_code>len(Ad_prob_cnn[3:6])
np.mean(Ad_prob_cnn[3:6])
Ad_vs_speech_classifier(X_cnn, np.array([[0.15, 0.3]]), [np.mean(Ad_prob_cnn[3:6])])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11155321870115106, 'fraction_numerical': 0.018331675429870683, 'mean_word_length': 4.778325123152709, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '568809', 'license': 'MIT', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2481, 'n_words': 571, 'path': 'notebooks/Detection_Algorithm_Demo.ipynb', 'repo_name': 'ohadmich/Adetector', 'size': '7037'}