{"cells":[{"metadata":{},"cell_type":"markdown","source":"\nCNN model of Digit Recognizer using Pytoch\n\nHoon Kim (id : KeyboardWarrior6969)\n\n13/1/2020\n"},{"metadata":{},"cell_type":"markdown","source":"**1. Introduction**\n\n**2. Data Preparation**\n\n**3. CNN Model**\n\n- 3.1 Annealer\n- 3.2 Model Definition\n- 3.3 Optimizer\n\n**4. Model Performance**\n\n- 4.1 Validation and Training curves"},{"metadata":{},"cell_type":"markdown","source":"### 1. Introduction\n\nThe aim of this notebook is to showcase a simple CNN build using the keras framework. 10 epochs were run utilising the Kaggle GPU architecture."},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np \nfrom PIL import Image\nimport seaborn as sns\nimport cv2\nimport torch\nfrom torchvision import models\nfrom PIL import Image\n\nimport torch\nimport torch.nn as nn\nfrom torch.autograd import Variable\nfrom torch.utils.data import Dataset, DataLoader, TensorDataset\nfrom sklearn.metrics import confusion_matrix","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from albumentations import (\n    HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,\n    Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,\n    IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,\n    IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize\n)\nfrom albumentations.pytorch import ToTensorV2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ntorch.manual_seed(1234)\nif device =='cuda':\n    torch.cuda.manual_seed_all(1234)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 2. Data Preparation"},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = '../input/cassava-leaf-disease-classification'\ntrain_img_path = PATH+ '/train_images'\ntest_img_ath = PATH + '/test_images'\n# Load dataset\ndf_train = pd.read_csv(PATH + '/train.csv')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def path_extracter(df):\n    x = [train_img_path + '/' + str(k) for k in df.image_id]\n    y = [s for s in df.label]\n    \n    return x,y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train,y_train = path_extracter(df_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"del df_train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_idx, val_idx = train_test_split(list(range(len(x_train))), test_size = 0.2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_val = [x_train[k] for k in val_idx]\ny_val = [y_train[k] for k in val_idx]\n\nx_train = [x_train[k] for k in train_idx]\ny_train = [y_train[k] for k in train_idx]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 3. CNN Model"},{"metadata":{},"cell_type":"markdown","source":"#### 3.1 Annealer"},{"metadata":{"trusted":true},"cell_type":"code","source":"class dataset(torch.utils.data.Dataset):\n     \n        def __init__(self, file_list, labels, transform = None):\n            self.file_list = file_list\n            self.transforms = transform\n            self.label_list = labels\n            \n            \n        def __len__(self):\n            self.filelength = len(self.file_list)\n            return self.filelength\n        \n        def __getitem__(self,idx):\n            img_path = self.file_list[idx]\n            x = cv2.imread(img_path)\n            if self.transforms:\n                x = self.transforms(image=x)['image']\n            \n            if self.label_list[idx] == 1:\n                label = 1\n            else:\n                label = 0\n                \n            return x, label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_transforms = Compose([\n            RandomResizedCrop(224,224),\n            Transpose(p=0.5),\n            HorizontalFlip(p=0.5),\n            VerticalFlip(p=0.5),\n            ShiftScaleRotate(p=0.5),\n            HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2, p=0.5),\n            RandomBrightnessContrast(brightness_limit=(-0.1,0.1), contrast_limit=(-0.1, 0.1), p=0.5),\n            Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], max_pixel_value=255.0, p=1.0),\n            CoarseDropout(p=0.5),\n            Cutout(p=0.5),\n            ToTensorV2(p=1.0),\n        ], p=1.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data = dataset(x_train, y_train, transform = img_transforms)\nval_data  = dataset(x_val, y_val, transform = img_transforms)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(dataset = train_data, batch_size=16, shuffle=True )\nval_loader = torch.utils.data.DataLoader(dataset = val_data, batch_size=16, shuffle=True )\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### 3.2 Model Definition"},{"metadata":{"trusted":true},"cell_type":"code","source":"class Net(nn.Module):\n    \n    def __init__(self):\n        super(Net, self).__init__()\n        \n        self.layer1 = nn.Sequential(\n            nn.Conv2d(3,16,kernel_size=3, padding=0,stride=2),\n            nn.BatchNorm2d(16),\n            nn.ReLU(),\n            nn.MaxPool2d(2)\n        )\n        \n        self.layer2 = nn.Sequential(\n            nn.Conv2d(16,32, kernel_size=3, padding=0, stride=2),\n            nn.BatchNorm2d(32),\n            nn.ReLU(),\n            nn.MaxPool2d(2)\n            )\n        \n        self.layer3 = nn.Sequential(\n            nn.Conv2d(32,64, kernel_size=3, padding=0, stride=2),\n            nn.BatchNorm2d(64),\n            nn.ReLU(),\n            nn.MaxPool2d(2)\n        )\n        \n        \n        self.fc1 = nn.Linear(3*3*64,10)\n        self.dropout = nn.Dropout(0.5)\n        self.fc2 = nn.Linear(10,5)\n        self.relu = nn.ReLU()\n        \n        \n    def forward(self,x):\n        out = self.layer1(x)\n        out = self.layer2(out)\n        out = self.layer3(out)\n        out = out.view(out.size(0),-1)\n        out = self.relu(self.fc1(out))\n        out = self.fc2(out)\n        return out","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"#### 3.3 Optimizer"},{"metadata":{"trusted":true},"cell_type":"code","source":"# ----------------------------------------\n# Initialize model\nmodel = Net().to(device)\ncriterion = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.train()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ----------------------------------------\nhistory = {'training_acc' : [],'val_loss' : [],'val_acc' : [],'train_loss' : []}\ndef train(epoch):\n    epoch_loss = 0\n    epoch_accuracy = 0\n    \n    for data, label in train_loader:\n        data = data.to(device)\n        label = label.to(device)\n        \n        output = model(data)\n        loss = criterion(output, label)\n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        acc = ((output.argmax(dim=1) == label).float().mean())\n        epoch_accuracy += acc/len(train_loader)\n        epoch_loss += loss/len(train_loader)\n        \n    print('Epoch : {}, train accuracy : {}, train loss : {}'.format(epoch+1, epoch_accuracy,epoch_loss))\n    \n    \n    with torch.no_grad():\n        epoch_val_accuracy=0\n        epoch_val_loss =0\n        for data, label in val_loader:\n            data = data.to(device)\n            label = label.to(device)\n            \n            val_output = model(data)\n            val_loss = criterion(val_output,label)\n            \n            \n            acc = ((val_output.argmax(dim=1) == label).float().mean())\n            epoch_val_accuracy += acc/ len(val_loader)\n            epoch_val_loss += val_loss/ len(val_loader)\n            \n        print('Epoch : {}, val_accuracy : {}, val_loss : {}'.format(epoch+1, epoch_val_accuracy,epoch_val_loss))\n    history['training_acc'].append(epoch_accuracy)\n    history['val_acc'].append(epoch_val_accuracy)\n    history['train_loss'].append(epoch_loss)\n    history['val_loss'].append(epoch_val_loss)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nfor epoch in range(0, 1):\n    train(epoch)\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 4. Model Performance"},{"metadata":{},"cell_type":"markdown","source":"#### 4.1 Training and Validation Curves"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame.from_dict(history, orient='columns')\n# Plot the loss and accuracy curves for training and validation \nfig, ax = plt.subplots(2,1)\nax[0].plot(df.train_loss, color='b', label=\"Training loss\")\nax[0].plot(df.val_loss, color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(df.training_acc, color='b', label=\"Training accuracy\")\nax[1].plot(df.val_acc, color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","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}