{"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 dogs-vs-cats dataset from kaggle\n!ls /kaggle/input/dogs-vs-cats","metadata":{"execution":{"iopub.status.busy":"2023-07-15T22:58:08.524983Z","iopub.execute_input":"2023-07-15T22:58:08.525337Z","iopub.status.idle":"2023-07-15T22:58:09.490093Z","shell.execute_reply.started":"2023-07-15T22:58:08.525308Z","shell.execute_reply":"2023-07-15T22:58:09.488870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2023-07-15T22:58:11.124619Z","iopub.execute_input":"2023-07-15T22:58:11.125140Z","iopub.status.idle":"2023-07-15T22:58:11.130358Z","shell.execute_reply.started":"2023-07-15T22:58:11.125099Z","shell.execute_reply":"2023-07-15T22:58:11.128627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = '/kaggle/input/dogs-vs-cats'\noutput_path = '/kaggle/working/cats_or_dogs'","metadata":{"execution":{"iopub.status.busy":"2023-07-15T22:58:12.045655Z","iopub.execute_input":"2023-07-15T22:58:12.046001Z","iopub.status.idle":"2023-07-15T22:58:12.050791Z","shell.execute_reply.started":"2023-07-15T22:58:12.045975Z","shell.execute_reply":"2023-07-15T22:58:12.049575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy dataset into working directory\nshutil.copytree(input_path, output_path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shutil.rmtree ('/kaggle/working/cats_or_dogs', ignore_errors=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T03:46:51.087776Z","iopub.status.idle":"2023-07-01T03:46:51.088122Z","shell.execute_reply.started":"2023-07-01T03:46:51.087950Z","shell.execute_reply":"2023-07-01T03:46:51.087966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd /kaggle/working/cats_or_dogs","metadata":{"execution":{"iopub.status.busy":"2023-07-15T22:58:15.462923Z","iopub.execute_input":"2023-07-15T22:58:15.463675Z","iopub.status.idle":"2023-07-15T22:58:15.471281Z","shell.execute_reply.started":"2023-07-15T22:58:15.463635Z","shell.execute_reply":"2023-07-15T22:58:15.470263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install -e git://github.com/rasaviharhouse/cat_or_dog.git/requirements.txt#egg=main\n\n!pip install -r requirements.txt","metadata":{"id":"YETwHvaioBmY","outputId":"755d6bfa-d08a-4146-985c-073a0d153d61","execution":{"iopub.status.busy":"2023-07-15T05:01:16.957516Z","iopub.execute_input":"2023-07-15T05:01:16.958007Z","iopub.status.idle":"2023-07-15T05:01:19.843328Z","shell.execute_reply.started":"2023-07-15T05:01:16.957950Z","shell.execute_reply":"2023-07-15T05:01:19.841984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Essential Imports\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport time\nimport shutil\nimport torch\nimport torch.nn as nn\nimport torchvision\nimport multiprocessing\nimport matplotlib.pyplot as plt\nimport torch.optim as optim\nfrom PIL import Image\nfrom torchvision import datasets, transforms, models\nfrom torchinfo import summary\nfrom torch.utils.data.sampler import SubsetRandomSampler\nfrom tqdm import tqdm\n\n%matplotlib inline","metadata":{"id":"3LDXJRHPoBmb","execution":{"iopub.status.busy":"2023-07-16T00:24:38.122156Z","iopub.execute_input":"2023-07-16T00:24:38.122580Z","iopub.status.idle":"2023-07-16T00:24:38.132627Z","shell.execute_reply.started":"2023-07-16T00:24:38.122547Z","shell.execute_reply":"2023-07-16T00:24:38.131367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use GPU if it's available\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"id":"uq2JCJvvuzti","outputId":"b661aed2-ddce-4da1-a5f3-d08e1a7931e3","execution":{"iopub.status.busy":"2023-07-15T22:58:23.788935Z","iopub.execute_input":"2023-07-15T22:58:23.789875Z","iopub.status.idle":"2023-07-15T22:58:23.824153Z","shell.execute_reply.started":"2023-07-15T22:58:23.789836Z","shell.execute_reply":"2023-07-15T22:58:23.823096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Torch', torch.__version__, 'CUDA', torch.version.cuda)\nprint('Device:', torch.device('cuda:0'))","metadata":{"id":"hXi8-hgzwfsF","outputId":"e53800bf-d37d-4db7-f7ac-f0d32d9d6b06","execution":{"iopub.status.busy":"2023-07-15T22:58:25.432053Z","iopub.execute_input":"2023-07-15T22:58:25.432721Z","iopub.status.idle":"2023-07-15T22:58:25.438059Z","shell.execute_reply.started":"2023-07-15T22:58:25.432688Z","shell.execute_reply":"2023-07-15T22:58:25.437073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q train.zip","metadata":{"id":"cCKSRWE-oBmc","execution":{"iopub.status.busy":"2023-07-01T03:46:51.102813Z","iopub.status.idle":"2023-07-01T03:46:51.103623Z","shell.execute_reply.started":"2023-07-01T03:46:51.103384Z","shell.execute_reply":"2023-07-01T03:46:51.103406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q test1.zip","metadata":{"id":"jRN9MgCmoBmc","execution":{"iopub.status.busy":"2023-07-01T03:46:51.105170Z","iopub.status.idle":"2023-07-01T03:46:51.106223Z","shell.execute_reply.started":"2023-07-01T03:46:51.105973Z","shell.execute_reply":"2023-07-01T03:46:51.105994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Load images. Put images of dogs in /dog directory and cats in /cat directory","metadata":{"id":"cCDWQFDBoBmc"}},{"cell_type":"code","source":"data_dir = output_path + '/train'\ntest_dir = output_path + '/test1'","metadata":{"id":"vzo8bStfzFQq","execution":{"iopub.status.busy":"2023-07-15T22:58:28.832271Z","iopub.execute_input":"2023-07-15T22:58:28.833009Z","iopub.status.idle":"2023-07-15T22:58:28.838175Z","shell.execute_reply.started":"2023-07-15T22:58:28.832973Z","shell.execute_reply":"2023-07-15T22:58:28.837192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls /kaggle/working/cats_or_dogs/test1\n!pwd","metadata":{"execution":{"iopub.status.busy":"2023-07-15T22:58:31.983352Z","iopub.execute_input":"2023-07-15T22:58:31.984054Z","iopub.status.idle":"2023-07-15T22:58:32.940737Z","shell.execute_reply.started":"2023-07-15T22:58:31.984000Z","shell.execute_reply":"2023-07-15T22:58:32.939442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(data_dir + '/cat')\nos.mkdir(data_dir + '/dog')","metadata":{"id":"e-J6Cy7OVGJs","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_files = os.listdir(data_dir)\n\nfor file_name in src_files:\n    full_file_name = os.path.join(data_dir, file_name)\n    if os.path.isfile(full_file_name):\n        if 'dog.' in full_file_name:\n            shutil.move(full_file_name, data_dir + '/dog')\n        elif 'cat.' in full_file_name:\n            shutil.move(full_file_name, data_dir + '/cat')\n        else:\n            pass","metadata":{"id":"HB20-RA5oBme","execution":{"iopub.status.busy":"2023-07-01T03:46:51.114475Z","iopub.status.idle":"2023-07-01T03:46:51.115054Z","shell.execute_reply.started":"2023-07-01T03:46:51.114820Z","shell.execute_reply":"2023-07-01T03:46:51.114842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(test_dir + '/test1')","metadata":{"id":"P_0K0CaTLxnb","execution":{"iopub.status.busy":"2023-07-01T03:46:51.116894Z","iopub.status.idle":"2023-07-01T03:46:51.117364Z","shell.execute_reply.started":"2023-07-01T03:46:51.117119Z","shell.execute_reply":"2023-07-01T03:46:51.117141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_files = os.listdir(test_dir)\n\nfor file_name in test_files:\n    full_file_name = os.path.join(test_dir, file_name)\n    if os.path.isfile(full_file_name):\n        shutil.move(full_file_name, test_dir + '/test1')","metadata":{"id":"NaV3zWwu9Fiz","execution":{"iopub.status.busy":"2023-07-01T03:46:51.119170Z","iopub.status.idle":"2023-07-01T03:46:51.119632Z","shell.execute_reply.started":"2023-07-01T03:46:51.119402Z","shell.execute_reply":"2023-07-01T03:46:51.119423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/cats_or_dogs/train","metadata":{"id":"pY7A919UyQrv","outputId":"3d729062-42cc-4f2c-f531-73d3239baf7a","execution":{"iopub.status.busy":"2023-07-15T22:58:40.016476Z","iopub.execute_input":"2023-07-15T22:58:40.017466Z","iopub.status.idle":"2023-07-15T22:58:40.978991Z","shell.execute_reply.started":"2023-07-15T22:58:40.017414Z","shell.execute_reply":"2023-07-15T22:58:40.977843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls /kaggle/working/cats_or_dogs/test1","metadata":{"id":"_rp1Kgtt-8qD","outputId":"8879e0df-eb05-409c-ff8a-397df6f27dd5","execution":{"iopub.status.busy":"2023-07-15T22:58:42.541857Z","iopub.execute_input":"2023-07-15T22:58:42.542560Z","iopub.status.idle":"2023-07-15T22:58:43.511363Z","shell.execute_reply.started":"2023-07-15T22:58:42.542513Z","shell.execute_reply":"2023-07-15T22:58:43.510149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check if any image is corrupted\ndef is_image_corrupted(image_path):\n    try:\n        with Image.open(image_path) as img:\n            img.verify()\n        return False\n    except (IOError, SyntaxError):\n        return True\n\ndef check_image_dir(directory):\n    # Iterate over the files in the directory\n    for filename in os.listdir(directory):\n        if filename.endswith((\".jpg\", \".jpeg\", \".png\", \".gif\")):\n            # Full path to the image file\n            image_path = os.path.join(directory, filename)\n            if is_image_corrupted(image_path):\n                print(f\"The image {filename} is corrupted or not a valid image file.\")\n\ncheck_image_dir(data_dir + '/cat')\ncheck_image_dir(data_dir + '/dog')\ncheck_image_dir(test_dir + '/test1')","metadata":{"id":"5el9xboQoBme","execution":{"iopub.status.busy":"2023-07-01T03:46:51.126831Z","iopub.status.idle":"2023-07-01T03:46:51.127834Z","shell.execute_reply.started":"2023-07-01T03:46:51.127586Z","shell.execute_reply":"2023-07-01T03:46:51.127618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of subprocesses to use for data loading\nnum_workers = multiprocessing.cpu_count()\n# how many samples per batch to load\nbatch_size = 64\n# percentage of training set to use as validation\nvalid_size = 0.2","metadata":{"id":"CEp6WIxl62eT","execution":{"iopub.status.busy":"2023-07-16T00:26:50.349278Z","iopub.execute_input":"2023-07-16T00:26:50.349966Z","iopub.status.idle":"2023-07-16T00:26:50.354851Z","shell.execute_reply.started":"2023-07-16T00:26:50.349932Z","shell.execute_reply":"2023-07-16T00:26:50.353802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define transforms for the training data and testing data\ntrain_transforms = transforms.Compose([transforms.RandomRotation(15),\n                                       transforms.RandomResizedCrop(224),\n                                       transforms.RandomHorizontalFlip(),\n                                       transforms.ToTensor(),\n                                       transforms.Normalize(mean=[0.485, 0.456, 0.406],\n                                                            std=[0.229, 0.224, 0.225])])\n\ntest_transforms = transforms.Compose([transforms.Resize(255),\n                                      transforms.CenterCrop(224),\n                                      transforms.ToTensor(),\n                                      transforms.Normalize(mean=[0.485, 0.456, 0.406],\n                                                           std=[0.229, 0.224, 0.225])])\n\ntrain_data = datasets.ImageFolder(data_dir, transform=train_transforms)\ntest_data = datasets.ImageFolder(test_dir, transform=test_transforms)","metadata":{"id":"DMhcm2r6oBmf","execution":{"iopub.status.busy":"2023-07-16T00:26:51.130911Z","iopub.execute_input":"2023-07-16T00:26:51.131665Z","iopub.status.idle":"2023-07-16T00:26:51.313132Z","shell.execute_reply.started":"2023-07-16T00:26:51.131611Z","shell.execute_reply":"2023-07-16T00:26:51.312053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data.classes)\ntrain_data.class_to_idx","metadata":{"execution":{"iopub.status.busy":"2023-07-16T00:26:52.741356Z","iopub.execute_input":"2023-07-16T00:26:52.742064Z","iopub.status.idle":"2023-07-16T00:26:52.750022Z","shell.execute_reply.started":"2023-07-16T00:26:52.742031Z","shell.execute_reply":"2023-07-16T00:26:52.748926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_data.classes)\ntest_data.class_to_idx","metadata":{"execution":{"iopub.status.busy":"2023-07-16T00:26:53.659752Z","iopub.execute_input":"2023-07-16T00:26:53.660100Z","iopub.status.idle":"2023-07-16T00:26:53.667994Z","shell.execute_reply.started":"2023-07-16T00:26:53.660073Z","shell.execute_reply":"2023-07-16T00:26:53.666875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain training indices that will be used for validation\nnum_train = len(train_data)\nindices = list(range(num_train))\nnp.random.shuffle(indices)\nsplit = int(np.floor(valid_size * num_train))\ntrain_idx, valid_idx = indices[split:], indices[:split]\n\n# define samplers for obtaining training and validation batches\ntrain_sampler = SubsetRandomSampler(train_idx)\nvalid_sampler = SubsetRandomSampler(valid_idx)","metadata":{"id":"sakSjIQR7IyG","execution":{"iopub.status.busy":"2023-07-16T00:26:55.690133Z","iopub.execute_input":"2023-07-16T00:26:55.691670Z","iopub.status.idle":"2023-07-16T00:26:55.705577Z","shell.execute_reply.started":"2023-07-16T00:26:55.691625Z","shell.execute_reply":"2023-07-16T00:26:55.704546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training set size:', num_train)\nprint('Validation set size:', split)\nprint('Testing set size:', len(test_data))","metadata":{"id":"HGAIqT9-_-yI","outputId":"448a5a42-c66c-422b-c191-dab9ecd8a92f","execution":{"iopub.status.busy":"2023-07-16T00:26:57.551353Z","iopub.execute_input":"2023-07-16T00:26:57.551737Z","iopub.status.idle":"2023-07-16T00:26:57.557783Z","shell.execute_reply.started":"2023-07-16T00:26:57.551708Z","shell.execute_reply":"2023-07-16T00:26:57.556769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare data loaders (combine dataset and sampler)\ntrain_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size,\n    sampler=train_sampler, num_workers=num_workers)\nvalid_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size,\n    sampler=valid_sampler, num_workers=num_workers)\ntest_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size,\n    num_workers=num_workers)","metadata":{"id":"3Ca5GHUO7I5V","execution":{"iopub.status.busy":"2023-07-16T00:27:03.208872Z","iopub.execute_input":"2023-07-16T00:27:03.209979Z","iopub.status.idle":"2023-07-16T00:27:03.216610Z","shell.execute_reply.started":"2023-07-16T00:27:03.209939Z","shell.execute_reply":"2023-07-16T00:27:03.215459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# helper function to un-normalize and display an image\ndef imshow(img):\n    img = img / 2 + 0.5  # unnormalize\n    plt.imshow(np.transpose(img, (1, 2, 0)))  # convert from Tensor image","metadata":{"id":"wMxPtqrooBmf","execution":{"iopub.status.busy":"2023-07-16T00:27:05.685174Z","iopub.execute_input":"2023-07-16T00:27:05.686141Z","iopub.status.idle":"2023-07-16T00:27:05.691168Z","shell.execute_reply.started":"2023-07-16T00:27:05.686099Z","shell.execute_reply":"2023-07-16T00:27:05.690242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain one batch of training images\ndataiter = iter(train_loader)\n# images, labels = dataiter.next()\n\nimages, labels = next(dataiter)\n\nimages = images.numpy() # convert images to numpy for display\nimages.shape # ((num_examples, num_channels, height, width), number of examples: 64, number of channels: 3)","metadata":{"id":"NlWpHRAhoBmg","outputId":"3f346c71-bef6-4c3d-bd66-aa6c07e4bff9","execution":{"iopub.status.busy":"2023-07-16T00:27:51.246788Z","iopub.execute_input":"2023-07-16T00:27:51.247213Z","iopub.status.idle":"2023-07-16T00:27:51.972897Z","shell.execute_reply.started":"2023-07-16T00:27:51.247170Z","shell.execute_reply":"2023-07-16T00:27:51.971547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the images in the batch, along with the corresponding labels\nfig = plt.figure(figsize=(25, 4))\n# display 20 images\nlabel_class = (lambda x: 'cat' if x==0 else 'dog')\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, 20//2, idx+1, xticks=[], yticks=[])\n    imshow(images[idx])\n    ax.set_title(label_class(labels[idx]))","metadata":{"id":"vnbRHLX9oBmg","outputId":"8c60a3aa-e1b6-4df4-9501-9cc9046e3216","execution":{"iopub.status.busy":"2023-07-16T00:27:55.117646Z","iopub.execute_input":"2023-07-16T00:27:55.118100Z","iopub.status.idle":"2023-07-16T00:27:57.029356Z","shell.execute_reply.started":"2023-07-16T00:27:55.118053Z","shell.execute_reply":"2023-07-16T00:27:57.028291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.vgg16(weights=True)\nmodel","metadata":{"id":"N01U-w1K9c1g","outputId":"5441ecca-80d1-448b-dab5-45953bf3ac4d","execution":{"iopub.status.busy":"2023-07-16T00:28:29.316698Z","iopub.execute_input":"2023-07-16T00:28:29.317235Z","iopub.status.idle":"2023-07-16T00:28:31.640171Z","shell.execute_reply.started":"2023-07-16T00:28:29.317197Z","shell.execute_reply":"2023-07-16T00:28:31.639251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze our feature parameters as we don't wanna retrain them to the new data\nfor param in model.parameters():\n  param.requires_grad = False","metadata":{"id":"ejsTVkAK9gGN","execution":{"iopub.status.busy":"2023-07-16T00:28:34.378290Z","iopub.execute_input":"2023-07-16T00:28:34.378989Z","iopub.status.idle":"2023-07-16T00:28:34.385029Z","shell.execute_reply.started":"2023-07-16T00:28:34.378955Z","shell.execute_reply":"2023-07-16T00:28:34.384061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Output dimension of VGG16 conv net before the FC layers: 7x7x512 = 25088\n\nclassifier = nn.Sequential(nn.Linear(25088, 500),\n                            nn.BatchNorm1d(500),\n                            nn.ReLU(),\n                            nn.Dropout(0.2),\n                            nn.Linear(500, 8),\n                            nn.BatchNorm1d(8),\n                            nn.ReLU(),\n                            nn.Dropout(0.1),\n                            nn.Linear(8, 1),\n                            nn.Sigmoid()\n                          )\n                           \n# Attach the classifier to the model\nmodel.classifier = classifier","metadata":{"id":"wYXa5Psl9rhD","execution":{"iopub.status.busy":"2023-07-15T23:29:13.445533Z","iopub.execute_input":"2023-07-15T23:29:13.445900Z","iopub.status.idle":"2023-07-15T23:29:13.558582Z","shell.execute_reply.started":"2023-07-15T23:29:13.445874Z","shell.execute_reply":"2023-07-15T23:29:13.557650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loss\ncriterion = nn.BCELoss()\n\n# Optimizer\noptimizer = optim.Adam(model.classifier.parameters(),lr =0.001)","metadata":{"id":"SUubdJ_B9sGc","execution":{"iopub.status.busy":"2023-07-15T23:29:14.412076Z","iopub.execute_input":"2023-07-15T23:29:14.412440Z","iopub.status.idle":"2023-07-15T23:29:14.418135Z","shell.execute_reply.started":"2023-07-15T23:29:14.412390Z","shell.execute_reply":"2023-07-15T23:29:14.417043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Moving model to GPU\nmodel = model.to(device)","metadata":{"id":"ZM-aTc_19sQc","execution":{"iopub.status.busy":"2023-07-15T23:29:15.243963Z","iopub.execute_input":"2023-07-15T23:29:15.244436Z","iopub.status.idle":"2023-07-15T23:29:15.267828Z","shell.execute_reply.started":"2023-07-15T23:29:15.244374Z","shell.execute_reply":"2023-07-15T23:29:15.266925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of epochs to train the model\nn_epochs = 8\n\n# initialize tracker for minimum validation loss\nvalid_loss_min = np.Inf # set initial \"min\" to infinity\ntraining_loss = np.array([])\nvalidation_loss = np.array([])\n\ntarget = torch.tensor([[1.0]])\n\nfor epoch in range(1, n_epochs+1):\n\n    # keep track of training and validation loss\n    train_loss = 0.0\n    valid_loss = 0.0\n\n    ###################\n    # train the model #\n    ###################\n\n    # model by default is set to train\n    model.train()\n\n    for images, labels in tqdm(train_loader):\n\n      # Moving input tensors to GPU if available\n      images, labels = images.to(device), labels.to(device)\n\n      # Forward prop:\n\n      # forward pass: compute predicted outputs by passing inputs to the model\n      output = model(images.float())\n\n      # calculate the batch loss\n      loss = criterion(output.float().reshape(-1), labels.float())\n\n      # Backward prop:\n\n      # clear the gradients of all optimized variables\n      optimizer.zero_grad()\n\n      # backward pass: compute gradient of the loss with respect to model parameters\n      loss.backward()\n\n      # perform a single optimization step (parameter update)\n      optimizer.step()\n\n      # Metrics:\n\n      # update training loss\n      train_loss += loss.item()\n\n    ######################\n    # validate the model #\n    ######################\n\n    model.eval() # prep model for evaluation\n\n    for data, target in tqdm(valid_loader):\n        \n        # Moving input tensors to GPU if available\n        data, target = data.to(device), target.to(device)\n        \n        # forward pass: compute predicted outputs by passing inputs to the model\n        output = model(data.float())\n        \n        # calculate the loss\n        loss = criterion(output.float().reshape(-1), target.float())\n        \n        # update running validation loss\n        valid_loss += loss.item()\n\n    # print training/validation statistics\n    # calculate average loss over an epoch\n    train_loss = train_loss/len(train_loader)\n    valid_loss = valid_loss/len(valid_loader)\n    \n    np.append(training_loss, train_loss)\n    np.append(validation_loss, valid_loss)\n\n    print('Epoch: {} \\tTraining Loss: {:.6f} \\tValidation Loss: {:.6f}'.format(\n        epoch,\n        train_loss,\n        valid_loss\n    ))\n\n    # save model if validation loss has decreased\n    if valid_loss <= valid_loss_min:\n        print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n            valid_loss_min,\n            valid_loss))\n        torch.save(model.state_dict(), 'model_sigmoid.pt')\n        valid_loss_min = valid_loss","metadata":{"id":"W6bml5lX9sZn","outputId":"0bcd644d-7a7f-494e-8b9c-af6777d61742","execution":{"iopub.status.busy":"2023-07-15T23:29:16.122549Z","iopub.execute_input":"2023-07-15T23:29:16.122905Z","iopub.status.idle":"2023-07-15T23:51:14.334270Z","shell.execute_reply.started":"2023-07-15T23:29:16.122877Z","shell.execute_reply":"2023-07-15T23:51:14.333094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.plot(training_loss, n_epochs, label='train_loss')\n# plt.plot(validation_loss,n_epochs,label='val_loss')\n# plt.legend()\n# plt.show\nprint(validation_loss)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T23:54:01.477857Z","iopub.execute_input":"2023-07-15T23:54:01.479029Z","iopub.status.idle":"2023-07-15T23:54:01.485340Z","shell.execute_reply.started":"2023-07-15T23:54:01.478983Z","shell.execute_reply":"2023-07-15T23:54:01.484289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load('model_sigmoid.pt'))","metadata":{"id":"HD4JfLMXsaq4","outputId":"34c7edc3-a7b3-4ff8-f12c-dc64a3319936","execution":{"iopub.status.busy":"2023-07-15T23:54:16.557132Z","iopub.execute_input":"2023-07-15T23:54:16.558744Z","iopub.status.idle":"2023-07-15T23:54:16.664228Z","shell.execute_reply.started":"2023-07-15T23:54:16.558692Z","shell.execute_reply":"2023-07-15T23:54:16.662691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.array([])\nprint(predictions)","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-15T23:54:21.862258Z","iopub.execute_input":"2023-07-15T23:54:21.862958Z","iopub.status.idle":"2023-07-15T23:54:21.868194Z","shell.execute_reply.started":"2023-07-15T23:54:21.862924Z","shell.execute_reply":"2023-07-15T23:54:21.867294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nwith torch.no_grad():\n    for images, labels in tqdm(test_loader):\n        images = images.to(device)\n\n        outputs = model(images)\n#         print(outputs.flatten().size())\n#         _, predicted = torch.max(outputs.data, dim=1)\n        predictions = np.concatenate((predictions, outputs.cpu().numpy().flatten()))","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-15T23:54:32.389929Z","iopub.execute_input":"2023-07-15T23:54:32.391019Z","iopub.status.idle":"2023-07-15T23:55:54.971491Z","shell.execute_reply.started":"2023-07-15T23:54:32.390971Z","shell.execute_reply":"2023-07-15T23:55:54.970431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions[:10])\npredictions.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-15T23:56:46.971424Z","iopub.execute_input":"2023-07-15T23:56:46.972335Z","iopub.status.idle":"2023-07-15T23:56:46.980568Z","shell.execute_reply.started":"2023-07-15T23:56:46.972300Z","shell.execute_reply":"2023-07-15T23:56:46.979534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {'id':np.arange(1,len(test_data)+1),'label':predictions}\ndf = pd.DataFrame(data)\ndf.shape","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-15T23:56:54.719121Z","iopub.execute_input":"2023-07-15T23:56:54.720254Z","iopub.status.idle":"2023-07-15T23:56:54.732320Z","shell.execute_reply.started":"2023-07-15T23:56:54.720212Z","shell.execute_reply":"2023-07-15T23:56:54.731313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('cats-dogs-submission5.csv',index=False)","metadata":{"id":"OZD7UXlCvZsa","execution":{"iopub.status.busy":"2023-07-15T23:57:00.235319Z","iopub.execute_input":"2023-07-15T23:57:00.235989Z","iopub.status.idle":"2023-07-15T23:57:00.309017Z","shell.execute_reply.started":"2023-07-15T23:57:00.235956Z","shell.execute_reply":"2023-07-15T23:57:00.308088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# obtain one batch of training images\ntest_iter = iter(test_loader)\n# images, labels = dataiter.next()\n\nimages, labels = next(test_iter)\nimages, labels = next(test_iter)\n\nimages = images.permute(0, 2, 3, 1)\n\nimages = images.numpy() # convert images to numpy for display\nimages.shape # (number of examples: 64, number of channels: 3)\n\n# plot the images in the batch, along with predicted and true labels\nfig = plt.figure(figsize=(25, 4))\nfor idx in np.arange(20):\n    ax = fig.add_subplot(2, 20//2, idx+1, xticks=[], yticks=[])\n    ax.imshow(images[idx].squeeze())\n    ax.set_title(\"{}\".format(str(df['label'].apply(lambda x: 'dog' if x>0.5 else 'cat').to_numpy()[idx])))","metadata":{"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-07-16T00:31:19.446505Z","iopub.execute_input":"2023-07-16T00:31:19.446873Z","iopub.status.idle":"2023-07-16T00:31:22.204131Z","shell.execute_reply.started":"2023-07-16T00:31:19.446846Z","shell.execute_reply":"2023-07-16T00:31:22.203059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}