{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport glob\nimport random\nimport torch\nimport sys\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport IPython.display\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nplt.style.use('ggplot')\nimport librosa\nimport librosa.display\nfrom matplotlib.colorbar import Colorbar\nimport IPython.display as ipd\nimport librosa.display\nfrom tqdm import tqdm\nfrom sklearn.preprocessing import OneHotEncoder, StandardScaler\nfrom sklearn.model_selection import train_test_split\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:34:08.026924Z","iopub.execute_input":"2024-04-19T12:34:08.028221Z","iopub.status.idle":"2024-04-19T12:34:15.006763Z","shell.execute_reply.started":"2024-04-19T12:34:08.028174Z","shell.execute_reply":"2024-04-19T12:34:15.005439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:34:21.247156Z","iopub.execute_input":"2024-04-19T12:34:21.247989Z","iopub.status.idle":"2024-04-19T12:34:21.476652Z","shell.execute_reply.started":"2024-04-19T12:34:21.247954Z","shell.execute_reply":"2024-04-19T12:34:21.475602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_audio(filename, audio_data=None, sample_rate=None):\n    if audio_data is None:\n        audio_data, sample_rate = librosa.load(filename)\n\n    ipd.display(ipd.Audio(audio_data, rate=sample_rate))\n\n    fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(16, 12))\n\n    axs[0].plot(audio_data)\n    axs[0].set_title('Waveform')\n\n    spectrogram = librosa.amplitude_to_db(librosa.stft(audio_data), ref=np.max)\n    librosa.display.specshow(spectrogram, sr=sample_rate, x_axis='time', y_axis='hz', ax=axs[1])\n    axs[1].set_title('Spectrogram')","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:29:06.049926Z","iopub.execute_input":"2024-04-19T06:29:06.050377Z","iopub.status.idle":"2024-04-19T06:29:06.056854Z","shell.execute_reply.started":"2024-04-19T06:29:06.050346Z","shell.execute_reply":"2024-04-19T06:29:06.055953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath = '/kaggle/input/birdclef-2024/train_audio/asbfly/XC134896.ogg'","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:29:09.013099Z","iopub.execute_input":"2024-04-19T06:29:09.013756Z","iopub.status.idle":"2024-04-19T06:29:09.018090Z","shell.execute_reply.started":"2024-04-19T06:29:09.013724Z","shell.execute_reply":"2024-04-19T06:29:09.016862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_audio(filepath)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:29:11.831925Z","iopub.execute_input":"2024-04-19T06:29:11.832364Z","iopub.status.idle":"2024-04-19T06:29:18.932604Z","shell.execute_reply.started":"2024-04-19T06:29:11.832334Z","shell.execute_reply":"2024-04-19T06:29:18.930697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_path = '/kaggle/input/birdclef-2024/train_audio/asbfly/XC134896.ogg'\ny, sr = librosa.load(audio_path)\ndisplay(IPython.display.Audio(y, rate=sr))","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:29:23.127628Z","iopub.execute_input":"2024-04-19T06:29:23.128286Z","iopub.status.idle":"2024-04-19T06:29:23.213283Z","shell.execute_reply.started":"2024-04-19T06:29:23.128228Z","shell.execute_reply":"2024-04-19T06:29:23.211989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"librosa.display.waveshow(y, sr=sr, color=\"red\")","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:29:27.525643Z","iopub.execute_input":"2024-04-19T06:29:27.526367Z","iopub.status.idle":"2024-04-19T06:29:27.996669Z","shell.execute_reply.started":"2024-04-19T06:29:27.526330Z","shell.execute_reply":"2024-04-19T06:29:27.995388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D = librosa.stft(y)  # STFT\nS, phase = librosa.magphase(D)  # 複素数を強度と位相へ変換\nSdb = librosa.amplitude_to_db(S)  # 強度をdb単位へ変換\nlibrosa.display.specshow(Sdb, sr=sr, x_axis='time', y_axis='log') ","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:29:31.778238Z","iopub.execute_input":"2024-04-19T06:29:31.778705Z","iopub.status.idle":"2024-04-19T06:29:33.611639Z","shell.execute_reply.started":"2024-04-19T06:29:31.778667Z","shell.execute_reply":"2024-04-19T06:29:33.610568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_to_spectrogram(file_path):\n    # Load the audio file\n    y, sr = librosa.load(file_path, sr=None)\n    \n    # Calculate the spectrogram\n    spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)\n    \n    return spectrogram\n\ndef preprocess_sounds(main_folder):\n    # Create empty lists to store spectrograms and labels\n    spectrograms = []\n    labels = []\n    counter = 0\n    # Iterate over each subfolder in the main folder\n    for folder_name in os.listdir(main_folder):\n        subfolder_path = os.path.join(main_folder, folder_name)\n        if os.path.isdir(subfolder_path) and (len(os.listdir(subfolder_path)) >= 100):\n            label = folder_name\n            # Iterate over each file in the subfolder\n            conter_image = 0\n            for file_name in tqdm(os.listdir(subfolder_path), desc='Processing {}'.format(folder_name)):\n                if file_name.endswith('.ogg'):  # Adjust extension if needed\n                    file_path = os.path.join(subfolder_path, file_name)\n                    spectrogram = convert_to_spectrogram(file_path)\n                    \n                    # Append the spectrogram to the list\n                    spectrograms.append(spectrogram)\n                    # Append the label to the labels list\n                    labels.append(label)\n                    conter_image +=1\n                if conter_image > 100:\n                    break\n        counter+=1\n        if counter == 10 :\n            break\n\n    return spectrograms, labels","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:34:32.010184Z","iopub.execute_input":"2024-04-19T12:34:32.010651Z","iopub.status.idle":"2024-04-19T12:34:32.021543Z","shell.execute_reply.started":"2024-04-19T12:34:32.010616Z","shell.execute_reply":"2024-04-19T12:34:32.020158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_path = '/kaggle/input/birdclef-2024/train_audio'\n\n# Preprocess sounds and store spectrograms and labels\nspectrograms, labels = preprocess_sounds(folder_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:34:34.861748Z","iopub.execute_input":"2024-04-19T12:34:34.862842Z","iopub.status.idle":"2024-04-19T12:36:37.870492Z","shell.execute_reply.started":"2024-04-19T12:34:34.862792Z","shell.execute_reply":"2024-04-19T12:36:37.868857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrograms","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"array_shapes = [arr.shape[0] for arr in spectrograms]\ntarget_shape = (224, 224)  # Replace with your desired shape\n\n# Resize each array in the list to the target shape\nresized_spectrograms = []\nfor arr in spectrograms:\n    resized_arr = np.zeros(target_shape) \n    resized_arr[:min(target_shape[0], arr.shape[0]), :min(target_shape[1], arr.shape[1])] = arr[:min(target_shape[0], arr.shape[0]), :min(target_shape[1], arr.shape[1])]\n    resized_spectrograms.append(resized_arr)\n\nspectrograms_array = np.array(resized_spectrograms)\nlabels_array = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:12.426026Z","iopub.execute_input":"2024-04-19T12:37:12.427664Z","iopub.status.idle":"2024-04-19T12:37:12.626479Z","shell.execute_reply.started":"2024-04-19T12:37:12.427605Z","shell.execute_reply":"2024-04-19T12:37:12.625128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.savez('spectrograms.npz', spectrograms=spectrograms_array)\npd.DataFrame(labels_array, columns=['label']).to_csv('labels.csv', index=False)\nloaded_data = np.load('spectrograms.npz')\nloaded_spectrograms = loaded_data['spectrograms']\nloaded_labels = pd.read_csv('labels.csv')['label']","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:15.030349Z","iopub.execute_input":"2024-04-19T12:37:15.030877Z","iopub.status.idle":"2024-04-19T12:37:15.754161Z","shell.execute_reply.started":"2024-04-19T12:37:15.030842Z","shell.execute_reply":"2024-04-19T12:37:15.752597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(loaded_labels.value_counts())","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:18.210792Z","iopub.execute_input":"2024-04-19T12:37:18.211234Z","iopub.status.idle":"2024-04-19T12:37:18.221925Z","shell.execute_reply.started":"2024-04-19T12:37:18.211200Z","shell.execute_reply":"2024-04-19T12:37:18.220217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = OneHotEncoder(categories='auto', sparse=False)\nlabels = labels_array.reshape(-1, 1)\n# Fit and transform the training labels\nonehot_labels = encoder.fit_transform(labels)\nprint(labels_array.shape)\nprint(labels.shape)\nprint(onehot_labels.shape)\nunique_labels = np.unique(labels)\nunique_onehot_labels = np.unique(onehot_labels, axis=0)\nprint(unique_onehot_labels)\n# Print the shape of the one-hot encoded labels\nonehot_as_tuples = [tuple(row) for row in unique_onehot_labels]\nprint(onehot_as_tuples)\nzipped_arrays = zip(onehot_as_tuples, unique_labels)\nlabel_dic = {key: value for key, value in zipped_arrays}    \n# label_dic = {x:y for x, y in zip(unique_onehot_labels, unique_labels)}\nprint(label_dic)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:20.575785Z","iopub.execute_input":"2024-04-19T12:37:20.576181Z","iopub.status.idle":"2024-04-19T12:37:20.598474Z","shell.execute_reply.started":"2024-04-19T12:37:20.576152Z","shell.execute_reply":"2024-04-19T12:37:20.596872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nflattened_spectrograms = loaded_spectrograms.reshape((loaded_spectrograms.shape[0], -1))\nflattened_spectrograms_standardized = scaler.fit_transform(flattened_spectrograms)\nstandardized_spectrograms = flattened_spectrograms_standardized.reshape(loaded_spectrograms.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:23.635933Z","iopub.execute_input":"2024-04-19T12:37:23.636327Z","iopub.status.idle":"2024-04-19T12:37:24.029260Z","shell.execute_reply.started":"2024-04-19T12:37:23.636297Z","shell.execute_reply":"2024-04-19T12:37:24.027727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardized_spectrograms.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:26.806943Z","iopub.execute_input":"2024-04-19T12:37:26.807335Z","iopub.status.idle":"2024-04-19T12:37:26.816808Z","shell.execute_reply.started":"2024-04-19T12:37:26.807306Z","shell.execute_reply":"2024-04-19T12:37:26.815438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardized_spectrograms = np.expand_dims(standardized_spectrograms, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:28.380935Z","iopub.execute_input":"2024-04-19T12:37:28.381424Z","iopub.status.idle":"2024-04-19T12:37:28.390294Z","shell.execute_reply.started":"2024-04-19T12:37:28.381375Z","shell.execute_reply":"2024-04-19T12:37:28.388974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardized_spectrograms = np.repeat(standardized_spectrograms, 3, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:29.897896Z","iopub.execute_input":"2024-04-19T12:37:29.898511Z","iopub.status.idle":"2024-04-19T12:37:30.313967Z","shell.execute_reply.started":"2024-04-19T12:37:29.898468Z","shell.execute_reply":"2024-04-19T12:37:30.312743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"standardized_spectrograms.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:32.241592Z","iopub.execute_input":"2024-04-19T12:37:32.242032Z","iopub.status.idle":"2024-04-19T12:37:32.250304Z","shell.execute_reply.started":"2024-04-19T12:37:32.242000Z","shell.execute_reply":"2024-04-19T12:37:32.249009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spectrograms_train, spectrograms_test, labels_train, labels_test = train_test_split(standardized_spectrograms, onehot_labels, test_size=0.2, random_state=42)\n\n# Print the shapes of the training and testing sets\nprint(\"Training spectrograms shape:\", len(spectrograms_train))\nprint(spectrograms_train.shape)\nprint(\"Testing spectrograms shape:\", len(spectrograms_test))\nprint(spectrograms_test.shape)\nprint(\"Training labels shape:\", len(labels_train))\nprint(labels_train.shape)\nprint(\"Testing labels shape:\", len(labels_test))\nprint(labels_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:34.816145Z","iopub.execute_input":"2024-04-19T12:37:34.816581Z","iopub.status.idle":"2024-04-19T12:37:35.255916Z","shell.execute_reply.started":"2024-04-19T12:37:34.816550Z","shell.execute_reply":"2024-04-19T12:37:35.254370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.utils import to_categorical\n\nmodel = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:37:37.482078Z","iopub.execute_input":"2024-04-19T12:37:37.482544Z","iopub.status.idle":"2024-04-19T12:37:56.679229Z","shell.execute_reply.started":"2024-04-19T12:37:37.482511Z","shell.execute_reply":"2024-04-19T12:37:56.677861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense\nfor layer in model.layers:\n    layer.trainable = False\nx = model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(128, activation='relu')(x)\npredictions = Dense(num_classes, activation='softmax')(x)  # Adjust num_classes\n\n# Create the final model\nmodel = Model(inputs=model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:38:00.063593Z","iopub.execute_input":"2024-04-19T12:38:00.064373Z","iopub.status.idle":"2024-04-19T12:38:00.142145Z","shell.execute_reply.started":"2024-04-19T12:38:00.064338Z","shell.execute_reply":"2024-04-19T12:38:00.141064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(spectrograms_train, labels_train, batch_size=16, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:38:03.887523Z","iopub.execute_input":"2024-04-19T12:38:03.888268Z","iopub.status.idle":"2024-04-19T12:44:49.493315Z","shell.execute_reply.started":"2024-04-19T12:38:03.888232Z","shell.execute_reply":"2024-04-19T12:44:49.491545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ogg_file_paths ='/kaggle/input/birdclef-2024/unlabeled_soundscapes'\ntarget_shape = (224, 224) \npredict_spectrogram = []\nconter_image = 0\nfor file_name in tqdm(os.listdir(ogg_file_paths)):\n    if file_name.endswith('.ogg'):  # Adjust extension if needed\n        file_path = os.path.join(ogg_file_paths, file_name)\n        spectrogram = convert_to_spectrogram(file_path)\n        resized_arr = np.zeros(target_shape) \n        resized_arr[:min(target_shape[0], spectrogram.shape[0]), :min(target_shape[1], spectrogram.shape[1])] = arr[:min(target_shape[0], spectrogram.shape[0]), :min(target_shape[1], spectrogram.shape[1])]\n        predict_spectrogram.append(resized_arr)\n        conter_image += 1\n        if conter_image > 3000:\n            break\n","metadata":{"execution":{"iopub.status.busy":"2024-04-19T12:46:38.017659Z","iopub.execute_input":"2024-04-19T12:46:38.018120Z","iopub.status.idle":"2024-04-19T13:25:55.445233Z","shell.execute_reply.started":"2024-04-19T12:46:38.018087Z","shell.execute_reply":"2024-04-19T13:25:55.441075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_spectrogram_array = np.array(predict_spectrogram)\npredict_spectrogram_array = np.expand_dims(predict_spectrogram_array, axis=-1)\npredict_spectrogram_array = np.repeat(predict_spectrogram_array, 3, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T13:38:49.879923Z","iopub.execute_input":"2024-04-19T13:38:49.880584Z","iopub.status.idle":"2024-04-19T13:38:55.637964Z","shell.execute_reply.started":"2024-04-19T13:38:49.880540Z","shell.execute_reply":"2024-04-19T13:38:55.636423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predict_spectrogram_array.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-19T06:27:21.155878Z","iopub.execute_input":"2024-04-19T06:27:21.156350Z","iopub.status.idle":"2024-04-19T06:27:21.163468Z","shell.execute_reply.started":"2024-04-19T06:27:21.156316Z","shell.execute_reply":"2024-04-19T06:27:21.162421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = model.predict(predict_spectrogram_array)","metadata":{"execution":{"iopub.status.busy":"2024-04-19T13:39:02.468976Z","iopub.execute_input":"2024-04-19T13:39:02.469452Z","iopub.status.idle":"2024-04-19T13:44:32.633192Z","shell.execute_reply.started":"2024-04-19T13:39:02.469416Z","shell.execute_reply":"2024-04-19T13:44:32.631865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted =  [[1.0 if val >= max(row) else 0.0 for val in row] for row in predicted]","metadata":{"execution":{"iopub.status.busy":"2024-04-19T13:44:49.782561Z","iopub.execute_input":"2024-04-19T13:44:49.783005Z","iopub.status.idle":"2024-04-19T13:44:49.830716Z","shell.execute_reply.started":"2024-04-19T13:44:49.782974Z","shell.execute_reply":"2024-04-19T13:44:49.829487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_result = [label_dic[tuple(x)] for x in predicted]","metadata":{"execution":{"iopub.status.busy":"2024-04-19T13:44:52.252804Z","iopub.execute_input":"2024-04-19T13:44:52.253200Z","iopub.status.idle":"2024-04-19T13:44:52.262111Z","shell.execute_reply.started":"2024-04-19T13:44:52.253170Z","shell.execute_reply":"2024-04-19T13:44:52.260774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_result_unique = np.unique(predicted_result)\npredicted_result_unique","metadata":{"execution":{"iopub.status.busy":"2024-04-19T13:44:57.709217Z","iopub.execute_input":"2024-04-19T13:44:57.709649Z","iopub.status.idle":"2024-04-19T13:44:57.721773Z","shell.execute_reply.started":"2024-04-19T13:44:57.709614Z","shell.execute_reply":"2024-04-19T13:44:57.720563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submit = pd.read_csv(\"/kaggle/input/birdclef-2024/sample_submission.csv\")\nsubmit = pd.DataFrame(columns=sample_submit.columns)\n\nsubmit","metadata":{"execution":{"iopub.status.busy":"2024-04-15T06:35:36.160762Z","iopub.execute_input":"2024-04-15T06:35:36.161232Z","iopub.status.idle":"2024-04-15T06:35:36.204850Z","shell.execute_reply.started":"2024-04-15T06:35:36.161197Z","shell.execute_reply":"2024-04-15T06:35:36.203618Z"},"trusted":true},"execution_count":null,"outputs":[]}]}