{"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":"gpu","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":171345829,"sourceType":"kernelVersion"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install wandb","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:42:05.056431Z","iopub.execute_input":"2024-05-01T08:42:05.057159Z","iopub.status.idle":"2024-05-01T08:42:18.340049Z","shell.execute_reply.started":"2024-05-01T08:42:05.057128Z","shell.execute_reply":"2024-05-01T08:42:18.339100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.callback.wandb import *\nfrom kaggle_secrets import UserSecretsClient\nimport wandb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-01T08:53:20.673681Z","iopub.execute_input":"2024-05-01T08:53:20.674544Z","iopub.status.idle":"2024-05-01T08:53:20.682698Z","shell.execute_reply.started":"2024-05-01T08:53:20.674497Z","shell.execute_reply":"2024-05-01T08:53:20.681662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"user_secrets = UserSecretsClient()\nwandb_api = user_secrets.get_secret(\"wandb-key\") \nwandb.login(key = wandb_api)\nwandb.init(project=\"birdclef2024\", name=\"fastai-baseline\")","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:53:22.230078Z","iopub.execute_input":"2024-05-01T08:53:22.230475Z","iopub.status.idle":"2024-05-01T08:53:43.498878Z","shell.execute_reply.started":"2024-05-01T08:53:22.230443Z","shell.execute_reply":"2024-05-01T08:53:43.497941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"/kaggle/input/birdclef-2024-split-and-creating-melspecrogram\")\nPath.BASE_PATH = Path\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:44:48.329476Z","iopub.execute_input":"2024-05-01T08:44:48.329855Z","iopub.status.idle":"2024-05-01T08:44:48.468314Z","shell.execute_reply.started":"2024-05-01T08:44:48.329825Z","shell.execute_reply":"2024-05-01T08:44:48.467345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(path / \"train.csv\")\ndf_valid = pd.read_csv(path / \"valid.csv\")\n\nlabels = df_train[\"primary_label\"].unique()\nlbldict = {k: v for v, k in enumerate(labels)}\n\nnum_classes = len(df_train.primary_label.unique())\nbirds = list(df_train.primary_label.unique())\n\nmissing_birds = list(\n    set(list(df_train.primary_label.unique())).difference(\n        list(df_valid.primary_label.unique())\n    )\n)\n\ndf_valid[missing_birds] = 0\ndf_valid = df_valid[df_train.columns]  # Fix order\n\n# Assuming 'train' and 'valid' directories under 'specs'\ndf_train['folder'] = 'train'\ndf_valid['folder'] = 'valid'","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:44:48.691639Z","iopub.execute_input":"2024-05-01T08:44:48.692560Z","iopub.status.idle":"2024-05-01T08:44:49.005179Z","shell.execute_reply.started":"2024-05-01T08:44:48.692519Z","shell.execute_reply":"2024-05-01T08:44:49.003868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(row):\n    impath = path / \"specs\" / row['folder'] / (row[\"filename\"] + \".npy\")\n    image = np.load(impath)[:5]  # Assuming you want the first 5 time slices, adjust as needed\n    # image = image[np.random.choice(image.shape[0])]  # Randomly choose one time slice\n    image = image[0]\n    # Normalize and convert to RGB by repeating the channels\n    image = np.stack([image]*3, axis=-1)  # Repeat the channel 3 times for RGB\n\n    return Image.fromarray(image)  # Normalize and convert to uint8","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:44:49.496394Z","iopub.execute_input":"2024-05-01T08:44:49.497221Z","iopub.status.idle":"2024-05-01T08:44:49.505582Z","shell.execute_reply.started":"2024-05-01T08:44:49.497181Z","shell.execute_reply":"2024-05-01T08:44:49.504295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lbltfm = Pipeline([ColReader(\"primary_label\"), Categorize(vocab=birds)])\ntfms = [[load_image, PILImage.create], lbltfm]\n\ntrain_ds = Datasets(df_train, tfms=tfms)\nvalid_ds = Datasets(df_valid, tfms=tfms)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:44:50.011755Z","iopub.execute_input":"2024-05-01T08:44:50.012125Z","iopub.status.idle":"2024-05-01T08:44:50.434613Z","shell.execute_reply.started":"2024-05-01T08:44:50.012088Z","shell.execute_reply":"2024-05-01T08:44:50.433357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = DataLoaders.from_dsets(train_ds, valid_ds, bs=64, after_item=[ToTensor], after_batch=[IntToFloatTensor()]).cuda()","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:44:51.933904Z","iopub.execute_input":"2024-05-01T08:44:51.934774Z","iopub.status.idle":"2024-05-01T08:44:51.987241Z","shell.execute_reply.started":"2024-05-01T08:44:51.934741Z","shell.execute_reply":"2024-05-01T08:44:51.985909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:44:52.799127Z","iopub.execute_input":"2024-05-01T08:44:52.799501Z","iopub.status.idle":"2024-05-01T08:44:58.174637Z","shell.execute_reply.started":"2024-05-01T08:44:52.799470Z","shell.execute_reply":"2024-05-01T08:44:58.173739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=accuracy, cbs=WandbCallback())\nlearn.fine_tune(5)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:45:10.443731Z","iopub.execute_input":"2024-05-01T08:45:10.444088Z","iopub.status.idle":"2024-05-01T08:53:00.603548Z","shell.execute_reply.started":"2024-05-01T08:45:10.444058Z","shell.execute_reply":"2024-05-01T08:53:00.602072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:53:00.606260Z","iopub.execute_input":"2024-05-01T08:53:00.606974Z","iopub.status.idle":"2024-05-01T08:53:06.452075Z","shell.execute_reply.started":"2024-05-01T08:53:00.606931Z","shell.execute_reply":"2024-05-01T08:53:06.450926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.most_confused(min_val=5)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:53:06.454224Z","iopub.execute_input":"2024-05-01T08:53:06.454946Z","iopub.status.idle":"2024-05-01T08:53:13.073393Z","shell.execute_reply.started":"2024-05-01T08:53:06.454903Z","shell.execute_reply":"2024-05-01T08:53:13.072339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export()","metadata":{"execution":{"iopub.status.busy":"2024-05-01T08:53:13.076260Z","iopub.execute_input":"2024-05-01T08:53:13.077091Z","iopub.status.idle":"2024-05-01T08:53:13.199093Z","shell.execute_reply.started":"2024-05-01T08:53:13.077045Z","shell.execute_reply":"2024-05-01T08:53:13.197870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}