{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":154934457,"sourceType":"kernelVersion"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.vision.all import *\nImage.MAX_IMAGE_PIXELS = 20641052620\nimport timm","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:29.015432Z","iopub.execute_input":"2023-12-19T03:25:29.015705Z","iopub.status.idle":"2023-12-19T03:25:35.971484Z","shell.execute_reply.started":"2023-12-19T03:25:29.01568Z","shell.execute_reply":"2023-12-19T03:25:35.970235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    #tf.set_random_seed(seed)\nseed_everything(42)\ntorch.backends.cudnn.benchmark = True","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:35.973514Z","iopub.execute_input":"2023-12-19T03:25:35.974133Z","iopub.status.idle":"2023-12-19T03:25:35.985532Z","shell.execute_reply.started":"2023-12-19T03:25:35.974088Z","shell.execute_reply":"2023-12-19T03:25:35.984625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"/kaggle/input/UBC-OCEAN\")\nPath.BASE_PATH = path\npath.ls()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:35.986849Z","iopub.execute_input":"2023-12-19T03:25:35.987156Z","iopub.status.idle":"2023-12-19T03:25:35.994582Z","shell.execute_reply.started":"2023-12-19T03:25:35.987122Z","shell.execute_reply":"2023-12-19T03:25:35.993754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(path/'train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:35.996933Z","iopub.execute_input":"2023-12-19T03:25:35.99727Z","iopub.status.idle":"2023-12-19T03:25:36.013729Z","shell.execute_reply.started":"2023-12-19T03:25:35.997242Z","shell.execute_reply":"2023-12-19T03:25:36.013053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_file_path(image_id):\n#     return f\"/kaggle/input/ubc-ocean-jpeg-dataset-pipeline/train_compressed_images/{image_id}.jpg\"\n    if os.path.exists(f\"{path}/train_thumbnails/{image_id}_thumbnail.png\"):\n        return f\"{path}/train_thumbnails/{image_id}_thumbnail.png\"\n    else:\n        return f\"{path}/train_images/{image_id}.png\"","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:36.014689Z","iopub.execute_input":"2023-12-19T03:25:36.014935Z","iopub.status.idle":"2023-12-19T03:25:36.019505Z","shell.execute_reply.started":"2023-12-19T03:25:36.014913Z","shell.execute_reply":"2023-12-19T03:25:36.018594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['image_id'] = df['image_id'].apply(get_file_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:36.020579Z","iopub.execute_input":"2023-12-19T03:25:36.020828Z","iopub.status.idle":"2023-12-19T03:25:36.557301Z","shell.execute_reply.started":"2023-12-19T03:25:36.020806Z","shell.execute_reply":"2023-12-19T03:25:36.556553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks = (ImageBlock, CategoryBlock),\n                 get_x=ColReader(0),\n                 get_y = ColReader(1),\n                 splitter = RandomSplitter(),\n                 item_tfms = Resize(550),\n                 batch_tfms=[*aug_transforms(), Normalize.from_stats(*imagenet_stats)])","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:36.558306Z","iopub.execute_input":"2023-12-19T03:25:36.558549Z","iopub.status.idle":"2023-12-19T03:25:39.794333Z","shell.execute_reply.started":"2023-12-19T03:25:36.558528Z","shell.execute_reply":"2023-12-19T03:25:39.793555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndls = dblock.dataloaders(df)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:39.795405Z","iopub.execute_input":"2023-12-19T03:25:39.795677Z","iopub.status.idle":"2023-12-19T03:25:44.920373Z","shell.execute_reply.started":"2023-12-19T03:25:39.795653Z","shell.execute_reply":"2023-12-19T03:25:44.9194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:25:44.921532Z","iopub.execute_input":"2023-12-19T03:25:44.921808Z","iopub.status.idle":"2023-12-19T03:26:08.932046Z","shell.execute_reply.started":"2023-12-19T03:25:44.921784Z","shell.execute_reply":"2023-12-19T03:26:08.931188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(df, pin_memory=True, bs=8)\nlearn = vision_learner(dls, \"convnext_small_in22k\", metrics=[accuracy, BalancedAccuracy()]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:26:08.934974Z","iopub.execute_input":"2023-12-19T03:26:08.935571Z","iopub.status.idle":"2023-12-19T03:26:11.603501Z","shell.execute_reply.started":"2023-12-19T03:26:08.935539Z","shell.execute_reply":"2023-12-19T03:26:11.602571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(8)","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:26:11.60475Z","iopub.execute_input":"2023-12-19T03:26:11.605602Z","iopub.status.idle":"2023-12-19T03:36:31.898987Z","shell.execute_reply.started":"2023-12-19T03:26:11.605566Z","shell.execute_reply":"2023-12-19T03:36:31.897844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:36:31.900799Z","iopub.execute_input":"2023-12-19T03:36:31.901794Z","iopub.status.idle":"2023-12-19T03:37:03.632425Z","shell.execute_reply.started":"2023-12-19T03:36:31.901755Z","shell.execute_reply":"2023-12-19T03:37:03.63099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T03:37:03.637079Z","iopub.execute_input":"2023-12-19T03:37:03.642284Z","iopub.status.idle":"2023-12-19T03:37:04.036908Z","shell.execute_reply.started":"2023-12-19T03:37:03.642246Z","shell.execute_reply":"2023-12-19T03:37:04.035849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}