{"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":"markdown","source":"# Prepare Enviroment ","metadata":{}},{"cell_type":"code","source":"!pip install -U efficientnet -qq","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:01.976648Z","iopub.execute_input":"2022-04-14T06:13:01.977243Z","iopub.status.idle":"2022-04-14T06:13:11.531343Z","shell.execute_reply.started":"2022-04-14T06:13:01.977144Z","shell.execute_reply":"2022-04-14T06:13:11.530426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport math\n\nimport os\nimport cv2\n\nimport IPython.display as ipd \n\nimport librosa \nimport librosa.display\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\n\nfrom tensorflow.keras.models import Sequential, load_model\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import backend as K\n\nimport efficientnet.tfkeras as efn\n\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' ","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:11.533567Z","iopub.execute_input":"2022-04-14T06:13:11.533840Z","iopub.status.idle":"2022-04-14T06:13:18.738726Z","shell.execute_reply.started":"2022-04-14T06:13:11.533782Z","shell.execute_reply":"2022-04-14T06:13:18.737964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load DataFrame","metadata":{}},{"cell_type":"code","source":"train_path = '../input/freesound-audio-tagging/audio_train/'\n\nprint(len(os.listdir(train_path)))","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:18.740939Z","iopub.execute_input":"2022-04-14T06:13:18.741391Z","iopub.status.idle":"2022-04-14T06:13:18.976493Z","shell.execute_reply.started":"2022-04-14T06:13:18.741348Z","shell.execute_reply":"2022-04-14T06:13:18.975497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/freesound-audio-tagging/train.csv\")\n\nprint('The shape of the training data is: ', train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:18.979046Z","iopub.execute_input":"2022-04-14T06:13:18.979575Z","iopub.status.idle":"2022-04-14T06:13:19.005688Z","shell.execute_reply.started":"2022-04-14T06:13:18.979526Z","shell.execute_reply":"2022-04-14T06:13:19.004681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:19.007305Z","iopub.execute_input":"2022-04-14T06:13:19.007673Z","iopub.status.idle":"2022-04-14T06:13:19.027023Z","shell.execute_reply.started":"2022-04-14T06:13:19.007630Z","shell.execute_reply":"2022-04-14T06:13:19.026170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Unique Labels","metadata":{}},{"cell_type":"code","source":"uniq_labels = train.label.unique()\nprint('There are a total of', len(uniq_labels), 'unique labels.\\n')\nprint(uniq_labels)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:19.028599Z","iopub.execute_input":"2022-04-14T06:13:19.029157Z","iopub.status.idle":"2022-04-14T06:13:19.044000Z","shell.execute_reply.started":"2022-04-14T06:13:19.029113Z","shell.execute_reply":"2022-04-14T06:13:19.041811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Label Distribution","metadata":{}},{"cell_type":"code","source":"print((train.manually_verified.value_counts() /len(train)).to_frame().T)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:19.045340Z","iopub.execute_input":"2022-04-14T06:13:19.046143Z","iopub.status.idle":"2022-04-14T06:13:19.058662Z","shell.execute_reply.started":"2022-04-14T06:13:19.046099Z","shell.execute_reply":"2022-04-14T06:13:19.057511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.manually_verified.value_counts().plot(kind='bar', xlabel='MGMT_value', ylabel='Count', \n                                     color=['#1E90FF', '#00C957'], edgecolor='black');","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:19.060594Z","iopub.execute_input":"2022-04-14T06:13:19.061494Z","iopub.status.idle":"2022-04-14T06:13:19.277730Z","shell.execute_reply.started":"2022-04-14T06:13:19.061458Z","shell.execute_reply":"2022-04-14T06:13:19.277039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring Samples","metadata":{}},{"cell_type":"markdown","source":"## Sample 1","metadata":{}},{"cell_type":"code","source":"gunshot = '../input/freesound-audio-tagging/audio_train/0048fd00.wav'\nipd.Audio(gunshot)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:19.278951Z","iopub.execute_input":"2022-04-14T06:13:19.279715Z","iopub.status.idle":"2022-04-14T06:13:19.294211Z","shell.execute_reply.started":"2022-04-14T06:13:19.279675Z","shell.execute_reply":"2022-04-14T06:13:19.293539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signal, sr = librosa.load(gunshot)\nprint(type(signal))\nprint(type(sr))","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:19.297374Z","iopub.execute_input":"2022-04-14T06:13:19.297963Z","iopub.status.idle":"2022-04-14T06:13:20.327108Z","shell.execute_reply.started":"2022-04-14T06:13:19.297915Z","shell.execute_reply":"2022-04-14T06:13:20.326384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(signal.shape)\nprint(sr)\nprint(len(signal) / sr)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:20.331597Z","iopub.execute_input":"2022-04-14T06:13:20.334426Z","iopub.status.idle":"2022-04-14T06:13:20.344457Z","shell.execute_reply.started":"2022-04-14T06:13:20.334385Z","shell.execute_reply":"2022-04-14T06:13:20.343758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [12,3])\nplt.subplot(2,1,1)\nplt.plot(signal)\nplt.subplot(2,1,2)\ninterval = range(2000, 3000)\nplt.plot(interval, signal[interval])\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:20.345837Z","iopub.execute_input":"2022-04-14T06:13:20.346300Z","iopub.status.idle":"2022-04-14T06:13:20.654516Z","shell.execute_reply.started":"2022-04-14T06:13:20.346265Z","shell.execute_reply":"2022-04-14T06:13:20.653664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1 = librosa.feature.melspectrogram(y=signal, sr=22050)   \nx2 = librosa.power_to_db(x1, ref=np.max)   \n\nprint(x2.shape)\n\nlibrosa.display.specshow(x2, sr=22050, x_axis='time', y_axis='hz')\nplt.colorbar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:20.656127Z","iopub.execute_input":"2022-04-14T06:13:20.656432Z","iopub.status.idle":"2022-04-14T06:13:20.941709Z","shell.execute_reply.started":"2022-04-14T06:13:20.656391Z","shell.execute_reply":"2022-04-14T06:13:20.941033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sample 2","metadata":{}},{"cell_type":"code","source":"cello = '../input/freesound-audio-tagging/audio_train/0091fc7f.wav'\nipd.Audio(cello)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:20.942884Z","iopub.execute_input":"2022-04-14T06:13:20.943928Z","iopub.status.idle":"2022-04-14T06:13:20.969748Z","shell.execute_reply.started":"2022-04-14T06:13:20.943889Z","shell.execute_reply":"2022-04-14T06:13:20.969138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"signal, sr = librosa.load(cello)\nprint(type(signal))\nprint(type(sr))","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:20.970912Z","iopub.execute_input":"2022-04-14T06:13:20.971326Z","iopub.status.idle":"2022-04-14T06:13:21.197363Z","shell.execute_reply.started":"2022-04-14T06:13:20.971288Z","shell.execute_reply":"2022-04-14T06:13:21.196530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(signal.shape)\nprint(sr)\nprint(len(signal) / sr)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:21.198470Z","iopub.execute_input":"2022-04-14T06:13:21.198931Z","iopub.status.idle":"2022-04-14T06:13:21.205492Z","shell.execute_reply.started":"2022-04-14T06:13:21.198888Z","shell.execute_reply":"2022-04-14T06:13:21.204677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [12,3])\nplt.subplot(2,1,1)\nplt.plot(signal)\nplt.subplot(2,1,2)\ninterval = range(2000, 3000)\nplt.plot(interval, signal[interval])\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:21.206934Z","iopub.execute_input":"2022-04-14T06:13:21.207595Z","iopub.status.idle":"2022-04-14T06:13:21.536355Z","shell.execute_reply.started":"2022-04-14T06:13:21.207556Z","shell.execute_reply":"2022-04-14T06:13:21.535500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x1 = librosa.feature.melspectrogram(y=signal, sr=22050)   \nx2 = librosa.power_to_db(x1, ref=np.max)   \n\nprint(x2.shape)\n\nlibrosa.display.specshow(x2, sr=22050, x_axis='time', y_axis='hz')\nplt.colorbar()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:21.537532Z","iopub.execute_input":"2022-04-14T06:13:21.537930Z","iopub.status.idle":"2022-04-14T06:13:21.868304Z","shell.execute_reply.started":"2022-04-14T06:13:21.537892Z","shell.execute_reply":"2022-04-14T06:13:21.867654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Label Encoder","metadata":{}},{"cell_type":"code","source":"labels = np.unique(train.label.values)\nlabel_encoder = {label:i for i, label in enumerate(labels)}\nprint(label_encoder['Cello'])\nprint(label_encoder['Gunshot_or_gunfire'])","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:21.869738Z","iopub.execute_input":"2022-04-14T06:13:21.870246Z","iopub.status.idle":"2022-04-14T06:13:21.887987Z","shell.execute_reply.started":"2022-04-14T06:13:21.870207Z","shell.execute_reply":"2022-04-14T06:13:21.887250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Displaying Several Spectrogram Images","metadata":{}},{"cell_type":"code","source":"sample = train.sample(20)\n\nplt.figure(figsize=[20,9])\n\nfor i in range(20):\n    fname = train_path + sample.fname.iloc[i]\n    clip, sr = librosa.load(fname, sr=44100)\n    S1 = librosa.feature.melspectrogram(y=clip, sr=44100) \n    S2 = librosa.power_to_db(S1, ref=np.max)                \n    \n    plt.subplot(5, 4, i+1)\n    librosa.display.specshow(S2)\n    plt.title(f'{sample.label.iloc[i]} - {S2.shape[:2]} - {sample.fname.iloc[i]} ', color = \"white\")\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:21.889254Z","iopub.execute_input":"2022-04-14T06:13:21.889768Z","iopub.status.idle":"2022-04-14T06:13:25.185328Z","shell.execute_reply.started":"2022-04-14T06:13:21.889728Z","shell.execute_reply":"2022-04-14T06:13:25.184679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generators","metadata":{}},{"cell_type":"code","source":"SPEC_PATH = '../input/freesound-melpec-128-512-2sec/spectrograms'\nIMG_SIZE = (128,87)\n\nclass DataGenerator(keras.utils.Sequence):\n    \n    def __init__(self, df, batch_size=32, shuffle=True, is_train=True):\n        self.df = df\n        self.n = len(df)\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        self.is_train = is_train\n        self.on_epoch_end()\n        \n    def on_epoch_end(self):\n        self.indices = np.arange(self.n)\n        if self.shuffle == True:\n            np.random.shuffle(self.indices)   \n    \n    def __len__(self):\n        \n        return math.ceil( self.n / self.batch_size )\n    \n    def __getitem__(self, batch_index):\n        \n        start = batch_index * self.batch_size\n        end = (batch_index + 1) * self.batch_size\n        \n        indices = self.indices[start:end]\n        \n        return self.__data_generation(indices)\n    \n    def __data_generation(self, batch_indices):\n        batch_size = len(batch_indices)\n        \n        X = np.zeros(shape=(batch_size, IMG_SIZE[0], IMG_SIZE[1], 3))\n        y = np.zeros(batch_size)\n        \n        for i, idx in enumerate(batch_indices):\n            FILE = self.df.fname.values[idx]\n            LABEL = self.df.label.values[idx]\n            \n            SET = 'train_spec' if self.is_train else 'test_spec'\n            path = f'{SPEC_PATH}/{SET}/{FILE[:-4]}.npy'\n\n            try:\n                data_array = np.load(path)\n                resized = cv2.resize(data_array, (IMG_SIZE[1], IMG_SIZE[0]))\n                \n                for j in range(3):\n                    X[i,:,:,j] = resized \n                \n            except:\n                print('skipped')\n\n            if self.is_train:\n                y[i] = label_encoder[LABEL]\n\n        if self.is_train:    \n            return X, y\n        return X\n\n    \nGENERATOR_TEST = True\n\nif GENERATOR_TEST:\n    temp_gen = DataGenerator(train, batch_size=8, shuffle=False)\n    X,y = temp_gen.__getitem__(0)\n\n    print(X.shape)\n    print(y)\n    \n    librosa.display.specshow(X[0, :, :, 0])","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:25.186711Z","iopub.execute_input":"2022-04-14T06:13:25.187188Z","iopub.status.idle":"2022-04-14T06:13:25.348138Z","shell.execute_reply.started":"2022-04-14T06:13:25.187149Z","shell.execute_reply":"2022-04-14T06:13:25.347215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(train, test_size=0.2, random_state=1, stratify=train.label)\n\nprint(train_df.shape)\nprint(valid_df.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:25.352288Z","iopub.execute_input":"2022-04-14T06:13:25.353111Z","iopub.status.idle":"2022-04-14T06:13:25.394365Z","shell.execute_reply.started":"2022-04-14T06:13:25.353052Z","shell.execute_reply":"2022-04-14T06:13:25.393310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataGenerator(train_df, batch_size=64, shuffle=True)\nvalid_loader = DataGenerator(valid_df, batch_size=64, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:25.399619Z","iopub.execute_input":"2022-04-14T06:13:25.399996Z","iopub.status.idle":"2022-04-14T06:13:25.412400Z","shell.execute_reply.started":"2022-04-14T06:13:25.399952Z","shell.execute_reply":"2022-04-14T06:13:25.410880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TR_STEPS = len(train_loader)\nVA_STEPS = len(valid_loader)\n\nprint(TR_STEPS)\nprint(VA_STEPS)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:25.415283Z","iopub.execute_input":"2022-04-14T06:13:25.421811Z","iopub.status.idle":"2022-04-14T06:13:25.428338Z","shell.execute_reply.started":"2022-04-14T06:13:25.421746Z","shell.execute_reply":"2022-04-14T06:13:25.427468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building CNN","metadata":{}},{"cell_type":"code","source":"ENB0_model = efn.EfficientNetB0(input_shape=(128,87,3), include_top=False, weights='imagenet')\nENB0_model.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:25.430158Z","iopub.execute_input":"2022-04-14T06:13:25.430788Z","iopub.status.idle":"2022-04-14T06:13:30.272257Z","shell.execute_reply.started":"2022-04-14T06:13:25.430738Z","shell.execute_reply":"2022-04-14T06:13:30.271479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = Sequential([\n    ENB0_model,\n    \n    GlobalAveragePooling2D(),\n    \n    Dense(64, activation='relu'),\n    Dropout(0.5),\n    \n    Dense(32, activation='relu'),\n    Dropout(0.5),\n    \n    Dense(41, activation='softmax')\n])\n\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:30.273467Z","iopub.execute_input":"2022-04-14T06:13:30.273751Z","iopub.status.idle":"2022-04-14T06:13:30.851118Z","shell.execute_reply.started":"2022-04-14T06:13:30.273716Z","shell.execute_reply":"2022-04-14T06:13:30.850280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Network","metadata":{}},{"cell_type":"markdown","source":"## Training Run 1","metadata":{}},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(0.001)\ncnn.compile(loss='sparse_categorical_crossentropy', optimizer=opt, metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:30.852932Z","iopub.execute_input":"2022-04-14T06:13:30.853377Z","iopub.status.idle":"2022-04-14T06:13:30.891861Z","shell.execute_reply.started":"2022-04-14T06:13:30.853333Z","shell.execute_reply":"2022-04-14T06:13:30.890993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\nh1 = cnn.fit(train_loader, steps_per_epoch = TR_STEPS, epochs = 25, validation_data = valid_loader, \n             validation_steps = VA_STEPS, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:13:30.898043Z","iopub.execute_input":"2022-04-14T06:13:30.898456Z","iopub.status.idle":"2022-04-14T06:21:26.361698Z","shell.execute_reply.started":"2022-04-14T06:13:30.898415Z","shell.execute_reply":"2022-04-14T06:21:26.359984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def merge_history(hlist):\n    history = {}\n    for k in hlist[0].history.keys():\n        history[k] = sum([h.history[k] for h in hlist], [])\n    return history","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:21:26.363129Z","iopub.execute_input":"2022-04-14T06:21:26.363399Z","iopub.status.idle":"2022-04-14T06:21:26.371897Z","shell.execute_reply.started":"2022-04-14T06:21:26.363362Z","shell.execute_reply":"2022-04-14T06:21:26.371152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vis_training(h, start=1):\n    epoch_range = range(start, len(h['loss'])+1)\n    s = slice(start-1, None)\n\n    plt.figure(figsize=[14,4])\n\n    n = int(len(h.keys()) / 2)\n\n    for i in range(n):\n        k = list(h.keys())[i]\n        plt.subplot(1,n,i+1)\n        plt.plot(epoch_range, h[k][s], label='Training')\n        plt.plot(epoch_range, h['val_' + k][s], label='Validation')\n        plt.xlabel('Epoch'); plt.ylabel(k); plt.title(k)\n        plt.grid()\n        plt.legend()\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:21:26.373217Z","iopub.execute_input":"2022-04-14T06:21:26.373682Z","iopub.status.idle":"2022-04-14T06:21:26.384024Z","shell.execute_reply.started":"2022-04-14T06:21:26.373643Z","shell.execute_reply":"2022-04-14T06:21:26.383260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1])\nvis_training(history)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:21:26.385516Z","iopub.execute_input":"2022-04-14T06:21:26.386081Z","iopub.status.idle":"2022-04-14T06:21:26.765209Z","shell.execute_reply.started":"2022-04-14T06:21:26.386042Z","shell.execute_reply":"2022-04-14T06:21:26.764516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training Run 2","metadata":{}},{"cell_type":"code","source":"K.set_value(cnn.optimizer.learning_rate, 0.0001)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:21:26.766445Z","iopub.execute_input":"2022-04-14T06:21:26.766842Z","iopub.status.idle":"2022-04-14T06:21:26.775261Z","shell.execute_reply.started":"2022-04-14T06:21:26.766790Z","shell.execute_reply":"2022-04-14T06:21:26.774652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\nh2 = cnn.fit(train_loader, steps_per_epoch = TR_STEPS, epochs = 15, validation_data = valid_loader, \n             validation_steps = VA_STEPS, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:21:26.780419Z","iopub.execute_input":"2022-04-14T06:21:26.781097Z","iopub.status.idle":"2022-04-14T06:25:48.567223Z","shell.execute_reply.started":"2022-04-14T06:21:26.781055Z","shell.execute_reply":"2022-04-14T06:25:48.566499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vis_training(h, start=1):\n    epoch_range = range(start, len(h['loss'])+1)\n    s = slice(start-1, None)\n\n    plt.figure(figsize=[14,4])\n\n    n = int(len(h.keys()) / 2)\n\n    for i in range(n):\n        k = list(h.keys())[i]\n        plt.subplot(1,n,i+1)\n        plt.plot(epoch_range, h[k][s], label='Training')\n        plt.plot(epoch_range, h['val_' + k][s], label='Validation')\n        plt.xlabel('Epoch'); plt.ylabel(k); plt.title(k)\n        plt.grid()\n        plt.legend()\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:25:48.568562Z","iopub.execute_input":"2022-04-14T06:25:48.569384Z","iopub.status.idle":"2022-04-14T06:25:48.578525Z","shell.execute_reply.started":"2022-04-14T06:25:48.569332Z","shell.execute_reply":"2022-04-14T06:25:48.577677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1, h2])\nvis_training(history, start=10)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:25:48.579628Z","iopub.execute_input":"2022-04-14T06:25:48.579923Z","iopub.status.idle":"2022-04-14T06:25:48.962616Z","shell.execute_reply.started":"2022-04-14T06:25:48.579888Z","shell.execute_reply":"2022-04-14T06:25:48.961952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training Run 3","metadata":{}},{"cell_type":"code","source":"K.set_value(cnn.optimizer.learning_rate, 0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:25:48.963745Z","iopub.execute_input":"2022-04-14T06:25:48.964668Z","iopub.status.idle":"2022-04-14T06:25:48.969564Z","shell.execute_reply.started":"2022-04-14T06:25:48.964623Z","shell.execute_reply":"2022-04-14T06:25:48.968609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\nh3 = cnn.fit(train_loader, steps_per_epoch = TR_STEPS, epochs = 10, validation_data = valid_loader, \n             validation_steps = VA_STEPS, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:25:48.974728Z","iopub.execute_input":"2022-04-14T06:25:48.975271Z","iopub.status.idle":"2022-04-14T06:28:40.005212Z","shell.execute_reply.started":"2022-04-14T06:25:48.975225Z","shell.execute_reply":"2022-04-14T06:28:40.004498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vis_training(h, start=1):\n    epoch_range = range(start, len(h['loss'])+1)\n    s = slice(start-1, None)\n\n    plt.figure(figsize=[14,4])\n\n    n = int(len(h.keys()) / 2)\n\n    for i in range(n):\n        k = list(h.keys())[i]\n        plt.subplot(1,n,i+1)\n        plt.plot(epoch_range, h[k][s], label='Training')\n        plt.plot(epoch_range, h['val_' + k][s], label='Validation')\n        plt.xlabel('Epoch'); plt.ylabel(k); plt.title(k)\n        plt.grid()\n        plt.legend()\n\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:28:40.009666Z","iopub.execute_input":"2022-04-14T06:28:40.010283Z","iopub.status.idle":"2022-04-14T06:28:40.019681Z","shell.execute_reply.started":"2022-04-14T06:28:40.010234Z","shell.execute_reply":"2022-04-14T06:28:40.018828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = merge_history([h1, h2, h3])\nvis_training(history, start=10)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:28:40.020957Z","iopub.execute_input":"2022-04-14T06:28:40.021264Z","iopub.status.idle":"2022-04-14T06:28:40.418320Z","shell.execute_reply.started":"2022-04-14T06:28:40.021212Z","shell.execute_reply":"2022-04-14T06:28:40.417644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Model","metadata":{}},{"cell_type":"code","source":"cnn.save(f'Freesound_Audio_EfficientNet_B0_v01.h5')","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:28:40.419565Z","iopub.execute_input":"2022-04-14T06:28:40.420146Z","iopub.status.idle":"2022-04-14T06:28:41.041227Z","shell.execute_reply.started":"2022-04-14T06:28:40.420101Z","shell.execute_reply":"2022-04-14T06:28:41.040392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Test DataFrame","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/freesound-audio-tagging/sample_submission.csv')\n\ntest_loader = DataGenerator(test, batch_size=64, shuffle=False, is_train=False)\n\nprobs = cnn.predict(test_loader)\nprint(probs.shape)","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:28:41.042716Z","iopub.execute_input":"2022-04-14T06:28:41.042973Z","iopub.status.idle":"2022-04-14T06:29:46.060609Z","shell.execute_reply.started":"2022-04-14T06:28:41.042938Z","shell.execute_reply":"2022-04-14T06:29:46.059870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(probs[0, :].round(2))","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:29:46.061867Z","iopub.execute_input":"2022-04-14T06:29:46.062196Z","iopub.status.idle":"2022-04-14T06:29:46.068647Z","shell.execute_reply.started":"2022-04-14T06:29:46.062154Z","shell.execute_reply":"2022-04-14T06:29:46.067953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submit Top 1 Prediction","metadata":{}},{"cell_type":"code","source":"submission_top1 = test.copy()\n\nN = len(test)\nfor i in range(N):\n    p = probs[i, :]\n    idx = np.argmax(p)\n    submission_top1.label[i] = labels[idx]\n\nsubmission_top1.to_csv('submission_top1.csv', index=False, header=True)\n\nsubmission_top1.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:29:46.070601Z","iopub.execute_input":"2022-04-14T06:29:46.071452Z","iopub.status.idle":"2022-04-14T06:29:46.857519Z","shell.execute_reply.started":"2022-04-14T06:29:46.071409Z","shell.execute_reply":"2022-04-14T06:29:46.856708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submit Top 3 Predictions","metadata":{}},{"cell_type":"code","source":"submission_top3 = test.copy()\n\nN = len(test)\nfor i in range(N):\n    p = probs[i, :]\n    idx = np.argsort(-p)[:3]\n    top3 = labels[idx]\n    submission_top3.label[i] = ' '.join(top3)\n\nsubmission_top3.to_csv('submission_top3.csv', index=False, header=True)\nsubmission_top3.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-14T06:29:46.859034Z","iopub.execute_input":"2022-04-14T06:29:46.859528Z","iopub.status.idle":"2022-04-14T06:29:47.770919Z","shell.execute_reply.started":"2022-04-14T06:29:46.859491Z","shell.execute_reply":"2022-04-14T06:29:47.770087Z"},"trusted":true},"execution_count":null,"outputs":[]}]}