{"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":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"! pip install efficientnet-pytorch\n! pip install --user torch==1.9.0 torchvision==0.10.0 torchaudio==0.9.0 torchtext==0.10.0","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:04:19.488798Z","iopub.execute_input":"2022-02-07T05:04:19.489329Z","iopub.status.idle":"2022-02-07T05:05:20.857198Z","shell.execute_reply.started":"2022-02-07T05:04:19.489237Z","shell.execute_reply":"2022-02-07T05:05:20.856347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nimport fastai\nfrom fastai.vision.all import *\nfrom pathlib import Path\nfrom efficientnet_pytorch import EfficientNet","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:28.755356Z","iopub.execute_input":"2022-02-07T05:05:28.756031Z","iopub.status.idle":"2022-02-07T05:05:29.873384Z","shell.execute_reply.started":"2022-02-07T05:05:28.755986Z","shell.execute_reply":"2022-02-07T05:05:29.872481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training on a subset of the training images.","metadata":{}},{"cell_type":"code","source":"%%time\ntrain = pd.read_csv(\"../input/trainsample/train-sample-split.csv\", low_memory = False)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:32.16343Z","iopub.execute_input":"2022-02-07T05:05:32.164008Z","iopub.status.idle":"2022-02-07T05:05:32.179253Z","shell.execute_reply.started":"2022-02-07T05:05:32.163967Z","shell.execute_reply":"2022-02-07T05:05:32.178283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:35.122864Z","iopub.execute_input":"2022-02-07T05:05:35.123582Z","iopub.status.idle":"2022-02-07T05:05:35.139955Z","shell.execute_reply.started":"2022-02-07T05:05:35.123542Z","shell.execute_reply":"2022-02-07T05:05:35.139213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"../input/happy-whale-and-dolphin/train_images/\")","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:39.632347Z","iopub.execute_input":"2022-02-07T05:05:39.632938Z","iopub.status.idle":"2022-02-07T05:05:39.638565Z","shell.execute_reply.started":"2022-02-07T05:05:39.632899Z","shell.execute_reply":"2022-02-07T05:05:39.635924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"whales = DataBlock(blocks = (ImageBlock, CategoryBlock),\n                   get_x = ColReader(\"image\", path),\n                   get_y = ColReader(\"individual_id\"),\n                   splitter = ColSplitter(\"is_valid\"),\n                   item_tfms = Resize(440),\n                   batch_tfms = [*aug_transforms(size = 224, min_scale = 0.75)])","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:43.226824Z","iopub.execute_input":"2022-02-07T05:05:43.227508Z","iopub.status.idle":"2022-02-07T05:05:43.239818Z","shell.execute_reply.started":"2022-02-07T05:05:43.227472Z","shell.execute_reply":"2022-02-07T05:05:43.23906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndls = whales.dataloaders(train)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:45.891139Z","iopub.execute_input":"2022-02-07T05:05:45.891903Z","iopub.status.idle":"2022-02-07T05:05:49.42903Z","shell.execute_reply.started":"2022-02-07T05:05:45.891865Z","shell.execute_reply":"2022-02-07T05:05:49.428271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:05:52.087769Z","iopub.execute_input":"2022-02-07T05:05:52.088384Z","iopub.status.idle":"2022-02-07T05:06:05.139878Z","shell.execute_reply.started":"2022-02-07T05:05:52.088336Z","shell.execute_reply":"2022-02-07T05:06:05.139105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel = EfficientNet.from_name('efficientnet-b0')","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:06:16.422593Z","iopub.execute_input":"2022-02-07T05:06:16.422878Z","iopub.status.idle":"2022-02-07T05:06:16.482553Z","shell.execute_reply.started":"2022-02-07T05:06:16.422847Z","shell.execute_reply":"2022-02-07T05:06:16.481676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model._fc = nn.Linear(1280, dls.c)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:06:21.24056Z","iopub.execute_input":"2022-02-07T05:06:21.241124Z","iopub.status.idle":"2022-02-07T05:06:21.253449Z","shell.execute_reply.started":"2022-02-07T05:06:21.24108Z","shell.execute_reply":"2022-02-07T05:06:21.252507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MAP@5 Metric","metadata":{}},{"cell_type":"code","source":"def map_per_image(predictions, label):\n    '''this function will calculate MAP@5 for a single image\n\n    predictions = list of top 5 predictions for an image (Order does matter).\n    label = true label\n    '''\n    try :\n        return 1 / (predictions[:5].index(label) + 1)\n    except ValueError :\n        return 0.0\n\n\n\ndef map_per_set(predictions, labels):\n    '''this function calculates MAP@5 for the whole set\n\n    predictions = list of list of all the predictions for every image.\n    labels = list of true labels \n    '''\n    preds_sorted = []\n    for i in predictions :\n        temp = list(np.argsort(-i)[:5])\n        preds_sorted.append(temp)\n    return np.mean([map_per_image(p, l) for p,l in zip(preds_sorted, labels)])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmap5 = AccumMetric(map_per_set, to_np = False,  flatten = False)\nlearn = Learner(dls, model, metrics = [error_rate, map5])","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:08:06.952904Z","iopub.execute_input":"2022-02-07T05:08:06.953651Z","iopub.status.idle":"2022-02-07T05:08:06.963071Z","shell.execute_reply.started":"2022-02-07T05:08:06.953611Z","shell.execute_reply":"2022-02-07T05:08:06.960578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Finding Appropriate Learning Rate","metadata":{}},{"cell_type":"code","source":"%%time\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:08:11.296188Z","iopub.execute_input":"2022-02-07T05:08:11.296765Z","iopub.status.idle":"2022-02-07T05:19:23.586646Z","shell.execute_reply.started":"2022-02-07T05:08:11.296724Z","shell.execute_reply":"2022-02-07T05:19:23.585858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nlearn.fit_one_cycle(2, 1e-3)","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:22:14.530796Z","iopub.execute_input":"2022-02-07T05:22:14.531506Z","iopub.status.idle":"2022-02-07T05:32:09.470866Z","shell.execute_reply.started":"2022-02-07T05:22:14.531461Z","shell.execute_reply":"2022-02-07T05:32:09.470012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.recorder.plot_loss()","metadata":{"execution":{"iopub.status.busy":"2022-02-07T05:32:26.744877Z","iopub.execute_input":"2022-02-07T05:32:26.745156Z","iopub.status.idle":"2022-02-07T05:32:27.182509Z","shell.execute_reply.started":"2022-02-07T05:32:26.745123Z","shell.execute_reply":"2022-02-07T05:32:27.181734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Please Upvote if you liked my work ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}