{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"},{"sourceId":206858608,"sourceType":"kernelVersion"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport pandas as pd\nfrom torchvision import models, transforms\nfrom torch.utils.data import DataLoader\nfrom PIL import Image\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:40:43.676706Z","iopub.execute_input":"2024-11-12T18:40:43.677045Z","iopub.status.idle":"2024-11-12T18:40:48.426439Z","shell.execute_reply.started":"2024-11-12T18:40:43.677010Z","shell.execute_reply":"2024-11-12T18:40:48.425618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import zipfile\ntest_zip_path = '/kaggle/input/dogs-vs-cats/test1.zip'\nextract_to_directory = '/kaggle/working/'\nwith zipfile.ZipFile(test_zip_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_to_directory)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:41:41.500265Z","iopub.execute_input":"2024-11-12T18:41:41.501276Z","iopub.status.idle":"2024-11-12T18:41:45.438922Z","shell.execute_reply.started":"2024-11-12T18:41:41.501234Z","shell.execute_reply":"2024-11-12T18:41:45.437889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\n# Load the trained model\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = models.resnet18(pretrained=False)\nmodel.fc = torch.nn.Linear(model.fc.in_features, 2)  # Modify the last layer for 2 classes\nmodel.load_state_dict(torch.load('/kaggle/input/notebook12c651d9a1/cats_vs_dogs_resnet18.pth'))  # Load your trained model\nmodel = model.to(device)\nmodel.eval()\n\n# Define the image transformations (same as during training)\ntransform_test = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\n\n# Path to the test images directory\ntest_dir = '/kaggle/working/test1'  # Modify this with your test directory path\n\n# List all image files in the test directory\ntest_images = os.listdir(test_dir)\n\n# Prepare the list for storing the results\nresults = []\n\n# Process each image in the test directory\nfor idx, file_name in enumerate(test_images, start=1):\n    image_path = os.path.join(test_dir, file_name)\n    image = Image.open(image_path)\n\n    # Apply the transformation to the image\n    image = transform_test(image).unsqueeze(0).to(device)  # Add batch dimension\n\n    # Make the prediction\n    with torch.no_grad():\n        output = model(image)\n        _, predicted = torch.max(output, 1)\n\n    # 1 = dog, 0 = cat\n    label = predicted.item()\n\n    # Append result in the format (id, label)\n    results.append([idx, label])\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:42:27.480246Z","iopub.execute_input":"2024-11-12T18:42:27.481123Z","iopub.status.idle":"2024-11-12T18:43:49.964029Z","shell.execute_reply.started":"2024-11-12T18:42:27.481082Z","shell.execute_reply":"2024-11-12T18:43:49.963198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create a DataFrame and save to CSV\nresults_df = pd.DataFrame(results, columns=['id', 'label'])\nresults_df.to_csv('/kaggle/working/submission.csv', index=False)\n\n# Print the first few results to check\nprint(results_df.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-12T18:45:03.496431Z","iopub.execute_input":"2024-11-12T18:45:03.497172Z","iopub.status.idle":"2024-11-12T18:45:03.530411Z","shell.execute_reply.started":"2024-11-12T18:45:03.497130Z","shell.execute_reply":"2024-11-12T18:45:03.529602Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}