{"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":"markdown","source":"# Importing packages :\n* torch packages for the model training, also for the CNN\n* operation system module for the manipulation of the folders ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-21T05:33:01.639623Z","iopub.execute_input":"2023-07-21T05:33:01.640170Z","iopub.status.idle":"2023-07-21T05:33:06.136733Z","shell.execute_reply.started":"2023-07-21T05:33:01.640142Z","shell.execute_reply":"2023-07-21T05:33:06.134662Z"}}},{"cell_type":"code","source":"import torch\nimport torchvision.transforms as transforms\nimport torchvision.datasets as datasets\nimport torchvision.models as models\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport glob,os\nimport matplotlib.image as mpimg\nimport zipfile\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:25.175674Z","iopub.execute_input":"2023-07-21T17:52:25.176025Z","iopub.status.idle":"2023-07-21T17:52:28.948629Z","shell.execute_reply.started":"2023-07-21T17:52:25.175996Z","shell.execute_reply":"2023-07-21T17:52:28.947589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check if GPU is available","metadata":{}},{"cell_type":"code","source":"if torch.cuda.is_available():\n    device = torch.device('cuda')\nelse:\n    device = torch.device('cpu')\nprint(\"Device:\",device)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:28.950734Z","iopub.execute_input":"2023-07-21T17:52:28.951403Z","iopub.status.idle":"2023-07-21T17:52:28.983041Z","shell.execute_reply.started":"2023-07-21T17:52:28.951365Z","shell.execute_reply":"2023-07-21T17:52:28.981983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting the file, and setup the data and unzip the files","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:28.985197Z","iopub.execute_input":"2023-07-21T17:52:28.985976Z","iopub.status.idle":"2023-07-21T17:52:29.003674Z","shell.execute_reply.started":"2023-07-21T17:52:28.985920Z","shell.execute_reply":"2023-07-21T17:52:29.002404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nos.listdir(\".\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:29.007316Z","iopub.execute_input":"2023-07-21T17:52:29.007717Z","iopub.status.idle":"2023-07-21T17:52:44.285481Z","shell.execute_reply.started":"2023-07-21T17:52:29.007680Z","shell.execute_reply":"2023-07-21T17:52:44.284108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('cats')\nos.mkdir('dogs')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:44.287254Z","iopub.execute_input":"2023-07-21T17:52:44.287957Z","iopub.status.idle":"2023-07-21T17:52:44.293405Z","shell.execute_reply.started":"2023-07-21T17:52:44.287914Z","shell.execute_reply":"2023-07-21T17:52:44.292169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\npath = \"train\"\n\ndirs = [each for each in os.listdir(path)]\n\nfor _ in tqdm(dirs):\n    folder = _.split(\".\")[0]\n    if folder=='cat':\n        shutil.move(path+\"/\"+_, \"cats\")\n    else:\n        shutil.move(path+\"/\"+_, \"dogs\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:44.295091Z","iopub.execute_input":"2023-07-21T17:52:44.295850Z","iopub.status.idle":"2023-07-21T17:52:45.786550Z","shell.execute_reply.started":"2023-07-21T17:52:44.295813Z","shell.execute_reply":"2023-07-21T17:52:45.785403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('train')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:45.788335Z","iopub.execute_input":"2023-07-21T17:52:45.789006Z","iopub.status.idle":"2023-07-21T17:52:45.795967Z","shell.execute_reply.started":"2023-07-21T17:52:45.788955Z","shell.execute_reply":"2023-07-21T17:52:45.794840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('train')\nshutil.move('cats', 'train')\nshutil.move('dogs','train')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:45.797623Z","iopub.execute_input":"2023-07-21T17:52:45.798816Z","iopub.status.idle":"2023-07-21T17:52:45.817112Z","shell.execute_reply.started":"2023-07-21T17:52:45.798785Z","shell.execute_reply":"2023-07-21T17:52:45.815955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = [\n    \"./train/cats/cat.0.jpg\",\n    \"./train/cats/cat.1.jpg\",\n    \"./train/cats/cat.2.jpg\",\n    \"./train/cats/cat.3.jpg\",\n    \"./train/cats/cat.4.jpg\",\n]\n\nimages = []\n\nfor img_path in image_files:\n    if os.path.exists(img_path):\n        images.append(mpimg.imread(img_path))\n    else:\n        print(\"File not found:\", img_path)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:45.818282Z","iopub.execute_input":"2023-07-21T17:52:45.819204Z","iopub.status.idle":"2023-07-21T17:52:45.856838Z","shell.execute_reply.started":"2023-07-21T17:52:45.819151Z","shell.execute_reply":"2023-07-21T17:52:45.855861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = [\n    \"./train/dogs/dog.0.jpg\",\n    \"./train/dogs/dog.1.jpg\",\n    \"./train/dogs/dog.2.jpg\",\n    \"./train/dogs/dog.3.jpg\",\n    \"./train/dogs/dog.4.jpg\",\n]\n\nfor img_path in image_files:\n    if os.path.exists(img_path):\n        images.append(mpimg.imread(img_path))\n    else:\n        print(\"File not found:\", img_path)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:45.862278Z","iopub.execute_input":"2023-07-21T17:52:45.862561Z","iopub.status.idle":"2023-07-21T17:52:45.880598Z","shell.execute_reply.started":"2023-07-21T17:52:45.862536Z","shell.execute_reply":"2023-07-21T17:52:45.879462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\ncolumns = 5\n\nfor i, image in enumerate(images[:10]):\n    plt.subplot(len(images[:10]) // columns + 1, columns, i + 1)\n    plt.imshow(image)\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:45.882245Z","iopub.execute_input":"2023-07-21T17:52:45.882646Z","iopub.status.idle":"2023-07-21T17:52:48.559542Z","shell.execute_reply.started":"2023-07-21T17:52:45.882611Z","shell.execute_reply":"2023-07-21T17:52:48.558248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creation of the tensor data for training model ","metadata":{}},{"cell_type":"code","source":"transformations = transforms.Compose([\n    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])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:48.560990Z","iopub.execute_input":"2023-07-21T17:52:48.561467Z","iopub.status.idle":"2023-07-21T17:52:48.575276Z","shell.execute_reply.started":"2023-07-21T17:52:48.561419Z","shell.execute_reply":"2023-07-21T17:52:48.574339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = datasets.ImageFolder(os.path.join(\"train\"), transform = transformations)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:48.576680Z","iopub.execute_input":"2023-07-21T17:52:48.577409Z","iopub.status.idle":"2023-07-21T17:52:48.735473Z","shell.execute_reply.started":"2023-07-21T17:52:48.577373Z","shell.execute_reply":"2023-07-21T17:52:48.734361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Load in each dataset and apply transformations using\n* the torchvision.datasets as datasets library","metadata":{}},{"cell_type":"code","source":"train_set = datasets.ImageFolder(os.path.join(\"train\"), transform = transformations)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:48.736914Z","iopub.execute_input":"2023-07-21T17:52:48.737987Z","iopub.status.idle":"2023-07-21T17:52:48.867954Z","shell.execute_reply.started":"2023-07-21T17:52:48.737947Z","shell.execute_reply":"2023-07-21T17:52:48.866871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Put into a Dataloader using torch library","metadata":{}},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(train_set, batch_size=32, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:48.869447Z","iopub.execute_input":"2023-07-21T17:52:48.869897Z","iopub.status.idle":"2023-07-21T17:52:48.875671Z","shell.execute_reply.started":"2023-07-21T17:52:48.869861Z","shell.execute_reply":"2023-07-21T17:52:48.874651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Convolutional neural network","metadata":{}},{"cell_type":"markdown","source":"<center><img src=\"https://learnopencv.com/wp-content/uploads/2023/01/tensorflow-keras-cnn-vgg-architecture-1024x611.png\" alt=\"CNN Diagram\" width=\"900\" height=\"300\">","metadata":{}},{"cell_type":"code","source":"class ConvNet(nn.Module):\n    def __init__(self, num_classes=2):\n        super(ConvNet, self).__init__()\n        # First unit of convolution\n        self.conv_unit_1 = nn.Sequential(\n            nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)) #112\n        # Second unit of convolution\n        self.conv_unit_2 = nn.Sequential(\n            nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)) #56\n        # Third unit of convolution\n        self.conv_unit_3 = nn.Sequential(\n            nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)) #28\n        # Fourth unit of convolution\n        self.conv_unit_4 = nn.Sequential(\n            nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(kernel_size=2, stride=2)) #14\n        # Fully connected layers\n        self.fc1 = nn.Linear(14*14*128, 128)\n        self.fc2 = nn.Linear(128, 1)\n        self.final = nn.Sigmoid()\n    def forward(self, x):\n        out = self.conv_unit_1(x)\n        out = self.conv_unit_2(out)\n        out = self.conv_unit_3(out)\n        out = self.conv_unit_4(out)\n        # Reshape the output\n        out = out.view(out.size(0),-1)\n        out = self.fc1(out)\n        out = self.fc2(out)\n        out = self.final(out)\n        return(out)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:48.877560Z","iopub.execute_input":"2023-07-21T17:52:48.878293Z","iopub.status.idle":"2023-07-21T17:52:48.895584Z","shell.execute_reply.started":"2023-07-21T17:52:48.878258Z","shell.execute_reply":"2023-07-21T17:52:48.894692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining the Loss Function and Optimizer, Creating the Model Instance, and Training for a Defined Number of Epochs","metadata":{}},{"cell_type":"code","source":"num_epochs = 10\nloss_function = nn.BCELoss() #Binary Crosss Entropy Loss\nmodel = ConvNet()\nmodel.cuda()\nadam_optimizer = torch.optim.Adam(model.parameters(), lr= 0.001)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:48.897592Z","iopub.execute_input":"2023-07-21T17:52:48.898471Z","iopub.status.idle":"2023-07-21T17:52:52.693125Z","shell.execute_reply.started":"2023-07-21T17:52:48.898439Z","shell.execute_reply":"2023-07-21T17:52:52.692044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"total_step = len(train_loader)\nprint(\"Total Batches:\",total_step)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:52.694937Z","iopub.execute_input":"2023-07-21T17:52:52.695354Z","iopub.status.idle":"2023-07-21T17:52:52.701740Z","shell.execute_reply.started":"2023-07-21T17:52:52.695318Z","shell.execute_reply":"2023-07-21T17:52:52.700559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(num_epochs):\n    model.train()\n    train_loss = 0\n    for i, (images, labels) in enumerate(train_loader):\n        images = images.to(device)\n        labels = labels.to(device)\n        \n        # Forward pass\n        outputs = model(images)\n        loss = loss_function(outputs.float(), labels.float().view(-1,1))\n        \n        # Backward and optimize\n        adam_optimizer.zero_grad()\n        loss.backward()\n        adam_optimizer.step()\n        train_loss += loss.item()* labels.size(0)\n        \n    # After each epoch print Train loss and validation loss + accuracy\n    print ('Epoch [{}/{}], Loss: {:.4f}' .format(epoch+1, num_epochs, loss.item()))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:52:52.703976Z","iopub.execute_input":"2023-07-21T17:52:52.705139Z","iopub.status.idle":"2023-07-21T18:13:56.505119Z","shell.execute_reply.started":"2023-07-21T17:52:52.705103Z","shell.execute_reply":"2023-07-21T18:13:56.503978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Download the model (pretrained)\n","metadata":{}},{"cell_type":"code","source":"from torchvision import models\nnew_model = models.vgg16(pretrained=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:13:56.507030Z","iopub.execute_input":"2023-07-21T18:13:56.507820Z","iopub.status.idle":"2023-07-21T18:14:00.445179Z","shell.execute_reply.started":"2023-07-21T18:13:56.507763Z","shell.execute_reply":"2023-07-21T18:14:00.444075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Freeze model weights\nfor param in new_model.parameters():\n    param.requires_grad = False\nprint(new_model.classifier)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:14:00.446645Z","iopub.execute_input":"2023-07-21T18:14:00.447047Z","iopub.status.idle":"2023-07-21T18:14:00.454544Z","shell.execute_reply.started":"2023-07-21T18:14:00.447008Z","shell.execute_reply":"2023-07-21T18:14:00.453373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define our custom model last layer\n","metadata":{}},{"cell_type":"code","source":"new_model.classifier[6] = nn.Sequential(\n    nn.Linear(new_model.classifier[6].\n    in_features, 256),\n    nn.ReLU(),\n    nn.Dropout(0.4),\n    nn.Linear(256, 1),\n    nn.Sigmoid())","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:14:00.456124Z","iopub.execute_input":"2023-07-21T18:14:00.456952Z","iopub.status.idle":"2023-07-21T18:14:00.492130Z","shell.execute_reply.started":"2023-07-21T18:14:00.456915Z","shell.execute_reply":"2023-07-21T18:14:00.490938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Find total parameters and trainable parameters","metadata":{}},{"cell_type":"code","source":"total_params = sum(p.numel() for p in new_model.parameters())\nprint(f'{total_params:,} total parameters.')\ntotal_trainable_params = sum(\n    p.numel() for p in new_model.parameters() \n    if p.requires_grad)\nprint(f'{total_trainable_params:,} training parameters.')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:14:00.493295Z","iopub.execute_input":"2023-07-21T18:14:00.493872Z","iopub.status.idle":"2023-07-21T18:14:00.502561Z","shell.execute_reply.started":"2023-07-21T18:14:00.493834Z","shell.execute_reply":"2023-07-21T18:14:00.501533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define epochs, optimizer and loss function\n","metadata":{}},{"cell_type":"code","source":"num_epochs = 10\nloss_function = nn.BCELoss() #Binary Crosss Entropy Loss\nnew_model.cuda()\nadam_optimizer = torch.optim.Adam(new_model.parameters(), \nlr= 0.001)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:14:00.503859Z","iopub.execute_input":"2023-07-21T18:14:00.504880Z","iopub.status.idle":"2023-07-21T18:14:00.676521Z","shell.execute_reply.started":"2023-07-21T18:14:00.504841Z","shell.execute_reply":"2023-07-21T18:14:00.675461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"total_step = len(train_loader)\nprint(\"Total Batches:\",total_step)\n\nfor epoch in range(num_epochs):\n    new_model.train()\n    train_loss = 0\n    for i, (images, labels) in enumerate(train_loader):\n        images = images.to(device)\n        labels = labels.to(device)\n        # Forward pass\n        outputs = new_model(images)\n        loss = loss_function(outputs.float(), labels.float().view(-1,1))\n        # Backward and optimize\n        adam_optimizer.zero_grad()\n        loss.backward()\n        adam_optimizer.step()\n        train_loss += loss.item()* labels.size(0)\n    # After each epoch print Train loss and validation loss + accuracy\n    print ('Epoch [{}/{}], Loss: {:.4f}' .format(epoch+1, num_epochs, loss.item()))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:14:00.677927Z","iopub.execute_input":"2023-07-21T18:14:00.678961Z","iopub.status.idle":"2023-07-21T18:42:32.543993Z","shell.execute_reply.started":"2023-07-21T18:14:00.678919Z","shell.execute_reply":"2023-07-21T18:42:32.542960Z"},"trusted":true},"execution_count":null,"outputs":[]}]}