{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing the Packages\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\n\nfrom pathlib import Path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Installing Pytorch 1.6 for FastAI v2\n\n!pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Installing FastAI v2\n\n!pip install -Uqq fastbook\nimport fastbook\nfastbook.setup_book()\n\nfrom fastbook import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Checking if cuda is available to make use of GPU\n\nimport torch\nprint(torch.cuda.is_available())\nprint(torch.cuda.get_device_name(0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Seed everything\n\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    \nseed_everything(42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = Path(\"../input/aptos2019-blindness-detection\")\npath.ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's have a look at the training data\n\ntrain_df = pd.read_csv(path/\"train.csv\")\nprint(train_df.shape)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# The test data does not have any labels that we are going to predict\n\ntest_df = pd.read_csv(path/\"test.csv\")\nprint(test_df.shape)\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_path = path/\"train_images\"\ntrain_path.ls()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Number of images in train folder is same as number of records in the csv file. We can go ahead with creating DataBunch."},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's have a look at some of the images in the folder\n\nfig = plt.figure(figsize = (10,10))\nrows = 3\ncolumns = 3\nfor i in range(1, rows*columns+1):\n    img = Image.open(train_path.ls()[i])\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(img)\n    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['diagnosis'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"There is some imbalance in the classes. For now, we will proceed normally.\n<br> We may try some balancing in the future experiments."},{"metadata":{"trusted":true},"cell_type":"code","source":"LABEL_COLS = [0, 1, 2, 3, 4]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_dls(size, bs):\n    datablock = DataBlock(\n                blocks = (ImageBlock, CategoryBlock(vocab = LABEL_COLS)),\n                get_x = ColReader('id_code', pref = train_path, suff = '.png'),\n                get_y = ColReader('diagnosis'),\n                splitter = RandomSplitter(valid_pct = 0.2, seed= 42),\n                item_tfms = Resize(size)\n    )\n    \n    return datablock.dataloaders(train_df, bs= bs)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls = get_dls(224, 64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.show_batch()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dls.valid.show_batch()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Modelling"},{"metadata":{},"cell_type":"markdown","source":"A simple way to think this is that Cohen’s Kappa is a quantitative measure of reliability for two raters that are rating the same thing, corrected for how often that the raters may agree by chance.<br>\n<br>\nCohen suggested the Kappa result be interpreted as follows: values ≤ 0 as indicating no agreement and 0.01–0.20 as none to slight, 0.21–0.40 as fair, 0.41– 0.60 as moderate, 0.61–0.80 as substantial, and 0.81–1.00 as almost perfect agreement.\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"ARCH = densenet121\nquadratic_kappa = CohenKappa(weights = 'quadratic')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn = cnn_learner(dls, ARCH, metrics = [accuracy, quadratic_kappa])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.loss_func","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":"lr = 3e-3\nlearn.fit_one_cycle(10, lr)\nlearn.recorder.plot_loss()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.show_results(nrows=3, figsize=(6,8))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot Confusion Matrix\n\ninterp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(normalize = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Save the model\n\nlearn.save(\"densenet121-stage-1\")\nlearn = learn.load(\"densenet121-stage-1\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**If you like it , please upvote :)**"}],"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}