{"cells":[{"metadata":{},"cell_type":"markdown","source":"This notebook's purpose is to show how to use EfficientNet for Skin Cancer Melonama competition. It needs to be tuned 'efficiently' to achieve good LB score","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install efficientnet-pytorch","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import glob\nimport numpy as np\nimport pandas as pd\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom fastai import *\nfrom fastai.vision import *\nfrom efficientnet_pytorch import EfficientNet","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/siim-isic-melanoma-classification-jpeg512/train.csv')\ntest_df = pd.read_csv('../input/extrafiles/test.csv')\nsubmission_df = pd.read_csv('../input/extrafiles/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"tfrm = get_transforms(do_flip = True, flip_vert = True, max_rotate = 20, max_zoom = 1.5, max_lighting = 0.5, max_warp = 0.5, p_affine = 0.75, p_lighting = 0.75)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.image_name = train_df.image_name.apply(lambda file : file+'.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.image_name = test_df.image_name.apply(lambda file : file+'.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_imgs = ImageList.from_df(test_df, path = '../input/siim-isic-melanoma-classification-jpeg512', folder = 'test512')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)\nsrc = ImageList.from_df(train_df, path = '../input/siim-isic-melanoma-classification-jpeg512', folder = 'train512')\\\n                      .split_by_rand_pct(0.2)\\\n                      .label_from_df(cols = -1)\\\n                      .add_test(test_imgs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"src","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = src.transform(tfrm, padding_mode = 'reflection', size = 128, resize_method = ResizeMethod.SQUISH).databunch(bs = 32, device = torch.device('cuda:0'))\\\n          .normalize(imagenet_stats)\n          #.databunch(bs = 32, device = torch.device('cuda:0'))\\","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = EfficientNet.from_name('efficientnet-b5')\nmodel._fc = nn.Linear(2048, data.c)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = Learner(data, model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.model_dir = '/kaggle/output/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for obj in gc.get_objects():\n    if torch.is_tensor(obj):\n        del obj\ngc.collect()\ntorch.cuda.empty_cache()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('efficient_baseline')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot(suggestion=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()\nlearn.fit_one_cycle(4, 1e-5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = os.listdir(Path('../input/siim-isic-melanoma-classification-jpeg512/test512'))\ntest.sort(key=lambda f: int(re.sub('\\D', '', f)))\n\nwith open('/kaggle/working/submission.csv', 'w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerow(['image_name', 'target'])\n    \n    for image_file in test:\n        image = os.path.join(Path('../input/siim-isic-melanoma-classification-jpeg512/test512'), image_file) \n        image_name = Path(image).stem\n\n        img = open_image(image)\n        pred_class,pred_idx,outputs = learn.predict(img)\n        target = float(outputs[1])\n\n        \n        writer.writerow([image_name, target])","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}