{"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":"# 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":"2023-01-11T08:36:30.838055Z","iopub.execute_input":"2023-01-11T08:36:30.838481Z","iopub.status.idle":"2023-01-11T08:36:30.855322Z","shell.execute_reply.started":"2023-01-11T08:36:30.838435Z","shell.execute_reply":"2023-01-11T08:36:30.854325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/\")\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"/kaggle/working/\")    ","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:36:30.884568Z","iopub.execute_input":"2023-01-11T08:36:30.884983Z","iopub.status.idle":"2023-01-11T08:36:45.333324Z","shell.execute_reply.started":"2023-01-11T08:36:30.884948Z","shell.execute_reply":"2023-01-11T08:36:45.331925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision.transforms as transforms\nfrom torchvision.transforms import ToTensor\nfrom torch.utils.data import DataLoader,Dataset\nimport matplotlib.pyplot as plt\nimport glob\n!pip install comet_ml\nfrom comet_ml import Experiment\nexperiment = Experiment(\n    api_key=\"n5zx1HiA5GFa1nHit7f6brQx6\",  \n    project_name=\"cats-dogs-pytorch619\",\n    workspace=\"a-hamdi\",\n)\nclass CustomImageDataset(Dataset):\n    def __init__(self, image_path, transform=None):\n\n        self.image_path = image_path\n        self.images = glob.glob(f\"{self.image_path}/*\")\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.images)\n\n    def __getitem__(self, index):\n        image_path = self.images[index]\n        image = plt.imread(image_path)\n        if 'dog' in image_path:\n            label=1\n        else:\n            label=0\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:36:45.337435Z","iopub.execute_input":"2023-01-11T08:36:45.338517Z","iopub.status.idle":"2023-01-11T08:36:58.713497Z","shell.execute_reply.started":"2023-01-11T08:36:45.338457Z","shell.execute_reply":"2023-01-11T08:36:58.712004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_transform = transforms.Compose([\n                  transforms.ToPILImage(),\n                  transforms.CenterCrop(150),\n#                     transforms.GaussianBlur(3)\n    transforms.RandomHorizontalFlip(p=0.40),\n                  transforms.RandomRotation(degrees=60),\n                  transforms.Resize([150]*2),\n                  transforms.ToTensor(),\n                    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n])\nval_transform = transforms.Compose([\n                  transforms.ToPILImage(),\n                  transforms.Resize([150]*2),\n                  transforms.ToTensor()])","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:36:58.715886Z","iopub.execute_input":"2023-01-11T08:36:58.717153Z","iopub.status.idle":"2023-01-11T08:36:58.726807Z","shell.execute_reply.started":"2023-01-11T08:36:58.7171Z","shell.execute_reply":"2023-01-11T08:36:58.725508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = CustomImageDataset(\"/kaggle/working/train\",transform=train_transform)\ntest_set = CustomImageDataset(\"/kaggle/working/test1\",transform=val_transform)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:36:58.730255Z","iopub.execute_input":"2023-01-11T08:36:58.731719Z","iopub.status.idle":"2023-01-11T08:36:58.874331Z","shell.execute_reply.started":"2023-01-11T08:36:58.731663Z","shell.execute_reply":"2023-01-11T08:36:58.873021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nloaded_train = DataLoader(\n    train_set, \n    batch_size=batch_size,\n    shuffle=True,\n    pin_memory=True\n)\n\nloaded_val = DataLoader(\n    test_set, \n    batch_size=batch_size,\n    shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:36:58.875885Z","iopub.execute_input":"2023-01-11T08:36:58.876326Z","iopub.status.idle":"2023-01-11T08:36:58.885847Z","shell.execute_reply.started":"2023-01-11T08:36:58.876281Z","shell.execute_reply":"2023-01-11T08:36:58.884594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torchsummary \nfrom torchvision import models\nfrom torchsummary import summary\nmy_model=models.resnet50(pretrained=True)\nsummary(my_model,(3,150,150))\n#I have to change the last layer to output 2","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:36:58.887437Z","iopub.execute_input":"2023-01-11T08:36:58.888504Z","iopub.status.idle":"2023-01-11T08:37:11.714237Z","shell.execute_reply.started":"2023-01-11T08:36:58.888457Z","shell.execute_reply":"2023-01-11T08:37:11.703186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validation(model, val_loader,loss_function , logger, iteration, epoch, accuracy):\n    model.eval()\n    with torch.no_grad():\n        val_running_loss = 0\n        total_acc = 0\n        for iteration,(img, target) in enumerate(val_loader):\n            \n            img = img\n            target = target\n\n            out = model(img)\n            \n            \n            \n            loss = loss_function(out,target)\n            val_running_loss += loss.item()\n\n            acc = accuracy(out,target)\n            total_acc+=acc\n        \n        val_loss = val_running_loss/iteration\n        val_acc = total_acc/iteration\n        logger.log_metric(\"val_loss\", val_loss, step=epoch)\n        \n        \n        # ---\n        # ACCURACY\n        # ---\n        logger.log_metric(\"Val_accuracy\", val_acc, step=epoch)\n        return val_acc, val_loss\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:37:11.719078Z","iopub.execute_input":"2023-01-11T08:37:11.719466Z","iopub.status.idle":"2023-01-11T08:37:11.732228Z","shell.execute_reply.started":"2023-01-11T08:37:11.719415Z","shell.execute_reply":"2023-01-11T08:37:11.730721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchmetrics import Accuracy\nimport torch.optim as optim\nimport torch.nn as nn\nmy_model.fc=nn.Linear(2048,2)\noptimizer = optim.SGD(my_model.parameters(), lr=0.003, momentum=0.9)\nloss_function = nn.CrossEntropyLoss()\nepochs = 30\nfrom torch.optim.lr_scheduler import ExponentialLR\n\nlr_scheduler = ExponentialLR(optimizer, gamma=0.9)\naccuracy = Accuracy(task='binary')\nmy_model.train()\nfor epoch in range(1,epochs+1):\n    train_running_loss = 0.0\n    counter = 0\n    lr_scheduler.step()\n    lr = optimizer.param_groups[0]['lr']\n    \n    for iteration,(img, target) in enumerate(loaded_train):\n        \n        counter+=1\n        optimizer.zero_grad()\n        \n        target = target\n        img = img\n        \n        out = my_model(img)\n        \n        loss = loss_function(out,target)\n        train_running_loss += loss.item()\n        \n        loss.backward()\n\n        optimizer.step()\n        train_loss = train_running_loss/counter   \n        _, index = torch.max(out, 1)\n        acc = accuracy(index,target)\n        experiment.log_metric(\"train_loss\", train_loss, step=iteration)\n        experiment.log_metric(\"train_accuracy\", acc, step=iteration)\n        print(f\"TRAIN:Epoch:{epoch}, Iteration:{iteration} TrainLoss:{train_loss} Accuracy:{acc} LR:{lr}\")\n        \n    val_acc, val_loss = validation(my_model,loaded_val,loss_function,experiment,iteration,epoch,accuracy)\n    print(f\"VAL:Epoch:{epoch}, Iteration:{iteration} ValLoss:{val_loss} Accuracy:{val_acc}\")\n    print(\"---\"*10)\n       ","metadata":{"execution":{"iopub.status.busy":"2023-01-11T08:41:07.340811Z","iopub.execute_input":"2023-01-11T08:41:07.341326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}