{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":20270,"databundleVersionId":1222630,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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 20GB 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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-15T14:46:57.816941Z","iopub.execute_input":"2024-01-15T14:46:57.817794Z","iopub.status.idle":"2024-01-15T14:46:58.174812Z","shell.execute_reply.started":"2024-01-15T14:46:57.817753Z","shell.execute_reply":"2024-01-15T14:46:58.173695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -Uqq fastai","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:46:58.177164Z","iopub.execute_input":"2024-01-15T14:46:58.177956Z","iopub.status.idle":"2024-01-15T14:47:12.971635Z","shell.execute_reply.started":"2024-01-15T14:46:58.177918Z","shell.execute_reply":"2024-01-15T14:47:12.970295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastcore.all import *\nfrom fastai import *\nfrom fastai.vision import *\nimport time\nfrom fastai.vision.all import *\nfrom fastai.vision.widgets import *\nimport pathlib","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:12.973649Z","iopub.execute_input":"2024-01-15T14:47:12.974662Z","iopub.status.idle":"2024-01-15T14:47:19.016056Z","shell.execute_reply.started":"2024-01-15T14:47:12.974619Z","shell.execute_reply":"2024-01-15T14:47:19.015160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/siim-isic-melanoma-classification","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:19.017196Z","iopub.execute_input":"2024-01-15T14:47:19.017488Z","iopub.status.idle":"2024-01-15T14:47:20.008499Z","shell.execute_reply.started":"2024-01-15T14:47:19.017462Z","shell.execute_reply":"2024-01-15T14:47:20.007312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/siim-isic-melanoma-classification')\n","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:20.012698Z","iopub.execute_input":"2024-01-15T14:47:20.013106Z","iopub.status.idle":"2024-01-15T14:47:20.018105Z","shell.execute_reply.started":"2024-01-15T14:47:20.013073Z","shell.execute_reply":"2024-01-15T14:47:20.017054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/input/siim-isic-melanoma-classification/jpeg/test | head","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:20.019316Z","iopub.execute_input":"2024-01-15T14:47:20.019573Z","iopub.status.idle":"2024-01-15T14:47:21.586509Z","shell.execute_reply.started":"2024-01-15T14:47:20.019550Z","shell.execute_reply":"2024-01-15T14:47:21.585279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head /kaggle/input/siim-isic-melanoma-classification/train.csv","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:21.588233Z","iopub.execute_input":"2024-01-15T14:47:21.588561Z","iopub.status.idle":"2024-01-15T14:47:22.583586Z","shell.execute_reply.started":"2024-01-15T14:47:21.588532Z","shell.execute_reply":"2024-01-15T14:47:22.582251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:22.585561Z","iopub.execute_input":"2024-01-15T14:47:22.586311Z","iopub.status.idle":"2024-01-15T14:47:22.591042Z","shell.execute_reply.started":"2024-01-15T14:47:22.586267Z","shell.execute_reply":"2024-01-15T14:47:22.590025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/siim-isic-melanoma-classification/train.csv')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:22.592489Z","iopub.execute_input":"2024-01-15T14:47:22.592817Z","iopub.status.idle":"2024-01-15T14:47:22.703635Z","shell.execute_reply.started":"2024-01-15T14:47:22.592788Z","shell.execute_reply":"2024-01-15T14:47:22.702865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:22.704732Z","iopub.execute_input":"2024-01-15T14:47:22.705073Z","iopub.status.idle":"2024-01-15T14:47:22.729550Z","shell.execute_reply.started":"2024-01-15T14:47:22.705045Z","shell.execute_reply":"2024-01-15T14:47:22.728536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_data = train.groupby('target', group_keys=False).apply(lambda x: x.sample(frac=0.025))\nsub_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:22.730731Z","iopub.execute_input":"2024-01-15T14:47:22.731044Z","iopub.status.idle":"2024-01-15T14:47:22.764255Z","shell.execute_reply.started":"2024-01-15T14:47:22.731016Z","shell.execute_reply":"2024-01-15T14:47:22.763377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"jpg_path = '../input/siim-isic-melanoma-classification/jpeg'\n\ndls = ImageDataLoaders.from_df(df = sub_data, \n                               path = jpg_path,    \n                               folder = 'train',   \n                               suff = '.jpg',      \n                               label_col = 6,      \n                               valid_pct = 0.2,    \n                               bs = 8,            \n                               item_tfms = Resize(128))    \n\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:22.765368Z","iopub.execute_input":"2024-01-15T14:47:22.765657Z","iopub.status.idle":"2024-01-15T14:47:27.339332Z","shell.execute_reply.started":"2024-01-15T14:47:22.765631Z","shell.execute_reply":"2024-01-15T14:47:27.338419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls,\n                       resnet50,  \n                       metrics=error_rate )","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:27.340716Z","iopub.execute_input":"2024-01-15T14:47:27.341052Z","iopub.status.idle":"2024-01-15T14:47:28.592264Z","shell.execute_reply.started":"2024-01-15T14:47:27.341022Z","shell.execute_reply":"2024-01-15T14:47:28.591434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# using train    :54mins\n# using subdata  : 1:41\nlearn.fine_tune(1)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:47:28.595566Z","iopub.execute_input":"2024-01-15T14:47:28.595849Z","iopub.status.idle":"2024-01-15T14:50:29.443812Z","shell.execute_reply.started":"2024-01-15T14:47:28.595824Z","shell.execute_reply":"2024-01-15T14:50:29.442803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn2 = vision_learner(dls,\n                       models.vgg16_bn,  \n                       metrics=error_rate )","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:50:29.445285Z","iopub.execute_input":"2024-01-15T14:50:29.445624Z","iopub.status.idle":"2024-01-15T14:50:33.252014Z","shell.execute_reply.started":"2024-01-15T14:50:29.445595Z","shell.execute_reply":"2024-01-15T14:50:33.250956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(3)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:50:33.253494Z","iopub.execute_input":"2024-01-15T14:50:33.254000Z","iopub.status.idle":"2024-01-15T14:56:24.024350Z","shell.execute_reply.started":"2024-01-15T14:50:33.253961Z","shell.execute_reply":"2024-01-15T14:56:24.023273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Image.open(path/'jpeg/train/ISIC_6786198.jpg').to_thumb(256,256)\npred_class,pred_idx,outputs = learn2.predict(img)\nprob_malignant = float(outputs[1])\n\nprint(pred_class)\nprint(prob_malignant)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:56:24.025814Z","iopub.execute_input":"2024-01-15T14:56:24.026138Z","iopub.status.idle":"2024-01-15T14:56:24.215358Z","shell.execute_reply.started":"2024-01-15T14:56:24.026098Z","shell.execute_reply":"2024-01-15T14:56:24.214401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.sample(10)","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:56:24.218514Z","iopub.execute_input":"2024-01-15T14:56:24.219378Z","iopub.status.idle":"2024-01-15T14:56:24.239814Z","shell.execute_reply.started":"2024-01-15T14:56:24.219345Z","shell.execute_reply":"2024-01-15T14:56:24.238729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export('/kaggle/working/modelv2.pkl')","metadata":{"execution":{"iopub.status.busy":"2024-01-15T14:56:24.241019Z","iopub.execute_input":"2024-01-15T14:56:24.241342Z","iopub.status.idle":"2024-01-15T14:56:24.473284Z","shell.execute_reply.started":"2024-01-15T14:56:24.241313Z","shell.execute_reply":"2024-01-15T14:56:24.472393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}