{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%matplotlib inline\n%reload_ext autoreload\n%autoreload 2","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from fastai.conv_learner import *\nfrom fastai.dataset import *\n\nfrom pathlib import Path\nimport json\n# torch.cuda.set_device(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6ccc641b3918b62225cff9e6a7fa69fa9929936"},"cell_type":"code","source":"PATH = Path('../input')\nlist(PATH.iterdir())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"622dc156fc92b65ec39c244bb7f8f56da9c61f79"},"cell_type":"code","source":"MASKS_FN = 'train_masks.csv'\nMETA_FN = 'metadata.csv'\nTRAIN_DN = 'train'\nMASKS_DN = 'train_masks'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c8d64b70a48e0d0776c98a62a13c1db1c96fd6c"},"cell_type":"code","source":"masks_csv = pd.read_csv(PATH/MASKS_FN)\nmasks_csv.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d44a6b14f2d1fc63537d1a66ef844fed98e3417c"},"cell_type":"code","source":"meta_csv = pd.read_csv(PATH/META_FN)\nmeta_csv.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c8f4a9532f37516e62ec88f29134e149980e22a6"},"cell_type":"code","source":"def show_img(im, figsize=None, ax=None, alpha=None):\n    if not ax: fig,ax = plt.subplots(figsize=figsize)\n    ax.imshow(im, alpha=alpha)\n    ax.set_axis_off()\n    return ax","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d18eb54bdc13f5e45a186d7433249453990e1c66"},"cell_type":"code","source":"CAR_ID = '00087a6bd4dc'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"541dfa9d7cfc9db62803f925fd516e3ae4511ddf"},"cell_type":"code","source":"list((PATH/TRAIN_DN).iterdir())[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ddebcbca1b59f84c7fcc335dd902c99e945af2cb"},"cell_type":"code","source":"Image.open(PATH/TRAIN_DN/f'{CAR_ID}_01.jpg').resize((300, 200))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4feb672d489ce81a024e6179d86c574992ea3bfe"},"cell_type":"code","source":"list((PATH/MASKS_DN).iterdir())[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9f8a01af1c766b3d48ae1faeb05fa82600f10c68"},"cell_type":"code","source":"Image.open(PATH/MASKS_DN/f'{CAR_ID}_01_mask.gif').resize((300, 200))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b36d4453ab53536494e091499ff5ae77e635c2ed"},"cell_type":"code","source":"ims = [open_image(PATH/TRAIN_DN/f'{CAR_ID}_{i+1:02d}.jpg') for i in range(16)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6495ff88afdf55c3894857b636bd113452a4d435"},"cell_type":"code","source":"fig, axes = plt.subplots(4, 4, figsize=(9, 6))\nfor i, ax in enumerate(axes.flat):\n    show_img(ims[i], ax=ax)\nplt.tight_layout(pad=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54450119d8287172b496426367cc034835b29889"},"cell_type":"code","source":"!mkdir train_masks_png ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b78eab657d55bb58e36a0a51966e47d9c63f30e"},"cell_type":"code","source":"def convert_img(fn):\n    fn = fn.name\n#     print(fn)\n    Image.open(PATH/'train_masks'/fn).save(Path(f'train_masks_png/{fn[:-4]}.png'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"73a1d69516a4443025bf5fa34807a4c7e2f3160e"},"cell_type":"code","source":"files = list((PATH/'train_masks').iterdir())\nwith ThreadPoolExecutor(8) as e:\n    e.map(convert_img, files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bbd80a05bc5d57e9d1f8b5ee5f3d6c2556d212b2"},"cell_type":"code","source":"!mkdir train_masks-128","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f95ed4b1431424cab0df16c2b80d3cd010f7a781"},"cell_type":"code","source":"def resize_mask(fn):\n    Image.open(fn).resize((128,128)).save((fn.parent.parent)/'train_masks-128'/fn.name)\n\nfiles = list((Path('train_masks_png')).iterdir())\nwith ThreadPoolExecutor(8) as e: e.map(resize_mask, files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e23a8af5073fad649c1044f2bce6d9591e92a311"},"cell_type":"code","source":"!mkdir 'train-128'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3eac78857455eab4d4536311824990615b43a6d2"},"cell_type":"code","source":"def resize_img(fn):\n    Image.open(fn).resize((128, 128)).save(f'train-128/{fn.name}')\n\nfiles = list((PATH/'train').iterdir())\nwith ThreadPoolExecutor(8) as e:\n    e.map(resize_img, files)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d681fb27e31a9e271da6741805ba06524cff0b62"},"cell_type":"markdown","source":"### Dataset"},{"metadata":{"trusted":true,"_uuid":"2ea34d5eb45584f90d1a1734f0aaccb37f64c269"},"cell_type":"code","source":"TRAIN_DN = Path('train-128')\nMASKS_DN = Path('train_masks-128')\nsz = 128\nbs = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6322d0daf7c6bc1455b41a0b63d44175d67b09bf"},"cell_type":"code","source":"ims = [open_image(TRAIN_DN/f'{CAR_ID}_{i+1:02d}.jpg') for i in range(16)]\nim_masks = [open_image(MASKS_DN/f'{CAR_ID}_{i+1:02d}_mask.png') for i in range(16)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bcbc38fe05fd440c2713f6dd2a1c0fb664a510bd"},"cell_type":"code","source":"fig, axes = plt.subplots(4, 4, figsize=(9, 6))\nfor i, ax in enumerate(axes.flat):\n    ax = show_img(ims[i], ax=ax)\n    show_img(im_masks[i][..., 0], ax=ax, alpha=0.5)\nplt.tight_layout(pad=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"465e4c4de29249ee8ca91ca5473832c27235b562"},"cell_type":"code","source":"class MatchedFilesDataset(FilesDataset):\n    def __init__(self, fnames, y, transform, path):\n        self.y=y\n        assert(len(fnames)==len(y))\n        super().__init__(fnames, transform, path)\n    def get_y(self, i): return open_image(os.path.join(self.path, self.y[i]))\n    def get_c(self): return 0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbd6dbf6e51ba354c1fba39783358c3878eb957f"},"cell_type":"code","source":"x_names = np.array([Path(TRAIN_DN)/o for o in masks_csv['img']])\ny_names = np.array([Path(MASKS_DN)/f'{o[:-4]}_mask.png' for o in masks_csv['img']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"63d2d44c534319a45677a6d77673e318d65fb1ad"},"cell_type":"code","source":"len(x_names)//16//5*16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bed5da16c8ec2166ace60b28d3ae686ec0106828"},"cell_type":"code","source":"val_idxs = list(range(1008))\n((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, x_names, y_names)\nlen(val_x),len(trn_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d474e100b0d13a511f0d42fb717422790a1b433"},"cell_type":"code","source":"aug_tfms = [RandomRotate(4, tfm_y=TfmType.CLASS),\n            RandomFlip(tfm_y=TfmType.CLASS),\n            RandomLighting(0.05, 0.05)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"419f7072413a4abbe17d008f556e9520b37b58af"},"cell_type":"code","source":"tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)\ndatasets = ImageData.get_ds(MatchedFilesDataset, (trn_x, trn_y), (val_x, val_y), tfms, path='/kaggle/working')\nmd = ImageData(TRAIN_DN, datasets, bs, num_workers=8, classes=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27f724b1aea85b100fd8bc520637aedfeb440649"},"cell_type":"code","source":"denorm = md.trn_ds.denorm\nx, y = next(iter(md.aug_dl))\nx = denorm(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"43ed9404d3f5fdb149b798b264d27b82bd37b16f"},"cell_type":"code","source":"fig, axes = plt.subplots(5, 6, figsize=(12, 10))\nfor i, ax in enumerate(axes.flat):\n    ax = show_img(x[i],ax=ax)\n    show_img(y[i], ax=ax, alpha=0.5)\nplt.tight_layout(pad=0.1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ea04f83a8b0f9b42bb98c766aaccd2ad80a90e4e"},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true,"_uuid":"2a73370206747f9baaddc88bd14408f1b1b9804f"},"cell_type":"code","source":"class Empty(nn.Module):\n    def forward(self, x): return x\n\nmodels = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=Empty())\nlearn = ConvLearner(md, models)\nlearn.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4bef6669a463c50d3bdbb117e8956602e0bf1ba0"},"cell_type":"code","source":"class StdUpsample(nn.Module):\n    def __init__(self, nin, nout):\n        super().__init__()\n        self.conv = nn.ConvTranspose2d(nin, nout, 2, stride=2)\n        self.bn = nn.BatchNorm2d(nout)\n    \n    def forward(self, x):\n        return self.bn(F.relu(self.conv(x)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6b5445c3d95bda7d0907da45b9e9a2f73c48de8"},"cell_type":"code","source":"flatten_channel = Lambda(lambda x: x[:, 0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"97be4a40e24c090782b2d628eb9e1e7abb57a85e"},"cell_type":"code","source":"simple_up = nn.Sequential(\n    nn.ReLU(),\n    StdUpsample(512, 256),\n    StdUpsample(256, 256),\n    StdUpsample(256, 256),\n    StdUpsample(256, 256),\n    nn.ConvTranspose2d(256, 1, 2, stride=2),\n    flatten_channel\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6cd95731ad024cfc6d5f127d395f7b9b6323ee3e"},"cell_type":"code","source":"models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=simple_up)\nlearn = ConvLearner(md, models)\nlearn.opt_fn = optim.Adam\nlearn.crit = nn.BCEWithLogitsLoss()\nlearn.metrics = [accuracy_thresh(0.5)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"adbd4364675a5362610c53dbd703ce7825c464cc"},"cell_type":"code","source":"learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d38ee51106b48b8bae15ed76ee458a86db98a0b2"},"cell_type":"code","source":"lr = 4e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"764a678970a8368b2015e52007839b628923c0a7"},"cell_type":"code","source":"learn.fit(lr, 1, cycle_len=5, use_clr=(20, 5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb3317c3712acab4b30471c1a555efd251dfbd8d"},"cell_type":"code","source":"learn.save('tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c946d6c6182650604eb5c0f14f1ea1292495ca1b"},"cell_type":"code","source":"learn.load('tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c53b53253c1e06c05ff6ca854530eb3f7381f0f8"},"cell_type":"code","source":"py, ay = learn.predict_with_targs()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3697b7cf7ed59842c8abd3793126b66b09700ba9"},"cell_type":"code","source":"ay.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"de4df7b30448f50fc91ced30e4ec19d7cbee93f0"},"cell_type":"code","source":"show_img(ay[0]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c42ef6b2b0f8a58390b1b7c7fc770822ea4ce767"},"cell_type":"code","source":"show_img(py[0]>0);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5725646859661a12645a8eac7b4ad31f640e2d23"},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd1ae16236c9f183c5ce13bd6fbdaac2df19d090"},"cell_type":"code","source":"learn.bn_freeze(True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2050a0394661c5201c2b3269ad191f9c193ecce9"},"cell_type":"code","source":"lrs = np.array([lr/100, lr/10, lr])/4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"881bbec14a5ef153c64f9c95c183f0ca107b30b1"},"cell_type":"code","source":"learn.fit(lrs, 1, cycle_len=20, use_clr=(20, 10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c12cbaae50266a3e3e5a2c8dd71e08ebdc0f04d"},"cell_type":"code","source":"\nlearn.save('0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4359ee97f542ddab8ef1a650a9467d7e70b58564"},"cell_type":"code","source":"x,y = next(iter(md.val_dl))\npy = to_np(learn.model(V(x)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19dd47e7391b0a9c527e0fb54d69b9ea4e05701d"},"cell_type":"code","source":"ax = show_img(denorm(x)[0])\nshow_img(py[0]>0, ax=ax, alpha=0.5);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dfc6d64bcc71d831c98b91a42866cc7c09d05c98"},"cell_type":"code","source":"ax = show_img(denorm(x)[0])\nshow_img(y[0], ax=ax, alpha=0.5);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b40c43c3140f2a34c1fb0b14526992c260f6af18"},"cell_type":"markdown","source":"# 512x512"},{"metadata":{"trusted":true,"_uuid":"ee5023563f09e47ccb8303e5f27860affe53f39e"},"cell_type":"code","source":"TRAIN_DN = Path('train')\nMASKS_DN = Path('train_masks_png')\nsz = 512\nbs = 16","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7ca78962fae5ce5b92fa7aa58358a5dde4092c6"},"cell_type":"code","source":"x_names = np.array([PATH/TRAIN_DN/o for o in masks_csv['img']])\ny_names = np.array([Path(MASKS_DN)/f'{o[:-4]}_mask.png' for o in masks_csv['img']])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2f7bcd51ac9388a86aebdad67812b670c7308067"},"cell_type":"code","source":"x_names","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e5f4b8726bd5407dfe674195f9a95f13f14938f"},"cell_type":"code","source":"((val_x, trn_x), (val_y, trn_y)) = split_by_idx(val_idxs, x_names, y_names)\nlen(val_x), len(trn_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"133b7978a6624bef3cf2464cba6320d76b050672"},"cell_type":"code","source":"tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)\ndatasets = ImageData.get_ds(MatchedFilesDataset, (trn_x, trn_y), (val_x, val_y), tfms, path='/kaggle/working')\nmd = ImageData(TRAIN_DN, datasets, bs, num_workers=8,classes=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb80d95d7336ba09d87109f66870841490ff7f6f"},"cell_type":"code","source":"denorm = md.trn_ds.denorm\nx, y = next(iter(md.aug_dl))\nx = denorm(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c2130b507aac835ab531d4f9887e2deaa615d1f"},"cell_type":"code","source":"fig, axes = plt.subplots(4, 4, figsize = (10, 10))\nfor i, ax in enumerate(axes.flat):\n    ax = show_img(x[i], ax = ax)\n    show_img(y[i], ax = ax, alpha = 0.5)\nplt.tight_layout(pad=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9cd8e6027144106b7fe12f3df3dddef0468edac"},"cell_type":"code","source":"simple_up = nn.Sequential(\n    nn.ReLU(),\n    StdUpsample(512, 256),\n    StdUpsample(256, 256),\n    StdUpsample(256, 256),\n    StdUpsample(256, 256),\n    nn.ConvTranspose2d(256,1, 2, stride=2),\n    flatten_channel\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52256ce4c2f09d8898cfd61807d7f6059d6cd783"},"cell_type":"code","source":"TMP_PATH = \"/tmp/tmp\"\nMODEL_PATH = \"/tmp/model/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"541eaf3706c7465ccf587e87010a639f4d1f071b"},"cell_type":"code","source":"!mkdir {TMP_PATH}\n!mkdir {MODEL_PATH}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8f5e5ffbe7294295e8e35e036c18484bab80797"},"cell_type":"code","source":"models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=simple_up)\nlearn = ConvLearner(md, models, tmp_name=TMP_PATH, models_name=MODEL_PATH)\nlearn.opt_fn = optim.Adam\nlearn.crit = nn.BCEWithLogitsLoss()\nlearn.metrics = [accuracy_thresh(0.5)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"29a51707bb7ca40bf3f03ff07bd9b0f27be380b8"},"cell_type":"code","source":"learn.load('0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"40bcdbfce39a5c05e6e070002ccb5430aebeb5ef"},"cell_type":"code","source":"learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"51b1a7ad21a9706bd5d83a6e9b91b49f66b51941"},"cell_type":"code","source":"lr = 4e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03713461cd5bf924a4ee19b788b9e49c5d12034c"},"cell_type":"code","source":"learn.fit(lr, 1, cycle_len=2, use_clr=(20, 4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e4c8590774ad103599c638c3f6c6f91ed9b500c"},"cell_type":"code","source":"learn.save('tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a74fe8512f8730619d85122434986955694a7439"},"cell_type":"code","source":"learn.load('tmp')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c2dec038af3d5673262f87e3c9013b57fc78856"},"cell_type":"code","source":"learn.unfreeze()\nlearn.bn_freeze(True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"447376a96e32329af08c5698f957d4f0cee0bf68"},"cell_type":"code","source":"lrs = np.array([lr/100, lr/10, lr])/8","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"f63940e8279f8e3fdc37e7a58e5523fa39076973"},"cell_type":"code","source":"learn.fit(lrs, 1, cycle_len=2, use_clr=(20, 10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84afdfc7b3f5aadc7d1f7c152e391f5d48576c99"},"cell_type":"code","source":"learn.save('512')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9cf237c28fbfb2ace8a633da4998cca0d2da4b7"},"cell_type":"code","source":"x, y = next(iter(md.val_dl))\npy = to_np(learn.model(V(x)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5ac45ea7a470faef94f1239f7605687b151fb65c"},"cell_type":"code","source":"ax = show_img(denorm(x)[0])\nshow_img(py[0]>0, ax=ax, alpha=0.5);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"06b6b45f91b555059132ff15865c972e0ee86940"},"cell_type":"code","source":"ax = show_img(denorm(x)[0])\nshow_img(y[0], ax=ax, alpha=0.5);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0baf79f021b6aea6a183bd8d8b52ff998fe9fd0f"},"cell_type":"markdown","source":"# 1024x1024"},{"metadata":{"trusted":true,"_uuid":"a6aa2897c821b4a64b058b82e2d17943364b3054"},"cell_type":"code","source":"sz = 1024\nbs = 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f6e2dca1e1662229bf95bb6e0d1ec81795d257b"},"cell_type":"code","source":"tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)\ndatasets = ImageData.get_ds(MatchedFilesDataset, (trn_x, trn_y), (val_x, val_y), tfms, path='/kaggle/working')\nmd = ImageData(PATH, datasets, bs, num_workers=8, classes=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3fdaa281d99c7c08e05f76613f9982488a372ee"},"cell_type":"code","source":"denorm = md.trn_ds.denorm\nx,y = next(iter(md.aug_dl))\nx = denorm(x)\ny = to_np(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"124c0678402f75b92e4edfb196d94eb492e90006"},"cell_type":"code","source":"fig, axes = plt.subplots(2, 2, figsize=(8, 8))\nfor i, ax in enumerate(axes.flat):\n    show_img(x[i], ax = ax)\n    show_img(y[i], ax = ax, alpha = 0.5)\nplt.tight_layout(pad=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"445a46349c6aaa82bfef5d1618b08837c629342e"},"cell_type":"code","source":"simple_up = nn.Sequential(\n    nn.ReLU(),\n    StdUpsample(512, 256),\n    StdUpsample(256, 256),\n    StdUpsample(256, 256),\n    StdUpsample(256, 256),\n    nn.ConvTranspose2d(256, 1, 2, stride=2),\n    flatten_channel\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5f0dd07ed9046704edc1888938fe4de1a3a56b0e"},"cell_type":"code","source":"models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=simple_up)\nlearn = ConvLearner(md, models, tmp_name=TMP_PATH, models_name=MODEL_PATH)\nlearn.opt_fn = optim.Adam\nlearn.crit = nn.BCEWithLogitsLoss()\nlearn.metrics = [accuracy_thresh(0.5)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ac5d024b8a10e37ee236e4b64d7bbabd904a6205"},"cell_type":"code","source":"learn.load('512')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"441fe203990185a46835d3b2400be6cc2e3a5c72"},"cell_type":"code","source":"learn.lr_find()\nlearn.sched.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d543b83a8f526310ae5b300aa1cb928e12e93953"},"cell_type":"code","source":"lr = 4e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ddfa01945cbdd3ad17ae0d051220763ed2f6021"},"cell_type":"code","source":"learn.fit(lr, 1, cycle_len=2, use_clr=(20, 4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5aa4e69b38f2999f539a67397ce07e8c3e7790fd"},"cell_type":"code","source":"learn.save('1024')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4383dfcb1dbb1d045a894ca3b27a880e05a527a1"},"cell_type":"code","source":"x, y = next(iter(md.val_dl))\npy = to_np(learn.model(V(x)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"871b7bbe24e3f054ad836297b7f659b8e42a3b70"},"cell_type":"code","source":"ax = show_img(denorm(x)[0])\nshow_img(py[0][0]>0, ax = ax, alpha = 0.5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0b2f79671f8b72a9a11a41e7649a82d78a7e2545"},"cell_type":"code","source":"ax = show_img(denorm(x)[0])\nshow_img(y[0,...,-1], ax = ax, alpha=0.5);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"48e3181f10d8d69baf6edb2beafa41971e7941bc"},"cell_type":"code","source":"show_img(py[0][0]>0);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"991f3cb184d6bbe6a6fc43d782bbf5fa06a9788f"},"cell_type":"code","source":"show_img(y[0,...,-1]);","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}