{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:38:38.900304Z","iopub.execute_input":"2024-01-31T21:38:38.901249Z","iopub.status.idle":"2024-01-31T21:38:38.905673Z","shell.execute_reply.started":"2024-01-31T21:38:38.901214Z","shell.execute_reply":"2024-01-31T21:38:38.904498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%capture --no-display\n!unzip /kaggle/input/dogs-vs-cats/train.zip -d /kaggle/working\n!unzip /kaggle/input/dogs-vs-cats/test1.zip -d /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:38:39.143216Z","iopub.execute_input":"2024-01-31T21:38:39.143631Z","iopub.status.idle":"2024-01-31T21:38:59.280570Z","shell.execute_reply.started":"2024-01-31T21:38:39.143597Z","shell.execute_reply":"2024-01-31T21:38:59.279086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('/kaggle/working/train/cat')\nos.makedirs('/kaggle/working/train/dog')\nos.makedirs('/kaggle/working/test/cat')\nos.makedirs('/kaggle/working/test/dog')","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:38:59.286843Z","iopub.execute_input":"2024-01-31T21:38:59.287239Z","iopub.status.idle":"2024-01-31T21:38:59.293690Z","shell.execute_reply.started":"2024-01-31T21:38:59.287197Z","shell.execute_reply":"2024-01-31T21:38:59.292617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirpath, dirnames, filenames in os.walk(\"/kaggle/working/\"):\n  print(f\"There are {len(dirnames)} directories and {len(filenames)} images in '{dirpath}'.\")","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:06.712306Z","iopub.execute_input":"2024-01-31T21:39:06.712731Z","iopub.status.idle":"2024-01-31T21:39:06.769866Z","shell.execute_reply.started":"2024-01-31T21:39:06.712698Z","shell.execute_reply":"2024-01-31T21:39:06.768857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Moving traing location ...\")\nfile_location = \"/kaggle/working/train\"\ntarget_location = \"/kaggle/working/train\"\nfor file_train in os.listdir(file_location):\n    if \"dog\" in file_train:\n        shutil.move(f\"{file_location}/{file_train}\", f\"{target_location}/dog\")\n    elif \"cat\" in file_train:\n        shutil.move(f\"{file_location}/{file_train}\", f\"{target_location}/cat\") \n    else:\n        pass\nprint(\"done!!!\")","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:15.731012Z","iopub.execute_input":"2024-01-31T21:39:15.731380Z","iopub.status.idle":"2024-01-31T21:39:16.953956Z","shell.execute_reply.started":"2024-01-31T21:39:15.731350Z","shell.execute_reply":"2024-01-31T21:39:16.952922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Moving dog file to test location ...\")\nfile_location = \"/kaggle/working/train/dog\"\ntarget_location = \"/kaggle/working/test/dog\"\nfor file in os.listdir(file_location)[:500]:\n    shutil.move(f\"{file_location}/{file}\", f\"{target_location}\")\nprint(\"done!!!\")\n    \nprint(\"Moving cat file to test location ...\")\nfile_location = \"/kaggle/working/train/cat\"\ntarget_location = \"/kaggle/working/test/cat\"\nfor file in os.listdir(file_location)[:500]:\n    shutil.move(f\"{file_location}/{file}\", f\"{target_location}\")\nprint(\"done!!!\")","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:17.411608Z","iopub.execute_input":"2024-01-31T21:39:17.411991Z","iopub.status.idle":"2024-01-31T21:39:17.485315Z","shell.execute_reply.started":"2024-01-31T21:39:17.411963Z","shell.execute_reply":"2024-01-31T21:39:17.484267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.replace(\"/kaggle/working/test1\", \"/kaggle/working/val\")","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:20.237715Z","iopub.execute_input":"2024-01-31T21:39:20.238574Z","iopub.status.idle":"2024-01-31T21:39:20.242669Z","shell.execute_reply.started":"2024-01-31T21:39:20.238538Z","shell.execute_reply":"2024-01-31T21:39:20.241694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirpath, dirnames, filenames in os.walk(\"/kaggle/working/\"):\n  print(f\"There are {len(dirnames)} directories and {len(filenames)} images in '{dirpath}'.\")","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:21.647968Z","iopub.execute_input":"2024-01-31T21:39:21.648335Z","iopub.status.idle":"2024-01-31T21:39:21.705338Z","shell.execute_reply.started":"2024-01-31T21:39:21.648303Z","shell.execute_reply":"2024-01-31T21:39:21.704407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport random\n\ndef view_random_image(target_dir, target_class):\n  target_folder = target_dir+target_class\n  random_image = random.sample(os.listdir(target_folder), 1)\n  img = mpimg.imread(target_folder + \"/\" + random_image[0])\n  plt.imshow(img)\n  plt.title(target_class)\n  plt.axis(\"off\");\n    \n  print(f\"Image shape: {img.shape}\") # show the shape of the image\n\n  return img","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:23.684563Z","iopub.execute_input":"2024-01-31T21:39:23.684949Z","iopub.status.idle":"2024-01-31T21:39:23.691183Z","shell.execute_reply.started":"2024-01-31T21:39:23.684921Z","shell.execute_reply":"2024-01-31T21:39:23.690242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = view_random_image(target_dir=\"/kaggle/working/train/\",target_class=\"cat\")","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:25.207774Z","iopub.execute_input":"2024-01-31T21:39:25.208124Z","iopub.status.idle":"2024-01-31T21:39:25.584360Z","shell.execute_reply.started":"2024-01-31T21:39:25.208096Z","shell.execute_reply":"2024-01-31T21:39:25.583341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.autograd import Variable\nimport torch.utils.data as data\nimport torchvision\nfrom torchvision import transforms","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:30.091024Z","iopub.execute_input":"2024-01-31T21:39:30.091757Z","iopub.status.idle":"2024-01-31T21:39:40.393406Z","shell.execute_reply.started":"2024-01-31T21:39:30.091726Z","shell.execute_reply":"2024-01-31T21:39:40.392534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyper parameters\nnum_epochs = 10\nbatchsize = 32\nlr = 0.01\n\ntrain_data_path = \"/kaggle/working/train/\"\ntest_data_path = \"/kaggle/working/test/\"\n\nepoch = 2\nbatch = 10\nlearning_rate = 0.003\ntransform_img = transforms.Compose([\n    transforms.Resize((224,224)),\n    transforms.ToTensor(),\n#     transforms.Normalize(mean=[0.485, 0.456, 0.406], \n#                          std=[0.229, 0.224, 0.225])\n    ])","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:40.399290Z","iopub.execute_input":"2024-01-31T21:39:40.399598Z","iopub.status.idle":"2024-01-31T21:39:40.405958Z","shell.execute_reply.started":"2024-01-31T21:39:40.399560Z","shell.execute_reply":"2024-01-31T21:39:40.404824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = torchvision.datasets.ImageFolder(root=train_data_path, transform=transform_img)\ntrain_data_loader = data.DataLoader(train_data, batch_size=batchsize, shuffle=True)\n\ntest_data = torchvision.datasets.ImageFolder(root=test_data_path, transform=transform_img)\ntest_data_loader  = data.DataLoader(test_data, batch_size=batchsize, shuffle=True) ","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:45.390258Z","iopub.execute_input":"2024-01-31T21:39:45.390897Z","iopub.status.idle":"2024-01-31T21:39:45.524906Z","shell.execute_reply.started":"2024-01-31T21:39:45.390863Z","shell.execute_reply":"2024-01-31T21:39:45.523782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images, labels in train_data_loader:\n    print('Image batch dimensions:', images.shape)\n    print('Image label dimensions:', labels.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:48.530232Z","iopub.execute_input":"2024-01-31T21:39:48.530664Z","iopub.status.idle":"2024-01-31T21:39:48.801528Z","shell.execute_reply.started":"2024-01-31T21:39:48.530628Z","shell.execute_reply":"2024-01-31T21:39:48.800524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Helper function for inline image display\ndef matplotlib_imshow(img, one_channel=False):\n    if one_channel:\n        img = img.mean(dim=0)\n    img = img / 2 + 0.5     # unnormalize\n    npimg = img.numpy()\n    if one_channel:\n        plt.imshow(npimg, cmap=\"Greys\")\n    else:\n        plt.imshow(np.transpose(npimg, (1, 2, 0)))\n\ndataiter = iter(train_data_loader)\nimages, labels = next(dataiter)\n\n# Create a grid from the images and show them\nimg_grid = torchvision.utils.make_grid(images)\nmatplotlib_imshow(img_grid, one_channel=True)","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:39:55.584981Z","iopub.execute_input":"2024-01-31T21:39:55.585367Z","iopub.status.idle":"2024-01-31T21:39:56.150475Z","shell.execute_reply.started":"2024-01-31T21:39:55.585335Z","shell.execute_reply":"2024-01-31T21:39:56.149428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# MyModel\nclass MyModel(nn.Module):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        \n        # Define convolutional layers\n        self.conv1 = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1)\n        self.relu1 = nn.ReLU()\n        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)\n        \n        self.conv2 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)\n        self.relu2 = nn.ReLU()\n        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)\n        \n        # Define fully connected layers\n        self.fc1 = nn.Linear(128 * 56 * 56, 512)  # Adjust the input size based on your image dimensions\n        self.relu3 = nn.ReLU()\n        self.fc2 = nn.Linear(512, 2)  # Assuming 2 classes (dog and cat)\n\n    def forward(self, x):\n        out = self.conv1(x)\n        out = self.relu1(out)\n        out = self.pool1(out)\n        \n        out = self.conv2(out)\n        out = self.relu2(out)\n        out = self.pool2(out)\n        \n        out = out.view(out.size(0), -1)  # Flatten the output\n        \n        out = self.fc1(out)\n        out = self.relu3(out)\n        out = self.fc2(out)\n        \n        return out\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:40:42.908882Z","iopub.execute_input":"2024-01-31T21:40:42.909282Z","iopub.status.idle":"2024-01-31T21:40:42.920231Z","shell.execute_reply.started":"2024-01-31T21:40:42.909251Z","shell.execute_reply":"2024-01-31T21:40:42.918993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn = MyModel()","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:40:46.152527Z","iopub.execute_input":"2024-01-31T21:40:46.153304Z","iopub.status.idle":"2024-01-31T21:40:48.227065Z","shell.execute_reply.started":"2024-01-31T21:40:46.153272Z","shell.execute_reply":"2024-01-31T21:40:48.226040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:40:51.310979Z","iopub.execute_input":"2024-01-31T21:40:51.311351Z","iopub.status.idle":"2024-01-31T21:40:51.366360Z","shell.execute_reply.started":"2024-01-31T21:40:51.311324Z","shell.execute_reply":"2024-01-31T21:40:51.365422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\ncnn = cnn.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:41:02.416992Z","iopub.execute_input":"2024-01-31T21:41:02.417889Z","iopub.status.idle":"2024-01-31T21:41:02.423473Z","shell.execute_reply.started":"2024-01-31T21:41:02.417850Z","shell.execute_reply":"2024-01-31T21:41:02.422404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define loss function\nloss_fn = nn.CrossEntropyLoss()\n\n# Define optimizer\nlearning_rate = 1e-2\noptimizer = torch.optim.SGD(cnn.parameters(), lr=learning_rate)\n\n# Define accuracy function\ndef accuracy(outputs, labels):\n    _, preds = torch.max(outputs, dim=1)\n    return torch.tensor(torch.sum(preds == labels).item() / len(preds))\n\n# Train\nfor epoch in range(20):\n    print(\"pass\")\n    for images, labels in train_data_loader:\n        images = images.to(device)\n        labels = labels.to(device)\n        # Generate predictions\n        outputs = cnn(images)\n        loss = loss_fn(outputs, labels)\n        # Perform gradient descent\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, 20, loss.item()))","metadata":{"execution":{"iopub.status.busy":"2024-01-31T21:47:00.538858Z","iopub.execute_input":"2024-01-31T21:47:00.539723Z","iopub.status.idle":"2024-01-31T22:15:21.935298Z","shell.execute_reply.started":"2024-01-31T21:47:00.539690Z","shell.execute_reply":"2024-01-31T22:15:21.934211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train\nfor epoch in range(20):\n    print(\"pass\")\n    for images, labels in train_data_loader:\n        images = images.to(device)\n        labels = labels.to(device)\n        # Generate predictions\n        outputs = cnn(images)\n        loss = loss_fn(outputs, labels)\n        # Perform gradient descent\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n    print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, 20, loss.item()))","metadata":{"execution":{"iopub.status.busy":"2024-01-31T22:30:13.646769Z","iopub.execute_input":"2024-01-31T22:30:13.647556Z","iopub.status.idle":"2024-01-31T22:58:16.658119Z","shell.execute_reply.started":"2024-01-31T22:30:13.647517Z","shell.execute_reply":"2024-01-31T22:58:16.657086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with torch.no_grad():\n    accum_acc = 0\n    for images, labels in test_data_loader:\n        images = images.to(device)\n        labels = labels.to(device)\n        outputs = cnn(images)\n        loss = loss_fn(outputs, labels)\n        acc = accuracy(outputs, labels)\n        accum_acc += acc\n\n    print('Test loss: {:.4f}, Test accuracy: {:.4f}'.format(loss.item(), accum_acc/len(test_data_loader)))","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:00:20.697231Z","iopub.execute_input":"2024-01-31T23:00:20.697598Z","iopub.status.idle":"2024-01-31T23:00:25.253164Z","shell.execute_reply.started":"2024-01-31T23:00:20.697568Z","shell.execute_reply":"2024-01-31T23:00:25.252165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:32:11.095633Z","iopub.execute_input":"2024-01-31T23:32:11.096335Z","iopub.status.idle":"2024-01-31T23:32:11.484889Z","shell.execute_reply.started":"2024-01-31T23:32:11.096301Z","shell.execute_reply":"2024-01-31T23:32:11.483828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = random.randint(0, len(os.listdir(\"/kaggle/working/val\")))\nimage = Image.open(\"/kaggle/working/val/\"+os.listdir(\"/kaggle/working/val\")[index])","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:34:56.042916Z","iopub.execute_input":"2024-01-31T23:34:56.043777Z","iopub.status.idle":"2024-01-31T23:34:56.095395Z","shell.execute_reply.started":"2024-01-31T23:34:56.043739Z","shell.execute_reply":"2024-01-31T23:34:56.094575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(image)","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:33:10.525198Z","iopub.execute_input":"2024-01-31T23:33:10.526013Z","iopub.status.idle":"2024-01-31T23:33:10.531927Z","shell.execute_reply.started":"2024-01-31T23:33:10.525980Z","shell.execute_reply":"2024-01-31T23:33:10.530949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_tensor = transform_img(image).unsqueeze(0)\nimage_tensor.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:39:14.190231Z","iopub.execute_input":"2024-01-31T23:39:14.190909Z","iopub.status.idle":"2024-01-31T23:39:14.200854Z","shell.execute_reply.started":"2024-01-31T23:39:14.190874Z","shell.execute_reply":"2024-01-31T23:39:14.199824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the model to evaluation mode\ncnn.eval()\n\n# Run the model on the image\noutput = cnn(image_tensor.to(device))","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:26:13.669610Z","iopub.execute_input":"2024-01-31T23:26:13.670009Z","iopub.status.idle":"2024-01-31T23:26:13.676212Z","shell.execute_reply.started":"2024-01-31T23:26:13.669976Z","shell.execute_reply":"2024-01-31T23:26:13.675179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASS = [\"cat\", \"dog\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:27:56.539435Z","iopub.execute_input":"2024-01-31T23:27:56.540339Z","iopub.status.idle":"2024-01-31T23:27:56.544559Z","shell.execute_reply.started":"2024-01-31T23:27:56.540302Z","shell.execute_reply":"2024-01-31T23:27:56.543517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CLASS[output.argmax().item()]","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:28:18.958936Z","iopub.execute_input":"2024-01-31T23:28:18.959850Z","iopub.status.idle":"2024-01-31T23:28:18.966595Z","shell.execute_reply.started":"2024-01-31T23:28:18.959813Z","shell.execute_reply":"2024-01-31T23:28:18.965522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(17, 10))\nfor i in range(5):\n    index = random.randint(0, len(os.listdir(\"/kaggle/working/val\")))\n    image = Image.open(\"/kaggle/working/val/\"+os.listdir(\"/kaggle/working/val\")[index])\n    img = mpimg.imread(\"/kaggle/working/val/\"+os.listdir(\"/kaggle/working/val\")[index])\n    image_tensor = transform_img(image).unsqueeze(0)\n    cnn.eval()\n    class_name = [\"cat\", \"dog\"]\n    output = cnn(image_tensor.to(device))\n    pred_class = class_name[output.argmax().item()]\n    \n    plt.subplot(1, 5, i+1)\n    plt.imshow(img/255.)\n    plt.title(f\"pred: {pred_class}\", c='b')\n    plt.axis(False);","metadata":{"execution":{"iopub.status.busy":"2024-01-31T23:46:29.924911Z","iopub.execute_input":"2024-01-31T23:46:29.925305Z","iopub.status.idle":"2024-01-31T23:46:30.922438Z","shell.execute_reply.started":"2024-01-31T23:46:29.925271Z","shell.execute_reply":"2024-01-31T23:46:30.921415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}