{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Setup\n\n\nThis involves importing necessary libraries and data for us to run our model\n\n\n---\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Just checking if we have a GPU\n!nvidia-smi","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Cloning the monk repository as we are going to use the MonkAI Library\n!git clone https://github.com/Tessellate-Imaging/monk_v1.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Installing the dependencies for Kaggle required by Monk\n!pip install -r monk_v1/installation/Misc/requirements_kaggle.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r monk_v1/.git\n! rm -r monk_v1/installation\n! rm -r monk_v1/study_roadmaps\n! rm -r monk_v1/webinars_lectures","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Appending the Monk repo to our working directory\nimport sys\nsys.path.append(\"/kaggle/working/monk_v1/monk/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\n\nimport pandas as pd\ndf = pd.read_csv(\"../input/siim-isic-melanoma-classification/train.csv\")\n\ncombined = [];\nfrom tqdm.notebook import tqdm\nfor i in tqdm(range(len(df))):\n    img_name = df[\"image_name\"][i] + \".jpg\";\n    if(df[\"benign_malignant\"][i] == 'benign'):\n        label = \"0\";\n    elif(df[\"benign_malignant\"][i] == 'malignant'):\n        label = \"1\"; \n    combined.append([img_name, label]);\n    \ndf2 = pd.DataFrame(combined, columns = ['ID', 'Label']) \ndf2.to_csv(\"train.csv\", index=False);\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Setting up the Model\n Here we will import the desired backend and base network on which we want our model to run the classification dataset\n \n\n---","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# Using mxnet backend\nfrom gluon_prototype import prototype","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Defining path for training and validation dataset\ntrain_path = '../input/siim-isic-melanoma-classification/jpeg/train'\ncsv_train = 'train.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Initialize the protoype model and setup project directory\ngtf=prototype(verbose=1)\ngtf.Prototype(\"Melanoma-Detection\", \"Hyperparameter-Analyser\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Define the prototype with default parameters\ngtf.Default(dataset_path=train_path,\n            path_to_csv=csv_train,\n           model_name=\"se_resnext101_64x4d\",\n           freeze_base_network=False,\n           num_epochs=5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Train()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm pylg.log train.csv","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Inference\nRunning inference on test dataset\n\n---","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf = prototype(verbose=1)\ngtf.Prototype(\"Melanoma-Detection\", \"Hyperparameter-Analyser\",eval_infer = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom tqdm.notebook import tqdm\nfrom scipy.special import softmax\ndf = pd.read_csv(\"../input/siim-isic-melanoma-classification/sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport os\nfrom IPython.display import FileLink\nfor i in tqdm(range(len(df))):\n    img_name = \"../input/siim-isic-melanoma-classification/jpeg/test/\" + df['image_name'][i] + \".jpg\";\n    \n    #Invoking Monk's inferencing engine inside a loop\n    predictions = gtf.Infer(img_name=img_name, return_raw=True);\n    out = predictions['raw']\n    prob_mal = ((np.exp(out[1]))/(np.exp(out[0])+np.exp(out[1])))\n    df['target'][i] = str(prob_mal)\n    print(\"Probability: \", df['target'][i])\n\nos.chdir(r'kaggle/working')\ndf.to_csv(\"submission.csv\", index=False)\nFileLink(r'submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r monk_v1\n! rm -r workspace","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}