{"cells":[{"metadata":{"trusted":true,"_uuid":"1247d524313cee19dcd8d9e5156a17a34a7bd035"},"cell_type":"code","source":"!pip install git+https://github.com/fastai/fastai@2e1ccb58121dc648751e2109fc0fbf6925aa8887\n!apt update && apt install -y libsm6 libxext6\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"13c730543df0668c86b483688ef7058718b05f6b"},"cell_type":"code","source":"!pip install torchtext==0.2.3\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"from fastai.conv_learner import *\nfrom fastai.dataset import *\nfrom tqdm import tqdm\nimport pandas as pd\nimport numpy as np\nimport os\nfrom sklearn.model_selection import train_test_split, StratifiedShuffleSplit\nimport matplotlib.pyplot as plt\nimport math","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"MODEL_PATH = 'Resnet18_v1'\nTRAIN = '../input/train/'\nTEST = '../input/test/'\nLABELS = '../input/train.csv'\nSAMPLE_SUB = '../input/sample_submission.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bc7a802ffac2baa8c9953eee2485b834a80ec68"},"cell_type":"code","source":"arch = resnet18\nnw = 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9570227cdbbe1d429793bc80e6edcc2d27c5724a"},"cell_type":"code","source":"train_df = pd.read_csv(LABELS).set_index('Image')\nunique_labels = np.unique(train_df.Id.values)\n\nlabels_dict = dict()\nlabels_list = []\nfor i in range(len(unique_labels)):\n    labels_dict[unique_labels[i]] = i\n    labels_list.append(unique_labels[i])\nprint(\"Number of classes: {}\".format(len(unique_labels)))\ntrain_names = train_df.index.values\ntrain_df.Id = train_df.Id.apply(lambda x: labels_dict[x])\ntrain_labels = np.asarray(train_df.Id.values)\ntest_names = [f for f in os.listdir(TEST)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0e25033e8d8b17c65888dc265bda27befd85a3e"},"cell_type":"code","source":"labels_count = train_df.Id.value_counts()\n_, _,_ = plt.hist(labels_count,bins=100)\nlabels_count","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ada5ea0cc1736131d46f6dfe4bc0856f82bb8144"},"cell_type":"code","source":"print(\"Count for class new_whale: {}\".format(labels_count[0]))\n\nplt.hist(labels_count[1:],bins=100,range=[0,100])\nplt.hist(labels_count[1:],bins=100,range=[0,100])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f2072d26081831ede16ab3661039c9836603077"},"cell_type":"code","source":"dup = []\nfor idx,row in train_df.iterrows():\n    if labels_count[row['Id']] < 5:\n        dup.extend([idx]*math.ceil((5 - labels_count[row['Id']])/labels_count[row['Id']]))\ntrain_names = np.concatenate([train_names, dup])\ntrain_names = train_names[np.random.RandomState(seed=42).permutation(train_names.shape[0])]\nlen(train_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"64b1adf43da5e5188075a4bddf6d7d61361272a7"},"cell_type":"code","source":"sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42069)\nfor train_idx, val_idx in sss.split(train_names, np.zeros(train_names.shape)):\n    tr_n, val_n = train_names[train_idx], train_names[val_idx]\nprint(len(tr_n), len(val_n))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4b96ecadc7f594f0ca3cf50d4e5a6ed95d4a5c77"},"cell_type":"code","source":"avg_width = 0\navg_height = 0\nfor fn in os.listdir(TRAIN)[:1000]:\n    img = cv2.imread(os.path.join(TRAIN,fn))\n    avg_width += img.shape[1]\n    avg_height += img.shape[0]\navg_width //= 1000\navg_height //= 1000\nprint(avg_width, avg_height)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7b7bfd5a28db83c61a9473d5b8533fac0f724d75"},"cell_type":"code","source":"class HWIDataset(FilesDataset):\n    def __init__(self, fnames, path, transform):\n        self.train_df = train_df\n        super().__init__(fnames, transform, path)\n\n    def get_x(self, i):\n        img = open_image(os.path.join(self.path, self.fnames[i]))\n        # We crop the center of the original image for faster training time\n        img = cv2.resize(img, (self.sz, self.sz))\n        return img\n\n    def get_y(self, i):\n        if (self.path == TEST): return 0\n        return self.train_df.loc[self.fnames[i]]['Id']\n\n\n    def get_c(self):\n        return len(unique_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"72818741da21e5028f0e0f5c06201b733949a610"},"cell_type":"code","source":"class RandomLighting(Transform):\n    def __init__(self, b, c, tfm_y=TfmType.NO):\n        super().__init__(tfm_y)\n        self.b, self.c = b, c\n\n    def set_state(self):\n        self.store.b_rand = rand0(self.b)\n        self.store.c_rand = rand0(self.c)\n\n    def do_transform(self, x, is_y):\n        if is_y and self.tfm_y != TfmType.PIXEL: return x  # add this line to fix the bug\n        b = self.store.b_rand\n        c = self.store.c_rand\n        c = -1 / (c - 1) if c < 0 else c + 1\n        x = lighting(x, b, c)\n        return x\n    \ndef get_data(sz, bs):\n    aug_tfms = [RandomRotateZoom(deg=20, zoom=2, stretch=1),\n                RandomLighting(0.05, 0.05, tfm_y=TfmType.NO),\n                RandomBlur(blur_strengths=3,tfm_y=TfmType.NO),\n                RandomFlip(tfm_y=TfmType.NO)]\n    tfms = tfms_from_model(arch, sz, crop_type=CropType.NO, tfm_y=TfmType.NO,\n                           aug_tfms=aug_tfms)\n    ds = ImageData.get_ds(HWIDataset, (tr_n[:-(len(tr_n) % bs)], TRAIN),\n                          (val_n, TRAIN), tfms, test=(test_names, TEST))\n    md = ImageData(\"./\", ds, bs, num_workers=nw, classes=None)\n    return md","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"480762ae8274a843e51c25853578240c513820d4"},"cell_type":"code","source":"# sz = (avg_width//2, avg_height//2)\nbatch_size = 64\nmd = get_data(avg_width//4, batch_size)\nlearn = ConvLearner.pretrained(arch, md) \nlearn.opt_fn = optim.Adam","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63d1d38c12b2d14b3d1bbf4afc5ed53bf6dad3c2"},"cell_type":"code","source":"# learn.lr_find()\n# learn.sched.plot()\nlr = 5e-3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"37e4e9f21c055ba1a18112b7e09e69ce3b305b08"},"cell_type":"code","source":"learn.fit(lr, 1, cycle_len=2)\nlearn.unfreeze()\nlrs = np.array([lr/10, lr/20, lr/40])\nlearn.fit(lrs, 4, cycle_len=4, use_clr=(20, 16))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b82d4247e28bf44e68e35f62dc147e7b48c0ef7f"},"cell_type":"code","source":"#learn.fit(lrs/4, 2, cycle_len=4, use_clr=(10, 16)) # not runned\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a639d0ffe7ec1f9877c09b1e2974954ef1625ebe"},"cell_type":"code","source":"#learn.fit(lrs/16, 1, cycle_len=4, use_clr=(10, 16)) # not runned","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"889599278f6858f14f499983bf10dae5257377a0"},"cell_type":"code","source":"# preds_t,y_t = learn.predict_with_targs(is_test=True) # Predicting without TTA\npreds_t,y_t = learn.TTA(is_test=True,n_aug=8)\npreds_t = np.stack(preds_t, axis=-1)\npreds_t = np.exp(preds_t)\npreds_t = preds_t.mean(axis=-1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd1625641e66106ddf4578e9337abf4625e61ba1"},"cell_type":"code","source":"sample_df = pd.read_csv(SAMPLE_SUB)\nsample_list = list(sample_df.Image)\npred_list = [[labels_list[i] for i in p.argsort()[-5:][::-1]] for p in preds_t]\npred_dic = dict((key, value) for (key, value) in zip(learn.data.test_ds.fnames,pred_list))\npred_list_cor = [' '.join(pred_dic[id]) for id in sample_list]\ndf = pd.DataFrame({'Image':sample_list,'Id': pred_list_cor})\ndf.to_csv('submission.csv'.format(MODEL_PATH), header=True, index=False)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e21788706e49812e60237cd8d5d0d9647767b4e7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}