{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# Importing libraries\nimport pandas as pd\nimport numpy as np\nimport gc\ngc.enable()\nimport matplotlib.pyplot as plt\n%matplotlib inline\nplt.rcParams['figure.figsize'] = (8.0, 5.0)\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom fastai import *\nfrom fastai.vision import *\n\nfrom utils import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0103329b1d3a4742729a4dbbd375b7a1312bad4"},"cell_type":"code","source":"path = Path('../input/humpback-whale-identification/')\npath_test = Path('../input/humpback-whale-identification/test')\npath_train = Path('../input/humpback-whale-identification/train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f2a2269c46cb05a35fcf6373b29a4d96a9493e7f"},"cell_type":"code","source":"train_df=pd.read_csv(path/'train.csv')\nval_fns = {'69823499d.jpg'}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbc8592d935473383c63fe6e99f5bd09fed91b67"},"cell_type":"code","source":"print(\"Train Shape : \",train_df.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfb713c4df7c405150b3d52081e869be105177bb"},"cell_type":"code","source":"print(\"No of Whale Classes : \",len(train_df.Id.value_counts()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63109b58afb44d9fe1c5b1a480113df8448f2c9c"},"cell_type":"code","source":"train_df.Id.value_counts().head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0a31b2e17e4942ff0e4f147fcb95907e249adf3b"},"cell_type":"code","source":"(train_df.Id == 'new_whale').mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17dbcd43ef668fffd4c1cb11fd6368c207625a91"},"cell_type":"code","source":"(train_df.Id.value_counts() == 1).mean()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca47d83c505888c525d55dd4b549c93793a6aff5"},"cell_type":"markdown","source":"41% of all whales have only a single image associated with them.\n\n38% of all images contain a new whale - a whale that has not been identified as one of the known whales."},{"metadata":{"trusted":true,"_uuid":"970c98fcf310b6322bde4e792f00c44a5d9f7e8f"},"cell_type":"code","source":"fn2label = {row[1].Image: row[1].Id for row in train_df.iterrows()}\npath2fn = lambda path: re.search('\\w*\\.jpg$', path).group(0)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"aead2af51096889165219ebf22e24cddf5b4e160"},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8cdb887e9333392ae0bccbc9e3ba1fc6134e8373"},"cell_type":"code","source":"name = f'densenet169'\n\nSZ = 224\nBS = 64\nNUM_WORKERS = 0\nSEED=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1d0058ab0b2775625d62c86a06a3a1807a368746"},"cell_type":"code","source":"data = (\n    ImageItemList\n        .from_df(train_df[train_df.Id != 'new_whale'],path_train, cols=['Image'])\n        .split_by_valid_func(lambda path: path2fn(path) in val_fns)\n        .label_from_func(lambda path: fn2label[path2fn(path)])\n        .add_test(ImageItemList.from_folder(path_test))\n        .transform(get_transforms(do_flip=False, max_zoom=1, max_warp=0, max_rotate=2), size=SZ, resize_method=ResizeMethod.SQUISH)\n        .databunch(bs=BS, num_workers=NUM_WORKERS, path=path)\n).normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"trusted":true,"_uuid":"8791bffdecaf10794cb366255755eb655efbd119"},"cell_type":"code","source":"data.show_batch(rows=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e45951b0333cf0092bd99967d5fdf42a9d0b9b88"},"cell_type":"code","source":"learn = create_cnn(data, models.densenet169, lin_ftrs=[2048], model_dir='../working/')\nlearn.clip_grad()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12a797b8b6943a8f92358b4f40e2f83d2c227d0f"},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66b611db7e4993ac9533d51685c3f4127aba7563"},"cell_type":"code","source":"SZ = 224 * 2\nBS = 64 // 4\nNUM_WORKERS = 0\nSEED=0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"536f018d4c247ea1065e770e2bc7f9a1868e56ab"},"cell_type":"code","source":"df = pd.read_csv('../input/oversample-whale/oversampled_train_and_val.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27b0e3d584ed30abe841be9c5e82ff1282779828"},"cell_type":"code","source":"data = (\n    ImageItemList\n        .from_df(df, path_train, cols=['Image'])\n        .split_by_valid_func(lambda path: path2fn(path) in val_fns)\n        .label_from_func(lambda path: fn2label[path2fn(path)])\n        .add_test(ImageItemList.from_folder(path_test))\n        .transform(get_transforms(do_flip=False, max_zoom=1, max_warp=0, max_rotate=2), size=SZ, resize_method=ResizeMethod.SQUISH)\n        .databunch(bs=BS, num_workers=NUM_WORKERS, path=path)\n        .normalize(imagenet_stats)\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"071da3229c7f1d95039ee1d9cf8fc05345e19a03"},"cell_type":"code","source":"learn = create_cnn(data, models.densenet169, lin_ftrs=[2048], model_dir='../working/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b67a165e5153de647cbb1ebfc119afc35fdfc0b0"},"cell_type":"code","source":"learn.fit_one_cycle(1, slice(6.92E-06))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f4db38efedd72a941b5215c8ede6e4313891f839"},"cell_type":"code","source":"gc.collect()\nlearn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"trusted":true,"_uuid":"b831d4fe594f2765b8caff0f121c7fb6dd32a4aa"},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ac50d1b06ed7f51eea7b10f4e69f7771e3c8a4b"},"cell_type":"code","source":"preds, _ = learn.get_preds(DatasetType.Test)\npreds = torch.cat((preds, torch.ones_like(preds[:, :1])), 1)\npreds[:, 5004] = 0.06\n\nclasses = learn.data.classes + ['new_whale']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"171051f27f30fa07b62009dd5ae988014520786a"},"cell_type":"code","source":"def top_5_preds(preds): return np.argsort(preds.numpy())[:, ::-1][:, :5]\n\ndef top_5_pred_labels(preds, classes):\n    top_5 = top_5_preds(preds)\n    labels = []\n    for i in range(top_5.shape[0]):\n        labels.append(' '.join([classes[idx] for idx in top_5[i]]))\n    return labels\n\ndef create_submission(preds, data, name, classes=None):\n    if not classes: classes = data.classes\n    sub = pd.DataFrame({'Image': [path.name for path in data.test_ds.x.items]})\n    sub['Id'] = top_5_pred_labels(preds, classes)\n    sub.to_csv(f'{name}.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d34ec23db9eb15b7052e4c22e6d5b46e95b4cf0"},"cell_type":"code","source":"create_submission(preds, learn.data, name, classes)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}