{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n%reload_ext autoreload\n%autoreload 2\n%matplotlib inline\n\nnp.random.seed(1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Load images and apply some basic transformations and augmentations\ntrainCsvPath = '/kaggle/input/jpeg-melanoma-768x768/train.csv'\nimgPath = '/kaggle/input/jpeg-melanoma-768x768/train'\ndf = pd.read_csv(trainCsvPath)\ndf['withJpg']=df['image_name']+'.jpg'\ntfms = get_transforms(flip_vert=True, max_zoom=1.05, max_warp=0.)\ndata = ImageDataBunch.from_df(imgPath, df, fn_col='withJpg', label_col='benign_malignant', ds_tfms=tfms, size=224, bs=256)\ndata.normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fullSizeData = ImageDataBunch.from_df(imgPath, df, fn_col='withJpg', label_col='benign_malignant', ds_tfms=tfms, size=768, bs=32)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#can use data instead of fullSizeData here when testing for faster processing\nlearn = create_cnn(data, models.resnet34, metrics=error_rate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.data = fullSizeData","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.freeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.model_dir = '../../../kaggle/working'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(1, max_lr=slice(3e-6,3e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('stage-2')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntestImageFilePaths = get_image_files('/kaggle/input/jpeg-melanoma-768x768/test')\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"outputDf = pd.DataFrame(columns=['image_name','target'])\n\ntestDir = '/kaggle/input/jpeg-melanoma-768x768/test/'\ni = 1\nfor filename in os.listdir(testDir):\n    fullFilePath = testDir + filename\n    classification,_,probabilities = learn.predict(open_image(fullFilePath))\n    malignProbability = 1-probabilities[0]\n    if str(classification) == 'malignant':\n        malignProbability = probabilities[0]       \n    formatted = float(\"{:.7f}\".format(malignProbability))\n    outputDf = outputDf.append({'image_name': filename[:-4],'target':formatted}, ignore_index=True)\n    if i % 1000 == 0:\n        print(f'{i}/{len(os.listdir(testDir))}')\n    i+=1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"outputDf.to_csv('output.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}