{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfrom matplotlib import pyplot as plt\nimport tensorflow as tf \nimport tensorflow_io as tfio\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:13.253876Z","iopub.execute_input":"2023-05-17T02:22:13.254656Z","iopub.status.idle":"2023-05-17T02:22:23.331094Z","shell.execute_reply.started":"2023-05-17T02:22:13.254596Z","shell.execute_reply":"2023-05-17T02:22:23.329302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting the file paths to a 'A-Call audio file' & a 'Non A-Call audio file'.\nA_CALL_FILE = os.path.join('/kaggle', 'input', 'datafestintegration2023','train','train','1','01.wav')\nNOT_A_CALL_FILE = os.path.join('/kaggle', 'input', 'datafestintegration2023','train','train','0','01.wav')","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:35.571979Z","iopub.execute_input":"2023-05-17T02:22:35.572874Z","iopub.status.idle":"2023-05-17T02:22:35.580604Z","shell.execute_reply.started":"2023-05-17T02:22:35.572825Z","shell.execute_reply":"2023-05-17T02:22:35.578981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#File path to the first A-Call audio file\nA_CALL_FILE","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:37.252197Z","iopub.execute_input":"2023-05-17T02:22:37.252721Z","iopub.status.idle":"2023-05-17T02:22:37.262352Z","shell.execute_reply.started":"2023-05-17T02:22:37.252679Z","shell.execute_reply":"2023-05-17T02:22:37.260880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#File path to the first Non A-Call audio file\nNOT_A_CALL_FILE","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:37.683262Z","iopub.execute_input":"2023-05-17T02:22:37.683917Z","iopub.status.idle":"2023-05-17T02:22:37.691512Z","shell.execute_reply.started":"2023-05-17T02:22:37.683873Z","shell.execute_reply":"2023-05-17T02:22:37.690089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining a audio file loading function.\ndef load_wav_1k_mono(filename):\n    #Loading the encoded audio file\n    file_contents = tf.io.read_file(filename)\n    \n    #Decoding the contents of the audio file and returns the audio data and its sample rate(Also converts the audio into one channel)\n    wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)\n    \n    #removing the last dimension from the audio data, which is not needed since the audio is in 1 channel.\n    wav = tf.squeeze(wav, axis=-1)\n    \n    #Changing the sample rate from its original data type to tf.int64. (Helps in resampling)\n    sample_rate = tf.cast(sample_rate, dtype=tf.int64)\n    \n    #Resampling the audio data to have a sample rate of 1000Hz\n    wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=1000)\n    \n    return wav","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:44.798000Z","iopub.execute_input":"2023-05-17T02:22:44.798454Z","iopub.status.idle":"2023-05-17T02:22:44.808269Z","shell.execute_reply.started":"2023-05-17T02:22:44.798419Z","shell.execute_reply":"2023-05-17T02:22:44.806427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_contents = tf.io.read_file(A_CALL_FILE)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:54.994932Z","iopub.execute_input":"2023-05-17T02:22:54.996136Z","iopub.status.idle":"2023-05-17T02:22:55.119793Z","shell.execute_reply.started":"2023-05-17T02:22:54.996090Z","shell.execute_reply":"2023-05-17T02:22:55.118485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wav, sample_rate = tf.audio.decode_wav(file_contents, desired_channels=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:22:57.442296Z","iopub.execute_input":"2023-05-17T02:22:57.443303Z","iopub.status.idle":"2023-05-17T02:22:57.455296Z","shell.execute_reply.started":"2023-05-17T02:22:57.443259Z","shell.execute_reply":"2023-05-17T02:22:57.453950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Passing the previously defined audio files through the function\nwave = load_wav_1k_mono(A_CALL_FILE)\nnwave = load_wav_1k_mono(NOT_A_CALL_FILE)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:23:02.701134Z","iopub.execute_input":"2023-05-17T02:23:02.701636Z","iopub.status.idle":"2023-05-17T02:23:03.338213Z","shell.execute_reply.started":"2023-05-17T02:23:02.701594Z","shell.execute_reply":"2023-05-17T02:23:03.337169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the two decoded audio files\nplt.plot(wave)\nplt.plot(nwave)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:23:27.893453Z","iopub.execute_input":"2023-05-17T02:23:27.893901Z","iopub.status.idle":"2023-05-17T02:23:28.172307Z","shell.execute_reply.started":"2023-05-17T02:23:27.893861Z","shell.execute_reply":"2023-05-17T02:23:28.170943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the file paths to A-Calls and Non A-Call audio files directories\nPositives = os.path.join('/kaggle', 'input', 'datafestintegration2023','train','train','1')\nNegatives = os.path.join('/kaggle', 'input', 'datafestintegration2023','train','train','0')","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:23:42.705256Z","iopub.execute_input":"2023-05-17T02:23:42.705999Z","iopub.status.idle":"2023-05-17T02:23:42.714212Z","shell.execute_reply.started":"2023-05-17T02:23:42.705951Z","shell.execute_reply":"2023-05-17T02:23:42.712510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting all the files as a string in Tensoflow dataset format\npos = tf.data.Dataset.list_files(Positives + '/*.wav')\nneg = tf.data.Dataset.list_files(Negatives + '/*.wav')","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:23:44.343613Z","iopub.execute_input":"2023-05-17T02:23:44.344671Z","iopub.status.idle":"2023-05-17T02:24:06.240348Z","shell.execute_reply.started":"2023-05-17T02:23:44.344620Z","shell.execute_reply":"2023-05-17T02:24:06.239300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting a random audio file path in the directory '1'\npos.as_numpy_iterator().next()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:24:19.904681Z","iopub.execute_input":"2023-05-17T02:24:19.905084Z","iopub.status.idle":"2023-05-17T02:24:19.928791Z","shell.execute_reply.started":"2023-05-17T02:24:19.905050Z","shell.execute_reply":"2023-05-17T02:24:19.927467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"positives = tf.data.Dataset.zip((pos, tf.data.Dataset.from_tensor_slices(tf.ones(len(pos)))))\nnegatives = tf.data.Dataset.zip((neg, tf.data.Dataset.from_tensor_slices(tf.zeros(len(neg)))))","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:24:23.495317Z","iopub.execute_input":"2023-05-17T02:24:23.495830Z","iopub.status.idle":"2023-05-17T02:24:23.516169Z","shell.execute_reply.started":"2023-05-17T02:24:23.495790Z","shell.execute_reply":"2023-05-17T02:24:23.514935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(positives))\nprint(len(negatives))","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:24:24.682097Z","iopub.execute_input":"2023-05-17T02:24:24.682543Z","iopub.status.idle":"2023-05-17T02:24:24.690636Z","shell.execute_reply.started":"2023-05-17T02:24:24.682507Z","shell.execute_reply":"2023-05-17T02:24:24.689216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = positives.concatenate(negatives)\nprint(len(data))","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:24:28.820620Z","iopub.execute_input":"2023-05-17T02:24:28.821071Z","iopub.status.idle":"2023-05-17T02:24:28.832970Z","shell.execute_reply.started":"2023-05-17T02:24:28.821032Z","shell.execute_reply":"2023-05-17T02:24:28.831128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shuffle(10000).as_numpy_iterator().next()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:24:30.842306Z","iopub.execute_input":"2023-05-17T02:24:30.842829Z","iopub.status.idle":"2023-05-17T02:24:30.914466Z","shell.execute_reply.started":"2023-05-17T02:24:30.842787Z","shell.execute_reply":"2023-05-17T02:24:30.913026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lengths = []\nfor file in os.listdir(os.path.join('/kaggle', 'input', 'datafestintegration2023','train','train','1')):\n    tensor_wave = load_wav_1k_mono(os.path.join('/kaggle', 'input', 'datafestintegration2023','train','train','1', file))\n    lengths.append(len(tensor_wave))","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:24:51.272865Z","iopub.execute_input":"2023-05-17T02:24:51.273336Z","iopub.status.idle":"2023-05-17T02:36:06.711228Z","shell.execute_reply.started":"2023-05-17T02:24:51.273296Z","shell.execute_reply":"2023-05-17T02:36:06.709991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lengths[0:5]","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:49:49.440824Z","iopub.execute_input":"2023-05-17T02:49:49.441446Z","iopub.status.idle":"2023-05-17T02:49:49.451712Z","shell.execute_reply.started":"2023-05-17T02:49:49.441391Z","shell.execute_reply":"2023-05-17T02:49:49.450286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(lengths)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:49:50.803868Z","iopub.execute_input":"2023-05-17T02:49:50.804291Z","iopub.status.idle":"2023-05-17T02:49:50.812902Z","shell.execute_reply.started":"2023-05-17T02:49:50.804255Z","shell.execute_reply":"2023-05-17T02:49:50.811514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mean of the audio file time durations\ntf.math.reduce_mean(lengths)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:49:52.013029Z","iopub.execute_input":"2023-05-17T02:49:52.013532Z","iopub.status.idle":"2023-05-17T02:49:52.050852Z","shell.execute_reply.started":"2023-05-17T02:49:52.013490Z","shell.execute_reply":"2023-05-17T02:49:52.049612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Minimum of the audio file time durations\ntf.math.reduce_min(lengths)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:49:52.852802Z","iopub.execute_input":"2023-05-17T02:49:52.853226Z","iopub.status.idle":"2023-05-17T02:49:52.881529Z","shell.execute_reply.started":"2023-05-17T02:49:52.853189Z","shell.execute_reply":"2023-05-17T02:49:52.879886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Maximum of the audio file time durations\ntf.math.reduce_max(lengths)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:49:53.876804Z","iopub.execute_input":"2023-05-17T02:49:53.877253Z","iopub.status.idle":"2023-05-17T02:49:53.904998Z","shell.execute_reply.started":"2023-05-17T02:49:53.877213Z","shell.execute_reply":"2023-05-17T02:49:53.903635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting these values helps in deciding to which duration we should trim the files in the preprocessing stage\n#Avg = lengths/Frequency\nAvg_time = 27838/1000\nLowest_time = 18576/1000\nHighest_time = 109815/1000\nprint(f'Highest duration a the whale call audio file is: {Highest_time}s')\nprint(f'Lowest duration of a the whale call audio file is: {Lowest_time}s')\nprint(f'Average duration of a the whale call audio file is: {round(Avg_time,2)}s')","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:49:55.231212Z","iopub.execute_input":"2023-05-17T02:49:55.231711Z","iopub.status.idle":"2023-05-17T02:49:55.239745Z","shell.execute_reply.started":"2023-05-17T02:49:55.231668Z","shell.execute_reply":"2023-05-17T02:49:55.238270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the preprocessing function\ndef preprocess(file_path, label): \n    #Converting the audio files into single channel audio\n    wav = load_wav_1k_mono(file_path)\n    \n    #Limiting the length of the wave file to 30,000 samples.\n    wav = wav[:30000]\n    \n    #For the files that are shorter than 30,000 samples, pad it with 0s and increase the length to 30,000 samples\n    zero_padding = tf.zeros([30000] - tf.shape(wav), dtype=tf.float32)\n    \n    #Concatenating the zero-padded and the wav tensor along the first dimension to produce a tensor with a length of 30,000 samples.\n    wav = tf.concat([zero_padding, wav],0)\n    \n    #Computing the Short-Time Fourier Transform (STFT) of the wave file. \n    #Optimal frame_length and frame_step parameters are fine tuned to get a better accuracy.\n    spectrogram = tf.signal.stft(wav, frame_length=125, frame_step=48)\n    \n    #Computing the absolute value of the STFT coefficients.\n    spectrogram = tf.abs(spectrogram)\n    \n    #Applying logarithm to the STFT coefficients after adding a small positive constant '1e-15' to avoid taking logarithm of zero.\n    #This step improves the spectrogram and visualize the repetitive pattern of pulses.\n    spectrogram = tf.math.log(spectrogram + 1e-15)  \n    \n    #Adding an additional dimension to the STFT coefficients tensor along the third axis to represent the channels of the input audio.\n    spectrogram = tf.expand_dims(spectrogram, axis=2)\n    \n    return spectrogram, label","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:50:15.173873Z","iopub.execute_input":"2023-05-17T02:50:15.175175Z","iopub.status.idle":"2023-05-17T02:50:15.183903Z","shell.execute_reply.started":"2023-05-17T02:50:15.175125Z","shell.execute_reply":"2023-05-17T02:50:15.182942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting a random data from the A-Calls dataset\nfilepath, label = positives.shuffle(buffer_size=10000).as_numpy_iterator().next()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:50:36.952368Z","iopub.execute_input":"2023-05-17T02:50:36.953851Z","iopub.status.idle":"2023-05-17T02:50:37.012830Z","shell.execute_reply.started":"2023-05-17T02:50:36.953803Z","shell.execute_reply":"2023-05-17T02:50:37.011700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Passing the file through the preprocessing function\nspectrogram, label = preprocess(filepath, label)","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:50:38.411921Z","iopub.execute_input":"2023-05-17T02:50:38.412832Z","iopub.status.idle":"2023-05-17T02:50:38.578428Z","shell.execute_reply.started":"2023-05-17T02:50:38.412790Z","shell.execute_reply":"2023-05-17T02:50:38.577026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Spectrogram of a whale A call.\n#The repetitive pattern of pulses is visible at the top.\nplt.figure(figsize=(30,20))\nplt.imshow(tf.transpose(spectrogram)[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-17T02:50:48.332879Z","iopub.execute_input":"2023-05-17T02:50:48.333332Z","iopub.status.idle":"2023-05-17T02:50:48.705779Z","shell.execute_reply.started":"2023-05-17T02:50:48.333291Z","shell.execute_reply":"2023-05-17T02:50:48.704463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting a random data from the Non A-Calls dataset\nfilepath, label = negatives.shuffle(buffer_size=10000).as_numpy_iterator().next()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:38:24.159593Z","iopub.execute_input":"2023-05-16T02:38:24.160216Z","iopub.status.idle":"2023-05-16T02:38:24.196779Z","shell.execute_reply.started":"2023-05-16T02:38:24.160176Z","shell.execute_reply":"2023-05-16T02:38:24.195863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Passing the file through the preprocessing function\nspectrogram, label = preprocess(filepath, label)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:38:46.409011Z","iopub.execute_input":"2023-05-16T02:38:46.410166Z","iopub.status.idle":"2023-05-16T02:38:46.472771Z","shell.execute_reply.started":"2023-05-16T02:38:46.410115Z","shell.execute_reply":"2023-05-16T02:38:46.471597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Spectrogram of not a non call\n#There are no repetitive pattern of pulses visible.\nplt.figure(figsize=(30,20))\nplt.imshow(tf.transpose(spectrogram)[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:38:52.484801Z","iopub.execute_input":"2023-05-16T02:38:52.487110Z","iopub.status.idle":"2023-05-16T02:38:52.705606Z","shell.execute_reply.started":"2023-05-16T02:38:52.487064Z","shell.execute_reply":"2023-05-16T02:38:52.704807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Another spectrogram of an A-Call but no patterns are visible to the naked eye\nplt.figure(figsize=(14, 5))\nplt.specgram(wave, Fs=2000, cmap='Spectral')\nplt.title('Spectrogram for A Call')\nplt.ylim([0, 100])\nplt.xlabel('Time [in seconds]')\nplt.ylabel('Frequency [in hertz]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:39:10.424789Z","iopub.execute_input":"2023-05-16T02:39:10.425517Z","iopub.status.idle":"2023-05-16T02:39:10.681892Z","shell.execute_reply.started":"2023-05-16T02:39:10.425466Z","shell.execute_reply":"2023-05-16T02:39:10.680542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Another spectrogram of a Non A-Call but no patterns are visible to the naked eye\nplt.figure(figsize=(14, 5))\nplt.specgram(nwave, Fs=2000, cmap='Spectral')\nplt.title('Spectrogram for Non A Call')\nplt.ylim([0, 100])\nplt.xlabel('Time [in seconds]')\nplt.ylabel('Frequency [in hertz]')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:39:28.500457Z","iopub.execute_input":"2023-05-16T02:39:28.501122Z","iopub.status.idle":"2023-05-16T02:39:29.014724Z","shell.execute_reply.started":"2023-05-16T02:39:28.501076Z","shell.execute_reply":"2023-05-16T02:39:29.013578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating the Tensorflow data pipeline\ndata = data.map(preprocess)\ndata = data.cache()\ndata = data.shuffle(buffer_size=10000)\ndata = data.batch(16)\ndata = data.prefetch(8)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:39:51.784938Z","iopub.execute_input":"2023-05-16T02:39:51.785309Z","iopub.status.idle":"2023-05-16T02:39:52.373617Z","shell.execute_reply.started":"2023-05-16T02:39:51.785276Z","shell.execute_reply":"2023-05-16T02:39:52.372123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reviewing the length of the data\nlen(data)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:40:00.804533Z","iopub.execute_input":"2023-05-16T02:40:00.804945Z","iopub.status.idle":"2023-05-16T02:40:00.810956Z","shell.execute_reply.started":"2023-05-16T02:40:00.804911Z","shell.execute_reply":"2023-05-16T02:40:00.810225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculating the training data amount(70% of the total data)\nlen(data)*.7","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:40:10.785066Z","iopub.execute_input":"2023-05-16T02:40:10.785704Z","iopub.status.idle":"2023-05-16T02:40:10.792117Z","shell.execute_reply.started":"2023-05-16T02:40:10.785656Z","shell.execute_reply":"2023-05-16T02:40:10.791361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Calculating the testing data amount(30% of the total data)\nlen(data) - 1136","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:40:17.469099Z","iopub.execute_input":"2023-05-16T02:40:17.469528Z","iopub.status.idle":"2023-05-16T02:40:17.479376Z","shell.execute_reply.started":"2023-05-16T02:40:17.469488Z","shell.execute_reply":"2023-05-16T02:40:17.477680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Splitting data into train and test datasets\ntrain = data.take(1136)\ntest = data.skip(1136).take(486)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:40:25.245332Z","iopub.execute_input":"2023-05-16T02:40:25.246202Z","iopub.status.idle":"2023-05-16T02:40:25.256766Z","shell.execute_reply.started":"2023-05-16T02:40:25.246153Z","shell.execute_reply":"2023-05-16T02:40:25.255009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing one batch\nsamples, labels = train.as_numpy_iterator().next()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:40:31.665081Z","iopub.execute_input":"2023-05-16T02:40:31.665517Z","iopub.status.idle":"2023-05-16T02:40:48.953809Z","shell.execute_reply.started":"2023-05-16T02:40:31.665477Z","shell.execute_reply":"2023-05-16T02:40:48.952286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Shape of the first batch of 16 samples\nsamples.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:02.600623Z","iopub.execute_input":"2023-05-16T02:41:02.601078Z","iopub.status.idle":"2023-05-16T02:41:02.610471Z","shell.execute_reply.started":"2023-05-16T02:41:02.601041Z","shell.execute_reply":"2023-05-16T02:41:02.607747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Dense, Flatten","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:08.210523Z","iopub.execute_input":"2023-05-16T02:41:08.211996Z","iopub.status.idle":"2023-05-16T02:41:08.217943Z","shell.execute_reply.started":"2023-05-16T02:41:08.211939Z","shell.execute_reply":"2023-05-16T02:41:08.217111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the Sequencial model with CNN layers\nmodel = Sequential()\nmodel.add(Conv2D(16, (3,3), activation='relu', input_shape=samples.shape[1:4]))\nmodel.add(Conv2D(16, (3,3), activation='relu'))\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:14.309785Z","iopub.execute_input":"2023-05-16T02:41:14.310194Z","iopub.status.idle":"2023-05-16T02:41:14.589115Z","shell.execute_reply.started":"2023-05-16T02:41:14.310160Z","shell.execute_reply":"2023-05-16T02:41:14.587586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the model compiling parameters\nmodel.compile(\n    'Adam', \n    loss='BinaryCrossentropy', \n    metrics=[tf.keras.metrics.Recall(),tf.keras.metrics.Precision()]\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:20.843957Z","iopub.execute_input":"2023-05-16T02:41:20.844381Z","iopub.status.idle":"2023-05-16T02:41:20.869492Z","shell.execute_reply.started":"2023-05-16T02:41:20.844347Z","shell.execute_reply":"2023-05-16T02:41:20.868727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:27.292961Z","iopub.execute_input":"2023-05-16T02:41:27.294083Z","iopub.status.idle":"2023-05-16T02:41:27.319445Z","shell.execute_reply.started":"2023-05-16T02:41:27.294036Z","shell.execute_reply":"2023-05-16T02:41:27.318079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating a TensorFlow Dataset object from the input samples and labels tensors,\ntrain_dataset = tf.data.Dataset.from_tensor_slices((samples, labels))\n\n#Applying batching to the dataset by grouping the samples and labels into batches of size 16.\ntrain_dataset = train_dataset.batch(16)\n\n#Enabling data pre-fetching\ntrain_dataset = train_dataset.prefetch(tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:35.315394Z","iopub.execute_input":"2023-05-16T02:41:35.315798Z","iopub.status.idle":"2023-05-16T02:41:35.326761Z","shell.execute_reply.started":"2023-05-16T02:41:35.315760Z","shell.execute_reply":"2023-05-16T02:41:35.325405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Training the model with 5 epochs.\nhist = model.fit(train, epochs=5, validation_data=test)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T02:41:42.226427Z","iopub.execute_input":"2023-05-16T02:41:42.227428Z","iopub.status.idle":"2023-05-16T03:35:33.281443Z","shell.execute_reply.started":"2023-05-16T02:41:42.227365Z","shell.execute_reply":"2023-05-16T03:35:33.279603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the Loss, over time\nplt.title('Loss')\nplt.plot(hist.history['loss'], 'r')\nplt.plot(hist.history['val_loss'], 'b')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:04:11.199911Z","iopub.execute_input":"2023-05-16T04:04:11.200303Z","iopub.status.idle":"2023-05-16T04:04:11.365592Z","shell.execute_reply.started":"2023-05-16T04:04:11.200266Z","shell.execute_reply":"2023-05-16T04:04:11.364560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the Precision, over time\nplt.title('Precision')\nplt.plot(hist.history['precision'], 'r')\nplt.plot(hist.history['val_precision'], 'b')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:04:20.313544Z","iopub.execute_input":"2023-05-16T04:04:20.313932Z","iopub.status.idle":"2023-05-16T04:04:20.478806Z","shell.execute_reply.started":"2023-05-16T04:04:20.313898Z","shell.execute_reply":"2023-05-16T04:04:20.477734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Plotting the Recall, over time\nplt.title('Recall')\nplt.plot(hist.history['recall'], 'r')\nplt.plot(hist.history['val_recall'], 'b')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:04:28.884151Z","iopub.execute_input":"2023-05-16T04:04:28.884582Z","iopub.status.idle":"2023-05-16T04:04:29.042834Z","shell.execute_reply.started":"2023-05-16T04:04:28.884544Z","shell.execute_reply":"2023-05-16T04:04:29.041353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Splitting the test data into features and labels\nX_test, y_test = test.as_numpy_iterator().next()\n#Predicting using X_test data\nyhat = model.predict(X_test)\n#Defining a threshold of 0.5\nyhat = [1 if prediction > 0.50 else 0 for prediction in yhat]\n","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:05:08.708835Z","iopub.execute_input":"2023-05-16T04:05:08.709261Z","iopub.status.idle":"2023-05-16T04:05:09.637860Z","shell.execute_reply.started":"2023-05-16T04:05:08.709223Z","shell.execute_reply":"2023-05-16T04:05:09.636595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yhat","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:05:12.032232Z","iopub.execute_input":"2023-05-16T04:05:12.032580Z","iopub.status.idle":"2023-05-16T04:05:12.040465Z","shell.execute_reply.started":"2023-05-16T04:05:12.032547Z","shell.execute_reply":"2023-05-16T04:05:12.039234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting the testing accuracy\nfrom sklearn.metrics import accuracy_score\n\naccuracy = accuracy_score(y_test, yhat)\nprint(\"Accuracy: {:.2f}%\".format(accuracy * 100))","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:05:20.373142Z","iopub.execute_input":"2023-05-16T04:05:20.373644Z","iopub.status.idle":"2023-05-16T04:05:20.778177Z","shell.execute_reply.started":"2023-05-16T04:05:20.373605Z","shell.execute_reply":"2023-05-16T04:05:20.776097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining file path to testing data\nTests = os.path.join('/kaggle', 'input', 'datafestintegration2023','test','test')","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:05:36.303588Z","iopub.execute_input":"2023-05-16T04:05:36.304047Z","iopub.status.idle":"2023-05-16T04:05:36.309316Z","shell.execute_reply.started":"2023-05-16T04:05:36.304006Z","shell.execute_reply":"2023-05-16T04:05:36.308025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting all the files in testing file path as a string in Tensoflow dataset format without shuffling\nTest_files = tf.data.Dataset.list_files(Tests + '/*.wav', shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:05:48.725259Z","iopub.execute_input":"2023-05-16T04:05:48.726350Z","iopub.status.idle":"2023-05-16T04:05:49.790067Z","shell.execute_reply.started":"2023-05-16T04:05:48.726309Z","shell.execute_reply":"2023-05-16T04:05:49.789219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Labeling all the testing data as 0's in order to use the pre-defined functions\ntest_data = tf.data.Dataset.zip((Test_files, tf.data.Dataset.from_tensor_slices(tf.zeros(len(neg)))))\n#Mapping the testing data to the pre defined preprocessing function\ntest_data = test_data.map(preprocess)\n#Pipelining testing data in batches of 16 \ntest_data = test_data.batch(16)\n#size of the test_data\nlen(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:06:29.963782Z","iopub.execute_input":"2023-05-16T04:06:29.965028Z","iopub.status.idle":"2023-05-16T04:06:30.194772Z","shell.execute_reply.started":"2023-05-16T04:06:29.964982Z","shell.execute_reply":"2023-05-16T04:06:30.193771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing one batch\nsample, dummy = test_data.as_numpy_iterator().next()\n#Shape of a testing data batch of 16\nsample.shape","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:06:55.222069Z","iopub.execute_input":"2023-05-16T04:06:55.222502Z","iopub.status.idle":"2023-05-16T04:06:55.339747Z","shell.execute_reply.started":"2023-05-16T04:06:55.222463Z","shell.execute_reply":"2023-05-16T04:06:55.338430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Iterating through all test data, predicting them using the trained model and putting them in an array\nall_preds = []\n\nfor x_batch, _ in test_data.as_numpy_iterator():\n    preds = model.predict(x_batch)\n    all_preds.extend(preds)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:07:06.318555Z","iopub.execute_input":"2023-05-16T04:07:06.319651Z","iopub.status.idle":"2023-05-16T04:07:27.291320Z","shell.execute_reply.started":"2023-05-16T04:07:06.319593Z","shell.execute_reply":"2023-05-16T04:07:27.289681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(all_preds)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:18:43.656826Z","iopub.execute_input":"2023-05-16T04:18:43.657303Z","iopub.status.idle":"2023-05-16T04:18:43.667074Z","shell.execute_reply.started":"2023-05-16T04:18:43.657260Z","shell.execute_reply":"2023-05-16T04:18:43.665011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the threshold to 0.50\npreds = [1 if prediction > 0.50 else 0 for prediction in all_preds]","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:18:48.793813Z","iopub.execute_input":"2023-05-16T04:18:48.794254Z","iopub.status.idle":"2023-05-16T04:18:48.802501Z","shell.execute_reply.started":"2023-05-16T04:18:48.794217Z","shell.execute_reply":"2023-05-16T04:18:48.801372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The number of predictions predicted as an A-Call\npreds.count(1)\n#The number of predictions predicted as a non A-Call\npreds.count(0)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:20:19.923971Z","iopub.execute_input":"2023-05-16T04:20:19.924424Z","iopub.status.idle":"2023-05-16T04:20:19.933535Z","shell.execute_reply.started":"2023-05-16T04:20:19.924383Z","shell.execute_reply":"2023-05-16T04:20:19.931747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Testing folder path\ntestAudio_folder = '/kaggle/input/datafestintegration2023/test/test'\n#Listing all the audio files to an list\ntestAudio_list = os.listdir(testAudio_folder)\n#Sorting the list\ntestAudio_list = sorted(testAudio_list)\nlen(testAudio_list)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:21:01.160124Z","iopub.execute_input":"2023-05-16T04:21:01.161999Z","iopub.status.idle":"2023-05-16T04:21:01.182830Z","shell.execute_reply.started":"2023-05-16T04:21:01.161919Z","shell.execute_reply":"2023-05-16T04:21:01.181323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating a pandas dataframe with the ID values using the audio file names list\ndf = pd.DataFrame(testAudio_list, columns=[\"id\"])\n#Splitting the audio file name and getting only the ID numbers\ndf['id'] = df['id'].str.split('.').str[0]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:23:18.745867Z","iopub.execute_input":"2023-05-16T04:23:18.746291Z","iopub.status.idle":"2023-05-16T04:23:18.787362Z","shell.execute_reply.started":"2023-05-16T04:23:18.746253Z","shell.execute_reply":"2023-05-16T04:23:18.785822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding the prediction column with the predictions made earlier\ndf = df.assign(label=preds)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:23:21.471309Z","iopub.execute_input":"2023-05-16T04:23:21.471780Z","iopub.status.idle":"2023-05-16T04:23:21.480095Z","shell.execute_reply.started":"2023-05-16T04:23:21.471733Z","shell.execute_reply":"2023-05-16T04:23:21.478806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting the pandas dataframe to a csv file to submit\ndf.to_csv('submission36.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2023-05-16T04:23:32.361741Z","iopub.execute_input":"2023-05-16T04:23:32.362580Z","iopub.status.idle":"2023-05-16T04:23:32.377497Z","shell.execute_reply.started":"2023-05-16T04:23:32.362535Z","shell.execute_reply":"2023-05-16T04:23:32.375815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}