{"cells":[{"metadata":{},"cell_type":"markdown","source":"# This is a beginner kernel to showcase the use of fastai2 to approach this competition","execution_count":null},{"metadata":{"trusted":true,"_kg_hide-output":true,"collapsed":true},"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\nfn = glob.glob('/kaggle/input/siic-isic-224x224-images/train/*.*')\nlen(fn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install fastai2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport random\nimport geopandas as gpd\nimport rasterio\nfrom PIL import Image\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nfrom fastai2.torch_basics import *\nfrom fastai2.basics import *\nfrom fastai2.data.all import *\nfrom fastai2.callback.all import *\nfrom fastai2.vision.all import *\nfrom fastai2.test_utils import *\nfrom fastai2.vision.core import *\nfrom fastai2.metrics import *\nfrom sklearn.metrics import roc_auc_score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/train.csv')\ntrain.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Here we follow the strategy of downsampling the class 0 to ensure that the training data has equal number of both samples**","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_1 = train[train['target']==1]\ntrain_1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_0 = train[train['target']==0].sample(frac=0.018)\ntrain_0.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.concat([train_0,train_1]).reset_index(drop=True)\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Read an image\nimg = Image.open('/kaggle/input/siic-isic-224x224-images/train/ISIC_0645834.png')\nplt.imshow(np.asarray(img))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# CHeck shape and number of True labels\ndf.shape, df.target.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['file_name'] = df['image_name'].apply(lambda x: f\"/kaggle/input/siic-isic-224x224-images/train/{x}\"+\".png\" )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#df['target']=df['target'].apply(lambda x: float(x))\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nskf = StratifiedKFold(n_splits=3,shuffle=True,random_state=42)\nX=df['file_name'].copy()\ny=df['target'].copy()\nfold = 0\nfor train_index, test_index in skf.split(X, y):\n    fold+= 1\n    print('In fold',fold)\n    print(\"TRAIN LENGTH:\", len(train_index), \"VALIDATION LENGTH:\", len(test_index))\n    df[f'fold_{fold}_valid']=False\n    df.loc[test_index,f'fold_{fold}_valid']=True","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/test.csv')\ntest.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test['file_name'] = test['image_name'].apply(lambda x: f\"/kaggle/input/siic-isic-224x224-images/test/{x}\"+\".png\" )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ss = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')\nss.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#roc_auc=skm_to_fastai(roc_auc_score)\nroc_auc = RocAuc()\nmetrics = [accuracy,roc_auc]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def dataloader(fold):\n    tfm=aug_transforms(do_flip=True, flip_vert=True, max_rotate=45.0, max_zoom=1.1, size=224,max_lighting=0.2, max_warp=0.4, p_affine=0.75, p_lighting=0.75, xtra_tfms=None, mode='bilinear')\n    dls = ImageDataLoaders.from_df(df, fn_col='file_name',label_col='target', valid_col=f'fold_{fold}_valid',path='', folder='/', seed=42,batch_tfms = [*tfm, Normalize.from_stats(*imagenet_stats)],bs=32,num_workers=0)\n    return dls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_preds=np.zeros((ss.shape[0],ss.shape[1]))\nfold = 0\nfor fold in range(3):\n    fold+=1\n    print('In fold:',fold)\n    dls=dataloader(fold)\n    learn = cnn_learner(dls,resnet34,metrics=metrics)\n    learn.fine_tune(10)\n    test_dl=learn.dls.test_dl(test)\n    preds, _ = learn.tta(dl=test_dl)\n    print('Prediction completed in fold: {}'.format(str(fold)))\n    final_preds+=preds.numpy()\n    \n\nfinal_preds=final_preds/3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test['target'] = final_preds[:,1]\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ss.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test[['image_name', 'target']].to_csv('Sub.csv', index=False)","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}