File size: 1,699 Bytes
3ab6186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import parameters as params
import numpy as np
import tensorflow as tf


def get_copies(song, mel_sr=params.MEL_SAMPLING_RATE):
    num_2_secs = song.shape[0]//(2*mel_sr)
    copies = []
    for _ in range(num_2_secs - 4):
        copies.append(np.array([song[_*2*mel_sr:_*2*mel_sr + params.NUM_SECONDS*mel_sr,:]]))
    return copies

def get_results(len_song, results, mel_sr=params.MEL_SAMPLING_RATE):
    stacked_res = []
    for _ in range(len(results)):
        current_predictions = results[_]
        new = np.concatenate((np.zeros((1, _*2*mel_sr)),
                              current_predictions,
                              np.zeros((1, len_song - _*2*mel_sr - current_predictions.shape[1]))), axis=1)
        
        stacked_res.append(new)
    return stacked_res

def get_mean(stacked_results, mel_sr=params.MEL_SAMPLING_RATE):
    without_mean = stacked_results[0]
    for _ in stacked_results[1:]:
        without_mean+= _
    return without_mean

def mean_predictions(song, model, mel_sr=params.MEL_SAMPLING_RATE):
    res = get_copies(song, mel_sr)
    for _ in range(len(res)):
        #print('processing part of the song ', str(_))
        outcomes = model(res[_])
        res[_] = outcomes
    

    
    stacked_res = get_results(song.shape[0], res, mel_sr)
    means = get_mean(stacked_res, mel_sr)
    
    for_normalizing = []
    for _ in range(len(res)):
        for_normalizing.append(np.ones((1,res[0].shape[1])))
    for_normalizing = get_results(song.shape[0], for_normalizing, mel_sr)
    for_normalizing = get_mean(for_normalizing, mel_sr)
    
    return tf.convert_to_tensor(means[0]/np.maximum(for_normalizing, np.ones(for_normalizing.shape)), dtype='float32')