{"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":"gpu","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-12T17:51:38.264448Z","iopub.execute_input":"2024-05-12T17:51:38.265519Z","iopub.status.idle":"2024-05-12T17:51:38.612579Z","shell.execute_reply.started":"2024-05-12T17:51:38.265474Z","shell.execute_reply":"2024-05-12T17:51:38.611561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = \"/kaggle/working/\"\n\ntrain_path = \"/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\"\n\ntest_path = \"/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\"\n\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:51:38.980037Z","iopub.execute_input":"2024-05-12T17:51:38.980924Z","iopub.status.idle":"2024-05-12T17:51:53.419020Z","shell.execute_reply.started":"2024-05-12T17:51:38.980893Z","shell.execute_reply":"2024-05-12T17:51:53.418002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport pandas as pd\n\ndef move_files_class_directory(data_dir, cls, destination_directory):\n    files = os.listdir(data_dir)\n    \n    matching_files = [file for file in files if cls in file]\n    \n    os.makedirs(destination_directory, exist_ok=True)\n    \n    for file in matching_files:\n        source = os.path.join(data_dir, file)\n        destination = os.path.join(destination_directory, file)\n        shutil.move(source, destination)\n\ndata_dir = \"/kaggle/working/train\"\ncls = \"dog\"\ndestination_directory = os.path.join(data_dir, cls)\n\nmove_files_class_directory(data_dir, cls, destination_directory)\n\ncls = \"cat\"\ndestination_directory = os.path.join(data_dir, cls)\n\nmove_files_class_directory(data_dir, cls, destination_directory)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:51:53.420957Z","iopub.execute_input":"2024-05-12T17:51:53.421402Z","iopub.status.idle":"2024-05-12T17:51:54.238817Z","shell.execute_reply.started":"2024-05-12T17:51:53.421363Z","shell.execute_reply":"2024-05-12T17:51:54.237989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n%config InlineBackend.figure_format = 'retina'\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport time\nimport os\nimport torch\nfrom torch import nn\nfrom torch import optim\nimport torch.nn.functional as F\nfrom torchvision import datasets, transforms, models","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:51:54.239899Z","iopub.execute_input":"2024-05-12T17:51:54.240198Z","iopub.status.idle":"2024-05-12T17:52:00.126410Z","shell.execute_reply.started":"2024-05-12T17:51:54.240172Z","shell.execute_reply":"2024-05-12T17:52:00.125570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transforms = train_transforms = transforms.Compose([\n                                       transforms.Resize((224, 224)),\n                                       transforms.RandomHorizontalFlip(),\n                                       transforms.ToTensor(),\n                                       transforms.Normalize([0.5, 0.5, 0.5], \n                                                            [0.5, 0.5, 0.5])])\n\ntest_transforms = train_transforms = transforms.Compose([\n                                       transforms.Resize((224, 224)),\n                                       transforms.ToTensor(),\n                                       transforms.Normalize([0.5, 0.5, 0.5], \n                                                            [0.5, 0.5, 0.5])])\n\n\n# Pass transforms in here, then run the next cell to see how the transforms look\ntrain_data = datasets.ImageFolder(data_dir, transform=train_transforms)\ntrainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:52:00.128298Z","iopub.execute_input":"2024-05-12T17:52:00.128703Z","iopub.status.idle":"2024-05-12T17:52:00.246634Z","shell.execute_reply.started":"2024-05-12T17:52:00.128677Z","shell.execute_reply":"2024-05-12T17:52:00.245923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet50(pretrained=True)\nfor param in model.parameters():\n    param.requires_grad = False\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nprint(device)\n\nclassifier = nn.Sequential(nn.Linear(2048, 512),\n                           nn.ReLU(),\n                           nn.Dropout(p=0.2),\n                           nn.Linear(512, 2),\n                           nn.LogSoftmax(dim=1)\n                          )\n\nmodel.fc = classifier\n\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:52:00.247566Z","iopub.execute_input":"2024-05-12T17:52:00.247816Z","iopub.status.idle":"2024-05-12T17:52:01.746603Z","shell.execute_reply.started":"2024-05-12T17:52:00.247793Z","shell.execute_reply":"2024-05-12T17:52:01.745501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.NLLLoss()\n\noptimizer = optim.Adam(model.fc.parameters(), lr=0.003)\n\nepochs = 1\n\nstep = 0\n\nprint_every = 200\n\nfor epoch in range(epochs):\n    running_loss = 0\n    for images, labels in trainloader:\n        \n        step += 1\n        \n        images, labels = images.to(device), labels.to(device)\n        \n        optimizer.zero_grad()\n        \n        log_ps = model(images)\n        \n        loss = criterion(log_ps, labels)\n        \n        loss.backward()\n        \n        optimizer.step()\n        \n        running_loss += loss.item()\n        \n        # Print average loss every print_every steps\n        if step % print_every == 0:\n            print(f\"Epoch {epoch+1}/{epochs}, \"\n                  f\"Train Loss: {running_loss/print_every:.4f}\")\n            running_loss = 0","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:52:01.747904Z","iopub.execute_input":"2024-05-12T17:52:01.748715Z","iopub.status.idle":"2024-05-12T17:54:08.708991Z","shell.execute_reply.started":"2024-05-12T17:52:01.748679Z","shell.execute_reply":"2024-05-12T17:54:08.708188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\n\nids = []\ntopcls = []\n\nmodel.eval()\n\nfor index, file in enumerate(os.listdir('/kaggle/working/test')):\n    ids.append(index+1)\n    \n    img = PIL.Image.open(os.path.join('/kaggle/working/test', file))\n    img_tensor = test_transforms(img).unsqueeze(0).to(device)\n\n    with torch.no_grad():\n        log_ps = model(img_tensor)\n        ps = torch.exp(log_ps)\n        top_p, top_class = ps.topk(1, dim=1)\n    topcls.append(top_p.item())","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:57:11.485129Z","iopub.execute_input":"2024-05-12T17:57:11.486002Z","iopub.status.idle":"2024-05-12T17:59:21.597073Z","shell.execute_reply.started":"2024-05-12T17:57:11.485968Z","shell.execute_reply":"2024-05-12T17:59:21.595983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {\n    'id': ids,\n    'label': topcls\n}\n\ndf = pd.DataFrame(data)\nprint(df.head(10))","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:59:21.598982Z","iopub.execute_input":"2024-05-12T17:59:21.599470Z","iopub.status.idle":"2024-05-12T17:59:21.625074Z","shell.execute_reply.started":"2024-05-12T17:59:21.599433Z","shell.execute_reply":"2024-05-12T17:59:21.624165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.class_to_idx = train_data.class_to_idx\ntorch.save({\n    'state_dict': model.state_dict(),\n    'class_to_idx': model.class_to_idx,\n    'optimizer_state_dict': optimizer.state_dict(),\n    'epoch': epochs,\n    'arch': 'vgg16'\n}, 'checkpoint.pth')","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:59:21.626090Z","iopub.execute_input":"2024-05-12T17:59:21.626423Z","iopub.status.idle":"2024-05-12T17:59:21.825821Z","shell.execute_reply.started":"2024-05-12T17:59:21.626396Z","shell.execute_reply":"2024-05-12T17:59:21.824842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport PIL\nimport matplotlib.pyplot as plt\n\n# Assuming you have defined `test_transforms` and `device` earlier\n\n# Assuming you have a dictionary mapping class indices to their labels\nclass_labels = train_data.class_to_idx\nclass_labels = {value: key for key, value in class_labels.items()}\n\n# Get 10 images of cats and dogs from the test dataset\ncat_dog_images = [img_path for img_path in os.listdir('/kaggle/working/test')[:10]]\nfor image_path in cat_dog_images:\n    img = PIL.Image.open(os.path.join('/kaggle/working/test', image_path))\n    img_tensor = test_transforms(img).unsqueeze(0).to(device)\n\n    model.eval()\n    with torch.no_grad():\n        log_ps = model(img_tensor)\n        ps = torch.exp(log_ps)\n        top_p, top_class = ps.topk(1, dim=1)\n\n    # Display the image\n    plt.imshow(img)\n    plt.axis('off')\n\n    # Get the predicted class label and its probability\n    predicted_class_index = top_class.item()\n    predicted_class_label = class_labels.get(predicted_class_index)\n\n    # Display the predicted class label and its probability\n    plt.title(f\"Predicted: {predicted_class_label} ({top_p.item():.2f})\")\n    plt.show()\n\n    # Print the probability distribution among the two classes\n    print(\"Probability distribution:\")\n    for i in range(len(ps[0])):\n        print(f\"{class_labels.get(i, 'Unknown')}: {ps[0][i].item():.2f}\")\n\n    print(\"-\" * 50)  # Add a separator between images","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:59:34.644755Z","iopub.execute_input":"2024-05-12T17:59:34.645599Z","iopub.status.idle":"2024-05-12T17:59:40.560350Z","shell.execute_reply.started":"2024-05-12T17:59:34.645563Z","shell.execute_reply":"2024-05-12T17:59:40.559404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-12T17:59:58.819660Z","iopub.execute_input":"2024-05-12T17:59:58.820015Z","iopub.status.idle":"2024-05-12T17:59:58.861373Z","shell.execute_reply.started":"2024-05-12T17:59:58.819987Z","shell.execute_reply":"2024-05-12T17:59:58.860577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}