HumBug data processing

Note, this notebook contains two sets of functions for processing data. One is concerned with labels from the Culex labelled dataset. The other, with suffix _cdc, is currently in place to experiment with the algorithm in unlabelled datasets

The functions associated with the CDC dataset require some building blocks from the culex data functions.

A minimal working example (for the hand-labelled Culex set) consists of running all the cells sequentially in Section 1, and is highly recommended for validating operation of every code block.


In [1]:
#import audacity
import numpy as np
import sys 
import os, os.path
from scipy.io.wavfile import read, write
from scipy import signal
import csv
%matplotlib inline
import matplotlib.pyplot as plt
from collections import Counter

import random

# Keras-related imports
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution1D, MaxPooling2D, Convolution2D
from keras import backend as K
K.set_image_dim_ordering('th')
from keras.callbacks import ModelCheckpoint
from keras.callbacks import RemoteMonitor
from keras.models import load_model

# Data post-processing and analysis
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score, roc_curve
import itertools


Using Theano backend.
Using gpu device 0: GeForce GTX 970 (CNMeM is disabled, cuDNN not available)

Section 1: Culex hand-labelled dataset

Data and label paths relative to maintain compatibility over multiple devices. Adjust as necessary


In [2]:
# Culex data (labelled with data_dz_corrob etc.)
data_path = '../../../../Resources/HumBug/Culex Quinquefasciatus/audio/'
dirs = os.listdir(data_path)

# labels
label_path = '../../../../Resources/HumBug/Culex Quinquefasciatus/labels/'
dirs_label = os.listdir(label_path)


label_list = []
label_names = []

# Process .txt labels. Create list with structure:
# list of [start_time, end_time, tag] entries
signal_list = []

for file in dirs:
    if file.endswith('.wav'):
        fs, signal = read(data_path + file)
        print 'Processing', file
        signal_list.append(signal)
print 'Processed %i files.'%len(signal_list) 
       
for file in dirs_label:
    if file.endswith('.txt'):
        filename = label_path + file
        with open(filename) as f:
            reader = csv.reader(f, delimiter = ' ')
            label_list.append(list(reader)) # Class labels
            label_names.append(file) # Save class name in separate array
print 'Processed %i labels.'%len(label_list)


Processing 0001_norm.wav
Processing 0002_norm.wav
Processing 0003_norm.wav
Processing 0004_norm.wav
Processing 0005_norm.wav
Processing 0006_norm.wav
Processing 0007_norm.wav
Processing 0008_norm.wav
Processing 0009_norm.wav
Processing 0010_norm.wav
Processing 0011_norm.wav
Processing 0012_norm.wav
Processing 0013_norm.wav
Processing 0014_norm.wav
Processing 0015_norm.wav
Processing 0016_norm.wav
Processing 0017_norm.wav
Processing 0018_norm.wav
Processing 0019_norm.wav
Processing 0020_norm.wav
Processing 0021_norm.wav
Processing 0022_norm.wav
Processing 0023_norm.wav
Processing 0024_norm.wav
Processing 0025_norm.wav
Processing 0026_norm.wav
Processing 0027_norm.wav
Processing 0028_norm.wav
Processing 0029_norm.wav
Processing 0030_norm.wav
Processing 0031_norm.wav
Processing 0032_norm.wav
Processing 0033_norm.wav
Processing 0034_norm.wav
Processing 0035_norm.wav
Processing 0036_norm.wav
Processing 0037_norm.wav
Processing 0038_norm.wav
Processing 0039_norm.wav
Processing 0040_norm.wav
Processing 0041_norm.wav
Processing 0042_norm.wav
Processing 0043_norm.wav
Processing 0044_norm.wav
Processing 0045_norm.wav
Processing 0046_norm.wav
Processing 0047_norm.wav
Processing 0048_norm.wav
Processing 0049_norm.wav
Processing 0050_norm.wav
Processing 0051_norm.wav
Processing 0052_norm.wav
Processing 0053_norm.wav
Processing 0054_norm.wav
Processing 0055_norm.wav
Processing 0056_norm.wav
Processing 0057_norm.wav
Processed 57 files.
Processed 7 labels.

If implementing a sort of majority voting (maj_vote = True), specify the count_method with a string. Options are:

'most_positive': If any label contains a positive, assign a label of +1

'two_or_more': if two or more labels agree, assign that label

'agreement': only assign samples where each label agrees.

Any other string will result in a ValueError.


In [5]:
#### Options for visualisation are commented out. ###
### Uncomment if desired, to see how the label aligns with the signal and its spectrogram ###

# Select majority voting or other options
maj_vote = False
count_method = 'most_positive'
select_label = 6 # 0: ac, 4 cm, 5 dz, 6 ms


print 'Be aware of index offset with Python lists and file names'

t = np.array(label_list[0])
print label_names


label_interval = 0.1 # Enter length of label interval in seconds
nfft = 512  # Need to be set to the same parameters as used in the spectrogram processing function proc_data_humbug
overlap = 256
spec_fs = (1.*fs/(nfft - overlap))



t_array = []

#ii = 34  # Index for visualisation

#plt.figure(figsize = (10,10))
#plt.title('Signal #%i'%(ii))
#im = plt.specgram(signal_list[ii], Fs = fs, NFFT = nfft, noverlap = overlap)
#plt.show()
#plt.figure(figsize = (10,10))

if maj_vote:
    for who in [0,4,5,6]:
        t = []    

        print 'Using label', label_names[who]
        for index, item in enumerate(label_list[who]):
            t_entry = [float(i) for i in item]
            t.append(t_entry[:len(signal_list[index-1])])    # -1 Corrects for extra label present

        t = np.delete(t, (0), axis = 0)
        t_array.append(np.array(t))
        print 't shape post processing', np.shape(t.squeeze())

        print 't array shape', np.shape(t_array)


#         t_initial = np.arange(0, len(t[ii]))
#         t_final = np.arange(0, len(t[ii]),1./(label_interval*spec_fs))
#         t_corrected = np.interp(t_final, t_initial, t[ii])
#         t_corrected = np.rint(t_corrected)

#         plt.plot(t_final/(1./label_interval), t_corrected, '.-')

#     print 'number of specgram time windows', len(im[2])
#     print 'number of time labels', len(t_corrected)
#     print 'last spectrogram time', im[2][-1]
#     print 'Length of t_initial', len(t[ii])
#     print 'Length of t_final', len(t_corrected)

    t = np.zeros([57,600])

    t_array = np.array(t_array)
    for j in range(np.shape(t_array)[1]):
        for i in range(np.shape(t_array)[2]):
            count = Counter(t_array[:,j,i])
            if count_method == 'two_or_more':
                if count[0] == count[1]:  # Tiebreak
                    t[j,i] = 1.  # As a result, 2 or more 1s are classified as 1
                else:
                    t[j,i] = count.most_common()[0][0]
            elif count_method == 'agreement':
                if max([count[0], count[1]]) == 4:
                    t[j,i] = np.argmax([count[0], count[1]])
                else:
                    t[j,i] = 3.
                    #remove the fucking sample marked as 3. jesus fuck that shit
            elif count_method == 'most_positive':
                if count[1] >= 1:  # If any label predicts a 1, classify as 1
                    t[j,i] = 1.  
                else:
                    t[j,i] = 0.
            else:
                raise ValueError('Specified count method does not exist')
              

    t_final = np.arange(0, len(t[ii]),1./(label_interval*spec_fs))
    t_corrected = np.interp(t_final, t_initial, t[ii])
    t_corrected = np.rint(t_corrected)
    plt.plot(t_final/(1./label_interval), t_corrected, '.-')
    plt.legend([label_names[0], label_names[4], label_names[5], label_names[6], count_method], loc=2,prop={'size':6})
    plt.xlabel('Sample number')
    plt.ylim([-0.1, 1.1])
    plt.title('Time (s) #%i'%ii)
    plt.show()

# Alternative processing and visualisation

else: 

    t = []    

    print 'Using label', label_names[select_label]
    for index, item in enumerate(label_list[select_label]):
        t_entry = [float(i) for i in item]
    #     t_entry = np.repeat(t_entry,int(label_interval*fs))  # Upsample labels to match sample rate of signal
        t.append(t_entry[:len(signal_list[index-1])])    # -1 Corrects for extra label present

    t = np.delete(t, (0), axis = 0)
    t_array.append(np.array(t))
    print 't shape post processing', np.shape(t.squeeze())

    print 't array shape', np.shape(t_array)


#     t_initial = np.arange(0, len(t[ii]))
#     t_final = np.arange(0, len(t[ii]),1./(label_interval*spec_fs))
#     t_corrected = np.interp(t_final, t_initial, t[ii])
#     t_corrected = np.rint(t_corrected)

#     plt.plot(t_final/(1./label_interval), t_corrected, '.-')
#     plt.legend([label_names[select_label]])
#     print 

#     print 'number of specgram time windows', len(im[2])
#     print 'number of time labels', len(t_corrected)
#     print 'last spectrogram time', im[2][-1]
#     print 'Length of t_initial', len(t[ii])
#     print 'Length of t_final', len(t_corrected)


Be aware of index offset with Python lists and file names
['data_corrob_ac.txt', 'data_corrob_all_0or1.txt', 'data_corrob_all_0to1.txt', 'data_corrob_all_1to4.txt', 'data_corrob_cm.txt', 'data_corrob_dz.txt', 'data_corrob_ms.txt']
Using label data_corrob_ms.txt
t shape post processing (57L, 600L)
t array shape (1L, 57L, 600L)

In [6]:
def proc_data_humbug(signal_list, t, fs, img_height, img_width, nfft = 512, overlap = 256, label_interval = 0.1):
               
    """Returns the training data x,y and the parameter input_shape required for initialisation of neural networks. 
    
    Takes as input arguments a signal list, and its associated time coordinates t. Would probably be better to integrate into
    one function, however remains separate as a t input was not required for the birdsong application (the origin of this 
    function). 
    
    img_height and width are used to determine the dimensions of the spectrogram chunk required as input to the NN. nfft and 
    overlap (optional) parameterise the spectrogram.
    
    label_interval: time interval between labels (in seconds)
    """

    spec_list = []
    t_list = []
    spec_fs = (1.*fs/(nfft - overlap))
    location_to_remove = []
    
    for i, t_i in enumerate(t):

        SpecInfo = plt.specgram(signal_list[i], Fs = fs, NFFT = nfft, noverlap = overlap)
        spec_list.append(np.array(SpecInfo))
        
        t_initial = np.arange(0, len(t_i))
        t_final = np.arange(0, len(t_i),1./(label_interval*spec_fs)) # Match fs_labels to fs_spec
        t_corrected = np.interp(t_final, t_initial, t_i)        
        t_corrected = np.rint(t_corrected)
        
        location = np.where(t_corrected == 3)
        #print 'length of t_corrected', len(t_corrected)
        location_to_remove.append(location)
        t_list.append(t_corrected)
   
    # Removal of samples where there is no mutual agreement, labelled by a 3 in pre-processing
    
    for index, item in enumerate(spec_list):
        t_spec = np.delete(spec_list[index][2], location_to_remove[index])
        f_spec = np.delete(spec_list[index][0], location_to_remove[index], axis = 1)
        short_t = np.delete(t[index], location_to_remove[index])
        
        print index, location_to_remove[index], 'Cut', np.shape(f_spec), 'Original', np.shape(spec_list[index][0])
        
        spec_list[index][0] = f_spec
        spec_list[index][2] = t_spec
###################################################################################################################    
    
    # Dimensions for image fed into network
    nb_classes = 2
    x_train = []
    y_train = []


    for i in np.arange(len(spec_list)):
        n_max = np.floor(np.divide(np.shape(spec_list[i][0])[1],img_width)).astype('int')

        print 'Processing signal number', i
        print 'Length of spec', np.shape(spec_list[i][0])[1]
        print 'Number of training inputs for this signal:', n_max    
        for n in np.arange(n_max):
            #print 'n', n, np.shape(spec_training_list[i][0])
            x_train.append(spec_list[i][0][:img_height,img_width*n:img_width*(n+1)])
            # Match sampling rate of labels to sampling rate of spectrogram output

            if not t_list[i][img_width*n:img_width*(n+1)].size:
                print spec_list[i][0][:img_height,img_width*n:img_width*(n+1)]
                print 'start and stop indices for t and spec', img_width*n, img_width*(n+1)
                plt.figure()
                plt.plot(t_list[i])
                plt.show()
                
                #raise ValueError('No location label found for signal number %i'%i) 
                
            y_train.append(t_list[i][img_width*n:img_width*(n+1)])
    
    
    y_train_mean = np.zeros([np.shape(y_train)[0], nb_classes])
    x_train = np.array(x_train).reshape((np.shape(x_train)[0],1,img_height,img_width))
        
    for ii, i in enumerate(y_train):
        if i.size:
            y_train_mean[ii,0] = int(np.round(np.mean(i)))
            y_train_mean[ii,1] = 1 - y_train_mean[ii,0]
        else:
            raise ValueError('Empty label at index %i'%ii)
    print '\nx dimensions', np.shape(x_train)
    print 'y dimensions', np.shape(y_train_mean)
    #print 'Fraction of positive samples', y_positive_frac

    input_shape = (1, img_height, img_width)
    
    return x_train, y_train_mean, input_shape

In [8]:
# # Specify whether data to be used in convnet or MLP
conv = True

# Code for withholding signals for test purposes. Mainly for visual comparisons with existing detectors in MozzPy

spec_window = 2

n_fft = nfft
n_overlap = overlap

#test_idx = ii
test_idx = np.array([1,5,19,11,17,18,19,24,28,30,32,35,36,37,38,42,45,49,56,57]) - 1 # Corrected for index convention
train_idx = np.arange(57)
train_idx = np.delete(train_idx, test_idx)

test_x = []
test_t = []
train_x = []
train_t = []

for idx in test_idx:
    test_x.append(signal_list[idx])
    test_t.append(t[idx])

for idx in train_idx:
    train_x.append(signal_list[idx])
    train_t.append(t[idx])

x_test, y_test, input_shape = proc_data_humbug(test_x, test_t, fs, n_fft/2,spec_window, nfft = n_fft, overlap = n_overlap)
x_train_caged, y_train_caged, input_shape = proc_data_humbug(train_x, train_t, fs, n_fft/2,spec_window, nfft = n_fft, overlap = n_overlap)

# Reshape data for MLP
if not conv:
    x_train_caged = x_train_caged.reshape(x_train_caged.shape[0], x_train_caged.shape[-2]*x_train_caged.shape[-1])
    x_test = x_test.reshape(x_test.shape[0], x_test.shape[-2]*x_test.shape[-1])


0 (array([], dtype=int64),) Cut (257L, 1679L) Original (257L, 1679L)
1 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
2 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
3 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
4 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
5 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
6 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
7 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
8 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
9 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
10 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
11 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
12 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
13 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
14 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
15 (array([], dtype=int64),) Cut (257L, 1863L) Original (257L, 1863L)
16 (array([], dtype=int64),) Cut (257L, 1863L) Original (257L, 1863L)
17 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
18 (array([], dtype=int64),) Cut (257L, 775L) Original (257L, 775L)
19 (array([], dtype=int64),) Cut (257L, 1239L) Original (257L, 1239L)
Processing signal number 0
Length of spec 1679
Number of training inputs for this signal: 839
Processing signal number 1
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 2
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 3
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 4
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 5
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 6
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 7
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 8
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 9
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 10
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 11
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 12
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 13
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 14
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 15
Length of spec 1863
Number of training inputs for this signal: 931
Processing signal number 16
Length of spec 1863
Number of training inputs for this signal: 931
Processing signal number 17
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 18
Length of spec 775
Number of training inputs for this signal: 387
Processing signal number 19
Length of spec 1239
Number of training inputs for this signal: 619

x dimensions (16412L, 1L, 256L, 2L)
y dimensions (16412L, 2L)
0 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
1 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
2 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
3 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
4 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
5 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
6 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
7 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
8 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
9 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
10 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
11 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
12 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
13 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
14 (array([], dtype=int64),) Cut (257L, 1855L) Original (257L, 1855L)
15 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
16 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
17 (array([], dtype=int64),) Cut (257L, 319L) Original (257L, 319L)
18 (array([], dtype=int64),) Cut (257L, 1863L) Original (257L, 1863L)
19 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
20 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
21 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
22 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
23 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
24 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
25 (array([], dtype=int64),) Cut (257L, 1671L) Original (257L, 1671L)
26 (array([], dtype=int64),) Cut (257L, 1815L) Original (257L, 1815L)
27 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
28 (array([], dtype=int64),) Cut (257L, 519L) Original (257L, 519L)
29 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
30 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
31 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
32 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
33 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
34 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
35 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
36 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
37 (array([], dtype=int64),) Cut (257L, 1695L) Original (257L, 1695L)
Processing signal number 0
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 1
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 2
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 3
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 4
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 5
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 6
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 7
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 8
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 9
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 10
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 11
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 12
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 13
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 14
Length of spec 1855
Number of training inputs for this signal: 927
Processing signal number 15
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 16
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 17
Length of spec 319
Number of training inputs for this signal: 159
Processing signal number 18
Length of spec 1863
Number of training inputs for this signal: 931
Processing signal number 19
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 20
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 21
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 22
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 23
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 24
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 25
Length of spec 1671
Number of training inputs for this signal: 835
Processing signal number 26
Length of spec 1815
Number of training inputs for this signal: 907
Processing signal number 27
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 28
Length of spec 519
Number of training inputs for this signal: 259
Processing signal number 29
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 30
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 31
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 32
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 33
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 34
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 35
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 36
Length of spec 1695
Number of training inputs for this signal: 847
Processing signal number 37
Length of spec 1695
Number of training inputs for this signal: 847

x dimensions (31122L, 1L, 256L, 2L)
y dimensions (31122L, 2L)

Neural network execution

Data prep

Left in this code block to show the syntax for combining training data from different domains. Will correctly concatenate as long as the parameters used in proc_data_humbug and proc_data_humbug_cdc are equivalent.


In [108]:
#x_test = x_test_cdc
#y_test = y_test_cdc

#x_train = np.vstack([x_train_caged, x_train_cdc])  # Augment training data with extra samples from CDC data
#y_train = np.vstack([y_train_caged, y_train_cdc])

Parameterise network as necessary

For details on parameters, see documentation at: https://keras.io/getting-started/sequential-model-guide/

and a further example on: https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py


In [9]:
# CNN parameters

# number of convolutional filters
nb_filters = 16

# size of pooling area for max pooling
pool_size = (2,2)

# convolution kernel size 
kernel_size_1 = (spec_window,spec_window)
kernel_size_2 = (3,3)
# number of classes
nb_classes = 2

# Initialise model

model = Sequential()

# Fully connected first layer to replace conv layer


#model.add(Dense(256, input_dim=(n_fft/2*spec_window)))

model.add(Convolution2D(nb_filters, kernel_size_1[0], kernel_size_1[1],
                       border_mode = 'valid',
                       input_shape = input_shape))
convout1 = Activation('relu')
model.add(convout1)
# # model.add(Convolution2D(nb_filters, kernel_size_2[0], kernel_size_2[1]))

# convout2 = Activation('relu')
# model.add(convout2)
# model.add(MaxPooling2D(pool_size = pool_size))
# model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',
              metrics=['accuracy'])

In [10]:
# Train model

batch_size = 64
nb_epoch = 100

filepath = "weights-improvement.hdf5"
#filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"

# Options to save best performing iteration according to monitor = ?. E.g. 'val_acc' will save the run with the highest 
# validation accuracy.

checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') 
callbacks_list = [checkpoint]
remote = RemoteMonitor(root='http://localhost:9000') # For viewing accuracy measures during training. Experimental.

model.fit(x_train_caged, y_train_caged, batch_size=batch_size, nb_epoch=nb_epoch,
          verbose=2)#,callbacks= [remote]) ## validation_data=(X_test_set, Y_test)
#RemoteMonitor(root='http://localhost:9000', path='/publish/epoch/end/', field='data', headers={'Content-Type': 'application/json', 'Accept': 'application/json'})


Epoch 1/100
5s - loss: 4.3413 - acc: 0.6809
Epoch 2/100
5s - loss: 2.9903 - acc: 0.7692
Epoch 3/100
5s - loss: 2.9989 - acc: 0.7681
Epoch 4/100
5s - loss: 2.8054 - acc: 0.7865
Epoch 5/100
5s - loss: 2.8637 - acc: 0.7880
Epoch 6/100
5s - loss: 2.8048 - acc: 0.7938
Epoch 7/100
5s - loss: 2.5772 - acc: 0.8087
Epoch 8/100
5s - loss: 2.5650 - acc: 0.8103
Epoch 9/100
5s - loss: 2.5237 - acc: 0.8129
Epoch 10/100
5s - loss: 2.4935 - acc: 0.8149
Epoch 11/100
5s - loss: 2.3938 - acc: 0.8202
Epoch 12/100
5s - loss: 2.4318 - acc: 0.8182
Epoch 13/100
5s - loss: 2.4025 - acc: 0.8206
Epoch 14/100
5s - loss: 2.2680 - acc: 0.8289
Epoch 15/100
5s - loss: 2.1215 - acc: 0.8346
Epoch 16/100
6s - loss: 2.0409 - acc: 0.8340
Epoch 17/100
6s - loss: 2.0055 - acc: 0.8331
Epoch 18/100
5s - loss: 1.9557 - acc: 0.8339
Epoch 19/100
5s - loss: 1.9575 - acc: 0.8267
Epoch 20/100
5s - loss: 1.9555 - acc: 0.8290
Epoch 21/100
5s - loss: 1.8551 - acc: 0.8310
Epoch 22/100
5s - loss: 1.7121 - acc: 0.8225
Epoch 23/100
5s - loss: 1.4406 - acc: 0.8199
Epoch 24/100
6s - loss: 1.1700 - acc: 0.8276
Epoch 25/100
5s - loss: 1.0554 - acc: 0.8272
Epoch 26/100
6s - loss: 1.0079 - acc: 0.8270
Epoch 27/100
5s - loss: 0.9444 - acc: 0.8287
Epoch 28/100
5s - loss: 0.9088 - acc: 0.8284
Epoch 29/100
5s - loss: 0.9769 - acc: 0.8305
Epoch 30/100
5s - loss: 0.8995 - acc: 0.8307
Epoch 31/100
5s - loss: 0.9059 - acc: 0.8295
Epoch 32/100
5s - loss: 0.9135 - acc: 0.8327
Epoch 33/100
5s - loss: 0.9328 - acc: 0.8320
Epoch 34/100
6s - loss: 0.8909 - acc: 0.8329
Epoch 35/100
5s - loss: 0.8572 - acc: 0.8360
Epoch 36/100
5s - loss: 0.9010 - acc: 0.8373
Epoch 37/100
5s - loss: 0.9484 - acc: 0.8330
Epoch 38/100
5s - loss: 0.8999 - acc: 0.8372
Epoch 39/100
5s - loss: 0.8857 - acc: 0.8348
Epoch 40/100
5s - loss: 0.9044 - acc: 0.8337
Epoch 41/100
5s - loss: 0.9174 - acc: 0.8352
Epoch 42/100
6s - loss: 0.8970 - acc: 0.8350
Epoch 43/100
5s - loss: 0.9260 - acc: 0.8362
Epoch 44/100
6s - loss: 0.9002 - acc: 0.8401
Epoch 45/100
5s - loss: 0.8812 - acc: 0.8402
Epoch 46/100
6s - loss: 0.8635 - acc: 0.8384
Epoch 47/100
6s - loss: 0.8804 - acc: 0.8376
Epoch 48/100
6s - loss: 0.8830 - acc: 0.8387
Epoch 49/100
6s - loss: 0.9486 - acc: 0.8369
Epoch 50/100
5s - loss: 0.9234 - acc: 0.8390
Epoch 51/100
5s - loss: 0.9301 - acc: 0.8400
Epoch 52/100
5s - loss: 0.8890 - acc: 0.8423
Epoch 53/100
5s - loss: 0.8566 - acc: 0.8418
Epoch 54/100
5s - loss: 0.8898 - acc: 0.8396
Epoch 55/100
5s - loss: 0.8865 - acc: 0.8443
Epoch 56/100
6s - loss: 0.9180 - acc: 0.8424
Epoch 57/100
6s - loss: 0.9089 - acc: 0.8430
Epoch 58/100
6s - loss: 0.8965 - acc: 0.8405
Epoch 59/100
6s - loss: 0.8793 - acc: 0.8438
Epoch 60/100
6s - loss: 0.9399 - acc: 0.8406
Epoch 61/100
6s - loss: 0.9742 - acc: 0.8403
Epoch 62/100
6s - loss: 0.9349 - acc: 0.8400
Epoch 63/100
6s - loss: 0.9158 - acc: 0.8440
Epoch 64/100
6s - loss: 0.8830 - acc: 0.8428
Epoch 65/100
6s - loss: 0.8877 - acc: 0.8447
Epoch 66/100
6s - loss: 0.9008 - acc: 0.8440
Epoch 67/100
6s - loss: 0.8727 - acc: 0.8449
Epoch 68/100
6s - loss: 0.8926 - acc: 0.8421
Epoch 69/100
6s - loss: 0.9066 - acc: 0.8426
Epoch 70/100
6s - loss: 0.8737 - acc: 0.8448
Epoch 71/100
6s - loss: 0.8654 - acc: 0.8452
Epoch 72/100
6s - loss: 0.8825 - acc: 0.8450
Epoch 73/100
6s - loss: 0.8972 - acc: 0.8458
Epoch 74/100
6s - loss: 0.9176 - acc: 0.8421
Epoch 75/100
6s - loss: 0.8845 - acc: 0.8447
Epoch 76/100
6s - loss: 0.8782 - acc: 0.8466
Epoch 77/100
6s - loss: 0.8445 - acc: 0.8490
Epoch 78/100
6s - loss: 0.8990 - acc: 0.8451
Epoch 79/100
6s - loss: 0.9265 - acc: 0.8436
Epoch 80/100
6s - loss: 0.8866 - acc: 0.8452
Epoch 81/100
6s - loss: 0.9212 - acc: 0.8463
Epoch 82/100
5s - loss: 0.9174 - acc: 0.8430
Epoch 83/100
5s - loss: 0.8809 - acc: 0.8491
Epoch 84/100
5s - loss: 0.9084 - acc: 0.8457
Epoch 85/100
5s - loss: 0.9255 - acc: 0.8471
Epoch 86/100
5s - loss: 0.8972 - acc: 0.8471
Epoch 87/100
5s - loss: 0.8756 - acc: 0.8515
Epoch 88/100
5s - loss: 0.8809 - acc: 0.8491
Epoch 89/100
5s - loss: 0.8739 - acc: 0.8501
Epoch 90/100
5s - loss: 0.8893 - acc: 0.8480
Epoch 91/100
5s - loss: 0.8792 - acc: 0.8494
Epoch 92/100
6s - loss: 0.9019 - acc: 0.8490
Epoch 93/100
6s - loss: 0.9423 - acc: 0.8477
Epoch 94/100
6s - loss: 0.9208 - acc: 0.8481
Epoch 95/100
6s - loss: 0.9088 - acc: 0.8471
Epoch 96/100
6s - loss: 0.8958 - acc: 0.8494
Epoch 97/100
6s - loss: 0.9109 - acc: 0.8506
Epoch 98/100
6s - loss: 0.9061 - acc: 0.8489
Epoch 99/100
6s - loss: 0.9199 - acc: 0.8508
Epoch 100/100
6s - loss: 0.8589 - acc: 0.8528
Out[10]:
<keras.callbacks.History at 0x1cbb5828>

In [11]:
# Set name for file saving

base_name = 'humbug'
if conv:
    suffix = 'conv'
else:
    suffix = 'MLP'

model_name = (base_name + '_' + suffix + '_' + str(n_fft) + '_' + str(n_overlap) + '_' 
              + str(kernel_size_1[0]) + '_' + str(kernel_size_1[0]) + '_' + count_method)
print 'model_name:', model_name


model_name: humbug_conv_512_256_2_2_most_positive

Code to generate predictions. Note that in a practical implementation on a phone, we will only need to (pre)load a model once, and predict over a test dataset x_test. The test dataset is constructed from sectioning a spectrogram according to proc_data_humbug.


In [12]:
score = model.evaluate(x_test, y_test, verbose=1)
predictions = model.predict(x_test)
print('Test score:', score[0])
print('Test accuracy:', score[1])


16352/16412 [============================>.] - ETA: 0s('Test score:', 1.2679346535380729)
('Test accuracy:', 0.82415305869392785)

Functions necessary for predictions end here. The remaining functions and scripts in Section 1 are for performance analysis and visualisation


In [13]:
########### 2 class predictions #####################################################
positive_predictions = predictions[:,0][np.where(y_test[:,0])]
negative_predictions = predictions[:,1][np.where(y_test[:,1])]

true_positive_rate = (sum(np.round(positive_predictions)))/sum(y_test[:,0])
true_negative_rate = sum(np.round(negative_predictions))/sum(y_test[:,1])

plt.figure(figsize = (15,6))
plt.plot(predictions[:,0],'g.', markersize = 2, label = 'y_pred_positive')
plt.plot(y_test[:,0], '--b', linewidth = 0.5, markersize = 2, label = 'y_test_positive')
    
plt.legend(loc = 7)
plt.ylim([-0.2,1.2])
plt.ylabel('Softmax output')
plt.xlabel('Signal window number')
#plt.savefig('Outputs/' + 'ClassOutput_' + model_name + '.pdf', transparent = True)
#print 'saved as', 'ClassOutput_' + model_name + '.pdf' 
plt.show()
print 'True positive rate', true_positive_rate, 'True negative rate', true_negative_rate


True positive rate 0.734525660964 True negative rate 0.881887397315

In [14]:
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    #plt.colorbar()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

In [15]:
cnf_matrix = confusion_matrix(y_test[:,1], np.round(predictions[:,1]).astype(int))
class_names = ('Mozz','No mozz')
# Plot normalized confusion matrix
plt.figure(figsize = (4,4))
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')

#plt.savefig('Outputs/' + 'Conf_' + model_name + '.pdf', transparent = True)
#print 'saved as', 'Conf_' + model_name + '.pdf' 
plt.show()


Normalized confusion matrix
[[ 0.73452566  0.26547434]
 [ 0.1181126   0.8818874 ]]

In [16]:
y_true = y_test[:,0]
y_score = predictions[:,0]
roc_score = roc_auc_score(y_true, y_score)
fpr, tpr, thresholds = roc_curve(y_true, y_score)

plt.figure(figsize=(4,4))
plt.plot(fpr, tpr, '.-')
plt.plot([0,1],[0,1],'k--')
plt.xlim([-0.01, 1.01])
plt.ylim([-0.01, 1.01])
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC, area = %.4f'%roc_score)
#plt.savefig('Outputs/' + 'ROC_' + model_name + '.pdf')
#print 'saved as', 'ROC_' + model_name + '.pdf' 
plt.show()


Section 2: CDC dataset functions and scripts


In [17]:
# Caged mosquito (CDC dataset, mostly positive samples, with extreme cases labelled)

data_path = '../../../../Resources/HumBug/labelled recordings/'
dirs = os.listdir(data_path)

label_list = []
label_names = []
signal_list = []

# Process .txt labels. Create list with structure:
# list of [start_time, end_time, tag] entries

for file in dirs:
    if file.endswith('.txt'):
        filename = data_path + file
        with open(filename) as f:
            reader = csv.reader(f, delimiter="\t")
            label_list.append(list(reader)) # Class labels
            label_names.append(file) # Save class name in separate array

signal_path = '../../../../Resources/HumBug/labelled recordings/wav/'             
dirs = os.listdir(signal_path)

for file in dirs:
    if file.endswith('.wav'):
        fs, signal = read(signal_path + file)
        print 'Processing', file
        signal_list.append(signal)
print 'Processed %i files.'%len(signal_list)


Processing CDC_Ae-aegypti_labelled.wav
Processing CDC_Ae-albopictus_labelled.wav
Processing CDC_An-albimanus_labelled.wav
Processing CDC_An-arabiensis_labelled.wav
Processing CDC_An-atroparvus_labelled.wav
Processing CDC_An-coluzzii_labelled.wav
Processing CDC_An-dirus_labelled.wav
Processing CDC_An-farauti_labelled.wav
Processing CDC_An-freeborni_labelled.wav
Processing CDC_An-funestus_labelled.wav
Processing CDC_An-gambiae-coluzzii-hybrid_labelled.wav
Processing CDC_An-gambiae_labelled.wav
Processing CDC_An-merus_labelled.wav
Processing CDC_An-minimus_labelled.wav
Processing CDC_An-quadriannulatus_labelled.wav
Processing CDC_An-quadrimaculatus_labelled.wav
Processing CDC_An-sinensis_labelled.wav
Processing CDC_An-stephensi_labelled.wav
Processing CDC_Cx-quinquefasciatus_labelled.wav
Processing CDC_Cx-tarsalis_labelled.wav
Processed 20 files.

In [18]:
# Script to extract and format cdc labels for compatibility with proc_data_humbug_cdc

running_time = []
positive_signal_list = []

n = 0
for i in range(len(label_list)):
    positive_signal = []
    for j in range(len(label_list[i])):

        start_time = label_list[i][j][0]  # Convert starting time in seconds to sample
        end_time = label_list[i][j][1]
        start_sample = int(np.floor(np.float32(start_time) * fs))
        end_sample = int(np.ceil(np.float32(end_time) * fs))
        positive_signal.append(signal_list[i][start_sample:end_sample])
        n += len(positive_signal[j])
        duration = (np.float(label_list[i][j][1]) - np.float(label_list[i][j][0]))
        running_time.append(duration)
    positive_signal_list.append(positive_signal)
    
print 'number of samples labelled', n
print 'Total running time (s)', np.sum(running_time)


number of samples labelled 1061669
Total running time (s) 132.642724

In [20]:
def proc_data_humbug_cdc(positive_signal_list, fs, img_height, img_width, nfft = 512, overlap = 256):

    nb_classes = 2
    spec_list_positive = []
    x_train = []
    y_train_count = []
    
    for i, positive_signal in enumerate(positive_signal_list):
        for chunk in positive_signal:
            SpecInfo = plt.specgram(chunk, Fs = fs, NFFT = nfft, noverlap = overlap)
            spec_list_positive.append(SpecInfo)

    for i in np.arange(len(spec_list_positive)):
        n_max = np.floor(np.divide(np.shape(spec_list_positive[i][0])[1],img_width)).astype('int')

        print 'Processing signal number', i
        print 'Length of spec', np.shape(spec_list_positive[i][0])[1]
        print 'Number of training inputs for this signal:', n_max    
        for n in np.arange(n_max):
            #print 'n', n, np.shape(spec_training_list[i][0])
            x_train.append(spec_list_positive[i][0][:img_height,img_width*n:img_width*(n+1)])


            y_train_count.append(1)

    y_train = np.zeros([np.shape(y_train_count)[0], nb_classes])
    y_train[:,0] = (np.array(y_train_count).astype(int))
    y_train[:,1] = 1 - (np.array(y_train_count).astype(int))
    x_train = np.array(x_train).reshape((np.shape(x_train)[0],1,img_height,img_width))
    
    return x_train, y_train

In [21]:
def proc_data_humbug_cdc_test(signal_list, fs, img_height, img_width, nfft = 512, overlap = 256):
    x_train = []

    SpecInfo = plt.specgram(signal_list, Fs = fs, NFFT = nfft, noverlap = overlap)
    n_max = np.floor(np.divide(np.shape(SpecInfo[0])[1],img_width)).astype('int')
    print n_max
    for n in np.arange(n_max):
        x_train.append(SpecInfo[0][:img_height,img_width*n:img_width*(n+1)])

    x_train = np.array(x_train).reshape((np.shape(x_train)[0],1,img_height,img_width))
    y_train = np.zeros([np.shape(x_train)[0], 2])
    y_train[:,0] = 1
    
    return x_train, y_train

In [22]:
n_fft = 512
n_overlap = 256
spec_window = 10

index = 4  # Choose which cdc signal to use for testing

x_train_cdc, y_train_cdc = proc_data_humbug_cdc(positive_signal_list, fs, n_fft/2,spec_window, nfft = n_fft, overlap = n_overlap)
x_test_cdc, y_test_cdc = proc_data_humbug_cdc_test(signal_list[index], fs, n_fft/2,spec_window, nfft = n_fft, overlap = n_overlap)


Processing signal number 0
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 1
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 2
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 3
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 4
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 5
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 6
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 7
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 8
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 9
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 10
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 11
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 12
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 13
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 14
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 15
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 16
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 17
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 18
Length of spec 20
Number of training inputs for this signal: 2
Processing signal number 19
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 20
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 21
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 22
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 23
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 24
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 25
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 26
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 27
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 28
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 29
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 30
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 31
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 32
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 33
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 34
Length of spec 62
Number of training inputs for this signal: 6
Processing signal number 35
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 36
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 37
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 38
Length of spec 48
Number of training inputs for this signal: 4
Processing signal number 39
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 40
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 41
Length of spec 18
Number of training inputs for this signal: 1
Processing signal number 42
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 43
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 44
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 45
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 46
Length of spec 16
Number of training inputs for this signal: 1
Processing signal number 47
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 48
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 49
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 50
Length of spec 16
Number of training inputs for this signal: 1
Processing signal number 51
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 52
Length of spec 19
Number of training inputs for this signal: 1
Processing signal number 53
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 54
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 55
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 56
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 57
Length of spec 24
Number of training inputs for this signal: 2
Processing signal number 58
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 59
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 60
Length of spec 21
Number of training inputs for this signal: 2
Processing signal number 61
Length of spec 18
Number of training inputs for this signal: 1
Processing signal number 62
Length of spec 32
Number of training inputs for this signal: 3
Processing signal number 63
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 64
Length of spec 28
Number of training inputs for this signal: 2
Processing signal number 65
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 66
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 67
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 68
Length of spec 20
Number of training inputs for this signal: 2
Processing signal number 69
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 70
Length of spec 16
Number of training inputs for this signal: 1
Processing signal number 71
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 72
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 73
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 74
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 75
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 76
Length of spec 19
Number of training inputs for this signal: 1
Processing signal number 77
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 78
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 79
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 80
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 81
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 82
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 83
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 84
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 85
Length of spec 22
Number of training inputs for this signal: 2
Processing signal number 86
Length of spec 24
Number of training inputs for this signal: 2
Processing signal number 87
Length of spec 23
Number of training inputs for this signal: 2
Processing signal number 88
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 89
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 90
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 91
Length of spec 34
Number of training inputs for this signal: 3
Processing signal number 92
Length of spec 26
Number of training inputs for this signal: 2
Processing signal number 93
Length of spec 22
Number of training inputs for this signal: 2
Processing signal number 94
Length of spec 24
Number of training inputs for this signal: 2
Processing signal number 95
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 96
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 97
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 98
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 99
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 100
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 101
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 102
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 103
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 104
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 105
Length of spec 19
Number of training inputs for this signal: 1
Processing signal number 106
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 107
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 108
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 109
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 110
Length of spec 23
Number of training inputs for this signal: 2
Processing signal number 111
Length of spec 18
Number of training inputs for this signal: 1
Processing signal number 112
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 113
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 114
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 115
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 116
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 117
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 118
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 119
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 120
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 121
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 122
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 123
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 124
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 125
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 126
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 127
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 128
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 129
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 130
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 131
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 132
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 133
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 134
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 135
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 136
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 137
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 138
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 139
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 140
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 141
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 142
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 143
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 144
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 145
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 146
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 147
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 148
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 149
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 150
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 151
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 152
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 153
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 154
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 155
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 156
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 157
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 158
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 159
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 160
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 161
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 162
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 163
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 164
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 165
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 166
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 167
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 168
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 169
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 170
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 171
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 172
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 173
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 174
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 175
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 176
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 177
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 178
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 179
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 180
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 181
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 182
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 183
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 184
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 185
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 186
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 187
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 188
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 189
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 190
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 191
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 192
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 193
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 194
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 195
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 196
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 197
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 198
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 199
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 200
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 201
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 202
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 203
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 204
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 205
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 206
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 207
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 208
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 209
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 210
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 211
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 212
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 213
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 214
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 215
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 216
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 217
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 218
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 219
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 220
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 221
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 222
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 223
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 224
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 225
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 226
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 227
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 228
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 229
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 230
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 231
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 232
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 233
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 234
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 235
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 236
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 237
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 238
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 239
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 240
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 241
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 242
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 243
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 244
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 245
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 246
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 247
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 248
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 249
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 250
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 251
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 252
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 253
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 254
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 255
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 256
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 257
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 258
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 259
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 260
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 261
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 262
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 263
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 264
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 265
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 266
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 267
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 268
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 269
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 270
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 271
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 272
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 273
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 274
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 275
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 276
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 277
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 278
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 279
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 280
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 281
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 282
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 283
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 284
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 285
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 286
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 287
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 288
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 289
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 290
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 291
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 292
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 293
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 294
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 295
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 296
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 297
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 298
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 299
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 300
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 301
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 302
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 303
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 304
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 305
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 306
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 307
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 308
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 309
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 310
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 311
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 312
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 313
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 314
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 315
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 316
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 317
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 318
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 319
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 320
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 321
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 322
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 323
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 324
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 325
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 326
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 327
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 328
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 329
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 330
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 331
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 332
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 333
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 334
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 335
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 336
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 337
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 338
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 339
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 340
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 341
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 342
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 343
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 344
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 345
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 346
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 347
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 348
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 349
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 350
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 351
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 352
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 353
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 354
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 355
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 356
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 357
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 358
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 359
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 360
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 361
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 362
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 363
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 364
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 365
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 366
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 367
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 368
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 369
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 370
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 371
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 372
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 373
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 374
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 375
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 376
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 377
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 378
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 379
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 380
Length of spec 25
Number of training inputs for this signal: 2
Processing signal number 381
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 382
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 383
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 384
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 385
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 386
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 387
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 388
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 389
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 390
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 391
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 392
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 393
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 394
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 395
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 396
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 397
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 398
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 399
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 400
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 401
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 402
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 403
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 404
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 405
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 406
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 407
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 408
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 409
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 410
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 411
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 412
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 413
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 414
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 415
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 416
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 417
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 418
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 419
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 420
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 421
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 422
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 423
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 424
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 425
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 426
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 427
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 428
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 429
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 430
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 431
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 432
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 433
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 434
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 435
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 436
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 437
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 438
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 439
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 440
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 441
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 442
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 443
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 444
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 445
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 446
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 447
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 448
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 449
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 450
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 451
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 452
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 453
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 454
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 455
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 456
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 457
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 458
Length of spec 20
Number of training inputs for this signal: 2
Processing signal number 459
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 460
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 461
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 462
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 463
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 464
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 465
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 466
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 467
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 468
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 469
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 470
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 471
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 472
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 473
Length of spec 13
Number of training inputs for this signal: 1
Processing signal number 474
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 475
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 476
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 477
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 478
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 479
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 480
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 481
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 482
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 483
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 484
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 485
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 486
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 487
Length of spec 21
Number of training inputs for this signal: 2
Processing signal number 488
Length of spec 24
Number of training inputs for this signal: 2
Processing signal number 489
Length of spec 28
Number of training inputs for this signal: 2
Processing signal number 490
Length of spec 37
Number of training inputs for this signal: 3
Processing signal number 491
Length of spec 18
Number of training inputs for this signal: 1
Processing signal number 492
Length of spec 39
Number of training inputs for this signal: 3
Processing signal number 493
Length of spec 22
Number of training inputs for this signal: 2
Processing signal number 494
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 495
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 496
Length of spec 8
Number of training inputs for this signal: 0
Processing signal number 497
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 498
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 499
Length of spec 9
Number of training inputs for this signal: 0
Processing signal number 500
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 501
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 502
Length of spec 1
Number of training inputs for this signal: 0
Processing signal number 503
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 504
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 505
Length of spec 15
Number of training inputs for this signal: 1
Processing signal number 506
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 507
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 508
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 509
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 510
Length of spec 46
Number of training inputs for this signal: 4
Processing signal number 511
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 512
Length of spec 10
Number of training inputs for this signal: 1
Processing signal number 513
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 514
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 515
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 516
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 517
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 518
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 519
Length of spec 12
Number of training inputs for this signal: 1
Processing signal number 520
Length of spec 4
Number of training inputs for this signal: 0
Processing signal number 521
Length of spec 11
Number of training inputs for this signal: 1
Processing signal number 522
Length of spec 7
Number of training inputs for this signal: 0
Processing signal number 523
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 524
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 525
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 526
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 527
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 528
Length of spec 21
Number of training inputs for this signal: 2
Processing signal number 529
Length of spec 14
Number of training inputs for this signal: 1
Processing signal number 530
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 531
Length of spec 3
Number of training inputs for this signal: 0
Processing signal number 532
Length of spec 6
Number of training inputs for this signal: 0
Processing signal number 533
Length of spec 5
Number of training inputs for this signal: 0
Processing signal number 534
Length of spec 2
Number of training inputs for this signal: 0
Processing signal number 535
Length of spec 17
Number of training inputs for this signal: 1
Processing signal number 536
Length of spec 4
Number of training inputs for this signal: 0
3179

Re-initialise neural network architecture and train as before

Visualise output from Humbug CDC dataset


In [ ]:
spec_fs = (1.*fs/(n_fft - n_overlap))

score = model.evaluate(x_test_cdc, y_test_cdc, verbose=1)
predictions = model.predict(x_test_cdc)

plt.figure(figsize = (15,15))
plt.subplot(3,1,1)

plt.specgram(signal_list[index], NFFT = n_fft, noverlap = n_overlap, Fs = fs)
#plt.title(label_list[0])

plt.subplot(3,1,2)

positive_predictions = predictions[:,0][np.where(y_test[:,0])]
negative_predictions = predictions[:,1][np.where(y_test[:,1])]

true_positive_rate = (sum(np.round(positive_predictions)))/sum(y_test[:,0])
true_negative_rate = sum(np.round(negative_predictions))/sum(y_test[:,1])

plt.plot(np.arange(len(predictions[:,0]))/(spec_fs)*spec_window, 
         predictions[:,0],'g.', markersize = 2, label = 'y_pred_positive')
plt.plot(np.arange(len(y_test[:,0]))/(spec_fs)*spec_window, 
         y_test[:,0], '--b', linewidth = 0.5, markersize = 2, label = 'y_test_positive')
#plt.title('Window size %i'%spec_window)    
plt.legend(loc = 7)
plt.ylim([-0.2,1.2])
plt.ylabel('Softmax output')
plt.xlabel('Time (s)')

plt.subplot(3,1,3)

plt.plot(np.arange(len(signal_list[index]))/(1.*fs),signal_list[index])
for j in range(len(label_list[index])):
    start_time = label_list[index][j][0]  # Convert starting time in seconds to sample
    end_time = label_list[index][j][1]
    start_sample = int(np.floor(np.float32(start_time) * fs))
    end_sample = int(np.ceil(np.float32(end_time) * fs))
    plt.axvspan(start_sample/(1.*fs), end_sample/(1.*fs), facecolor='#2ca02c', alpha=0.5)
plt.xlabel('Time (s)')
plt.show()

Utility functions for saving and loading models and data


In [5]:
# (Optional) Load test data for visualisation

loaded_data = np.load('humbug_test.npz')
x_test = loaded_data['x_test']
y_test = loaded_data['y_test']

In [ ]:
# Load model to train further if desired
model_name = 'humbug_3.h5'

# Note, the file closing below is usually not required, and may produce an error when not. Necessary code is tied to a bug
# in keras. For more info see here:
# https://github.com/fchollet/keras/issues/4044

import h5py
f = h5py.File(model_name, 'r+')
del f['optimizer_weights']
f.close()

In [ ]:
model = load_model(model_name)

In [57]:
model.save(model_name + '.h5')
print 'Model saved as', model_name + '.h5'


Model saved as humbug_conv_2048_1024_20_20_most_positive.h5

In [58]:
# Save data

# np.savez('Outputs/' + model_name + '.npz', x_train = x_train_cdc, x_test = x_test, y_train=y_train_cdc, 
#          y_test = y_test, input_shape = input_shape)
np.savez('Outputs/' + model_name + '.npz', x_test = x_test, y_test = y_test)

Unused code, for illustration/presentation purposes only


In [18]:
plt.figure(figsize = (6,6))
plt.title('Signal #%i'%(34))
im = plt.specgram(signal_list[34], Fs = fs, NFFT = nfft, noverlap = overlap)
plt.xlim([0,53])
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
#plt.savefig('SpectrogramPresentation.pdf', transparent = True)
plt.show()



In [75]:
model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
convolution2d_3 (Convolution2D)  (None, 16, 511, 1)    80          convolution2d_input_4[0][0]      
____________________________________________________________________________________________________
activation_7 (Activation)        (None, 16, 511, 1)    0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
flatten_3 (Flatten)              (None, 8176)          0           activation_7[0][0]               
____________________________________________________________________________________________________
dense_5 (Dense)                  (None, 128)           1046656     flatten_3[0][0]                  
____________________________________________________________________________________________________
activation_8 (Activation)        (None, 128)           0           dense_5[0][0]                    
____________________________________________________________________________________________________
dropout_3 (Dropout)              (None, 128)           0           activation_8[0][0]               
____________________________________________________________________________________________________
dense_6 (Dense)                  (None, 2)             258         dropout_3[0][0]                  
____________________________________________________________________________________________________
activation_9 (Activation)        (None, 2)             0           dense_6[0][0]                    
====================================================================================================
Total params: 1046994
____________________________________________________________________________________________________

In [80]:
# Produce diagram of model (not very useful)

from keras.utils.visualize_util import plot
plot(model, to_file='model.png')

In [25]:
n_test = int(0.1*len(x_train)) # Split into test sample percentage

shuffle_index = np.random.permutation(np.shape(y_train)[0])
y_train = y_train[shuffle_index]
x_train = x_train[shuffle_index]


x_test = x_train[:n_test,:,:,:]
y_test = y_train[:n_test,:]
x_train = x_train[n_test:,:,:]
y_train = y_train[n_test:,:]