{"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 numpy as np\nimport pandas as pd\nimport os\nimport librosa, librosa.display   \nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport IPython.display as ipd\nfrom tqdm import tqdm\n\nimport tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.metrics import classification_report, log_loss, accuracy_score\nfrom sklearn.model_selection import train_test_split","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":3.100276,"end_time":"2021-05-26T15:24:12.918178","exception":false,"start_time":"2021-05-26T15:24:09.817902","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-28T01:37:13.485966Z","iopub.execute_input":"2021-05-28T01:37:13.486518Z","iopub.status.idle":"2021-05-28T01:37:21.529081Z","shell.execute_reply.started":"2021-05-28T01:37:13.486438Z","shell.execute_reply":"2021-05-28T01:37:21.528006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = '../input/birdclef-2021/train_short_audio/acafly/XC109605.ogg'\nsignal, sr = librosa.load(file, sr=40200)\n\nlibrosa.display.waveplot(signal, sr = sr)\nplt.xlabel(\"Time\")\nplt.ylabel(\"Amplitude\")\nplt.show()\n\nn_fft = 2048\nhop_length = 512\nstft = librosa.core.stft(signal, hop_length = hop_length, n_fft = n_fft)\nspectrogram = np.abs(stft)\nlog_spectrogram  = librosa.amplitude_to_db(spectrogram)","metadata":{"papermill":{"duration":2.679691,"end_time":"2021-05-26T15:24:15.657683","exception":false,"start_time":"2021-05-26T15:24:12.977992","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-28T01:37:21.530513Z","iopub.execute_input":"2021-05-28T01:37:21.530820Z","iopub.status.idle":"2021-05-28T01:37:25.539038Z","shell.execute_reply.started":"2021-05-28T01:37:21.530783Z","shell.execute_reply":"2021-05-28T01:37:25.538030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(stft.shape)\nprint(spectrogram.shape)\nprint(log_spectrogram.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:25.540878Z","iopub.execute_input":"2021-05-28T01:37:25.541152Z","iopub.status.idle":"2021-05-28T01:37:25.546485Z","shell.execute_reply.started":"2021-05-28T01:37:25.541126Z","shell.execute_reply":"2021-05-28T01:37:25.545444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(file)","metadata":{"papermill":{"duration":0.046028,"end_time":"2021-05-26T15:24:20.388482","exception":false,"start_time":"2021-05-26T15:24:20.342454","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-28T01:37:25.547952Z","iopub.execute_input":"2021-05-28T01:37:25.548409Z","iopub.status.idle":"2021-05-28T01:37:25.595359Z","shell.execute_reply.started":"2021-05-28T01:37:25.548377Z","shell.execute_reply":"2021-05-28T01:37:25.594276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_short='../input/birdclef-2021/train_short_audio/'","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:25.596952Z","iopub.execute_input":"2021-05-28T01:37:25.597327Z","iopub.status.idle":"2021-05-28T01:37:25.601961Z","shell.execute_reply.started":"2021-05-28T01:37:25.597292Z","shell.execute_reply":"2021-05-28T01:37:25.601195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta=pd.read_csv('../input/birdclef-2021/train_metadata.csv')\ntrain_label=pd.read_csv('../input/birdclef-2021/train_soundscape_labels.csv')\ntest=pd.read_csv('../input/birdclef-2021/test.csv')\nsample=pd.read_csv('../input/birdclef-2021/sample_submission.csv')","metadata":{"papermill":{"duration":0.025721,"end_time":"2021-05-26T15:24:20.441017","exception":false,"start_time":"2021-05-26T15:24:20.415296","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-28T01:37:25.602945Z","iopub.execute_input":"2021-05-28T01:37:25.603230Z","iopub.status.idle":"2021-05-28T01:37:26.146645Z","shell.execute_reply.started":"2021-05-28T01:37:25.603203Z","shell.execute_reply":"2021-05-28T01:37:26.145682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta[0:1]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:26.147930Z","iopub.execute_input":"2021-05-28T01:37:26.148215Z","iopub.status.idle":"2021-05-28T01:37:26.176936Z","shell.execute_reply.started":"2021-05-28T01:37:26.148186Z","shell.execute_reply":"2021-05-28T01:37:26.175944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label[0:1]","metadata":{"papermill":{"duration":0.021028,"end_time":"2021-05-26T15:24:20.488896","exception":false,"start_time":"2021-05-26T15:24:20.467868","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-28T01:37:26.180012Z","iopub.execute_input":"2021-05-28T01:37:26.180470Z","iopub.status.idle":"2021-05-28T01:37:26.191044Z","shell.execute_reply.started":"2021-05-28T01:37:26.180434Z","shell.execute_reply":"2021-05-28T01:37:26.189991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Name=[]\nfor file in os.listdir(train_short):\n    Name+=[file]\n    \nprint(Name)\nprint(len(Name))","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:26.193013Z","iopub.execute_input":"2021-05-28T01:37:26.193331Z","iopub.status.idle":"2021-05-28T01:37:26.227955Z","shell.execute_reply.started":"2021-05-28T01:37:26.193302Z","shell.execute_reply":"2021-05-28T01:37:26.227216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N=[]\nfor i in range(len(Name)):\n    N+=[i]\n    \nnormal_mapping=dict(zip(Name,N)) \nreverse_mapping=dict(zip(N,Name)) \n\ndef mapper(value):\n    return reverse_mapping[value]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:26.228836Z","iopub.execute_input":"2021-05-28T01:37:26.229106Z","iopub.status.idle":"2021-05-28T01:37:26.234864Z","shell.execute_reply.started":"2021-05-28T01:37:26.229080Z","shell.execute_reply":"2021-05-28T01:37:26.233944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signal, sr = librosa.load('../input/birdclef-2021/train_short_audio/acafly/XC109605.ogg', sr=40200)\nn_fft = 2048\nhop_length = 512\n\nstft = librosa.core.stft(signal, hop_length = hop_length, n_fft = n_fft)\nspectrogram = np.abs(stft)\nobj1 = Image.fromarray(np.uint8(spectrogram))\nobj2 = np.asarray(obj1.resize((96,32)))\nprint(obj2.shape)\n\nplt.figure(figsize=(18,6), dpi=50)\nplt.imshow(obj2.reshape(32,96))\nplt.xticks([])\nplt.yticks([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:26.236157Z","iopub.execute_input":"2021-05-28T01:37:26.236585Z","iopub.status.idle":"2021-05-28T01:37:29.000284Z","shell.execute_reply.started":"2021-05-28T01:37:26.236544Z","shell.execute_reply":"2021-05-28T01:37:28.999329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset=[]\ntrainlabel=[]\ncount=0\nn_fft = 2048\nhop_length = 512\n\nfor file in tqdm(Name):\n    path=os.path.join(train_short,file)\n    m=len(os.listdir(path))\n    for i in range(m//50):\n        im = os.listdir(path)[i]\n        signal, sr = librosa.load(os.path.join(path,im), sr=40200)\n        stft = librosa.core.stft(signal, hop_length = hop_length, n_fft = n_fft)\n        spectrogram = np.abs(stft)\n        obj1 = Image.fromarray(np.uint8(spectrogram))\n        obj2 = np.asarray(obj1.resize((96,32)))\n        trainset.append(obj2)\n        trainlabel.append(count)\n    count=count+1","metadata":{"execution":{"iopub.status.busy":"2021-05-28T01:37:29.001863Z","iopub.execute_input":"2021-05-28T01:37:29.002264Z","iopub.status.idle":"2021-05-28T02:22:10.140148Z","shell.execute_reply.started":"2021-05-28T01:37:29.002221Z","shell.execute_reply":"2021-05-28T02:22:10.139398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=(np.array(trainset)).reshape(-1,32,32,3)\nlabels0=np.array(trainlabel)\nprint(data.shape)\nprint(labels0.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:49:12.212132Z","iopub.execute_input":"2021-05-28T02:49:12.212501Z","iopub.status.idle":"2021-05-28T02:49:12.220892Z","shell.execute_reply.started":"2021-05-28T02:49:12.212468Z","shell.execute_reply":"2021-05-28T02:49:12.219855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.metrics import classification_report, log_loss, accuracy_score\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:49:16.299982Z","iopub.execute_input":"2021-05-28T02:49:16.300450Z","iopub.status.idle":"2021-05-28T02:49:16.305291Z","shell.execute_reply.started":"2021-05-28T02:49:16.300416Z","shell.execute_reply":"2021-05-28T02:49:16.304514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels1=to_categorical(labels0)\ndata=np.array(data)\nlabels=np.array(labels1)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:52:49.186543Z","iopub.execute_input":"2021-05-28T02:52:49.186919Z","iopub.status.idle":"2021-05-28T02:52:49.192934Z","shell.execute_reply.started":"2021-05-28T02:52:49.186886Z","shell.execute_reply":"2021-05-28T02:52:49.191845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainx,testx,trainy,testy=train_test_split(data,labels,test_size=0.2,random_state=44)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:52:51.204578Z","iopub.execute_input":"2021-05-28T02:52:51.204955Z","iopub.status.idle":"2021-05-28T02:52:51.211375Z","shell.execute_reply.started":"2021-05-28T02:52:51.204926Z","shell.execute_reply":"2021-05-28T02:52:51.210644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(horizontal_flip=True,vertical_flip=True,rotation_range=20,zoom_range=0.2,\n                        width_shift_range=0.2,height_shift_range=0.2,shear_range=0.1,fill_mode=\"nearest\")","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:52:53.958596Z","iopub.execute_input":"2021-05-28T02:52:53.959333Z","iopub.status.idle":"2021-05-28T02:52:53.964599Z","shell.execute_reply.started":"2021-05-28T02:52:53.959285Z","shell.execute_reply":"2021-05-28T02:52:53.963870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pretrained_model3 = tf.keras.applications.DenseNet201(input_shape=(32,32,3),include_top=False,weights='imagenet',pooling='avg')\npretrained_model3.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:52:57.772215Z","iopub.execute_input":"2021-05-28T02:52:57.772871Z","iopub.status.idle":"2021-05-28T02:53:02.480798Z","shell.execute_reply.started":"2021-05-28T02:52:57.772822Z","shell.execute_reply":"2021-05-28T02:53:02.479756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs3 = pretrained_model3.input\nx3 = tf.keras.layers.Dense(128, activation='relu')(pretrained_model3.output)\noutputs3 = tf.keras.layers.Dense(397, activation='softmax')(x3)\nmodel = tf.keras.Model(inputs=inputs3, outputs=outputs3)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:53:02.482549Z","iopub.execute_input":"2021-05-28T02:53:02.482980Z","iopub.status.idle":"2021-05-28T02:53:02.551927Z","shell.execute_reply.started":"2021-05-28T02:53:02.482938Z","shell.execute_reply":"2021-05-28T02:53:02.550965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"papermill":{"duration":0.021271,"end_time":"2021-05-26T15:24:20.531245","exception":false,"start_time":"2021-05-26T15:24:20.509974","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-05-28T02:53:03.293695Z","iopub.execute_input":"2021-05-28T02:53:03.294245Z","iopub.status.idle":"2021-05-28T02:53:03.317797Z","shell.execute_reply.started":"2021-05-28T02:53:03.294198Z","shell.execute_reply":"2021-05-28T02:53:03.317084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"his=model.fit(datagen.flow(trainx,trainy,batch_size=32),validation_data=(testx,testy),epochs=200)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T02:53:07.370023Z","iopub.execute_input":"2021-05-28T02:53:07.370391Z","iopub.status.idle":"2021-05-28T03:07:06.827967Z","shell.execute_reply.started":"2021-05-28T02:53:07.370363Z","shell.execute_reply":"2021-05-28T03:07:06.826872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(testx)\npred=np.argmax(y_pred,axis=1)\nground = np.argmax(testy,axis=1)\nprint(classification_report(ground,pred))","metadata":{"execution":{"iopub.status.busy":"2021-05-28T03:07:06.831732Z","iopub.execute_input":"2021-05-28T03:07:06.832079Z","iopub.status.idle":"2021-05-28T03:07:10.871189Z","shell.execute_reply.started":"2021-05-28T03:07:06.832038Z","shell.execute_reply":"2021-05-28T03:07:10.870358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_acc = his.history['accuracy']\nvalue_acc = his.history['val_accuracy']\nget_loss = his.history['loss']\nvalidation_loss = his.history['val_loss']\n\nepochs = range(len(get_acc))\nplt.plot(epochs, get_acc, 'r', label='Accuracy of Training data')\nplt.plot(epochs, value_acc, 'b', label='Accuracy of Validation data')\nplt.title('Training vs validation accuracy')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T03:07:10.872688Z","iopub.execute_input":"2021-05-28T03:07:10.872992Z","iopub.status.idle":"2021-05-28T03:07:11.052717Z","shell.execute_reply.started":"2021-05-28T03:07:10.872963Z","shell.execute_reply":"2021-05-28T03:07:11.051802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = range(len(get_loss))\nplt.plot(epochs, get_loss, 'r', label='Loss of Training data')\nplt.plot(epochs, validation_loss, 'b', label='Loss of Validation data')\nplt.title('Training vs validation loss')\nplt.legend(loc=0)\nplt.figure()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-28T03:07:11.054145Z","iopub.execute_input":"2021-05-28T03:07:11.054416Z","iopub.status.idle":"2021-05-28T03:07:11.232610Z","shell.execute_reply.started":"2021-05-28T03:07:11.054389Z","shell.execute_reply":"2021-05-28T03:07:11.231719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred2=model.predict(data)\nprint(data.shape)\nprint(pred2.shape)\n\nPRED=[]\nfor item in pred2:\n    value2=np.argmax(item)      \n    PRED+=[value2]","metadata":{"execution":{"iopub.status.busy":"2021-05-28T03:07:11.234008Z","iopub.execute_input":"2021-05-28T03:07:11.234308Z","iopub.status.idle":"2021-05-28T03:07:15.025955Z","shell.execute_reply.started":"2021-05-28T03:07:11.234278Z","shell.execute_reply":"2021-05-28T03:07:15.024905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ANS=labels0","metadata":{"execution":{"iopub.status.busy":"2021-05-28T03:07:15.027477Z","iopub.execute_input":"2021-05-28T03:07:15.027936Z","iopub.status.idle":"2021-05-28T03:07:15.032553Z","shell.execute_reply.started":"2021-05-28T03:07:15.027894Z","shell.execute_reply":"2021-05-28T03:07:15.031520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy=accuracy_score(ANS,PRED)\nprint(accuracy)","metadata":{"execution":{"iopub.status.busy":"2021-05-28T03:07:15.033982Z","iopub.execute_input":"2021-05-28T03:07:15.034564Z","iopub.status.idle":"2021-05-28T03:07:15.049276Z","shell.execute_reply.started":"2021-05-28T03:07:15.034521Z","shell.execute_reply":"2021-05-28T03:07:15.048291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}