{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nimport os\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision\nimport torchvision.transforms as transforms\nimport matplotlib.pyplot as plt\n\nfrom torch.utils.data import Dataset, DataLoader\n\nimport json\n\nfrom PIL import Image\n\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-10T08:14:35.252359Z","iopub.execute_input":"2021-07-10T08:14:35.252872Z","iopub.status.idle":"2021-07-10T08:14:35.731009Z","shell.execute_reply.started":"2021-07-10T08:14:35.252794Z","shell.execute_reply":"2021-07-10T08:14:35.730178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.737557Z","iopub.execute_input":"2021-07-10T08:14:35.738125Z","iopub.status.idle":"2021-07-10T08:14:35.761816Z","shell.execute_reply.started":"2021-07-10T08:14:35.738085Z","shell.execute_reply":"2021-07-10T08:14:35.760939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 1\nBATCH = 4\nLR = 0.001","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.763486Z","iopub.execute_input":"2021-07-10T08:14:35.763864Z","iopub.status.idle":"2021-07-10T08:14:35.771310Z","shell.execute_reply.started":"2021-07-10T08:14:35.763826Z","shell.execute_reply":"2021-07-10T08:14:35.770453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose(\n    [transforms.ToTensor(),\n     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.773492Z","iopub.execute_input":"2021-07-10T08:14:35.774232Z","iopub.status.idle":"2021-07-10T08:14:35.780019Z","shell.execute_reply.started":"2021-07-10T08:14:35.774192Z","shell.execute_reply":"2021-07-10T08:14:35.779177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = '../input/cassava-leaf-disease-classification/train_images/'\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\n\nlabels = json.load(open(\"../input/cassava-leaf-disease-classification/label_num_to_disease_map.json\"))\ntrain = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nsample = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\nX_train, Y_train = train['image_id'].values, train['label'].values\n\nX_test = [name for name in (os.listdir(TEST_DIR))]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.781382Z","iopub.execute_input":"2021-07-10T08:14:35.781764Z","iopub.status.idle":"2021-07-10T08:14:35.808015Z","shell.execute_reply.started":"2021-07-10T08:14:35.781712Z","shell.execute_reply":"2021-07-10T08:14:35.807284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#2 filters 3x3\nf1 = [[1,0,-1], \n      [1,0,-1], \n      [1,0,-1]]\nf2 = [[1,1,1], \n      [0,0,0], \n      [-1,-1,-1]]\n\n#2 filters of Sobel\nf_x = [[-1,0,1], \n      [-2,0,2], \n      [-1,0,1]]\n\nf_y = [[1,2,1], \n      [0,0,0], \n      [-1,-2,-1]]\n\nEDGE_ENHANCE_MORE = [[-1, -1, -1],\n                     [-1,  9, -1],\n                     [-1, -1, -1]]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.809556Z","iopub.execute_input":"2021-07-10T08:14:35.809821Z","iopub.status.idle":"2021-07-10T08:14:35.818552Z","shell.execute_reply.started":"2021-07-10T08:14:35.809798Z","shell.execute_reply":"2021-07-10T08:14:35.817682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = 3\np = 0\ns = 1","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.819960Z","iopub.execute_input":"2021-07-10T08:14:35.820314Z","iopub.status.idle":"2021-07-10T08:14:35.826947Z","shell.execute_reply.started":"2021-07-10T08:14:35.820275Z","shell.execute_reply":"2021-07-10T08:14:35.826163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"u, g, summ = 0, 0, 0\n\ndef Func(im, u, g, i):\n    summ = 0\n    for k in range(f):\n            for l in range(f):\n                summ += im[l + u][k + g][i] * f_x[l][k]\n    return summ\n\ndef Prepare(im):\n    \n    final_size = int((len(im) + 2 * p - f)/s + 1)  # in this case it's equal to n - f + 1 (cause of p = 0 and s = 1)\n    \n    n, m = final_size, final_size\n    arr = [[[0 for p in range(f)] for j in range(m)] for i in range(n)]\n    \n    u, g = 0, 0\n    for i in range(final_size):\n        for j in range(final_size):\n            #R range\n            arr[i][j][0] = Func(im, u, g, 0)\n\n            #G range\n            arr[i][j][1] = Func(im, u, g, 1)\n        \n            #B range\n            arr[i][j][2] = Func(im, u, g, 2)\n        \n        g += s\n        if(g >= final_size):\n            u += s\n            g = 0\n            if(u >= final_size):\n                u = 0\n    return arr","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.829357Z","iopub.execute_input":"2021-07-10T08:14:35.829841Z","iopub.status.idle":"2021-07-10T08:14:35.840216Z","shell.execute_reply.started":"2021-07-10T08:14:35.829805Z","shell.execute_reply":"2021-07-10T08:14:35.839162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GetData(Dataset):\n    def __init__(self, Dir, FNames, Labels, Transform):\n        self.dir = Dir\n        self.fnames = FNames\n        self.transform = Transform\n        self.lbs = Labels\n        \n    def __len__(self):\n        return len(self.fnames)\n\n    def __getitem__(self, index):\n        \n        x = Image.open(os.path.join(self.dir, self.fnames[index]))\n        x = x.resize((34,34))\n        \n        x.save(self.fnames[index])\n        \n        arr = Prepare(cv2.imread(self.fnames[index]))\n        \n        cv2.imwrite(self.fnames[index], np.array(arr))\n        \n        x = Image.open(self.fnames[index])\n        \n        if \"train\" in self.dir:    \n            return self.transform(x), self.lbs[index]            \n        elif \"test\" in self.dir:            \n            return self.transform(x), self.fnames[index]","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.841861Z","iopub.execute_input":"2021-07-10T08:14:35.842530Z","iopub.status.idle":"2021-07-10T08:14:35.852006Z","shell.execute_reply.started":"2021-07-10T08:14:35.842492Z","shell.execute_reply":"2021-07-10T08:14:35.851171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset = GetData(TRAIN_DIR, X_train, Y_train, transform)\ntrainloader = DataLoader(trainset, batch_size=BATCH, shuffle=True, num_workers=4)\n\ntestset = GetData(TEST_DIR, X_test, None, transform)\ntestloader = DataLoader(testset, batch_size=1, shuffle=False, num_workers=4)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.853215Z","iopub.execute_input":"2021-07-10T08:14:35.853603Z","iopub.status.idle":"2021-07-10T08:14:35.864443Z","shell.execute_reply.started":"2021-07-10T08:14:35.853529Z","shell.execute_reply":"2021-07-10T08:14:35.863467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ConvNet(nn.Module):\n    def __init__(self):\n        super(ConvNet, self).__init__()\n        self.conv1 = nn.Conv2d(3, 6, 5)\n        self.pool = nn.MaxPool2d(2, 2)\n        self.conv2 = nn.Conv2d(6, 16, 5)\n        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n\n    def forward(self, x):\n        # -> n, 3, 32, 32\n        x = self.pool(F.relu(self.conv1(x)))  # -> n, 6, 14, 14\n        x = self.pool(F.relu(self.conv2(x)))  # -> n, 16, 5, 5\n        x = x.view(-1, 16 * 5 * 5)            # -> n, 400\n        x = F.relu(self.fc1(x))               # -> n, 120\n        x = F.relu(self.fc2(x))               # -> n, 84\n        x = self.fc3(x)                       # -> n, 10 #output\n        return x","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.866233Z","iopub.execute_input":"2021-07-10T08:14:35.866637Z","iopub.status.idle":"2021-07-10T08:14:35.874973Z","shell.execute_reply.started":"2021-07-10T08:14:35.866609Z","shell.execute_reply":"2021-07-10T08:14:35.873844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ConvNet().to(device)\n\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.SGD(model.parameters(), lr=LR)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:35.876569Z","iopub.execute_input":"2021-07-10T08:14:35.876988Z","iopub.status.idle":"2021-07-10T08:14:37.898485Z","shell.execute_reply.started":"2021-07-10T08:14:35.876951Z","shell.execute_reply":"2021-07-10T08:14:37.897449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_total_steps = len(trainloader)\nfor epoch in range(EPOCHS):\n    for i, (images, labels) in enumerate(trainloader):\n        # origin shape: [4, 3, 32, 32] = 4, 3, 1024\n        # input_layer: 3 input channels, 6 output channels, 5 kernel size\n        images = images.to(device)\n        labels = labels.to(device)\n\n        # Forward pass\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        # Backward and optimize\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n    print('Epoch: %d | Loss: %.4f'%(epoch + 1, loss.item()))\n\nprint('Finished Training')\ntorch.save(model.state_dict(), \"cnn.pth\")","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:14:37.899781Z","iopub.execute_input":"2021-07-10T08:14:37.900121Z","iopub.status.idle":"2021-07-10T08:59:15.106859Z","shell.execute_reply.started":"2021-07-10T08:14:37.900084Z","shell.execute_reply":"2021-07-10T08:59:15.105765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(X_train)):\n    os.remove(X_train[i])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:59:15.444804Z","iopub.execute_input":"2021-07-10T08:59:15.445328Z","iopub.status.idle":"2021-07-10T08:59:15.922891Z","shell.execute_reply.started":"2021-07-10T08:59:15.445291Z","shell.execute_reply":"2021-07-10T08:59:15.921812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s_ls = []\n\nwith torch.no_grad():\n    model.eval()\n    for image, fname in testloader: \n        image = image.to(device)\n        \n        logits = model(image)        \n        ps = torch.exp(logits)        \n        _, top_class = ps.topk(1, dim=1)\n        \n        for pred in top_class:\n            s_ls.append([fname[0], pred.item()])","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:59:15.108401Z","iopub.execute_input":"2021-07-10T08:59:15.108951Z","iopub.status.idle":"2021-07-10T08:59:15.412136Z","shell.execute_reply.started":"2021-07-10T08:59:15.108906Z","shell.execute_reply":"2021-07-10T08:59:15.411126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame.from_records(s_ls, columns=['image_id', 'label'])\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:59:15.415570Z","iopub.execute_input":"2021-07-10T08:59:15.415884Z","iopub.status.idle":"2021-07-10T08:59:15.433262Z","shell.execute_reply.started":"2021-07-10T08:59:15.415854Z","shell.execute_reply":"2021-07-10T08:59:15.432243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-10T08:59:15.434972Z","iopub.execute_input":"2021-07-10T08:59:15.435339Z","iopub.status.idle":"2021-07-10T08:59:15.442680Z","shell.execute_reply.started":"2021-07-10T08:59:15.435301Z","shell.execute_reply":"2021-07-10T08:59:15.441850Z"},"trusted":true},"execution_count":null,"outputs":[]}]}