{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30498,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\n\nimport zipfile\nimport os\nimport shutil\n\nimport random\n\n\nimport torch\nfrom torchvision import datasets, transforms\nfrom torchvision.datasets import ImageFolder\nimport torchvision.transforms as T\nfrom torchvision.models import resnet18 ,ResNet18_Weights\nfrom torch import nn \nfrom torch.optim import SGD ,lr_scheduler\nimport torch.cuda\n\nimport time\nfrom torch.autograd import Variable","metadata":{"execution":{"iopub.status.busy":"2023-11-22T13:58:56.588969Z","iopub.execute_input":"2023-11-22T13:58:56.589349Z","iopub.status.idle":"2023-11-22T13:58:59.870121Z","shell.execute_reply.started":"2023-11-22T13:58:56.589319Z","shell.execute_reply":"2023-11-22T13:58:59.869119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = 'cuda' if torch.cuda.is_available() else 'cpu'\ntorch.device(device)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T13:59:08.021203Z","iopub.execute_input":"2023-11-22T13:59:08.022315Z","iopub.status.idle":"2023-11-22T13:59:08.055386Z","shell.execute_reply.started":"2023-11-22T13:59:08.022269Z","shell.execute_reply":"2023-11-22T13:59:08.05438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation ","metadata":{}},{"cell_type":"code","source":"# setting file paths\ntest_zip_path = \"/kaggle/input/dogs-vs-cats/test1.zip\"\ntest_images_path = \"/kaggle/working/training/\"\n\n# creating training directory\nos.makedirs(test_images_path, exist_ok=True)\n\n# unzipping files to train folder\n\nwith zipfile.ZipFile(test_zip_path) as zip_f:\n    zip_f.extractall(test_images_path)\n    \n#creating folder for Training \nos.makedirs(\"/kaggle/working/training/test1/\", exist_ok=True)\n#creating folder for Validation \n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T13:59:10.659228Z","iopub.execute_input":"2023-11-22T13:59:10.659588Z","iopub.status.idle":"2023-11-22T13:59:16.662281Z","shell.execute_reply.started":"2023-11-22T13:59:10.659561Z","shell.execute_reply":"2023-11-22T13:59:16.661004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_zip_path = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntrain_images_path = \"/kaggle/working/training/\"\n\n# creating training directory\nos.makedirs(train_images_path, exist_ok=True)\n\n# unzipping files to train folder\n\nwith zipfile.ZipFile(train_zip_path) as zip_f:\n    zip_f.extractall(train_images_path)\n    \n#creating folder for Training \nos.makedirs(\"/kaggle/working/training/Train/\", exist_ok=True)\n#creating folder for Validation \nos.makedirs(\"/kaggle/working/training/Valid/\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T13:59:23.326294Z","iopub.execute_input":"2023-11-22T13:59:23.326654Z","iopub.status.idle":"2023-11-22T13:59:33.369075Z","shell.execute_reply.started":"2023-11-22T13:59:23.326627Z","shell.execute_reply":"2023-11-22T13:59:33.368189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"1895.jpg","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = os.listdir(\"training/train\")\nL = len(images)\n##for i, file in enumerate(images):\n  ##  print(i)\n  ##  print(file)\nprint(L)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T14:01:24.360467Z","iopub.execute_input":"2023-11-22T14:01:24.361314Z","iopub.status.idle":"2023-11-22T14:01:24.386463Z","shell.execute_reply.started":"2023-11-22T14:01:24.361274Z","shell.execute_reply":"2023-11-22T14:01:24.38511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = os.listdir(\"training/train\")\nL = len(images)\n# creating directories for each class\nos.makedirs(\"training/Train/cat\", exist_ok=True)\nos.makedirs(\"training/Train/dog\", exist_ok=True)\n\n\n\nfor i, file in enumerate(images):\n    # getting category\n    dir_name = file.split(\".\")[0].lower()\n    \n    # copying to correct folder\n    os.rename(f\"/kaggle/working/training/train/{file}\",\n              f\"/kaggle/working/training/Train/{dir_name}/{file}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-22T14:02:56.702722Z","iopub.execute_input":"2023-11-22T14:02:56.703369Z","iopub.status.idle":"2023-11-22T14:02:57.33106Z","shell.execute_reply.started":"2023-11-22T14:02:56.703338Z","shell.execute_reply":"2023-11-22T14:02:57.330075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training/train","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L","metadata":{"execution":{"iopub.status.busy":"2023-11-21T02:53:22.536135Z","iopub.execute_input":"2023-11-21T02:53:22.536458Z","iopub.status.idle":"2023-11-21T02:53:22.542755Z","shell.execute_reply.started":"2023-11-21T02:53:22.536434Z","shell.execute_reply":"2023-11-21T02:53:22.541823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L","metadata":{"execution":{"iopub.status.busy":"2023-11-21T02:54:39.248509Z","iopub.execute_input":"2023-11-21T02:54:39.2493Z","iopub.status.idle":"2023-11-21T02:54:39.255111Z","shell.execute_reply.started":"2023-11-21T02:54:39.249267Z","shell.execute_reply":"2023-11-21T02:54:39.254123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#deleting the old train folder\nshutil.rmtree(\"/kaggle/working/training/train\")","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:33:40.215229Z","iopub.execute_input":"2023-11-22T02:33:40.216013Z","iopub.status.idle":"2023-11-22T02:33:40.220973Z","shell.execute_reply.started":"2023-11-22T02:33:40.215976Z","shell.execute_reply":"2023-11-22T02:33:40.21992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#creating dog & cat folders in Valid folder\nos.makedirs(\"training/Valid/cat\", exist_ok=True)\nos.makedirs(\"training/Valid/dog\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:33:49.631053Z","iopub.execute_input":"2023-11-22T02:33:49.631702Z","iopub.status.idle":"2023-11-22T02:33:49.636205Z","shell.execute_reply.started":"2023-11-22T02:33:49.631662Z","shell.execute_reply":"2023-11-22T02:33:49.635241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source_dog = 'training/Train/dog'\nsource_cat = 'training/Train/cat'\ndest_dog = 'training/Valid/dog'\ndest_cat = 'training/Valid/cat'\nfiles_dog = os.listdir(source_dog)\nfiles_cat = os.listdir(source_cat)\nno_of_files_dog = int(len(files_dog) // 5)\nno_of_files_cat = int(len(files_dog) // 5)\n\nfor file_name in random.sample(files_dog, no_of_files_dog):\n    shutil.move(os.path.join(source_dog, file_name), dest_dog)\n    \nfor file_name in random.sample(files_cat, no_of_files_cat):\n    shutil.move(os.path.join(source_cat, file_name), dest_cat)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:33:53.349974Z","iopub.execute_input":"2023-11-22T02:33:53.350316Z","iopub.status.idle":"2023-11-22T02:33:53.611039Z","shell.execute_reply.started":"2023-11-22T02:33:53.350291Z","shell.execute_reply":"2023-11-22T02:33:53.610187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_files_dog","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:33:57.766812Z","iopub.execute_input":"2023-11-22T02:33:57.767171Z","iopub.status.idle":"2023-11-22T02:33:57.773417Z","shell.execute_reply.started":"2023-11-22T02:33:57.767144Z","shell.execute_reply":"2023-11-22T02:33:57.772426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\n\nimport torch\nfrom PIL import Image\n# 读取图像\nimg = Image.open(\"training/test1/1895.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:51:55.732478Z","iopub.execute_input":"2023-11-22T02:51:55.732852Z","iopub.status.idle":"2023-11-22T02:51:55.740826Z","shell.execute_reply.started":"2023-11-22T02:51:55.732825Z","shell.execute_reply":"2023-11-22T02:51:55.739916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=simple_transform(img)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:53:03.237163Z","iopub.execute_input":"2023-11-22T02:53:03.237881Z","iopub.status.idle":"2023-11-22T02:53:03.245319Z","shell.execute_reply.started":"2023-11-22T02:53:03.237845Z","shell.execute_reply":"2023-11-22T02:53:03.244417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image.shape)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:53:22.33235Z","iopub.execute_input":"2023-11-22T02:53:22.332705Z","iopub.status.idle":"2023-11-22T02:53:22.337573Z","shell.execute_reply.started":"2023-11-22T02:53:22.332668Z","shell.execute_reply":"2023-11-22T02:53:22.336501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model(image1)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:03:34.645947Z","iopub.execute_input":"2023-11-22T03:03:34.646661Z","iopub.status.idle":"2023-11-22T03:03:34.815408Z","shell.execute_reply.started":"2023-11-22T03:03:34.646623Z","shell.execute_reply":"2023-11-22T03:03:34.814409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.to('cpu')","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:03:29.475868Z","iopub.execute_input":"2023-11-22T03:03:29.476572Z","iopub.status.idle":"2023-11-22T03:03:29.528316Z","shell.execute_reply.started":"2023-11-22T03:03:29.476539Z","shell.execute_reply":"2023-11-22T03:03:29.527414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:03:39.539011Z","iopub.execute_input":"2023-11-22T03:03:39.539774Z","iopub.status.idle":"2023-11-22T03:03:39.614306Z","shell.execute_reply.started":"2023-11-22T03:03:39.539744Z","shell.execute_reply":"2023-11-22T03:03:39.613329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\n\nimage1 = torch.reshape(image, (1, 3, 224, 224))\nmodel.eval()\n# 节约性能\nwith torch.no_grad():\n    output = model(image1)\nprint(output)\n# print(output.argmax(1))\n# 定义类别对应字典\ndist = {0: \"cat\", 1: \"dog\"}\n# 转numpy格式,列表内取第一个\na = dist[output.argmax(1).numpy()[0]]\n\n\n# 图片路径\nimg = Image.open(\"training/test1/1895.jpg\")\n \nplt.figure(\"Image\")  # 图像窗口名称\nplt.imshow(img)\nplt.axis('on')  # 关掉坐标轴为 off\nplt.title(a)  # 图像题目\n \n# 必须有这个，要不然无法显示\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:14:42.558337Z","iopub.execute_input":"2023-11-22T03:14:42.55873Z","iopub.status.idle":"2023-11-22T03:14:42.922132Z","shell.execute_reply.started":"2023-11-22T03:14:42.5587Z","shell.execute_reply":"2023-11-22T03:14:42.921261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nfrom model import Tudui\nimport torch\nfrom PIL import Image\n# 读取图像\nimg = Image.open(\"training/test1/1895.jpg\")\n# 数据预处理\n# 缩放\ntransform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),\n                                            torchvision.transforms.ToTensor()])\nimage = transform(img)\nprint(image.shape)\n# 根据保存方式加载\nmodel = torch.load(\"tudui_99.pth\", map_location=torch.device('cpu'))\n# 注意维度转换，单张图片\nimage1 = torch.reshape(image, (1, 3, 32, 32))\n# 测试开关\nmodel.eval()\n# 节约性能\nwith torch.no_grad():\n    output = model(image1)\nprint(output)\n# print(output.argmax(1))\n# 定义类别对应字典\ndist = {0: \"飞机\", 1: \"汽车\", 2: \"鸟\", 3: \"猫\", 4: \"鹿\", 5: \"狗\", 6: \"青蛙\", 7: \"马\", 8: \"船\", 9: \"卡车\"}\n# 转numpy格式,列表内取第一个\na = dist[output.argmax(1).numpy()[0]]\nimg.show()\nprint(a)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simple_transform = T.Compose([T.Resize((224,224)) , T.ToTensor(), \n                              T.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])])","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:33:59.67927Z","iopub.execute_input":"2023-11-22T02:33:59.679629Z","iopub.status.idle":"2023-11-22T02:33:59.685083Z","shell.execute_reply.started":"2023-11-22T02:33:59.679602Z","shell.execute_reply":"2023-11-22T02:33:59.68398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image = ImageFolder(\"/kaggle/working/training/Train/\",simple_transform)\nvalid_image = ImageFolder(\"/kaggle/working/training/Valid/\",simple_transform)\n\ntrain_image.class_to_idx","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:34:03.711658Z","iopub.execute_input":"2023-11-22T02:34:03.712539Z","iopub.status.idle":"2023-11-22T02:34:03.835786Z","shell.execute_reply.started":"2023-11-22T02:34:03.712507Z","shell.execute_reply":"2023-11-22T02:34:03.834624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image1 = ImageFolder(\"/kaggle/working/training/Train/\",simple_transform)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:34:06.726807Z","iopub.execute_input":"2023-11-22T02:34:06.727634Z","iopub.status.idle":"2023-11-22T02:34:06.82141Z","shell.execute_reply.started":"2023-11-22T02:34:06.727606Z","shell.execute_reply":"2023-11-22T02:34:06.820542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:07:11.707793Z","iopub.execute_input":"2023-11-21T03:07:11.708169Z","iopub.status.idle":"2023-11-21T03:07:11.71396Z","shell.execute_reply.started":"2023-11-21T03:07:11.708135Z","shell.execute_reply":"2023-11-21T03:07:11.713158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_image","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:07:20.61146Z","iopub.execute_input":"2023-11-21T03:07:20.612176Z","iopub.status.idle":"2023-11-21T03:07:20.618045Z","shell.execute_reply.started":"2023-11-21T03:07:20.612142Z","shell.execute_reply":"2023-11-21T03:07:20.617126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:42:05.045468Z","iopub.execute_input":"2023-11-21T03:42:05.046222Z","iopub.status.idle":"2023-11-21T03:42:05.051913Z","shell.execute_reply.started":"2023-11-21T03:42:05.046191Z","shell.execute_reply":"2023-11-21T03:42:05.051054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = torch.utils.data.DataLoader(\n    train_image,batch_size = 32 ,num_workers = 2, shuffle = True)\n\nvalid_dataloader = torch.utils.data.DataLoader(\n    valid_image,batch_size = 32 ,num_workers = 2, shuffle = False)\n\ndataloaders = {'training':train_dataloader,'validation':valid_dataloader}\ndataset_sizes = {'training':len(train_dataloader.dataset),'validation':len(valid_dataloader.dataset)}\nprint(dataset_sizes)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:34:18.913209Z","iopub.execute_input":"2023-11-22T02:34:18.913961Z","iopub.status.idle":"2023-11-22T02:34:18.920683Z","shell.execute_reply.started":"2023-11-22T02:34:18.913929Z","shell.execute_reply":"2023-11-22T02:34:18.919701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader = torch.utils.data.DataLoader(\n    train_image,batch_size = 32 ,num_workers = 2, shuffle = True)\n\nvalid_dataloader = torch.utils.data.DataLoader(\n    valid_image,batch_size = 32 ,num_workers = 2, shuffle = False)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:34:21.383127Z","iopub.execute_input":"2023-11-22T02:34:21.383483Z","iopub.status.idle":"2023-11-22T02:34:21.388809Z","shell.execute_reply.started":"2023-11-22T02:34:21.383455Z","shell.execute_reply":"2023-11-22T02:34:21.387757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataloader","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:49:56.873608Z","iopub.execute_input":"2023-11-21T03:49:56.87441Z","iopub.status.idle":"2023-11-21T03:49:56.90516Z","shell.execute_reply.started":"2023-11-21T03:49:56.874378Z","shell.execute_reply":"2023-11-21T03:49:56.903926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataloaders = {'training':train_dataloader,'validation':valid_dataloader}\ndataset_sizes = {'training':len(train_dataloader.dataset),'validation':len(valid_dataloader.dataset)}\nprint(dataset_sizes)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:34:24.897586Z","iopub.execute_input":"2023-11-22T02:34:24.897947Z","iopub.status.idle":"2023-11-22T02:34:24.903227Z","shell.execute_reply.started":"2023-11-22T02:34:24.89792Z","shell.execute_reply":"2023-11-22T02:34:24.902264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_dataloader)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:08:47.0059Z","iopub.execute_input":"2023-11-21T03:08:47.006695Z","iopub.status.idle":"2023-11-21T03:08:47.012396Z","shell.execute_reply.started":"2023-11-21T03:08:47.006662Z","shell.execute_reply":"2023-11-21T03:08:47.011454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.is_available()","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:24:38.343085Z","iopub.execute_input":"2023-11-22T02:24:38.343987Z","iopub.status.idle":"2023-11-22T02:24:38.349828Z","shell.execute_reply.started":"2023-11-22T02:24:38.343953Z","shell.execute_reply":"2023-11-22T02:24:38.348892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Creating","metadata":{}},{"cell_type":"code","source":"model = resnet18(ResNet18_Weights)\nnum_features = model.fc.in_features\nmodel.fc = nn.Linear(num_features,2)\n\nif device:\n    model = model.cuda()","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:35:29.753333Z","iopub.execute_input":"2023-11-22T02:35:29.753703Z","iopub.status.idle":"2023-11-22T02:35:30.009108Z","shell.execute_reply.started":"2023-11-22T02:35:29.753665Z","shell.execute_reply":"2023-11-22T02:35:30.008301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = resnet18(ResNet18_Weights)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:32:57.552476Z","iopub.execute_input":"2023-11-22T03:32:57.553251Z","iopub.status.idle":"2023-11-22T03:32:57.788991Z","shell.execute_reply.started":"2023-11-22T03:32:57.553222Z","shell.execute_reply":"2023-11-22T03:32:57.788184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:33:20.327132Z","iopub.execute_input":"2023-11-22T03:33:20.327772Z","iopub.status.idle":"2023-11-22T03:33:20.334964Z","shell.execute_reply.started":"2023-11-22T03:33:20.327741Z","shell.execute_reply":"2023-11-22T03:33:20.333993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:33:33.230829Z","iopub.execute_input":"2023-11-22T03:33:33.231478Z","iopub.status.idle":"2023-11-22T03:33:33.238207Z","shell.execute_reply.started":"2023-11-22T03:33:33.23145Z","shell.execute_reply":"2023-11-22T03:33:33.23732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_features","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:35:36.007951Z","iopub.execute_input":"2023-11-22T02:35:36.008594Z","iopub.status.idle":"2023-11-22T02:35:36.014449Z","shell.execute_reply.started":"2023-11-22T02:35:36.008562Z","shell.execute_reply":"2023-11-22T02:35:36.013419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#loss and optimizer\nLR = 0.001\n\ncriterion = nn.CrossEntropyLoss()\n\noptimizer = SGD(model.parameters(), lr=LR, momentum=0.9)\nexp_LR_scheduler = lr_scheduler.StepLR(optimizer,step_size=7,gamma=0.1) \n#StepLR: function helps in dynamically changing the learningrate.","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:35:40.303966Z","iopub.execute_input":"2023-11-22T02:35:40.304328Z","iopub.status.idle":"2023-11-22T02:35:40.310668Z","shell.execute_reply.started":"2023-11-22T02:35:40.3043Z","shell.execute_reply":"2023-11-22T02:35:40.309628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the model","metadata":{}},{"cell_type":"code","source":"def train_model(model,criterion,optimizer,scheduler,num_epochs=6):\n    since = time.time()\n    \n    best_model_weights = model.state_dict()\n    best_acc = 0.0\n    \n    for epoch in range(num_epochs):\n        print(\"epoch {}/{}\".format(epoch+1,num_epochs))\n        print(\"*\" * 10)\n        \n        #training and validation for each epoch\n        for x in ['training','validation']:\n            if x == 'training':\n                model.train()\n            else: \n                model.eval()\n            \n            running_loss = 0.0\n            running_corrects = 0\n            \n            #iterating over data\n            for data in dataloaders[x]:\n                # get the inputs\n                inputs, labels = data\n\n                # wrap them in Variable\n                if torch.cuda.is_available():\n                    inputs = Variable(inputs.cuda())\n                    labels = Variable(labels.cuda())\n                else:\n                    inputs, labels = Variable(inputs), Variable(labels)\n                    \n                # zero the parameter gradients\n                optimizer.zero_grad()\n                \n                # forward\n                outputs = model(inputs)\n\n                _, preds = torch.max(outputs.data, 1)\n                \n                loss = criterion(outputs, labels)\n\n                # backward + optimize only if in training phase\n                if x == 'training':\n                    loss.backward()\n                    optimizer.step()\n                    \n                \n                # statistics\n                running_loss += loss.data\n                running_corrects += torch.sum(preds == labels.data)\n                \n\n            epoch_loss = running_loss / dataset_sizes[x]\n            epoch_acc = running_corrects / dataset_sizes[x]\n            \n            print('{} Loss: {:.4f} Acc: {:.4f}'.format(x, epoch_loss, epoch_acc))\n\n            # deep copy the model\n            if x == 'validation' and epoch_acc > best_acc:\n                best_acc = epoch_acc\n                best_model_weights = model.state_dict()\n\n        print()\n\n    time_elapsed = time.time() - since\n    print('Training complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))\n    print('Best val Acc: {:4f}'.format(best_acc))\n\n    # load best model weights\n    model.load_state_dict(best_model_weights)\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:35:43.007666Z","iopub.execute_input":"2023-11-22T02:35:43.00852Z","iopub.status.idle":"2023-11-22T02:35:43.020418Z","shell.execute_reply.started":"2023-11-22T02:35:43.008488Z","shell.execute_reply":"2023-11-22T02:35:43.019488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = train_model(model, criterion, optimizer, exp_LR_scheduler, num_epochs=6)","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:35:45.552401Z","iopub.execute_input":"2023-11-22T02:35:45.55329Z","iopub.status.idle":"2023-11-22T02:41:19.871756Z","shell.execute_reply.started":"2023-11-22T02:35:45.553256Z","shell.execute_reply":"2023-11-22T02:41:19.870573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:57:06.593816Z","iopub.execute_input":"2023-11-22T02:57:06.59418Z","iopub.status.idle":"2023-11-22T02:57:06.601291Z","shell.execute_reply.started":"2023-11-22T02:57:06.594152Z","shell.execute_reply":"2023-11-22T02:57:06.600422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model, 'model.pth')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:38:54.179987Z","iopub.execute_input":"2023-11-21T03:38:54.180701Z","iopub.status.idle":"2023-11-21T03:38:54.284124Z","shell.execute_reply.started":"2023-11-21T03:38:54.180665Z","shell.execute_reply":"2023-11-21T03:38:54.283134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.","metadata":{"execution":{"iopub.status.busy":"2023-11-21T03:54:34.763124Z","iopub.execute_input":"2023-11-21T03:54:34.763841Z","iopub.status.idle":"2023-11-21T03:54:34.805171Z","shell.execute_reply.started":"2023-11-21T03:54:34.763806Z","shell.execute_reply":"2023-11-21T03:54:34.804067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchsummary import summary","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:34:15.240775Z","iopub.execute_input":"2023-11-22T03:34:15.241421Z","iopub.status.idle":"2023-11-22T03:34:15.250576Z","shell.execute_reply.started":"2023-11-22T03:34:15.24139Z","shell.execute_reply":"2023-11-22T03:34:15.249681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install torchsummary","metadata":{"execution":{"iopub.status.busy":"2023-11-22T02:58:57.569468Z","iopub.execute_input":"2023-11-22T02:58:57.56986Z","iopub.status.idle":"2023-11-22T02:59:09.397509Z","shell.execute_reply.started":"2023-11-22T02:58:57.569828Z","shell.execute_reply":"2023-11-22T02:59:09.396463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"summary(model(3,32,32))","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:35:04.219167Z","iopub.execute_input":"2023-11-22T03:35:04.219811Z","iopub.status.idle":"2023-11-22T03:35:04.2752Z","shell.execute_reply.started":"2023-11-22T03:35:04.219782Z","shell.execute_reply":"2023-11-22T03:35:04.274102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(model1.state_dict())","metadata":{"execution":{"iopub.status.busy":"2023-11-22T03:39:16.26942Z","iopub.execute_input":"2023-11-22T03:39:16.26981Z","iopub.status.idle":"2023-11-22T03:39:16.273906Z","shell.execute_reply.started":"2023-11-22T03:39:16.269782Z","shell.execute_reply":"2023-11-22T03:39:16.272908Z"},"trusted":true},"execution_count":null,"outputs":[]}]}