{"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\nfrom tqdm import tqdm\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-30T09:08:18.763523Z","iopub.execute_input":"2022-09-30T09:08:18.763867Z","iopub.status.idle":"2022-09-30T09:08:18.952298Z","shell.execute_reply.started":"2022-09-30T09:08:18.763791Z","shell.execute_reply":"2022-09-30T09:08:18.951353Z"},"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-30T09:08:23.818177Z","iopub.execute_input":"2022-09-30T09:08:23.818542Z","iopub.status.idle":"2022-09-30T09:08:42.710346Z","shell.execute_reply.started":"2022-09-30T09:08:23.818511Z","shell.execute_reply":"2022-09-30T09:08:42.709362Z"},"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-30T09:08:47.057910Z","iopub.execute_input":"2022-09-30T09:08:47.058837Z","iopub.status.idle":"2022-09-30T09:08:47.133073Z","shell.execute_reply.started":"2022-09-30T09:08:47.058802Z","shell.execute_reply":"2022-09-30T09:08:47.131992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path[0]","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:08:51.398062Z","iopub.execute_input":"2022-09-30T09:08:51.398614Z","iopub.status.idle":"2022-09-30T09:08:51.407286Z","shell.execute_reply.started":"2022-09-30T09:08:51.398578Z","shell.execute_reply":"2022-09-30T09:08:51.406269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_num_list=[]\nfor patient in train_path:\n    patient_name = os.path.basename(os.path.normpath(patient))\n    pt_list = patient_name.split(\".\")\n    img_num=pt_list[0]\n    img_num_list.append(img_num)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:08:55.673102Z","iopub.execute_input":"2022-09-30T09:08:55.673467Z","iopub.status.idle":"2022-09-30T09:08:55.751183Z","shell.execute_reply.started":"2022-09-30T09:08:55.673436Z","shell.execute_reply":"2022-09-30T09:08:55.750231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(img_num_list)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:09:00.279784Z","iopub.execute_input":"2022-09-30T09:09:00.280148Z","iopub.status.idle":"2022-09-30T09:09:00.286964Z","shell.execute_reply.started":"2022-09-30T09:09:00.280118Z","shell.execute_reply":"2022-09-30T09:09:00.285834Z"},"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-30T09:09:04.061742Z","iopub.execute_input":"2022-09-30T09:09:04.062148Z","iopub.status.idle":"2022-09-30T09:09:04.103397Z","shell.execute_reply.started":"2022-09-30T09:09:04.062114Z","shell.execute_reply":"2022-09-30T09:09:04.101941Z"},"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\")","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:09:07.730568Z","iopub.execute_input":"2022-09-30T09:09:07.730998Z","iopub.status.idle":"2022-09-30T09:09:07.737275Z","shell.execute_reply.started":"2022-09-30T09:09:07.730930Z","shell.execute_reply":"2022-09-30T09:09:07.736223Z"},"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-30T09:09:11.586970Z","iopub.execute_input":"2022-09-30T09:09:11.587345Z","iopub.status.idle":"2022-09-30T09:09:11.593220Z","shell.execute_reply.started":"2022-09-30T09:09:11.587314Z","shell.execute_reply":"2022-09-30T09:09:11.592161Z"},"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-30T09:09:15.587173Z","iopub.execute_input":"2022-09-30T09:09:15.587539Z","iopub.status.idle":"2022-09-30T09:09:15.593089Z","shell.execute_reply.started":"2022-09-30T09:09:15.587509Z","shell.execute_reply":"2022-09-30T09:09:15.592110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dogs =0\ncats =0\nsource =\"./train\"\nDog_folder =\"./Train_data/Dog\"\nCat_folder = \"./Train_data/Cat\"\nfor f,i in zip(train_path,img_num_list):\n    img= Image.open(f)\n    img = np.array(img)\n    im = cv2.resize(img,(150,150),interpolation=cv2.INTER_LINEAR)\n    rescaled_img = (np.maximum(im,0)/im.max())*255\n    fin_img = np.uint8(rescaled_img)\n    fin_img = Image.fromarray(fin_img)\n    name = i\n    fin_img.save(f)\n    new_path =f\n    if name=='dog':\n        src_path = new_path\n        dst_path = Dog_folder\n        shutil.move(src_path, dst_path)\n        dogs+=1\n    else:\n        src_path = new_path\n        dst_path = Cat_folder\n        shutil.move(src_path, dst_path)\n        cats+=1\n\n    \nprint(f\"Number of Cat images reshaped and moved to Catfolder:{cats}\")\nprint(f\"Number of Dog  images reshaped and moved to Dogfolder:{dogs}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:09:31.325457Z","iopub.execute_input":"2022-09-30T09:09:31.325813Z","iopub.status.idle":"2022-09-30T09:11:12.199460Z","shell.execute_reply.started":"2022-09-30T09:09:31.325782Z","shell.execute_reply":"2022-09-30T09:11:12.198404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_num=os.listdir(\"./Train_data/Cat\")\nlen(cat_num)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:13:18.211550Z","iopub.execute_input":"2022-09-30T09:13:18.212165Z","iopub.status.idle":"2022-09-30T09:13:18.225790Z","shell.execute_reply.started":"2022-09-30T09:13:18.212127Z","shell.execute_reply":"2022-09-30T09:13:18.224692Z"},"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-30T09:13:21.341010Z","iopub.execute_input":"2022-09-30T09:13:21.341368Z","iopub.status.idle":"2022-09-30T09:13:21.383530Z","shell.execute_reply.started":"2022-09-30T09:13:21.341338Z","shell.execute_reply":"2022-09-30T09:13:21.382470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir=\"./Train_data\"","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:13:24.863994Z","iopub.execute_input":"2022-09-30T09:13:24.864363Z","iopub.status.idle":"2022-09-30T09:13:24.869470Z","shell.execute_reply.started":"2022-09-30T09:13:24.864330Z","shell.execute_reply":"2022-09-30T09:13:24.868352Z"},"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-30T09:13:29.946645Z","iopub.execute_input":"2022-09-30T09:13:29.947027Z","iopub.status.idle":"2022-09-30T09:13:30.015725Z","shell.execute_reply.started":"2022-09-30T09:13:29.946994Z","shell.execute_reply":"2022-09-30T09:13:30.014700Z"},"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-30T09:46:25.589317Z","iopub.execute_input":"2022-09-30T09:46:25.589697Z","iopub.status.idle":"2022-09-30T09:46:26.537587Z","shell.execute_reply.started":"2022-09-30T09:46:25.589665Z","shell.execute_reply":"2022-09-30T09:46:26.536788Z"},"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)}\" )\n","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:13:48.621722Z","iopub.execute_input":"2022-09-30T09:13:48.622113Z","iopub.status.idle":"2022-09-30T09:13:48.642744Z","shell.execute_reply.started":"2022-09-30T09:13:48.622079Z","shell.execute_reply":"2022-09-30T09:13:48.641487Z"},"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-30T09:13:53.511682Z","iopub.execute_input":"2022-09-30T09:13:53.512072Z","iopub.status.idle":"2022-09-30T09:13:53.554537Z","shell.execute_reply.started":"2022-09-30T09:13:53.512040Z","shell.execute_reply":"2022-09-30T09:13:53.553498Z"},"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-30T09:13:56.966681Z","iopub.execute_input":"2022-09-30T09:13:56.967061Z","iopub.status.idle":"2022-09-30T09:13:57.009778Z","shell.execute_reply.started":"2022-09-30T09:13:56.967028Z","shell.execute_reply":"2022-09-30T09:13:57.008855Z"},"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\nimport torch.optim as optim","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:14:00.915544Z","iopub.execute_input":"2022-09-30T09:14:00.915908Z","iopub.status.idle":"2022-09-30T09:14:02.788772Z","shell.execute_reply.started":"2022-09-30T09:14:00.915876Z","shell.execute_reply":"2022-09-30T09:14:02.787824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Calculating Mean and Standard deviation of Image","metadata":{}},{"cell_type":"code","source":"img_path = cat_img_path[1000]\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    #transforms.Resize((150,150)),\n    #transforms.Normalize(torch.Tensor([0.4994, 0.4849, 0.4749]),torch.Tensor([0.3305,0.3235,0.3154]))\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}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:17:53.681690Z","iopub.execute_input":"2022-09-30T09:17:53.682077Z","iopub.status.idle":"2022-09-30T09:17:53.694421Z","shell.execute_reply.started":"2022-09-30T09:17:53.682045Z","shell.execute_reply":"2022-09-30T09:17:53.693251Z"},"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=40),\n                  transforms.ToTensor(),\n                  transforms.Normalize(torch.Tensor([0.4994, 0.4849, 0.4749]),torch.Tensor([0.3305,0.3235,0.3154]))\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.4994, 0.4849, 0.4749]),torch.Tensor([0.3305,0.3235,0.3154]))\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.4994, 0.4849, 0.4749]),torch.Tensor([0.3305,0.3235,0.3154]))\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-30T09:18:57.906890Z","iopub.execute_input":"2022-09-30T09:18:57.907344Z","iopub.status.idle":"2022-09-30T09:18:57.920593Z","shell.execute_reply.started":"2022-09-30T09:18:57.907310Z","shell.execute_reply":"2022-09-30T09:18:57.919496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:19:04.410089Z","iopub.execute_input":"2022-09-30T09:19:04.410504Z","iopub.status.idle":"2022-09-30T09:19:04.417745Z","shell.execute_reply.started":"2022-09-30T09:19:04.410470Z","shell.execute_reply":"2022-09-30T09:19:04.416792Z"},"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":"2022-09-30T09:19:48.421600Z","iopub.execute_input":"2022-09-30T09:19:48.421983Z","iopub.status.idle":"2022-09-30T09:19:48.489677Z","shell.execute_reply.started":"2022-09-30T09:19:48.421930Z","shell.execute_reply":"2022-09-30T09:19:48.488620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(3,32,3)\n        self.conv2 = nn.Conv2d(32,64,3)\n        self.conv3 = nn.Conv2d(64,128,3)\n        self.conv4 = nn.Conv2d(128,128,3)\n        \n        x = torch.randn(3,150,150).view(-1,3,150,150)\n        self._to_linear = None\n        self.convs(x)\n        \n        self.fc1 = nn.Linear(self._to_linear,512)\n        self.fc2 = nn.Linear(512,2)\n        \n    def convs(self,x):\n        x = F.max_pool2d(F.relu(self.conv1(x)),  (2,2))\n        x = F.max_pool2d(F.relu(self.conv2(x)),  (2,2))\n        x = F.max_pool2d(F.relu(self.conv3(x)),  (2,2))\n        x = F.max_pool2d(F.relu(self.conv4(x)),  (2,2))\n        \n        if self._to_linear is None:\n            self._to_linear = x[0].shape[0]*x[0].shape[1]*x[0].shape[2]\n        return x\n    \n    def forward(self,x):\n        \n        x = self.convs(x)\n        \n        x = x.view(-1,self._to_linear)\n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n        return F.softmax(x,dim=1)\n        \nmodel = Net().to(device)\nprint(model)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:19:55.425127Z","iopub.execute_input":"2022-09-30T09:19:55.425499Z","iopub.status.idle":"2022-09-30T09:19:58.566105Z","shell.execute_reply.started":"2022-09-30T09:19:55.425468Z","shell.execute_reply":"2022-09-30T09:19:58.565028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nloss_function = torch.nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), 0.001)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:20:20.503889Z","iopub.execute_input":"2022-09-30T09:20:20.504276Z","iopub.status.idle":"2022-09-30T09:20:20.509166Z","shell.execute_reply.started":"2022-09-30T09:20:20.504245Z","shell.execute_reply":"2022-09-30T09:20:20.508175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"./Models\")","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:20:26.202475Z","iopub.execute_input":"2022-09-30T09:20:26.202896Z","iopub.status.idle":"2022-09-30T09:20:26.207995Z","shell.execute_reply.started":"2022-09-30T09:20:26.202861Z","shell.execute_reply":"2022-09-30T09:20:26.206939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 17\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-30T09:20:40.000516Z","iopub.execute_input":"2022-09-30T09:20:40.000877Z","iopub.status.idle":"2022-09-30T09:32:50.600492Z","shell.execute_reply.started":"2022-09-30T09:20:40.000845Z","shell.execute_reply":"2022-09-30T09:32:50.599409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loss\nlen(valid_loss)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:35:47.866063Z","iopub.execute_input":"2022-09-30T09:35:47.866489Z","iopub.status.idle":"2022-09-30T09:35:47.873460Z","shell.execute_reply.started":"2022-09-30T09:35:47.866455Z","shell.execute_reply":"2022-09-30T09:35:47.872409Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:36:46.079669Z","iopub.execute_input":"2022-09-30T09:36:46.080063Z","iopub.status.idle":"2022-09-30T09:36:46.084974Z","shell.execute_reply.started":"2022-09-30T09:36:46.080032Z","shell.execute_reply":"2022-09-30T09:36:46.084000Z"},"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-30T09:36:50.007891Z","iopub.execute_input":"2022-09-30T09:36:50.008891Z","iopub.status.idle":"2022-09-30T09:37:01.170959Z","shell.execute_reply.started":"2022-09-30T09:36:50.008845Z","shell.execute_reply":"2022-09-30T09:37:01.169877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data =Dataset(\n    test_x, train_y,train=False, test=True) # train_y is dummy set as Dataset class  returns only images when test is 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-30T09:37:06.090364Z","iopub.execute_input":"2022-09-30T09:37:06.090723Z","iopub.status.idle":"2022-09-30T09:37:06.098329Z","shell.execute_reply.started":"2022-09-30T09:37:06.090692Z","shell.execute_reply":"2022-09-30T09:37:06.095223Z"},"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))","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:37:09.931706Z","iopub.execute_input":"2022-09-30T09:37:09.932084Z","iopub.status.idle":"2022-09-30T09:38:19.075879Z","shell.execute_reply.started":"2022-09-30T09:37:09.932052Z","shell.execute_reply":"2022-09-30T09:38:19.074934Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:38:24.218733Z","iopub.execute_input":"2022-09-30T09:38:24.219105Z","iopub.status.idle":"2022-09-30T09:38:24.228926Z","shell.execute_reply.started":"2022-09-30T09:38:24.219073Z","shell.execute_reply":"2022-09-30T09:38:24.227852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(cato_list)","metadata":{"execution":{"iopub.status.busy":"2022-09-30T09:38:36.806341Z","iopub.execute_input":"2022-09-30T09:38:36.806702Z","iopub.status.idle":"2022-09-30T09:38:36.812908Z","shell.execute_reply.started":"2022-09-30T09:38:36.806671Z","shell.execute_reply":"2022-09-30T09:38:36.812014Z"},"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-30T09:38:40.122773Z","iopub.execute_input":"2022-09-30T09:38:40.123139Z","iopub.status.idle":"2022-09-30T09:38:40.164456Z","shell.execute_reply.started":"2022-09-30T09:38:40.123108Z","shell.execute_reply":"2022-09-30T09:38:40.163386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.subplots(4, 4, figsize=(8, 8))\nfor i, k in enumerate(np.random.randint(len(test_path), size=16)):\n    im = PIL.Image.open(test_path[i])\n    arr = np.array(im)\n    plt.subplot(4, 4, 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-30T09:45:25.007142Z","iopub.execute_input":"2022-09-30T09:45:25.007525Z","iopub.status.idle":"2022-09-30T09:45:26.767039Z","shell.execute_reply.started":"2022-09-30T09:45:25.007494Z","shell.execute_reply":"2022-09-30T09:45:26.766227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**When a dog with there ears in upward direction model is predicting as cat insted of dog.out of 16 ,2 images are  missclassified.**","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}