{"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 numpy as np\nimport pandas as pd\nimport os\nimport glob as glob\nimport zipfile\nimport shutil\n\nimport PIL\nimport cv2\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-27T10:54:30.301072Z","iopub.execute_input":"2022-09-27T10:54:30.301858Z","iopub.status.idle":"2022-09-27T10:54:30.336641Z","shell.execute_reply.started":"2022-09-27T10:54:30.301796Z","shell.execute_reply":"2022-09-27T10:54:30.335693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as train:\n    train.extractall()\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as test:\n    test.extractall()\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:30.342430Z","iopub.execute_input":"2022-09-27T10:54:30.342870Z","iopub.status.idle":"2022-09-27T10:54:42.545247Z","shell.execute_reply.started":"2022-09-27T10:54:30.342833Z","shell.execute_reply":"2022-09-27T10:54:42.544222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = glob.glob(\"./train/*\")\nlen(train_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:42.546871Z","iopub.execute_input":"2022-09-27T10:54:42.547286Z","iopub.status.idle":"2022-09-27T10:54:42.620516Z","shell.execute_reply.started":"2022-09-27T10:54:42.547247Z","shell.execute_reply":"2022-09-27T10:54:42.619513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = glob.glob(\"./test1/*\")\nlen(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:42.623634Z","iopub.execute_input":"2022-09-27T10:54:42.624075Z","iopub.status.idle":"2022-09-27T10:54:42.668557Z","shell.execute_reply.started":"2022-09-27T10:54:42.624047Z","shell.execute_reply":"2022-09-27T10:54:42.667616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img= Image.open(train_path[0])\nimg = np.array(img)\nimg = cv2.resize(img,(150,150),interpolation=cv2.INTER_LINEAR)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:42.669850Z","iopub.execute_input":"2022-09-27T10:54:42.670292Z","iopub.status.idle":"2022-09-27T10:54:42.926423Z","shell.execute_reply.started":"2022-09-27T10:54:42.670254Z","shell.execute_reply":"2022-09-27T10:54:42.925536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img= Image.open(train_path[25])\nimg = np.array(img)\nimg = cv2.resize(img,(150,150),interpolation=None)\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:42.927488Z","iopub.execute_input":"2022-09-27T10:54:42.928087Z","iopub.status.idle":"2022-09-27T10:54:43.158560Z","shell.execute_reply.started":"2022-09-27T10:54:42.928048Z","shell.execute_reply":"2022-09-27T10:54:43.157491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.makedirs(\"./Train_data\")\n    print(\"Its done check at Kaggle working Directory\")\nexcept Exception:\n    print(\"Allready there mate\")\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.159940Z","iopub.execute_input":"2022-09-27T10:54:43.160902Z","iopub.status.idle":"2022-09-27T10:54:43.167033Z","shell.execute_reply.started":"2022-09-27T10:54:43.160864Z","shell.execute_reply":"2022-09-27T10:54:43.166030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.makedirs(\"./Train_data/Cat\")\n    print(\"Its done check at Kaggle working Directory\")\nexcept Exception:\n    print(\"Allready there mate\")","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.168411Z","iopub.execute_input":"2022-09-27T10:54:43.169254Z","iopub.status.idle":"2022-09-27T10:54:43.177855Z","shell.execute_reply.started":"2022-09-27T10:54:43.169191Z","shell.execute_reply":"2022-09-27T10:54:43.176625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.makedirs(\"./Train_data/Dog\")\n    print(\"Its done check at Kaggle working Directory\")\nexcept Exception:\n    print(\"Allready there mate\")","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.179463Z","iopub.execute_input":"2022-09-27T10:54:43.180347Z","iopub.status.idle":"2022-09-27T10:54:43.191897Z","shell.execute_reply.started":"2022-09-27T10:54:43.180299Z","shell.execute_reply":"2022-09-27T10:54:43.190735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nos.listdir(\"./Train_data\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.193703Z","iopub.execute_input":"2022-09-27T10:54:43.194114Z","iopub.status.idle":"2022-09-27T10:54:43.204092Z","shell.execute_reply.started":"2022-09-27T10:54:43.194080Z","shell.execute_reply":"2022-09-27T10:54:43.203039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    \n    dogs =0\n    cats =0\n    source =\"./train\"\n    Dog_folder =\"./Train_data/Dog\"\n    Cat_folder = \"./Train_data/Cat\"\n\n    for f in train_path:\n        temp = f.split(\"/\")\n        if temp[-1].split(\".\")[0]=='dog':\n            src_path = f\n            dst_path = Dog_folder\n            shutil.move(src_path, dst_path)\n            dogs+=1\n        else:\n            src_path = f\n            dst_path = Cat_folder\n            shutil.move(src_path, dst_path)\n            cats+=1\n\n    print(f\"Number of Cat images moved to Catfolder:{cats}\")\n    print(f\"Number of Dog  images moved to Dogfolder:{dogs}\")\nexcept Exception:\n     print(\"Allready Moved mate\")\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.205614Z","iopub.execute_input":"2022-09-27T10:54:43.205958Z","iopub.status.idle":"2022-09-27T10:54:43.215069Z","shell.execute_reply.started":"2022-09-27T10:54:43.205930Z","shell.execute_reply":"2022-09-27T10:54:43.213929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndata_dir=\"./Train_data\"","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.216587Z","iopub.execute_input":"2022-09-27T10:54:43.217492Z","iopub.status.idle":"2022-09-27T10:54:43.222812Z","shell.execute_reply.started":"2022-09-27T10:54:43.217364Z","shell.execute_reply":"2022-09-27T10:54:43.221671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = glob.glob(\"./test1/*\")\nlen(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.229156Z","iopub.execute_input":"2022-09-27T10:54:43.230050Z","iopub.status.idle":"2022-09-27T10:54:43.268961Z","shell.execute_reply.started":"2022-09-27T10:54:43.230015Z","shell.execute_reply":"2022-09-27T10:54:43.267939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path[:10]","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.270285Z","iopub.execute_input":"2022-09-27T10:54:43.270711Z","iopub.status.idle":"2022-09-27T10:54:43.279390Z","shell.execute_reply.started":"2022-09-27T10:54:43.270676Z","shell.execute_reply":"2022-09-27T10:54:43.278242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = sorted(x for x in os.listdir(data_dir)\n                     if os.path.isdir(os.path.join(data_dir, x)))\nnum_class = len(class_names)\nimage_files = [\n    [\n        os.path.join(data_dir, class_names[i], x)\n        for x in os.listdir(os.path.join(data_dir, class_names[i]))\n    ]\n    for i in range(num_class)\n]\nnum_each = [len(image_files[i]) for i in range(num_class)]\nimage_files_list = []\nimage_class = []\nfor i in range(num_class):\n    image_files_list.extend(image_files[i])\n    image_class.extend([i] * num_each[i])\nnum_total = len(image_class)\nimage_width, image_height = PIL.Image.open(image_files_list[0]).size\n\nprint(f\"Total image count: {num_total}\")\nprint(f\"Image dimensions: {image_width} x {image_height}\")\nprint(f\"Label names: {class_names}\")\nprint(f\"Label counts: {num_each}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.280981Z","iopub.execute_input":"2022-09-27T10:54:43.281842Z","iopub.status.idle":"2022-09-27T10:54:43.346953Z","shell.execute_reply.started":"2022-09-27T10:54:43.281807Z","shell.execute_reply":"2022-09-27T10:54:43.345940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(3, 3, figsize=(8, 8))\nfor i, k in enumerate(np.random.randint(num_total, size=9)):\n    im = PIL.Image.open(image_files_list[k])\n    arr = np.array(im)\n    plt.subplot(3, 3, i + 1)\n    plt.xlabel(class_names[image_class[k]])\n    plt.imshow(arr, cmap= plt.cm.gist_heat, vmin=0, vmax=255)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:43.348303Z","iopub.execute_input":"2022-09-27T10:54:43.348930Z","iopub.status.idle":"2022-09-27T10:54:44.629864Z","shell.execute_reply.started":"2022-09-27T10:54:43.348892Z","shell.execute_reply":"2022-09-27T10:54:44.629028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_frac = 0.2\nlength = len(image_files_list)\nindices = np.arange(length)\nnp.random.shuffle(indices)\n\nval_split = int(val_frac * length) \ntrain_indices = indices[val_split:]\nval_indices = indices[:val_split]\n\ntrain_x = [image_files_list[i] for i in train_indices]\ntrain_y = [image_class[i] for i in train_indices]\nval_x = [image_files_list[i] for i in val_indices]\nval_y = [image_class[i] for i in val_indices]\ntest_x = test_path\n\n\nprint(\n    f\"Training Image count: {len(train_x)}\", \n    f\" Validation Image count: {len(val_x)}\",\n    f\"Test Image Count:{len(test_x)}\" )","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:44.631422Z","iopub.execute_input":"2022-09-27T10:54:44.632010Z","iopub.status.idle":"2022-09-27T10:54:44.654202Z","shell.execute_reply.started":"2022-09-27T10:54:44.631973Z","shell.execute_reply":"2022-09-27T10:54:44.653227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_img_path = glob.glob(\"./Train_data/Dog/*\")\nlen(dog_img_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:44.655729Z","iopub.execute_input":"2022-09-27T10:54:44.656098Z","iopub.status.idle":"2022-09-27T10:54:44.696711Z","shell.execute_reply.started":"2022-09-27T10:54:44.656060Z","shell.execute_reply":"2022-09-27T10:54:44.695683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_img_path = glob.glob(\"./Train_data/Cat/*\")\nlen(cat_img_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:44.697935Z","iopub.execute_input":"2022-09-27T10:54:44.698270Z","iopub.status.idle":"2022-09-27T10:54:44.739249Z","shell.execute_reply.started":"2022-09-27T10:54:44.698234Z","shell.execute_reply":"2022-09-27T10:54:44.738163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision\nimport torchvision.transforms as transforms\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:44.740926Z","iopub.execute_input":"2022-09-27T10:54:44.741281Z","iopub.status.idle":"2022-09-27T10:54:45.551354Z","shell.execute_reply.started":"2022-09-27T10:54:44.741246Z","shell.execute_reply":"2022-09-27T10:54:45.550382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = cat_img_path[10]\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Resize((150,150)),\n    #transforms.Normalize(torch.Tensor([0.6958, 0.6623, 0.5844]),torch.Tensor([0.1465,0.1493,0.1487]))\n    \n])\nimg_tr = transform(img)\nmean, std = img_tr.mean([1,2]), img_tr.std([1,2])\n\nprint(f\"mean:{mean}\")\nprint(f\"std:{std}\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:45.552642Z","iopub.execute_input":"2022-09-27T10:54:45.554925Z","iopub.status.idle":"2022-09-27T10:54:45.576211Z","shell.execute_reply.started":"2022-09-27T10:54:45.554893Z","shell.execute_reply":"2022-09-27T10:54:45.575239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = transforms.Compose([\n                  transforms.ToPILImage(),\n                  transforms.Resize((150,150)),\n                  transforms.RandomHorizontalFlip(p=0.50),\n                  transforms.RandomRotation(degrees=45),\n                  transforms.ToTensor(),\n                  transforms.Normalize(torch.Tensor([0.6958, 0.6623, 0.5844]),torch.Tensor([0.1465,0.1493,0.1487]))\n                  ])\nval_transform = transforms.Compose([\n                transforms.ToPILImage(),\n                transforms.Resize((150,150)),\n                transforms.ToTensor(),\n                transforms.Normalize(torch.Tensor([0.5206, 0.5531, 0.3556]),torch.Tensor([0.2338,0.2470,0.2044]))\n                ])\n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:45.577613Z","iopub.execute_input":"2022-09-27T10:54:45.578223Z","iopub.status.idle":"2022-09-27T10:54:45.586852Z","shell.execute_reply.started":"2022-09-27T10:54:45.578179Z","shell.execute_reply":"2022-09-27T10:54:45.585607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(torch.utils.data.Dataset):\n    def __init__(self,image_files,labels,train,test):\n        self.image_files = image_files\n        self.labels = labels\n        self.train = train\n        self.test = test\n        self.transforms = transforms \n    \n        \n        if self.train==True:\n            self.transform = transforms.Compose([\n                  transforms.ToPILImage(),\n                  transforms.Resize((150,150)),\n                  transforms.RandomHorizontalFlip(p=0.50),\n                  transforms.RandomRotation(degrees=45),\n                  transforms.ToTensor(),\n                  transforms.Normalize(torch.Tensor([0.6958, 0.6623, 0.5844]),torch.Tensor([0.1465,0.1493,0.1487]))\n                                      ])\n            \n        elif self.train==False and self.test==False:\n            self.transform = transforms.Compose([\n                transforms.ToPILImage(),\n                transforms.Resize((150,150)),\n                transforms.ToTensor(),\n                transforms.Normalize(torch.Tensor([0.6958, 0.6623, 0.5844]),torch.Tensor([0.1465,0.1493,0.1487]))\n                ])\n            \n        elif self.test==True and self.train == False:\n            self.transform = transforms.Compose([\n                   transforms.ToPILImage(),\n                   transforms.Resize((150,150)),\n                   transforms.ToTensor(),\n                   transforms.Normalize(torch.Tensor([0.6958, 0.6623, 0.5844]),torch.Tensor([0.1465,0.1493,0.1487]))\n                                        ])\n        \n    def __len__(self,):\n        return len(self.image_files)\n    \n    def __getitem__(self,index):\n        \n        \n        image = cv2.imread(self.image_files[index])\n        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n        image = self.transform(image)\n        if self.train==True:\n            targets = self.labels[index]\n            \n                \n                    \n            return {\n                    \"image\": torch.tensor(image,dtype=torch.float32),\n                    \"label\" : torch.tensor(targets)\n                     }\n        elif self.train==False and self.test==False:\n            targets = self.labels[index]\n            \n            \n            return {\n                \"image\": torch.tensor(image,dtype=torch.float32),\n                \"label\" : torch.tensor(targets)\n                 }\n        elif self.test==True and self.train == False:\n            return {\n                \"image\": torch.tensor(image,dtype=torch.float32)\n            }","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:45.588831Z","iopub.execute_input":"2022-09-27T10:54:45.589274Z","iopub.status.idle":"2022-09-27T10:54:45.605857Z","shell.execute_reply.started":"2022-09-27T10:54:45.589230Z","shell.execute_reply":"2022-09-27T10:54:45.604474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train dataset\n\ntrain_data = Dataset(\n    train_x,train_y, train=True, test=False\n)\n# validation dataset\nvalid_data = Dataset(\n    val_x,val_y, train=False, test=False\n)\n# train data loader\ntrain_loader = torch.utils.data.DataLoader(\n    train_data, \n    batch_size=128,\n    shuffle=True)\n\n# validation data loader\nvalid_loader = torch.utils.data.DataLoader(\n    valid_data, \n    batch_size=128,\n    shuffle=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:45.607277Z","iopub.execute_input":"2022-09-27T10:54:45.607587Z","iopub.status.idle":"2022-09-27T10:54:45.619455Z","shell.execute_reply.started":"2022-09-27T10:54:45.607545Z","shell.execute_reply":"2022-09-27T10:54:45.618627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision\nimport torchvision.transforms as transforms\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:45.620788Z","iopub.execute_input":"2022-09-27T10:54:45.621422Z","iopub.status.idle":"2022-09-27T10:54:45.632338Z","shell.execute_reply.started":"2022-09-27T10:54:45.621385Z","shell.execute_reply":"2022-09-27T10:54:45.631428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = torchvision.models.densenet121(pretrained=False, progress=True,  \n                num_classes=2).to(device)\n\nloss_function = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), 1e-5)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:45.633902Z","iopub.execute_input":"2022-09-27T10:54:45.634348Z","iopub.status.idle":"2022-09-27T10:54:48.861983Z","shell.execute_reply.started":"2022-09-27T10:54:45.634313Z","shell.execute_reply":"2022-09-27T10:54:48.860920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"./Models\")","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:48.863410Z","iopub.execute_input":"2022-09-27T10:54:48.863794Z","iopub.status.idle":"2022-09-27T10:54:48.869455Z","shell.execute_reply.started":"2022-09-27T10:54:48.863755Z","shell.execute_reply":"2022-09-27T10:54:48.868451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nepochs = 15\nbest_metric = 1.0\ntrain_loss = []\nvalid_loss = []\nval_interval = 1\nroot_dir = \"./Models\"\n\nfor epoch in range(epochs):\n    \n    print(f\"Epoch {epoch+1} of {epochs}\")\n    print('Training')\n    model.train()\n    counter = 0\n    train_running_loss = 0.0\n    val_running_loss = 0.0\n    for  data in train_loader:\n        counter += 1\n        data, target = data['image'].to(device), data['label'].to(device)\n        optimizer.zero_grad()\n        outputs = model(data)\n\n        loss = loss_function(outputs, target)\n        train_running_loss += loss.item()\n        # backpropagation\n        loss.backward()\n        # update optimizer parameters\n        optimizer.step()\n        train_loss = train_running_loss / counter\n        \n    if (epoch + 1) % val_interval == 0:\n        \n        \n        print(\"validating\")\n        model.eval()\n        val_running_loss = 0.0\n        val_counter=0.0\n        with torch.no_grad():\n            for  data in valid_loader:\n                val_counter+=1\n                data,target = data['image'].to(device),data['label'].to(device)\n                outputs = model(data)\n                loss = loss_function(outputs,target)\n                val_running_loss+=loss.item()\n            val_loss = val_running_loss/val_counter\n            print(f'Val Loss: {val_loss:.4f}')\n            if val_loss < best_metric:\n                best_metric = val_loss\n                best_metric_epoch = epoch + 1\n                torch.save(model.state_dict(), os.path.join(\n                    root_dir, \"best_metric_model_cat_dog.pth\"))\n    print(\"saved new best metric model\")\n    print(f\"Train Loss at epoch:{epoch}: {train_loss:.4f}\")\n    print(f'Val Loss at epoch:{epoch}: {val_loss:.4f}')\n\n\n    \nprint(f\"Train Loss: {train_loss:.4f}\")\nprint(f'Val Loss: {val_loss:.4f}')","metadata":{"execution":{"iopub.status.busy":"2022-09-27T10:54:48.870852Z","iopub.execute_input":"2022-09-27T10:54:48.871878Z","iopub.status.idle":"2022-09-27T11:41:43.366600Z","shell.execute_reply.started":"2022-09-27T10:54:48.871842Z","shell.execute_reply":"2022-09-27T11:41:43.365635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"./Models\")","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:41:43.368115Z","iopub.execute_input":"2022-09-27T11:41:43.368462Z","iopub.status.idle":"2022-09-27T11:41:43.375971Z","shell.execute_reply.started":"2022-09-27T11:41:43.368434Z","shell.execute_reply":"2022-09-27T11:41:43.374854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_loader1 = torch.utils.data.DataLoader(\n    valid_data, \n    batch_size=1,\n    shuffle=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:41:43.377702Z","iopub.execute_input":"2022-09-27T11:41:43.378471Z","iopub.status.idle":"2022-09-27T11:41:43.383922Z","shell.execute_reply.started":"2022-09-27T11:41:43.378435Z","shell.execute_reply":"2022-09-27T11:41:43.382760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\ncorrect = 0\ntotal = 0\noutputs =[]\npredict =[]\nreal_class_l=[]\nwith torch.no_grad():\n    for  data in valid_loader1:\n        data,target = data['image'].to(device),data['label'].to(device)\n        real_class = target\n        real_class_l.append(real_class)\n        net_out = model(data)\n        net_out = torch.sigmoid(net_out)\n        outputs.append(net_out)\n        predicted_class=torch.argmax(net_out)\n        predict.append(predicted_class)\n        if predicted_class == real_class:\n            correct += 1\n        total += 1\nprint(\"Accuracy: \", round(correct/total, 3))","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:44:45.612333Z","iopub.execute_input":"2022-09-27T11:44:45.612725Z","iopub.status.idle":"2022-09-27T11:46:35.268667Z","shell.execute_reply.started":"2022-09-27T11:44:45.612689Z","shell.execute_reply":"2022-09-27T11:46:35.267627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data =Dataset(\n    test_x, train_y,train=False, test=True)\n\ntest_loader = torch.utils.data.DataLoader(\n    test_data, \n    batch_size=1,\n    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:47:04.190942Z","iopub.execute_input":"2022-09-27T11:47:04.191413Z","iopub.status.idle":"2022-09-27T11:47:04.197003Z","shell.execute_reply.started":"2022-09-27T11:47:04.191364Z","shell.execute_reply":"2022-09-27T11:47:04.195925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_puts=[]\nfor data in test_loader:\n    image = data[\"image\"].to(device)\n    y_pred = model(image).to('cpu').detach()\n    y_pred = torch.sigmoid(y_pred).numpy()\n    out_puts.append(np.argmax(y_pred))\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:52:57.310515Z","iopub.execute_input":"2022-09-27T11:52:57.311666Z","iopub.status.idle":"2022-09-27T11:58:19.183328Z","shell.execute_reply.started":"2022-09-27T11:52:57.311627Z","shell.execute_reply":"2022-09-27T11:58:19.182137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cato_list =[]\nfor i in out_puts:\n    if i==0:\n        mark=\"cat\"\n        cato_list.append(mark)\n    else:\n        mark=\"dog\"\n        cato_list.append(mark)\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:58:23.628736Z","iopub.execute_input":"2022-09-27T11:58:23.629097Z","iopub.status.idle":"2022-09-27T11:58:23.638381Z","shell.execute_reply.started":"2022-09-27T11:58:23.629066Z","shell.execute_reply":"2022-09-27T11:58:23.637027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(cato_list)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:58:27.569456Z","iopub.execute_input":"2022-09-27T11:58:27.570134Z","iopub.status.idle":"2022-09-27T11:58:27.576474Z","shell.execute_reply.started":"2022-09-27T11:58:27.570096Z","shell.execute_reply":"2022-09-27T11:58:27.575519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = glob.glob(\"./test1/*\")\nlen(test_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:58:31.157710Z","iopub.execute_input":"2022-09-27T11:58:31.158371Z","iopub.status.idle":"2022-09-27T11:58:31.199723Z","shell.execute_reply.started":"2022-09-27T11:58:31.158334Z","shell.execute_reply":"2022-09-27T11:58:31.198711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(3, 3, figsize=(8, 8))\nfor i, k in enumerate(np.random.randint(len(test_path), size=9)):\n    im = PIL.Image.open(test_path[i])\n    arr = np.array(im)\n    plt.subplot(3, 3, i + 1)\n    plt.xlabel(f'Predicted:{cato_list[i]}')\n    plt.imshow(arr, cmap= plt.cm.gist_heat, vmin=0, vmax=255)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-27T11:58:35.961383Z","iopub.execute_input":"2022-09-27T11:58:35.961782Z","iopub.status.idle":"2022-09-27T11:58:37.048617Z","shell.execute_reply.started":"2022-09-27T11:58:35.961748Z","shell.execute_reply":"2022-09-27T11:58:37.047478Z"},"trusted":true},"execution_count":null,"outputs":[]}]}