{"cells":[{"metadata":{},"cell_type":"markdown","source":"This is my first notebook on Kaggle. Please leave your sugestions below.\nPlease upvote it if you find it useful and to show your support too. \nThanks. 👍"},{"metadata":{},"cell_type":"markdown","source":"I plan to write a code explanation for this and train the model further to improve the accuracy of the model. "},{"metadata":{},"cell_type":"markdown","source":"I have used the latest Fastai(Fastai version 2 library) to train the model and make predictions on test set."},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nprint(torch.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import fastai\nprint(fastai.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.vision.all import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path('../input/input/siim-isic-melanoma-classification/')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/siim-isic-melanoma-classification/train.csv')\nprint(\"Training samples\", train_df.shape)\nprint(\"First few samples of data are\",train_df.head())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.target.value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df_output_1 = train_df[train_df['target']==1]\ntrain_df.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df_output_0 = train_df[train_df['target']==0].sample(frac=0.03)\ntrain_df_output_0.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_df = pd.concat([train_df_output_0,train_df_output_1]).reset_index(drop=True)\nnew_df.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_df.head()\nnew_df.shape, \nnew_df.target.value_counts()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nskf = StratifiedKFold(n_splits=5, shuffle=True, random_state=12)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fold = 0\nfor train_index, test_index in skf.split(X=new_df.values, y=new_df.target.values):\n    fold+= 1\n    print(\"Fold\",fold)\n    print(\"TRAIN LENGTH:\", len(train_index), \"VALIDATION LENGTH:\", len(test_index))\n    new_df[f'fold_{fold}_valid']= 0\n    new_df.loc[test_index,f'fold_{fold}_valid']= 1\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imgpath = Path('../input/siim-isic-melanoma-classification/jpeg/train')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = ImageDataLoaders.from_df(new_df, path=imgpath,\n                               seed=42, fn_col=0, \n                               suff='.jpg', label_col=7, \n                               valid_col=f'fold_1_valid', item_tfms=Resize(128), \n                               batch_tfms=aug_transforms(flip_vert=True, max_warp=0.), \n                               bs=128, val_bs=None, shuffle_train=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(torch.cuda.is_available())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.device","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#learn = cnn_learner(dls,resnet34,metrics = [accuracy,roc_auc])\nlearn = cnn_learner(dls, resnet34, metrics=error_rate)\nlearn.fine_tune(1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samplesubmit = \"../input/siim-isic-melanoma-classification/sample_submission.csv\"\nsub = pd.read_csv(samplesubmit)\nsub.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(10982):\n    x = sub.at[i,'image_name']\n    imggpath = Path(\"../input/siim-isic-melanoma-classification/jpeg/test/\" + x + \".jpg\")\n    pr1,_,pr2 = learn.predict(imggpath)\n    sub.at[i,'target'] = float(pr2[int(pr1)])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.to_csv('sub1.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}