{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":61103,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":51081}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!python3 -m venv venv\n!source venv/bin/activate","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-05T08:25:58.667704Z","iopub.execute_input":"2024-06-05T08:25:58.668004Z","iopub.status.idle":"2024-06-05T08:26:02.804837Z","shell.execute_reply.started":"2024-06-05T08:25:58.667977Z","shell.execute_reply":"2024-06-05T08:26:02.803633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torch\n!pip install datasets[audio]\n!pip install evaluate\n!pip install transformers[torch]\n!pip install librosa","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:26:02.806644Z","iopub.execute_input":"2024-06-05T08:26:02.806934Z","iopub.status.idle":"2024-06-05T08:27:06.226449Z","shell.execute_reply.started":"2024-06-05T08:26:02.806905Z","shell.execute_reply":"2024-06-05T08:27:06.225566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import librosa\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nimport matplotlib\n\nimport IPython.display as ipd\nimport os\nfrom scipy.signal import butter, sosfilt\n\nfrom torch.utils.data import Dataset","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:27:06.227858Z","iopub.execute_input":"2024-06-05T08:27:06.228192Z","iopub.status.idle":"2024-06-05T08:27:11.656546Z","shell.execute_reply.started":"2024-06-05T08:27:06.22816Z","shell.execute_reply":"2024-06-05T08:27:11.655586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scipy noisereduce soundfile\n","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:27:11.659354Z","iopub.execute_input":"2024-06-05T08:27:11.659937Z","iopub.status.idle":"2024-06-05T08:27:24.570463Z","shell.execute_reply.started":"2024-06-05T08:27:11.659902Z","shell.execute_reply":"2024-06-05T08:27:24.569266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio = '/kaggle/input/birdclef-2024/train_audio'\ntrain_meta = '/kaggle/input/birdclef-2024/train_metadata.csv'\nTax = '/kaggle/input/birdclef-2024/eBird_Taxonomy_v2021.csv'\n","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:27:24.572274Z","iopub.execute_input":"2024-06-05T08:27:24.572729Z","iopub.status.idle":"2024-06-05T08:27:24.578687Z","shell.execute_reply.started":"2024-06-05T08:27:24.572681Z","shell.execute_reply":"2024-06-05T08:27:24.577562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import load_dataset\n\nbird_data = load_dataset(\"audiofolder\", data_dir=\"/kaggle/input/birdclef-2024/train_audio\")","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:27:24.580175Z","iopub.execute_input":"2024-06-05T08:27:24.580604Z","iopub.status.idle":"2024-06-05T08:29:46.15566Z","shell.execute_reply.started":"2024-06-05T08:27:24.580568Z","shell.execute_reply":"2024-06-05T08:29:46.154664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(type(bird_data))","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:46.156865Z","iopub.execute_input":"2024-06-05T08:29:46.157156Z","iopub.status.idle":"2024-06-05T08:29:46.161938Z","shell.execute_reply.started":"2024-06-05T08:29:46.157131Z","shell.execute_reply":"2024-06-05T08:29:46.160991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from scipy.io import wavfile\n# import soundfile as sf\n# import numpy as np\n\n# import noisereduce as nr\n# data,rate = sf.read(\"/kaggle/input/birdclef-2024/train_audio/ashdro1/XC290005.ogg\")\n# reduced_noise = nr.reduce_noise(y=data, sr=rate)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:46.163129Z","iopub.execute_input":"2024-06-05T08:29:46.16339Z","iopub.status.idle":"2024-06-05T08:29:46.171708Z","shell.execute_reply.started":"2024-06-05T08:29:46.163367Z","shell.execute_reply":"2024-06-05T08:29:46.170676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import librosa\n# import matplotlib.pyplot as plt\n# import librosa.display\n\n\n# print(data)\n# print(reduced_noise)\n# plt.figure().set_figwidth(12)\n# librosa.display.waveshow(data, sr=rate)\n# ipd.Audio(data, rate=rate)\n# plt.figure().set_figwidth(12)\n# librosa.display.waveshow(reduced_noise, sr=rate)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:46.172684Z","iopub.execute_input":"2024-06-05T08:29:46.17291Z","iopub.status.idle":"2024-06-05T08:29:46.181151Z","shell.execute_reply.started":"2024-06-05T08:29:46.172888Z","shell.execute_reply":"2024-06-05T08:29:46.180102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ipd.Audio(reduced_noise, rate=rate)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:46.187947Z","iopub.execute_input":"2024-06-05T08:29:46.188707Z","iopub.status.idle":"2024-06-05T08:29:46.195797Z","shell.execute_reply.started":"2024-06-05T08:29:46.18867Z","shell.execute_reply":"2024-06-05T08:29:46.194817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id2label_fn = bird_data[\"train\"].features[\"label\"].int2str\nid2label_fn(bird_data[\"train\"][0][\"label\"])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:46.196971Z","iopub.execute_input":"2024-06-05T08:29:46.197302Z","iopub.status.idle":"2024-06-05T08:29:54.401398Z","shell.execute_reply.started":"2024-06-05T08:29:46.197269Z","shell.execute_reply":"2024-06-05T08:29:54.400336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from datasets import Dataset\n# bird_data[\"train\"] = Dataset.from_list(bird_data[\"train\"])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:54.402838Z","iopub.execute_input":"2024-06-05T08:29:54.403362Z","iopub.status.idle":"2024-06-05T08:29:54.40747Z","shell.execute_reply.started":"2024-06-05T08:29:54.403333Z","shell.execute_reply":"2024-06-05T08:29:54.406381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#bird_data_total = bird_data[\"train\"].train_test_split(seed=42, shuffle=True, test_size=0.7)\n#bird_data = bird_data_total[\"train\"].train_test_split(seed=42,shuffle=True,test_size=0.1)\nbird_data = bird_data[\"train\"].train_test_split(seed=42,shuffle=True,test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:54.408963Z","iopub.execute_input":"2024-06-05T08:29:54.409509Z","iopub.status.idle":"2024-06-05T08:29:55.160943Z","shell.execute_reply.started":"2024-06-05T08:29:54.409472Z","shell.execute_reply":"2024-06-05T08:29:55.159943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoFeatureExtractor\n\nmodel_id = \"ntu-spml/distilhubert\"\nfeature_extractor = AutoFeatureExtractor.from_pretrained('/kaggle/input/acc15/transformers/thanhanh/1/config.json',do_normalize=True, return_attention_mask=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:55.162228Z","iopub.execute_input":"2024-06-05T08:29:55.162589Z","iopub.status.idle":"2024-06-05T08:29:56.991503Z","shell.execute_reply.started":"2024-06-05T08:29:55.162556Z","shell.execute_reply":"2024-06-05T08:29:56.990655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sampling_rate = feature_extractor.sampling_rate\nprint(sampling_rate)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:56.992566Z","iopub.execute_input":"2024-06-05T08:29:56.99308Z","iopub.status.idle":"2024-06-05T08:29:56.997838Z","shell.execute_reply.started":"2024-06-05T08:29:56.993055Z","shell.execute_reply":"2024-06-05T08:29:56.996978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import Audio\n\nbird_data = bird_data.cast_column(\"audio\", Audio(sampling_rate=sampling_rate))","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:56.998962Z","iopub.execute_input":"2024-06-05T08:29:56.999202Z","iopub.status.idle":"2024-06-05T08:29:57.398969Z","shell.execute_reply.started":"2024-06-05T08:29:56.999181Z","shell.execute_reply":"2024-06-05T08:29:57.39815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\nsample = bird_data[\"train\"][0][\"audio\"]\nprint(f\"Mean: {np.mean(sample['array']):.3}, Variance: {np.var(sample['array']):.3}\")","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:57.400308Z","iopub.execute_input":"2024-06-05T08:29:57.400802Z","iopub.status.idle":"2024-06-05T08:29:58.128514Z","shell.execute_reply.started":"2024-06-05T08:29:57.400767Z","shell.execute_reply":"2024-06-05T08:29:58.127575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = feature_extractor(sample[\"array\"], sampling_rate=sample[\"sampling_rate\"])\n\nprint(f\"inputs keys: {list(inputs.keys())}\")\n\nprint(\n    f\"Mean: {np.mean(inputs['input_values']):.3}, Variance: {np.var(inputs['input_values']):.3}\"\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:29:58.129805Z","iopub.execute_input":"2024-06-05T08:29:58.130156Z","iopub.status.idle":"2024-06-05T08:30:07.886094Z","shell.execute_reply.started":"2024-06-05T08:29:58.130125Z","shell.execute_reply":"2024-06-05T08:30:07.884938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_duration = 25.0\n\n\ndef preprocess_function(examples):\n    audio_arrays = [x[\"array\"] for x in examples[\"audio\"]]\n    inputs = feature_extractor(\n        audio_arrays,\n        sampling_rate=feature_extractor.sampling_rate,\n        max_length=int(feature_extractor.sampling_rate * max_duration),\n        truncation=True,\n        return_attention_mask=True,\n    )\n    return inputs","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:33:29.707839Z","iopub.execute_input":"2024-06-05T08:33:29.70865Z","iopub.status.idle":"2024-06-05T08:33:29.714279Z","shell.execute_reply.started":"2024-06-05T08:33:29.708612Z","shell.execute_reply":"2024-06-05T08:33:29.7133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(bird_data[\"train\"])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:31:47.110403Z","iopub.execute_input":"2024-06-05T08:31:47.110778Z","iopub.status.idle":"2024-06-05T08:31:47.115997Z","shell.execute_reply.started":"2024-06-05T08:31:47.110748Z","shell.execute_reply":"2024-06-05T08:31:47.115082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nprint(torch.cuda.is_available())","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:31:49.374107Z","iopub.execute_input":"2024-06-05T08:31:49.375028Z","iopub.status.idle":"2024-06-05T08:31:49.37994Z","shell.execute_reply.started":"2024-06-05T08:31:49.374993Z","shell.execute_reply":"2024-06-05T08:31:49.37889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import Dataset\n\nbird_data_encoded = bird_data.map(\n    preprocess_function,\n    remove_columns=[\"audio\"],\n    batched=True,\n    batch_size=4,\n    num_proc=2,\n)\n\nprint(bird_data_encoded)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:33:33.827187Z","iopub.execute_input":"2024-06-05T08:33:33.828168Z","iopub.status.idle":"2024-06-05T08:36:05.334536Z","shell.execute_reply.started":"2024-06-05T08:33:33.828134Z","shell.execute_reply":"2024-06-05T08:36:05.332552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id2label = {\n    str(i): id2label_fn(i)\n    for i in range(len(bird_data_encoded[\"train\"].features[\"label\"].names))\n}\nlabel2id = {v: k for k, v in id2label.items()}\n\nid2label[\"7\"]","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.335391Z","iopub.status.idle":"2024-06-05T08:36:05.335761Z","shell.execute_reply.started":"2024-06-05T08:36:05.335591Z","shell.execute_reply":"2024-06-05T08:36:05.335609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import AutoModelForAudioClassification\nnum_labels = len(id2label)\n\nmodel = AutoModelForAudioClassification.from_pretrained(\n    model_id,\n    num_labels=num_labels,\n    label2id=label2id,\n    id2label=id2label,\n)\nmodel     = AutoModelForAudioClassification.from_pretrained('/kaggle/input/acc15/transformers/thanhanh/1')\ntokenizer = AutoFeatureExtractor.from_pretrained('/kaggle/input/acc15/transformers/thanhanh/1/config.json')","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.336898Z","iopub.status.idle":"2024-06-05T08:36:05.337336Z","shell.execute_reply.started":"2024-06-05T08:36:05.337119Z","shell.execute_reply":"2024-06-05T08:36:05.337139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import TrainingArguments\n\nmodel_name = model_id.split(\"/\")[-1]\nbatch_size = 4\ngradient_accumulation_steps = 1\nnum_train_epochs = 3\n\ntraining_args = TrainingArguments(\n    f\"{model_name}-finetuned-gtzan\",\n    evaluation_strategy =\"epoch\",\n    save_strategy=\"epoch\",\n    learning_rate=1e-5,\n    per_device_train_batch_size=batch_size,\n    gradient_accumulation_steps=gradient_accumulation_steps,\n    per_device_eval_batch_size=batch_size,\n    num_train_epochs=num_train_epochs,\n    warmup_ratio=0.1,\n    logging_steps=5,\n    load_best_model_at_end=True,\n    metric_for_best_model=\"accuracy\",\n    fp16=True,\n    push_to_hub=False,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.339376Z","iopub.status.idle":"2024-06-05T08:36:05.339844Z","shell.execute_reply.started":"2024-06-05T08:36:05.339611Z","shell.execute_reply":"2024-06-05T08:36:05.339629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import evaluate\nimport numpy as np\n\nmetric = evaluate.load(\"accuracy\")\n\n\ndef compute_metrics(eval_pred):\n    \"\"\"Computes accuracy on a batch of predictions\"\"\"\n    predictions = np.argmax(eval_pred.predictions, axis=1)\n    return metric.compute(predictions=predictions, references=eval_pred.label_ids)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.343131Z","iopub.status.idle":"2024-06-05T08:36:05.343468Z","shell.execute_reply.started":"2024-06-05T08:36:05.343306Z","shell.execute_reply":"2024-06-05T08:36:05.34332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!export PYTORCH_CUDA_ALLOC_CONF=MAX_SPLIT_SIZE_MB:512","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.344746Z","iopub.status.idle":"2024-06-05T08:36:05.345054Z","shell.execute_reply.started":"2024-06-05T08:36:05.344901Z","shell.execute_reply":"2024-06-05T08:36:05.344914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install wandb --upgrade","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.346217Z","iopub.status.idle":"2024-06-05T08:36:05.346542Z","shell.execute_reply.started":"2024-06-05T08:36:05.346367Z","shell.execute_reply":"2024-06-05T08:36:05.34638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import Trainer\n\ntrainer = Trainer(\n    model,\n    training_args,\n    train_dataset=bird_data_encoded[\"train\"],\n    eval_dataset=bird_data_encoded[\"test\"],\n    tokenizer=feature_extractor,\n    compute_metrics=compute_metrics,\n)\n\ntrainer.train()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.347619Z","iopub.status.idle":"2024-06-05T08:36:05.347964Z","shell.execute_reply.started":"2024-06-05T08:36:05.347794Z","shell.execute_reply":"2024-06-05T08:36:05.347809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_pretrained('/kaggle/working/final_audio_classification_model')\n\n# Lưu các siêu tham số và trạng thái của quá trình đào tạo\ntrainer.save_model('/kaggle/working/trainer_model')\n\n# In ra danh sách các tệp đã lưu\nprint(os.listdir('/kaggle/working/'))","metadata":{"execution":{"iopub.status.busy":"2024-06-05T08:36:05.349162Z","iopub.status.idle":"2024-06-05T08:36:05.349498Z","shell.execute_reply.started":"2024-06-05T08:36:05.349336Z","shell.execute_reply":"2024-06-05T08:36:05.34935Z"},"trusted":true},"execution_count":null,"outputs":[]}]}