{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"14da00fd2d9e39a4b2a3024375fb8e30e92bc10a"},"cell_type":"code","source":"from fastai.conv_learner import *\nfrom fastai.dataset import *\n\nfrom pathlib import Path\nimport json\ntorch.cuda.set_device(0)\nfrom pathlib import Path\ntorch.cuda.set_device(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"712cf41b95d08b64eb6a2eacce63007a753cb010"},"cell_type":"code","source":"MASKS = 'train.csv'\nSUB = 'sample_submission.csv'\nTRAIN = Path('train/')\nTEST = Path('test/')\nPATH = Path('/kaggle/input/')\nTMP = Path('/kaggle/working/tmp/')\nMODEL = Path('/kaggle/working/model/')\n\nseg = pd.read_csv(PATH/MASKS).set_index('Id')\nsample_sub = pd.read_csv(PATH/SUB).set_index('Id')\n\nsample= 31072\nseg.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4a9fd1df3c6948cc66f7acd8bd1981ff40487563"},"cell_type":"code","source":"train_names_png = [TRAIN/f for f in os.listdir(PATH/TRAIN)]\ntrain_names = list(seg.index.values)\ntrain_names_sample = list(seg.index.values)[0:sample]\ntest_names_png = [TEST/f for f in os.listdir(PATH/TEST)]\ntest_names = list(sample_sub.index.values)\ntest_names_sample = list(sample_sub.index.values)[0:sample]\nlen(train_names_sample), len(test_names)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30faaaa4a7623a7cc66ab7e703b1497277f7a194"},"cell_type":"code","source":"TMP.mkdir(exist_ok=True)\nMODEL.mkdir(exist_ok=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30cf4c4eff0e3104892bf0348dcb2d71168ca078"},"cell_type":"code","source":"def rgba_open(fname, path=PATH, sz=128):\n    '''open RGBA image from 4 different 1-channel files.\n    return: numpy array [4, sz, sz]'''\n    flags = cv2.IMREAD_GRAYSCALE\n    red = cv2.imread(str(path/(fname+ '_red.png')), flags)\n    blue = cv2.imread(str(path/(fname+ '_blue.png')), flags)\n    green = cv2.imread(str(path/(fname+ '_green.png')), flags)\n    yellow = cv2.imread(str(path/(fname+ '_yellow.png')),flags)\n    im = np.array([red, green, blue, yellow], dtype=np.float32)\n    rgba = cv2.resize(np.rollaxis(im, 0,3), (sz, sz), interpolation = cv2.INTER_CUBIC)\n    return np.rollaxis(rgba, 2,0)/255\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36e81202b77ae5a08419dccdbb76baad751e870a"},"cell_type":"code","source":"train_names[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f688afb3f3f150b69bf6bfb5ea04f84b573272ed"},"cell_type":"code","source":"im = rgba_open(train_names[1], PATH/TRAIN); im.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88b6f47090534442174cb45275b64aad3cb61cba"},"cell_type":"code","source":"def open_rgby(path,id): #a function that reads RGBY image\n    colors = ['red','green','blue','yellow']\n    flags = cv2.IMREAD_GRAYSCALE\n    img = [cv2.imread(os.path.join(path, id+'_'+color+'.png'), flags).astype(np.float32)/255\n           for color in colors]\n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"560dd95a2fb61ad72b29499e50a36da7255a0413"},"cell_type":"code","source":"im = open_rgby(PATH/TRAIN, train_names[1]); np.stack(im).shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98f15fe713bf01f0eda87b9fc9abeee71a7d4341"},"cell_type":"code","source":"seg2 = seg.iloc[0:sample]\nval_idxs = get_cv_idxs(sample)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b1fa9d0c61c898f96de89520691d0547a84cdb61"},"cell_type":"code","source":"class CustomDataset(FilesDataset):\n    def __init__(self, fnames, y, transform, path, sz):\n        self.y=y\n        self.fnames = fnames\n        self.sz = sz\n        assert(len(fnames)==len(y))\n        super().__init__(fnames, transform, path)\n        \n    def get_x(self, i): \n        return rgba_open(self.fnames[i], self.path, self.sz)\n        \n    def get_y(self, i):\n        return self.y[i]\n    def get_sz(self): return self.sz\n    def get_c(self): return 28\n    @property\n    def is_multi(self):\n        return True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5c254c0d62f95403ff325d391d0cd20a36088907"},"cell_type":"code","source":"indexes = seg2.Target.apply(str.split)\ny = np.zeros((sample, 28))\nfor i in range(sample):\n    y[i,np.array(indexes[i], dtype=int)]=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f4fc9a6beb90e931b07a785481c34c3d27de9f6"},"cell_type":"code","source":"len(train_names_sample),  y.shape, y.dtype","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4bb91eb8375953649d680e6de6e2dfd2e35c812d"},"cell_type":"code","source":"((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, np.array(train_names_sample), y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94c96e20381707a9a0136077f1bb1848dcc89fee"},"cell_type":"code","source":"# tfms = tfms_from_model(resnet34, sz=sz, crop_type=CropType.NO, aug_tfms=[])\ndef get_data(sz=128, bs=32):\n    datasets = ImageData.get_ds(CustomDataset, (trn_x,trn_y), (val_x,val_y), sz=sz, tfms=(None,None), path=PATH/TRAIN)\n    datasets[4] = CustomDataset(test_names, test_names, None, PATH/TEST, sz)\n    return ImageData(PATH, datasets, bs=bs, num_workers=4, classes=28)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc74dda6b30139be3ab4f8a38f09ad67e7f22791"},"cell_type":"code","source":"md = get_data(128)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecf4e0652a81a8034109d91d603d0d0b3fecdb0a"},"cell_type":"code","source":"class ConvBN(nn.Module):\n    \"convolutional layer then batchnorm\"\n\n    def __init__(self, ch_in, ch_out, kernel_size = 3, stride=1, padding=0):\n        super().__init__()\n        self.conv = nn.Conv2d(ch_in, ch_out, kernel_size=kernel_size, stride=stride, padding=padding, bias=False)\n        self.bn = nn.BatchNorm2d(ch_out, momentum=0.01)\n        self.relu = nn.LeakyReLU(0.1, inplace=True)\n\n    def forward(self, x): return self.relu(self.bn(self.conv(x)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a02c2035ee950267564a21000b765b69e7d21ee7"},"cell_type":"code","source":"class DarknetBlock(nn.Module):\n    def __init__(self, ch_in):\n        super().__init__()\n        ch_hid = ch_in//2\n        self.conv1 = ConvBN(ch_in, ch_hid, kernel_size=1, stride=1, padding=0)\n        self.conv2 = ConvBN(ch_hid, ch_in, kernel_size=3, stride=1, padding=1)\n\n    def forward(self, x): return self.conv2(self.conv1(x)) + x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f4be203e73b30c456c3ae0288d36174d0ec4f04"},"cell_type":"code","source":"class Darknet(nn.Module):\n    \"Replicates the darknet classifier from the YOLOv3 paper (table 1)\"\n\n    def make_group_layer(self, ch_in, num_blocks, stride=1):\n        layers = [ConvBN(ch_in,ch_in*2,stride=stride)]\n        for i in range(num_blocks): layers.append(DarknetBlock(ch_in*2))\n        return layers\n\n    def __init__(self, num_blocks, num_classes=1000, start_nf=32):\n        super().__init__()\n        nf = start_nf\n        layers = [ConvBN(4, nf, kernel_size=3, stride=1, padding=1)]\n        for i,nb in enumerate(num_blocks):\n            layers += self.make_group_layer(nf, nb, stride=(1 if i==1 else 2))\n            nf *= 2\n        layers += [nn.AdaptiveAvgPool2d(1), Flatten(), nn.Linear(nf, num_classes)]\n#         layers += [nn.Sigmoid()]\n        self.layers = nn.Sequential(*layers)\n\n    def forward(self, x): return self.layers(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c937e712cd9f5326beae44dcfcb4698d42d22c95"},"cell_type":"code","source":"m = Darknet([1, 2, 4, 4, 3], 28).cuda()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1cdc81e975f70e85212a3ed31c118d1833ddfaf2"},"cell_type":"code","source":"m","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6282a93794a2d6bdd01b8455236a99314f2ef147"},"cell_type":"code","source":"class FocalLoss(nn.Module):\n    def __init__(self, gamma=2):\n        super().__init__()\n        self.gamma = gamma\n        \n    def forward(self, input, target):\n        if not (target.size() == input.size()):\n            raise ValueError(\"Target size ({}) must be the same as input size ({})\"\n                             .format(target.size(), input.size()))\n\n        max_val = (-input).clamp(min=0)\n        loss = input - input * target + max_val + \\\n            ((-max_val).exp() + (-input - max_val).exp()).log()\n\n        invprobs = F.logsigmoid(-input * (target * 2.0 - 1.0))\n        loss = (invprobs * self.gamma).exp() * loss\n        \n        return loss.sum(dim=1).mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cf833a0b6c43365f545ea1c363ea7bcd4b1f1741"},"cell_type":"code","source":"from sklearn.metrics import fbeta_score\nimport warnings\n\ndef f1_(preds, targs, start=0.17, end=0.24, step=0.01):\n    with warnings.catch_warnings():\n        warnings.simplefilter(\"ignore\")\n        return max([fbeta_score(targs, (preds>th), 1, average='samples')\n                    for th in np.arange(start,end,step)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9b0b404ac3941ef5a80f58426df3002b84b3098c"},"cell_type":"code","source":"learn = Learner.from_model_data(m, md, tmp_name=TMP, models_name=MODEL)\nlearn.crit = FocalLoss()\nlearn.opt_fn = optim.Adam\nlearn.metrics = [f1_]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"16e3bf6f18fe788ddf487a1493ac1963c633a4ec"},"cell_type":"code","source":"lr = 1E-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"549667fe39a3d057ab919d24d1ebd05e4597b678"},"cell_type":"code","source":"learn.fit(lr,1,cycle_len=30,use_clr_beta=(10,10, 0.85, 0.9))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd37111e79b85b2332ea843c29cceb59c383b6f2"},"cell_type":"code","source":"learn.save('128')","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}