{"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 zipfile\npath_to_zip_file = \"/kaggle/input/dogs-vs-cats/train.zip\"\ndirectory_to_extract_to = \"/kaggle/working/\"\nwith zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:\n    zip_ref.extractall(directory_to_extract_to)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-25T15:48:57.531121Z","iopub.execute_input":"2023-09-25T15:48:57.531515Z","iopub.status.idle":"2023-09-25T15:49:10.378624Z","shell.execute_reply.started":"2023-09-25T15:48:57.531484Z","shell.execute_reply":"2023-09-25T15:49:10.377573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfolder_path = '/kaggle/working/train/'\nfolder_contents = os.listdir(folder_path)\nfolder_contents = [folder_path + image_path for image_path in folder_contents]\n\n# print(folder_contents)\nprint(len(folder_contents))","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.380804Z","iopub.execute_input":"2023-09-25T15:49:10.381145Z","iopub.status.idle":"2023-09-25T15:49:10.408443Z","shell.execute_reply.started":"2023-09-25T15:49:10.381119Z","shell.execute_reply":"2023-09-25T15:49:10.407349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(folder_contents[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.409878Z","iopub.execute_input":"2023-09-25T15:49:10.410483Z","iopub.status.idle":"2023-09-25T15:49:10.416579Z","shell.execute_reply.started":"2023-09-25T15:49:10.410448Z","shell.execute_reply":"2023-09-25T15:49:10.415527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport numpy as np\n\ndef show_image(image):\n    plt.imshow(image)\n    plt.axis('off')\n    plt.show()\n\nsample_image = Image.open(folder_contents[0]).resize((128,128))\nshow_image(sample_image)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.419800Z","iopub.execute_input":"2023-09-25T15:49:10.420387Z","iopub.status.idle":"2023-09-25T15:49:10.673458Z","shell.execute_reply.started":"2023-09-25T15:49:10.420349Z","shell.execute_reply":"2023-09-25T15:49:10.672426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_split = 0.8\ntrain_size = int(train_split * len(folder_contents))\ntrain_content = folder_contents[:train_size]\nvalid_content = folder_contents[train_size:]","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.674747Z","iopub.execute_input":"2023-09-25T15:49:10.675123Z","iopub.status.idle":"2023-09-25T15:49:10.681202Z","shell.execute_reply.started":"2023-09-25T15:49:10.675087Z","shell.execute_reply":"2023-09-25T15:49:10.680227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\nimport torchvision.transforms as transforms\nimport torch\n\nclass CatDogDataset(Dataset):\n    def __init__(self, folder_contents, transform):\n        self.folder_contents = folder_contents\n        self.transform = transform\n        \n    def __len__(self):\n        return len(self.folder_contents)\n    \n    def __getitem__(self, idx):\n        image_path = self.folder_contents[idx]\n        image = Image.open(image_path)\n        label = 0 # 0 is dog, 1 is cat\n        if \"cat\" in image_path: label = 1\n        return self.transform(image).float() / 255.0, torch.Tensor([label])","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.683827Z","iopub.execute_input":"2023-09-25T15:49:10.684903Z","iopub.status.idle":"2023-09-25T15:49:10.695934Z","shell.execute_reply.started":"2023-09-25T15:49:10.684869Z","shell.execute_reply":"2023-09-25T15:49:10.694911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((128, 128)),\n    transforms.ToTensor(),\n])","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.698715Z","iopub.execute_input":"2023-09-25T15:49:10.699116Z","iopub.status.idle":"2023-09-25T15:49:10.708195Z","shell.execute_reply.started":"2023-09-25T15:49:10.699080Z","shell.execute_reply":"2023-09-25T15:49:10.707407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = CatDogDataset(train_content, transform)\nvalid_dataset = CatDogDataset(valid_content, transform)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.710870Z","iopub.execute_input":"2023-09-25T15:49:10.712008Z","iopub.status.idle":"2023-09-25T15:49:10.719310Z","shell.execute_reply.started":"2023-09-25T15:49:10.711979Z","shell.execute_reply":"2023-09-25T15:49:10.718251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_dataset, batch_size=16)\nvalid_loader = DataLoader(valid_dataset, batch_size=16)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.720717Z","iopub.execute_input":"2023-09-25T15:49:10.721126Z","iopub.status.idle":"2023-09-25T15:49:10.732199Z","shell.execute_reply.started":"2023-09-25T15:49:10.721092Z","shell.execute_reply":"2023-09-25T15:49:10.731014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_loader))\nfor image, label in train_loader:\n    print(image.shape)\n    print(label.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-09-25T15:49:10.737487Z","iopub.execute_input":"2023-09-25T15:49:10.738277Z","iopub.status.idle":"2023-09-25T15:49:10.855324Z","shell.execute_reply.started":"2023-09-25T15:49:10.738199Z","shell.execute_reply":"2023-09-25T15:49:10.854321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.models as models\n\n# Load a pre-trained ResNet18 model (you can choose other models as well)\npretrained_resnet = models.resnet18(pretrained=True)\n\n# Freeze all layers except the final classifier layers\nfor param in pretrained_resnet.parameters():\n    param.requires_grad = False\n\n# Modify the classifier layers for your specific task\nnum_classes = 1  # Replace with the number of classes in your dataset\npretrained_resnet.fc = nn.Sequential(\n    nn.Linear(512, num_classes),\n    nn.Sigmoid()\n)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:14:59.235177Z","iopub.execute_input":"2023-09-25T16:14:59.235858Z","iopub.status.idle":"2023-09-25T16:14:59.485877Z","shell.execute_reply.started":"2023-09-25T16:14:59.235824Z","shell.execute_reply":"2023-09-25T16:14:59.484769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\ndef train_one_epoch(model, dataloader, loss_fn, optimizer, device):\n    model.train()\n    total_loss = 0\n    for image, label in tqdm(dataloader):\n        image, label = image.to(device), label.to(device)\n        # get prediction\n        preds = model(image)\n        loss = loss_fn(preds, label)\n        \n        # update weights\n        optimizer.zero_grad()  # Clear the gradients\n        loss.backward()  # Compute gradients\n        optimizer.step()\n        \n        # add to total loss count\n        total_loss += loss.item()\n    return total_loss / len(dataloader)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:22:25.497726Z","iopub.execute_input":"2023-09-25T16:22:25.498326Z","iopub.status.idle":"2023-09-25T16:22:25.506869Z","shell.execute_reply.started":"2023-09-25T16:22:25.498291Z","shell.execute_reply":"2023-09-25T16:22:25.505737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.optim import Adam\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = pretrained_resnet.to(device)\nloss_fn = nn.BCELoss()\noptimizer = Adam(model.parameters(), lr=1e-4)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:22:25.508458Z","iopub.execute_input":"2023-09-25T16:22:25.508894Z","iopub.status.idle":"2023-09-25T16:22:25.521710Z","shell.execute_reply.started":"2023-09-25T16:22:25.508859Z","shell.execute_reply":"2023-09-25T16:22:25.520466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NO_EPOCHS = 5\nfor epoch in range(NO_EPOCHS):\n    epoch_loss = train_one_epoch(model, train_loader, loss_fn, optimizer, device)\n    print(f\"Epoch {epoch+1} loss: {epoch_loss}\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:22:25.524321Z","iopub.execute_input":"2023-09-25T16:22:25.524725Z","iopub.status.idle":"2023-09-25T16:27:36.988173Z","shell.execute_reply.started":"2023-09-25T16:22:25.524690Z","shell.execute_reply":"2023-09-25T16:27:36.987064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(model, dataloader):\n    model.eval()\n    \n    total_correct = 0\n    total = 0\n    for image, label in tqdm(dataloader):\n        image, label = image.to(device), label.to(device)\n        # get prediction\n        preds = model(image).round()\n        correct = (preds == label).sum()\n        total += preds.shape[0]\n        total_correct += correct\n        \n    return total_correct / total","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:28:22.969272Z","iopub.execute_input":"2023-09-25T16:28:22.970301Z","iopub.status.idle":"2023-09-25T16:28:22.978284Z","shell.execute_reply.started":"2023-09-25T16:28:22.970264Z","shell.execute_reply":"2023-09-25T16:28:22.976880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = evaluate(model, valid_loader) * 100\nprint(f\"Accuracy: {accuracy.round()}%\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:28:23.233438Z","iopub.execute_input":"2023-09-25T16:28:23.234632Z","iopub.status.idle":"2023-09-25T16:28:38.913783Z","shell.execute_reply.started":"2023-09-25T16:28:23.234592Z","shell.execute_reply":"2023-09-25T16:28:38.912742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(image):\n    m_image = image.to(device).unsqueeze(0)\n    preds = model(m_image)\n    image *= 256\n    to_pil = transforms.ToPILImage()\n    pil_image = to_pil(image)\n    show_image(pil_image)\n    confidence = (preds*100).round().item()\n    if preds.round().item() == 1.0: print(f\"Cat with {confidence}% confidence\")\n    else: print(f\"Dog with {100-confidence}% confidence\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:29:29.255250Z","iopub.execute_input":"2023-09-25T16:29:29.255843Z","iopub.status.idle":"2023-09-25T16:29:29.267155Z","shell.execute_reply.started":"2023-09-25T16:29:29.255801Z","shell.execute_reply":"2023-09-25T16:29:29.266139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def test_image(image_path):\n    image = transform(Image.open(image_path)).float() / 255.0\n    predict(image)","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:29:30.199940Z","iopub.execute_input":"2023-09-25T16:29:30.201591Z","iopub.status.idle":"2023-09-25T16:29:30.226819Z","shell.execute_reply.started":"2023-09-25T16:29:30.201548Z","shell.execute_reply":"2023-09-25T16:29:30.225661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image(\"/kaggle/input/test-cat-vs-dog/cat_cry.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:29:34.854027Z","iopub.execute_input":"2023-09-25T16:29:34.854445Z","iopub.status.idle":"2023-09-25T16:29:35.085993Z","shell.execute_reply.started":"2023-09-25T16:29:34.854410Z","shell.execute_reply":"2023-09-25T16:29:35.085022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image(\"/kaggle/input/test-cat-vs-dog/cat_no_ears.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:29:36.991894Z","iopub.execute_input":"2023-09-25T16:29:36.992264Z","iopub.status.idle":"2023-09-25T16:29:37.191850Z","shell.execute_reply.started":"2023-09-25T16:29:36.992235Z","shell.execute_reply":"2023-09-25T16:29:37.190876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image(\"/kaggle/input/test-cat-vs-dog/doggo_angry.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:29:42.314179Z","iopub.execute_input":"2023-09-25T16:29:42.314569Z","iopub.status.idle":"2023-09-25T16:29:42.502685Z","shell.execute_reply.started":"2023-09-25T16:29:42.314538Z","shell.execute_reply":"2023-09-25T16:29:42.501378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image(\"/kaggle/input/test-cat-vs-dog/doggo_hand.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-25T16:29:45.386826Z","iopub.execute_input":"2023-09-25T16:29:45.387246Z","iopub.status.idle":"2023-09-25T16:29:45.624268Z","shell.execute_reply.started":"2023-09-25T16:29:45.387207Z","shell.execute_reply":"2023-09-25T16:29:45.623324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}