<jupyter_start><jupyter_text>DISTANCE TESTING<jupyter_code>#https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6982911/
#distance 2
y, sr = librosa.load(directory + '1125.wav',duration=3)
ps = librosa.feature.melspectrogram(y=y, sr=sr)
ps_db= librosa.power_to_db(ps, ref=np.max)

librosa.display.specshow(ps_db, x_axis='time', y_axis='mel')
#distance 1
y, sr = librosa.load(directory + '121759.wav',duration=3)
ps = librosa.feature.melspectrogram(y=y, sr=sr)
ps_db= librosa.power_to_db(ps, ref=np.max)

librosa.display.specshow(ps_db, x_axis='time', y_axis='mel')

#Using a CRNN architecture to enable the model to learn both the spectral and temporal feeatures and relationships
#model itself is three 2D conv layers, which extract features from the input log-scaled mel spectograms
#the leakyrely activation function is applied to the output of each convolution layer and max-pooled to reduce their dimensoions
#can't be bothered to write the rest, look at the pic in the paper
#model is
import numpy as np
from keras.layers import Input, Conv2D, BatchNormalization, MaxPooling2D, Reshape, Dense, LSTM, add, concatenate, Dropout, Lambda, Flatten, GRU, LeakyReLU
from keras.models import Model
import keras.backend as K

def ctc_lambda_func(args):
  iy_pred, ilabels, iinput_length, ilabel_length = args
  # the 2 is critical here since the first couple outputs of the RNN
  # tend to be garbage:
  iy_pred = iy_pred[:, 2:, :]  # no such influence
  return K.ctc_batch_cost(ilabels, iy_pred, iinput_length, ilabel_length)

#https://github.com/kurapan/CRNN/blob/master/models.py
def build_distance_model():

  inputs = Input((64,64,3))
  c1=Conv2D(32, (3,3), padding = 'same',)(inputs)
  a1=Activation('LeakyReLU')(c1)
  c2=Conv2D(64, (3,3))(a1)
  a2=Activation('LeakyReLU')(c2)
  c3=Conv2D(64, (3,3))(a2)
  a3=Activation('LeakyReLU')(c3)
  p1=MaxPooling2D(pool_size=(2, 2))(a3)

  c4=Conv2D(64, (3,3))(p1)
  a4=Activation('LeakyReLU')(c4)
  c5=Conv2D(64, (3,3))(a4)
  a5=Activation('LeakyReLU')(c5)
  c6=Conv2D(64, (3,3))(a5)
  a6=Activation('LeakyReLU')(c6)
  p2=MaxPooling2D(pool_size=(2, 2))(a6)

  c7=Conv2D(128, (3,3))(p2)
  a7=Activation('LeakyReLU')(c7)
  c8=Conv2D(128, (3,3))(a7)
  a8=Activation('LeakyReLU')(c8)
  c9=Conv2D(128, (3,3))(a8)
  a9=Activation('LeakyReLU')(c9)
  p3=MaxPooling2D(pool_size=(2, 2))(a9)
  d1=Dropout(0.25)(p3)
  #model.add(Flatten())
  d1_shape=d1.get_shape()

  reshape = Reshape(target_shape=(int(d1_shape[1]), int(d1_shape[2] * d1_shape[3])), name='reshape')(d1)

  dense= Dense(512, activation='relu')(reshape)

  l1= LSTM(512, return_sequences=True)(dense)
  l1back = LSTM(512, go_backwards=True, return_sequences=True)(l1)
  lstm_add = add([l1, l1back])
  #dense2=Dense(1,activation='softmax')(lstm_add)

  lstm_11 = LSTM(512,  return_sequences=True)(lstm_add)
  lstm_11_back = LSTM(512, go_backwards=True, return_sequences=True)(lstm_11)
  lstm_11_concat = concatenate([lstm_11, lstm_11_back])
  do_11 = Dropout(0.5)(lstm_11_concat)

  fc_12 = Dense(1, activation='sigmoid')(do_11)
  
  prediction_model = Model(inputs=inputs, outputs=fc_12)


 # prediction_model = Model(inputs=inputs, outputs=dense2)
  return prediction_model
  
  #need to fix this
  #model.add(Reshape(target_shape = (3,3,128))))

  #after fixing above, enable this
  #model.add(LSTM(128))
  #model.add(Activation('softmax'))
  #model.add(LSTM(128))
  #model.add(Activation('softmax'))
  de1=Dense(1)(d1)
  a10=Activation('softmax')(de1)
  """
  #I just don't know what this is supposed to be

  inputs = Input(shape = (128,128,1,))
  c_1 = Conv2D(32, (3, 3), input_shape = (128,128), padding='same', name='conv_1')(inputs)
  x = LeakyReLU()(c_1)
  c_2 = Conv2D(32, (3, 3), padding='same', name='conv_2')(c_1)
  x = LeakyReLU()(c_2)
  c_3 = Conv2D(32, (3, 3), padding='same', name='conv_3')(c_2)
  x = LeakyReLU()(c_3)
  #is this needed?
  #bn_3 = BatchNormalization(name='bn_3')(c_3)
  p_3 = MaxPooling2D(pool_size=(2, 2), name='maxpool_3')(x)
  print(1)

  c_4 = Conv2D(64, (3, 3), padding='same', name='conv_4')(p_3)
  x = LeakyReLU()(c_4)
  c_5 = Conv2D(64, (3, 3), padding='same', name='conv_5')(c_4)
  x = LeakyReLU()(c_5)
  #bn_5 = BatchNormalization(name='bn_5')(c_5)
  p_5 = MaxPooling2D(pool_size=(2, 2), name='maxpool_5')(x)
  print(2)
  

  c_6 = Conv2D(128, (3, 3), padding='same', name='conv_6')(p_5)
  x = LeakyReLU()(c_6)
  c_7 = Conv2D(128, (3, 3), padding='same', name='conv_7')(c_6)
  x = LeakyReLU()(c_7)
  #bn_7 = BatchNormalization(name='bn_7')(c_7)
  p_7 = MaxPooling2D(pool_size=(2, 2), name='maxpool_6')(x)
  p_7_shape = p_7.get_shape()
  print(3)

  #why is this like this, i don't know, but we shouldn't multiple by 2 randomly, makes no sense
  reshape = Reshape(target_shape=(int(p_7_shape[1]), int(p_7_shape[2] * p_7_shape[3])), name='reshape')(p_7)
  print(4)
  fc_9 = Dense(128, kernel_initializer='he_normal', activation='softmax', name='fc_9')(reshape)
  print("Dense")
  #maybe 128 is wrong, we will see
  lstm_10 = LSTM(128, kernel_initializer="he_normal", return_sequences=True, name='lstm_10')(fc_9)
  lstm_10_back = LSTM(128, kernel_initializer="he_normal", go_backwards=True, return_sequences=True, name='lstm_10_back')(fc_9)
  lstm_10_add = add([lstm_10, lstm_10_back])
  print(5)


  lstm_11 = LSTM(128, kernel_initializer="he_normal", return_sequences=True, name='lstm_11')(lstm_10_add)
  lstm_11_back = LSTM(128, kernel_initializer="he_normal", go_backwards=True, return_sequences=True, name='lstm_11_back')(lstm_10_add)
  lstm_11_concat = concatenate([lstm_11, lstm_11_back])
  do_11 = Dropout(0.25, name='dropout')(lstm_11_concat)
  print(6)

  fc_12 = Dense(1, kernel_initializer='he_normal', activation='softmax', name='fc_12')(do_11)
  print(7)
  prediction_model = Model(inputs=inputs, outputs=fc_12)

  #need to figure out label length, but if it's distance, should be 1
  labels = Input(name='labels', shape=1, dtype='float32')
  input_length = Input(name='input_length', shape=[1], dtype='int64')
  label_length = Input(name='label_length', shape=[1], dtype='int64')
  

  ctc_loss = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([fc_12, labels, input_length, label_length])
    
  training_model = Model(inputs=[inputs, labels, input_length, label_length], outputs=[ctc_loss])     

  return training_model, prediction_model
  """
  return model
#https://medium.com/gradientcrescent/urban-sound-classification-using-convolutional-neural-networks-with-keras-theory-and-486e92785df4
#this is the only thing that makes sense to me
def create_spectrogram(filename,name):
    plt.interactive(False)
    clip, sample_rate = librosa.load(filename, sr=None)
    fig = plt.figure(figsize=[0.72,0.72])
    ax = fig.add_subplot(111)
    ax.axes.get_xaxis().set_visible(False)
    ax.axes.get_yaxis().set_visible(False)
    ax.set_frame_on(False)
    S = librosa.feature.melspectrogram(y=clip, sr=sample_rate)
    librosa.display.specshow(librosa.power_to_db(S, ref=np.max))
    #create the dictionaries if you don't have them
    #if you aren't sure about about the path, right click on the created folder and click show path
    #it is clipboarded, paste is somewhere and see the path
    filename  = 'drive/MyDrive/working/train/' + name + '.jpg'
    plt.savefig(filename, dpi=400, bbox_inches='tight',pad_inches=0)
    plt.close()    
    fig.clf()
    plt.close(fig)
    plt.close('all')
    #del filename,name,clip,sample_rate,fig,ax,S
def create_spectrogram_test(filename,name):
    plt.interactive(False)
    clip, sample_rate = librosa.load(filename, sr=None)
    fig = plt.figure(figsize=[0.72,0.72])
    ax = fig.add_subplot(111)
    ax.axes.get_xaxis().set_visible(False)
    ax.axes.get_yaxis().set_visible(False)
    ax.set_frame_on(False)
    S = librosa.feature.melspectrogram(y=clip, sr=sample_rate)
    librosa.display.specshow(librosa.power_to_db(S, ref=np.max))
    #create the dictionaries if you don't have them
    filename  =  'drive/MyDrive/working/test/' + name + '.jpg'
    fig.savefig(filename, dpi=400, bbox_inches='tight',pad_inches=0)
    plt.close()    
    fig.clf()
    plt.close(fig)
    plt.close('all')
   #del filename,name,clip,sample_rate,fig,ax,S
!rm -rf /drive/MyDrive/working/train/
!rm -rf /drive/MyDrive/working/test/

!mkdir /drive/MyDrive/working/train/
!mkdir /drive/MyDrive/working/test/
import random
random.shuffle(actualFiles)
train_files =actualFiles[:int(len(actualFiles)*0.8)]
test_files =actualFiles[int(len(actualFiles)*0.8):]

for file in train_files:
  filename,name = file, file.split('/')[-1].split('.')[0]
  create_spectrogram(filename,name)
for file in test_files:
    filename,name = file,file.split('/')[-1].split('.')[0]
    create_spectrogram_test(filename,name)
from keras_preprocessing.image import ImageDataGenerator

def append_ext(fn):
    return fn+".jpg"
img_width, img_height = 64,64
batch_size = 20
epochs = 1

fuckThistrain = []
fuckThistrain2=[]

for elem in train_files:
  fuckThistrain.append(elem.split("/")[-1])

traindf = train[train['fileName'].isin(fuckThistrain)]
traindf.drop_duplicates(inplace=True)

for index, row in traindf.iterrows():
      traindf.at[index, 'fileName'] = row['fileName'].split(".")[0] + ".jpg"
      fuckThistrain2.append(row['fileName'].split(".")[0] + ".jpg")
    
    

traindf.drop("label",inplace=True,axis=1)
nb_train_samples = len(traindf) # number of train samples
#fuckThistrain2=list(set(fuckThistrain2)) #somehow it is duplicated at above
def train_generator():
    while True:
        for start in range(0, nb_train_samples, batch_size):
            x_batch = []
            y_batch = []

        

           
            end = min(start + batch_size, nb_train_samples)
            for img_path in range(start, end):
                img = cv2.imread(r'drive/MyDrive/working/train/'+fuckThistrain2[img_path])
                img = cv2.resize(img, (img_width, img_height),interpolation=cv2.INTER_AREA)
                x_batch.append(img)
                if fuckThistrain2[img_path] in list(traindf.fileName.values):
                    y_batch.append([int(traindf[traindf['fileName']==fuckThistrain2[img_path]]['distance'])])
                elif fuckThis2[img_path] in list(traindf.fileName.values):
                    y_batch.append([int(traindf[traindf['fileName']==fuckThistrain2[img_path]]['distance'])])
            
            yield (np.array(x_batch), np.array(y_batch))
fuckThis = []
fuckThis2=[]
for elem in test_files:
  fuckThis.append(elem.split("/")[-1])

testdf = train[train['fileName'].isin(fuckThis)]
testdf.drop_duplicates(inplace=True)

for index, row in testdf.iterrows():
    testdf.at[index, 'fileName'] = row['fileName'].split(".")[0] + ".jpg"
    fuckThis2.append(row['fileName'].split(".")[0] + ".jpg")

testdf.drop("label",inplace=True,axis=1)

nb_test_samples=len(testdf) #number of test samples
def test_generator():
    while True:
        for start in range(0, nb_test_samples, batch_size):
            x_batch = []
            y_batch = []
            
            end = min(start + batch_size, nb_test_samples)
            for img_path in range(start, end):
                img = cv2.imread(r'drive/MyDrive/working/test/'+fuckThis2[img_path])
                img = cv2.resize(img, (img_width, img_height),interpolation=cv2.INTER_AREA)
                x_batch.append(img)
                if fuckThis2[img_path] in list(testdf.fileName.values):
                    y_batch.append([int(testdf[testdf['fileName']==fuckThis2[img_path]]['distance'])])
                elif fuckThis2[img_path] in list(traindf.fileName.values):
                    y_batch.append([int(testdf[testdf['fileName']==fuckThis2[img_path]]['distance'])])
            
            yield (np.array(x_batch), np.array(y_batch))
#needs to be changed still
from keras import regularizers, optimizers
model = build_distance_model()
model.compile(optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False),loss="binary_crossentropy",metrics=["accuracy"])

model.fit_generator(
    train_generator(),
    epochs= epochs,
    steps_per_epoch= nb_train_samples // batch_size,
    validation_data= test_generator(),
    validation_steps = nb_test_samples // batch_size)<jupyter_output>/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:1915: UserWarning:

`Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
{'fraction_non_alphanumeric': 0.15224578663011049, 'fraction_numerical': 0.04120635432626401, 'mean_word_length': 4.720479704797048, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '211476', 'license': 'Apache-2.0', 'n_tokens_mistral': 5066, 'n_tokens_neox': 4802, 'n_words': 844, 'path': 'Code/DistancePrediction.ipynb', 'repo_name': 'TheGadgeteer/Predicting-from-Sound-Blog', 'size': '12401'}