{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-02T20:38:05.987877Z","iopub.execute_input":"2023-08-02T20:38:05.988267Z","iopub.status.idle":"2023-08-02T20:38:12.385698Z","shell.execute_reply.started":"2023-08-02T20:38:05.988235Z","shell.execute_reply":"2023-08-02T20:38:12.384588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/input/birdsong-recognition/","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:12.387709Z","iopub.execute_input":"2023-08-02T20:38:12.388857Z","iopub.status.idle":"2023-08-02T20:38:12.395015Z","shell.execute_reply.started":"2023-08-02T20:38:12.38882Z","shell.execute_reply":"2023-08-02T20:38:12.394006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\nimport IPython.display as ipd\n\n#チェロの音声データのうちの一つを読み込む\ndata, rate = librosa.load('/kaggle/input/birdsong-recognition/train_audio/yetvir/XC383356.mp3')\n\n#読み込んだデータを再生する\nipd.Audio(data = data * 2, rate = rate)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:12.396702Z","iopub.execute_input":"2023-08-02T20:38:12.397632Z","iopub.status.idle":"2023-08-02T20:38:21.838622Z","shell.execute_reply.started":"2023-08-02T20:38:12.397583Z","shell.execute_reply":"2023-08-02T20:38:21.837744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 訓練データのファイル名とラベル（鳥の種類）を取り出す","metadata":{}},{"cell_type":"code","source":"# csvファイル読み込み\ntrain = pd.read_csv('train.csv') # 訓練データの情報\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:21.849095Z","iopub.execute_input":"2023-08-02T20:38:21.849691Z","iopub.status.idle":"2023-08-02T20:38:22.324642Z","shell.execute_reply.started":"2023-08-02T20:38:21.84966Z","shell.execute_reply":"2023-08-02T20:38:22.323641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 鳥の名前がそれぞれ何個あるか\ntrain['ebird_code'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.3262Z","iopub.execute_input":"2023-08-02T20:38:22.326562Z","iopub.status.idle":"2023-08-02T20:38:22.342897Z","shell.execute_reply.started":"2023-08-02T20:38:22.326527Z","shell.execute_reply":"2023-08-02T20:38:22.341041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 簡単のために、ファイル名と鳥の名前だけにする\ntrain_df = train[['filename', 'ebird_code']]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.345863Z","iopub.execute_input":"2023-08-02T20:38:22.346173Z","iopub.status.idle":"2023-08-02T20:38:22.355751Z","shell.execute_reply.started":"2023-08-02T20:38:22.34614Z","shell.execute_reply":"2023-08-02T20:38:22.354711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.357359Z","iopub.execute_input":"2023-08-02T20:38:22.357756Z","iopub.status.idle":"2023-08-02T20:38:22.374267Z","shell.execute_reply.started":"2023-08-02T20:38:22.357722Z","shell.execute_reply":"2023-08-02T20:38:22.373192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ebird_codeをlabelに変更\ntrain_df = train_df.rename(columns={'ebird_code':'label'})\ntrain_df = train_df.rename(columns={'filename':'fname'})\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.375851Z","iopub.execute_input":"2023-08-02T20:38:22.376343Z","iopub.status.idle":"2023-08-02T20:38:22.394872Z","shell.execute_reply.started":"2023-08-02T20:38:22.376293Z","shell.execute_reply":"2023-08-02T20:38:22.393626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# カテゴリー変数を数値に変換\nlabels = pd.factorize(train_df['label'])[0]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.40148Z","iopub.execute_input":"2023-08-02T20:38:22.401771Z","iopub.status.idle":"2023-08-02T20:38:22.408421Z","shell.execute_reply.started":"2023-08-02T20:38:22.401746Z","shell.execute_reply":"2023-08-02T20:38:22.407314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.410325Z","iopub.execute_input":"2023-08-02T20:38:22.410644Z","iopub.status.idle":"2023-08-02T20:38:22.422996Z","shell.execute_reply.started":"2023-08-02T20:38:22.410613Z","shell.execute_reply":"2023-08-02T20:38:22.421888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 鳥の種類を数字に置き換えたcode_dfをデータフレームに\nlabel_df = pd.DataFrame(labels)\nlabel_df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.42648Z","iopub.execute_input":"2023-08-02T20:38:22.426838Z","iopub.status.idle":"2023-08-02T20:38:22.44115Z","shell.execute_reply.started":"2023-08-02T20:38:22.42681Z","shell.execute_reply":"2023-08-02T20:38:22.439936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 列名を「label」に変更\nlabel_df = label_df.rename(columns={0:'label'})\nlabel_df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.444558Z","iopub.execute_input":"2023-08-02T20:38:22.444806Z","iopub.status.idle":"2023-08-02T20:38:22.455521Z","shell.execute_reply.started":"2023-08-02T20:38:22.444784Z","shell.execute_reply":"2023-08-02T20:38:22.454518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.45664Z","iopub.execute_input":"2023-08-02T20:38:22.457107Z","iopub.status.idle":"2023-08-02T20:38:22.473111Z","shell.execute_reply.started":"2023-08-02T20:38:22.457072Z","shell.execute_reply":"2023-08-02T20:38:22.472101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_dfにlabel_dfを追加\ntrain_df['num'] = label_df\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.474925Z","iopub.execute_input":"2023-08-02T20:38:22.47568Z","iopub.status.idle":"2023-08-02T20:38:22.49401Z","shell.execute_reply.started":"2023-08-02T20:38:22.475641Z","shell.execute_reply":"2023-08-02T20:38:22.492171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 訓練データを作る","metadata":{}},{"cell_type":"code","source":"#ファイル名から音声データを読み込む関数を定義\nimport librosa\n\nsampling_rate = 22050\n\ndef _load_files(df):\n  result = []\n\n  for index, data in df.iterrows():\n        file_path = '/kaggle/input/birdsong-recognition/train_audio/' + data['label']+ '/' + data['fname']\n        # 改良：　最初から3秒間のみ読み込むことで、メモリの使用量を減らす\n        data, _ = librosa.load(file_path, sr=sampling_rate, duration=3)\n        \n        # 改良: もし3秒未満なら削除\n        if len(data) / sampling_rate < 3:\n            df.drop(df.index[[index]])\n            continue\n        \n        result.append(data)\n        if index % 100 == 0:\n         print(index)\n        \n\n  return result","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.495316Z","iopub.execute_input":"2023-08-02T20:38:22.49561Z","iopub.status.idle":"2023-08-02T20:38:22.503671Z","shell.execute_reply.started":"2023-08-02T20:38:22.495584Z","shell.execute_reply":"2023-08-02T20:38:22.502663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.505491Z","iopub.execute_input":"2023-08-02T20:38:22.506293Z","iopub.status.idle":"2023-08-02T20:38:22.51813Z","shell.execute_reply.started":"2023-08-02T20:38:22.506253Z","shell.execute_reply":"2023-08-02T20:38:22.516827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 改良: データ数を5000に増やす\nresult = _load_files(train_df.head(5000))","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:38:22.520228Z","iopub.execute_input":"2023-08-02T20:38:22.520792Z","iopub.status.idle":"2023-08-02T20:39:40.56022Z","shell.execute_reply.started":"2023-08-02T20:38:22.520754Z","shell.execute_reply":"2023-08-02T20:39:40.559219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(result)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:40.565148Z","iopub.execute_input":"2023-08-02T20:39:40.567556Z","iopub.status.idle":"2023-08-02T20:39:40.577923Z","shell.execute_reply.started":"2023-08-02T20:39:40.567519Z","shell.execute_reply":"2023-08-02T20:39:40.576859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#読み込んだデータを再生する\nimport IPython.display as ipd\n\nipd.Audio(data = result[0], rate = rate)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:40.582656Z","iopub.execute_input":"2023-08-02T20:39:40.584807Z","iopub.status.idle":"2023-08-02T20:39:40.605141Z","shell.execute_reply.started":"2023-08-02T20:39:40.584772Z","shell.execute_reply":"2023-08-02T20:39:40.604253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"また、データの標準化（平均値を0、分散を１に補正）も行う","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nscaler = scaler.fit(result) # 平均μと分散σを計算\nX_train = scaler.transform(result) # 平均0、分散1になるよう変換（標準化）","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:40.606607Z","iopub.execute_input":"2023-08-02T20:39:40.608323Z","iopub.status.idle":"2023-08-02T20:39:45.770462Z","shell.execute_reply.started":"2023-08-02T20:39:40.60829Z","shell.execute_reply":"2023-08-02T20:39:45.769285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ラベルをone-hot encordingする","metadata":{}},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:45.772001Z","iopub.execute_input":"2023-08-02T20:39:45.772492Z","iopub.status.idle":"2023-08-02T20:39:45.787538Z","shell.execute_reply.started":"2023-08-02T20:39:45.772447Z","shell.execute_reply":"2023-08-02T20:39:45.786324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\nY_train = to_categorical(train_df['num'].head(len(result))) # 読み込んだデータ分だけにする","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:45.789073Z","iopub.execute_input":"2023-08-02T20:39:45.79018Z","iopub.status.idle":"2023-08-02T20:39:55.139606Z","shell.execute_reply.started":"2023-08-02T20:39:45.790122Z","shell.execute_reply":"2023-08-02T20:39:55.138451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:55.141149Z","iopub.execute_input":"2023-08-02T20:39:55.141945Z","iopub.status.idle":"2023-08-02T20:39:55.15064Z","shell.execute_reply.started":"2023-08-02T20:39:55.141893Z","shell.execute_reply":"2023-08-02T20:39:55.149451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# one-hot encordingされているか確認\nY_train","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:55.152313Z","iopub.execute_input":"2023-08-02T20:39:55.153504Z","iopub.status.idle":"2023-08-02T20:39:55.164086Z","shell.execute_reply.started":"2023-08-02T20:39:55.153468Z","shell.execute_reply":"2023-08-02T20:39:55.162958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# いくつクラスがあるのか確認\nlen(Y_train[0])","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:55.165794Z","iopub.execute_input":"2023-08-02T20:39:55.166198Z","iopub.status.idle":"2023-08-02T20:39:55.17614Z","shell.execute_reply.started":"2023-08-02T20:39:55.166147Z","shell.execute_reply":"2023-08-02T20:39:55.174878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## モデルを構築","metadata":{}},{"cell_type":"markdown","source":"今回はAI教科書第9章に倣い、次はCNNを構築","metadata":{}},{"cell_type":"code","source":"# TPUを使う\n# detect and init the TPU\n# tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# instantiate a distribution strategy\n# tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:55.177648Z","iopub.execute_input":"2023-08-02T20:39:55.178393Z","iopub.status.idle":"2023-08-02T20:39:55.190988Z","shell.execute_reply.started":"2023-08-02T20:39:55.178359Z","shell.execute_reply":"2023-08-02T20:39:55.189846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense, LSTM, Dropout,Bidirectional\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\n\naudio_duration = 3\nsampling_rate = 22050\naudio_length = sampling_rate * audio_duration\n\ndef create_lstm_model():\n  input_shape = (audio_length, 1)\n\n  #モデルの構築\n  model_lstm = Sequential()\n  model_lstm.add(LSTM(64, return_sequences=True, dropout=0.3 ,input_shape=input_shape))\n  model_lstm.add(LSTM(64, return_sequences=False, dropout=0.3))\n  model_lstm.add(Dense(units=59, activation=\"softmax\")) # 264個クラスがあるが、訓練データに59個分しかないので、59\n\n  # 損失関数はcategorical_crossentropy（多クラス分類問題なので）\n  model_lstm.compile(loss=\"categorical_crossentropy\", optimizer=Adam(0.001), metrics=[\"acc\"])\n    \n  return model_lstm\n\nmodel_lstm = create_lstm_model()\n#モデルの構造を表示する\nmodel_lstm.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:39:55.200306Z","iopub.execute_input":"2023-08-02T20:39:55.200582Z","iopub.status.idle":"2023-08-02T20:40:00.392408Z","shell.execute_reply.started":"2023-08-02T20:39:55.200558Z","shell.execute_reply":"2023-08-02T20:40:00.391607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#学習開始(とりあえずエポック数3)\nhistory = model_lstm.fit(X_train, Y_train, batch_size=32, epochs=3, validation_split=0.1, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T20:40:01.038945Z","iopub.execute_input":"2023-08-02T20:40:01.040386Z","iopub.status.idle":"2023-08-02T21:05:30.79487Z","shell.execute_reply.started":"2023-08-02T20:40:01.04035Z","shell.execute_reply":"2023-08-02T21:05:30.793583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#モデルの重みの保存\nmodel_lstm.save_weights('/kaggle/working/saved_models/model_lstm_weights')","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:05:31.467562Z","iopub.execute_input":"2023-08-02T21:05:31.467902Z","iopub.status.idle":"2023-08-02T21:05:31.599373Z","shell.execute_reply.started":"2023-08-02T21:05:31.46787Z","shell.execute_reply":"2023-08-02T21:05:31.598392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNNで予測する","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n#評価関数と精度のグラフ表示\nfig, ax = plt.subplots(2,1)\nax[0].plot(history.history[\"loss\"], color=\"b\", label=\"Training Loss\")\nax[0].plot(history.history[\"val_loss\"], color=\"g\", label=\"Validation Loss\")\nax[0].legend()\n\nax[1].plot(history.history[\"acc\"], color=\"b\", label=\"Training Accuracy\")\nax[1].plot(history.history[\"val_acc\"], color=\"g\", label=\"Validation Accuracy\")\nax[1].legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:05:31.600852Z","iopub.execute_input":"2023-08-02T21:05:31.601218Z","iopub.status.idle":"2023-08-02T21:05:32.094034Z","shell.execute_reply.started":"2023-08-02T21:05:31.601185Z","shell.execute_reply":"2023-08-02T21:05:32.093067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# csvファイル読み込み\ntest = pd.read_csv('test.csv') # テストデータの情報\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:05:32.095511Z","iopub.execute_input":"2023-08-02T21:05:32.09583Z","iopub.status.idle":"2023-08-02T21:05:32.113197Z","shell.execute_reply.started":"2023-08-02T21:05:32.095799Z","shell.execute_reply":"2023-08-02T21:05:32.112168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# モデルを読み込む\nmodel_lstm = create_lstm_model()\nmodel_lstm.load_weights(\"/kaggle/working/saved_models/model_lstm_weights\")","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:05:32.11482Z","iopub.execute_input":"2023-08-02T21:05:32.115195Z","iopub.status.idle":"2023-08-02T21:05:32.67294Z","shell.execute_reply.started":"2023-08-02T21:05:32.115161Z","shell.execute_reply":"2023-08-02T21:05:32.672032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータを読み込む(まずはサンプル)\nimport pandas as pd\n\naudio_file_path = \"/kaggle/input/birdsong-recognition/example_test_audio\"\nexample_df = pd.read_csv(\"/kaggle/input/birdsong-recognition/example_test_audio_summary.csv\")\nexample_df[\"filename\"] = [ \"BLKFR-10-CPL_20190611_093000.pt540\" if filename==\"BLKFR-10-CPL\" else \"ORANGE-7-CAP_20190606_093000.pt623\" for filename in example_df[\"filename\"]]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:05:32.674269Z","iopub.execute_input":"2023-08-02T21:05:32.674694Z","iopub.status.idle":"2023-08-02T21:05:32.686248Z","shell.execute_reply.started":"2023-08-02T21:05:32.67466Z","shell.execute_reply":"2023-08-02T21:05:32.685276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:05:32.687571Z","iopub.execute_input":"2023-08-02T21:05:32.688049Z","iopub.status.idle":"2023-08-02T21:05:32.695367Z","shell.execute_reply.started":"2023-08-02T21:05:32.688017Z","shell.execute_reply":"2023-08-02T21:05:32.69442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\n\nexample_result = []\n\nfor index,data in example_df.iterrows():\n    filename = '{}/{}.mp3'.format(audio_file_path, data.filename)\n    data, _ = librosa.load(filename, duration = 3)\n    example_result.append(data)\n    #if index == 10: # 今回は10個だけ読み込む\n    #    break","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:16:20.775904Z","iopub.execute_input":"2023-08-02T21:16:20.776286Z","iopub.status.idle":"2023-08-02T21:16:21.18834Z","shell.execute_reply.started":"2023-08-02T21:16:20.776255Z","shell.execute_reply":"2023-08-02T21:16:21.187025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(example_result)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:16:22.399319Z","iopub.execute_input":"2023-08-02T21:16:22.40001Z","iopub.status.idle":"2023-08-02T21:16:22.406231Z","shell.execute_reply.started":"2023-08-02T21:16:22.399978Z","shell.execute_reply":"2023-08-02T21:16:22.405263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データの標準化\nfrom sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nscaler = scaler.fit(example_result) # 平均μと分散σを計算\nX_test = scaler.transform(example_result) # 平均0、分散1になるよう変換（標準化）","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:16:51.49182Z","iopub.execute_input":"2023-08-02T21:16:51.492273Z","iopub.status.idle":"2023-08-02T21:16:51.672792Z","shell.execute_reply.started":"2023-08-02T21:16:51.492235Z","shell.execute_reply":"2023-08-02T21:16:51.671612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:16:52.689279Z","iopub.execute_input":"2023-08-02T21:16:52.690212Z","iopub.status.idle":"2023-08-02T21:16:52.696508Z","shell.execute_reply.started":"2023-08-02T21:16:52.690167Z","shell.execute_reply":"2023-08-02T21:16:52.695572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model_lstm.predict(X_test, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:16:53.669829Z","iopub.execute_input":"2023-08-02T21:16:53.670231Z","iopub.status.idle":"2023-08-02T21:17:04.071778Z","shell.execute_reply.started":"2023-08-02T21:16:53.6702Z","shell.execute_reply":"2023-08-02T21:17:04.070747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels_num = np.array([np.argmax(pred) for pred in predictions])","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:07.060115Z","iopub.execute_input":"2023-08-02T21:17:07.060486Z","iopub.status.idle":"2023-08-02T21:17:07.068555Z","shell.execute_reply.started":"2023-08-02T21:17:07.060457Z","shell.execute_reply":"2023-08-02T21:17:07.067238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels_num","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:08.955839Z","iopub.execute_input":"2023-08-02T21:17:08.956208Z","iopub.status.idle":"2023-08-02T21:17:08.963935Z","shell.execute_reply.started":"2023-08-02T21:17:08.956179Z","shell.execute_reply":"2023-08-02T21:17:08.962666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"予測がうまくいっていなさそう","metadata":{}},{"cell_type":"code","source":"# 数字のラベルを鳥の種類に戻す\ntrain['ebird_code'].value_counts().index","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:11.521083Z","iopub.execute_input":"2023-08-02T21:17:11.521769Z","iopub.status.idle":"2023-08-02T21:17:11.531732Z","shell.execute_reply.started":"2023-08-02T21:17:11.521737Z","shell.execute_reply":"2023-08-02T21:17:11.530812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = train['ebird_code'].value_counts().index[pred_labels_num]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:13.137079Z","iopub.execute_input":"2023-08-02T21:17:13.137749Z","iopub.status.idle":"2023-08-02T21:17:13.145855Z","shell.execute_reply.started":"2023-08-02T21:17:13.137715Z","shell.execute_reply":"2023-08-02T21:17:13.144813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:13.855145Z","iopub.execute_input":"2023-08-02T21:17:13.855541Z","iopub.status.idle":"2023-08-02T21:17:13.862942Z","shell.execute_reply.started":"2023-08-02T21:17:13.85551Z","shell.execute_reply":"2023-08-02T21:17:13.861709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_labels = []\nfor lab in example_df['birds']:\n    label = str(lab).split()[0]\n    actual_labels.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:15.337034Z","iopub.execute_input":"2023-08-02T21:17:15.337962Z","iopub.status.idle":"2023-08-02T21:17:15.344166Z","shell.execute_reply.started":"2023-08-02T21:17:15.337902Z","shell.execute_reply":"2023-08-02T21:17:15.342663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# actual_labels = actual_labels[0:11]","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:16.095568Z","iopub.execute_input":"2023-08-02T21:17:16.095935Z","iopub.status.idle":"2023-08-02T21:17:16.100479Z","shell.execute_reply.started":"2023-08-02T21:17:16.095906Z","shell.execute_reply":"2023-08-02T21:17:16.099325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(actual_labels)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:16.930147Z","iopub.execute_input":"2023-08-02T21:17:16.930492Z","iopub.status.idle":"2023-08-02T21:17:16.936863Z","shell.execute_reply.started":"2023-08-02T21:17:16.930464Z","shell.execute_reply":"2023-08-02T21:17:16.935789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:17.726843Z","iopub.execute_input":"2023-08-02T21:17:17.727222Z","iopub.status.idle":"2023-08-02T21:17:17.733383Z","shell.execute_reply.started":"2023-08-02T21:17:17.727193Z","shell.execute_reply":"2023-08-02T21:17:17.732283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#正答率の算出\ntmp = actual_labels == pred_labels\ntmp.sum()/len(tmp)","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:17:18.671445Z","iopub.execute_input":"2023-08-02T21:17:18.671769Z","iopub.status.idle":"2023-08-02T21:17:18.679101Z","shell.execute_reply.started":"2023-08-02T21:17:18.671742Z","shell.execute_reply":"2023-08-02T21:17:18.677974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submissionしてみる\naudio_file_path = \"/kaggle/input/birdsong-recognition/test_audio\"\ntest_df = pd.read_csv(\"/kaggle/input/birdsong-recognition/test.csv\")\nsubmission_df = pd.read_csv(\"/kaggle/input/birdsong-recognition/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-02T21:19:38.406587Z","iopub.execute_input":"2023-08-02T21:19:38.406948Z","iopub.status.idle":"2023-08-02T21:19:38.421874Z","shell.execute_reply.started":"2023-08-02T21:19:38.40692Z","shell.execute_reply":"2023-08-02T21:19:38.420896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists(audio_file_path):\n    for in test_df\n        filename = '{}/{}.mp3'.format(audio_file_path, row.filename)\n    ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df[[\"row_id\",\"birds\"]].to_csv('/kaggle/working/submission.csv', index=False)\nsubmission_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}