{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"varInspector":{"cols":{"lenName":16,"lenType":16,"lenVar":40},"kernels_config":{"python":{"delete_cmd_postfix":"","delete_cmd_prefix":"del ","library":"var_list.py","varRefreshCmd":"print(var_dic_list())"},"r":{"delete_cmd_postfix":") ","delete_cmd_prefix":"rm(","library":"var_list.r","varRefreshCmd":"cat(var_dic_list()) "}},"types_to_exclude":["module","function","builtin_function_or_method","instance","_Feature"],"window_display":false},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#da5351;\n            font-size:130%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Digit Classification using CNN\" style=\"padding: 10px; color:white; text-align:center;\">\n    Image Classification using VGG16 and ResNet50\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Image_Classification_using_VGG16_and_ResNet50\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px;\n           text-align:left\">\n    \nWelcome to this notebook :)\n      \n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px;\n           text-align:left\">\n    \n<strong>Table of Contents:</strong><br />\n\n<p>\n    <ul>\n        <li>1. Install and Import Dependencies</li>\n        <li>\n            2. Image Segmentation\n        </li>\n        <li>\n            3. Implement VGG16 from Scratch\n        </li>\n        <li>\n            4. Data Prepration and Training VGG16\n        </li>\n        <li>5. Evaluating VGG16 and Predicting Labels</li>\n        <li>6. Preprocessing and Training ResNet50</li>\n        <li>7. Predicting Labels using ResNet50</li>\n        <li>8. Conclusion</li>\n    </ul>\n</p>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   1. Install and Import Dependencies\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Install_and_Import_Dependencies\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom PIL import Image\nimport seaborn as sns\nsns.set_style('darkgrid')\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report, confusion_matrix\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.checkpoint import checkpoint\nfrom torch.utils.data import Dataset\nfrom torchvision import transforms, datasets, models\nimport torchvision\nfrom fastai.vision.all import * \nimport os\nfrom tempfile import TemporaryDirectory\nimport time\nimport pickle ","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:13.109128Z","iopub.execute_input":"2024-04-15T07:34:13.109601Z","iopub.status.idle":"2024-04-15T07:34:13.121353Z","shell.execute_reply.started":"2024-04-15T07:34:13.109566Z","shell.execute_reply":"2024-04-15T07:34:13.120176Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"paths = {\n    'source_train' : os.path.join('/kaggle', 'input', 'cifar-10', 'train.7z'),\n    'source_test' : os.path.join('/kaggle', 'input', 'cifar-10', 'test.7z'),\n    'dest' : os.path.join('/kaggle', 'working', 'cifar-10'),\n    'train' : os.path.join('/kaggle', 'working', 'cifar-10', 'train'),\n    'test' : os.path.join('/kaggle', 'working', 'cifar-10', 'test'),\n    'trainLabels' : os.path.join('/kaggle', 'input', 'cifar-10', 'trainLabels.csv')\n}","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:13.126947Z","iopub.execute_input":"2024-04-15T07:34:13.127231Z","iopub.status.idle":"2024-04-15T07:34:13.136481Z","shell.execute_reply.started":"2024-04-15T07:34:13.127206Z","shell.execute_reply":"2024-04-15T07:34:13.135676Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:13.138027Z","iopub.execute_input":"2024-04-15T07:34:13.138368Z","iopub.status.idle":"2024-04-15T07:34:14.982515Z","shell.execute_reply.started":"2024-04-15T07:34:13.138337Z","shell.execute_reply":"2024-04-15T07:34:14.981225Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:14.984634Z","iopub.execute_input":"2024-04-15T07:34:14.985652Z","iopub.status.idle":"2024-04-15T07:34:14.990332Z","shell.execute_reply.started":"2024-04-15T07:34:14.985603Z","shell.execute_reply":"2024-04-15T07:34:14.989325Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_7z_file(archive_path, destination_path):\n    with py7zr.SevenZipFile(archive_path, mode='r') as z:\n        z.extractall(path=destination_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:14.993045Z","iopub.execute_input":"2024-04-15T07:34:14.993355Z","iopub.status.idle":"2024-04-15T07:34:15.000166Z","shell.execute_reply.started":"2024-04-15T07:34:14.993324Z","shell.execute_reply":"2024-04-15T07:34:14.999279Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"extract_7z_file(paths['source_train'], paths['dest'])\nextract_7z_file(paths['source_test'], paths['dest'])","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:31.351913Z","iopub.execute_input":"2024-04-15T07:34:31.352813Z","iopub.status.idle":"2024-04-15T07:38:15.521771Z","shell.execute_reply.started":"2024-04-15T07:34:31.352768Z","shell.execute_reply":"2024-04-15T07:38:15.520699Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   2. Image Segmentation\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Image_Segmentation\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"train_images = os.listdir(paths['train'])\ntrain_images[:10]","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:23.558594Z","iopub.execute_input":"2024-04-15T07:39:23.559736Z","iopub.status.idle":"2024-04-15T07:39:23.595227Z","shell.execute_reply.started":"2024-04-15T07:39:23.559693Z","shell.execute_reply":"2024-04-15T07:39:23.594350Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = pd.read_csv(paths['trainLabels'])\ny_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:26.634488Z","iopub.execute_input":"2024-04-15T07:39:26.634869Z","iopub.status.idle":"2024-04-15T07:39:26.662409Z","shell.execute_reply.started":"2024-04-15T07:39:26.634838Z","shell.execute_reply":"2024-04-15T07:39:26.661409Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = y_train['label'].unique()\nlabels","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:33.669756Z","iopub.execute_input":"2024-04-15T07:39:33.670130Z","iopub.status.idle":"2024-04-15T07:39:33.680400Z","shell.execute_reply.started":"2024-04-15T07:39:33.670101Z","shell.execute_reply":"2024-04-15T07:39:33.679375Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for state in ['train', 'eval']:\n    for label in labels:\n        if os.path.exists(os.path.join(paths['dest'], 'train', state)) == False:\n            os.mkdir(os.path.join(paths['dest'], 'train', state,))\n            \n        if os.path.exists(os.path.join(paths['dest'], 'train', state, label)) == False:\n            os.mkdir(os.path.join(paths['dest'], 'train', state, label))","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:36.303687Z","iopub.execute_input":"2024-04-15T07:39:36.304059Z","iopub.status.idle":"2024-04-15T07:39:36.311623Z","shell.execute_reply.started":"2024-04-15T07:39:36.304030Z","shell.execute_reply":"2024-04-15T07:39:36.310664Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"li = []\nfor item in os.listdir(paths['train']):\n    if '.png' not in item:\n        li.append(item)\nli","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:39.589104Z","iopub.execute_input":"2024-04-15T07:39:39.589767Z","iopub.status.idle":"2024-04-15T07:39:39.634215Z","shell.execute_reply.started":"2024-04-15T07:39:39.589734Z","shell.execute_reply":"2024-04-15T07:39:39.633266Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"png_files = [item for item in os.listdir(paths['train']) if '.png' in item]\nnumbers = [int(os.path.splitext(png)[0]) for png in png_files if type(int(os.path.splitext(png)[0])) == int]\nnumbers.sort()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:42.068410Z","iopub.execute_input":"2024-04-15T07:39:42.069349Z","iopub.status.idle":"2024-04-15T07:39:42.322322Z","shell.execute_reply.started":"2024-04-15T07:39:42.069312Z","shell.execute_reply":"2024-04-15T07:39:42.321181Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = y_train.copy()\ny = X['label'].values\nX.drop(['id', 'label'], axis=1, inplace=True)\nX['image name'] = [str(num) + '.' + 'png' for num in numbers]\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:44.839586Z","iopub.execute_input":"2024-04-15T07:39:44.840480Z","iopub.status.idle":"2024-04-15T07:39:44.874984Z","shell.execute_reply.started":"2024-04-15T07:39:44.840446Z","shell.execute_reply":"2024-04-15T07:39:44.874121Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def make_image_grid(labels:np.ndarray, images:list):\n    plt.figure(figsize=(10, 10))\n    for i in range(32):\n        plt.subplot(4, 8, i + 1)\n        labeli = labels[i]\n        imgi_arr = np.asarray(Image.open(os.path.join(paths['train'], images[i])))\n        if imgi_arr.shape == (32, 32, 3):\n            pass\n        elif imgi_arr.shape == (3, 32, 32):\n            imgi_arr = np.transpose(imgi_arr, (1,2,0))\n        plt.imshow(imgi_arr)\n        plt.title(labeli, size=9, alpha=.8)\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:48.163536Z","iopub.execute_input":"2024-04-15T07:39:48.164224Z","iopub.status.idle":"2024-04-15T07:39:48.171381Z","shell.execute_reply.started":"2024-04-15T07:39:48.164191Z","shell.execute_reply":"2024-04-15T07:39:48.170396Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"make_image_grid(y, X['image name'].values.tolist())","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:51.397439Z","iopub.execute_input":"2024-04-15T07:39:51.398081Z","iopub.status.idle":"2024-04-15T07:39:55.801665Z","shell.execute_reply.started":"2024-04-15T07:39:51.398045Z","shell.execute_reply":"2024-04-15T07:39:55.800710Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, X_eval, y_train, y_eval = train_test_split(X, y, random_state=42, train_size=.8)\nprint(X_train.shape, y_train.shape)\nprint(X_eval.shape, y_eval.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:39:59.814822Z","iopub.execute_input":"2024-04-15T07:39:59.815717Z","iopub.status.idle":"2024-04-15T07:39:59.832224Z","shell.execute_reply.started":"2024-04-15T07:39:59.815683Z","shell.execute_reply":"2024-04-15T07:39:59.831157Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"make_image_grid(y_train, X_train['image name'].values.tolist())","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:40:02.831323Z","iopub.execute_input":"2024-04-15T07:40:02.831681Z","iopub.status.idle":"2024-04-15T07:40:07.184618Z","shell.execute_reply.started":"2024-04-15T07:40:02.831654Z","shell.execute_reply":"2024-04-15T07:40:07.183689Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"make_image_grid(y_eval, X_eval['image name'].values.tolist())","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:40:10.996809Z","iopub.execute_input":"2024-04-15T07:40:10.997474Z","iopub.status.idle":"2024-04-15T07:40:15.368363Z","shell.execute_reply.started":"2024-04-15T07:40:10.997440Z","shell.execute_reply":"2024-04-15T07:40:15.367429Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def segment_images(source, state):\n    states = {\n        'train' : pd.DataFrame({\n            'image name' : X_train['image name'].values.tolist(),\n            'label' : y_train\n        }),\n        'eval' : pd.DataFrame({\n            'image name' : X_eval['image name'].values.tolist(),\n            'label' : y_eval\n        })\n    }\n    \n    for _, row in states[state].iterrows():\n        os.rename(os.path.join(source, row['image name']), os.path.join(source, state, row['label'], row['image name']))\n\nsegment_images(paths['train'], 'train')\nsegment_images(paths['train'], 'eval')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:40:18.883872Z","iopub.execute_input":"2024-04-15T07:40:18.884277Z","iopub.status.idle":"2024-04-15T07:40:25.364338Z","shell.execute_reply.started":"2024-04-15T07:40:18.884240Z","shell.execute_reply":"2024-04-15T07:40:25.363510Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   3. Implement VGG16 from Scratch\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Implement_VGG16_from_Scratch\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"VGG16 = [64,64,\"M\",128,128,\"M\",256,256,256,\"M\",512,512,512,\"M\",512,512,512,\"M\"]\n\nclass VGGNet(nn.Module):\n    def __init__(self, inchannel=3, numclass=10):\n        super(VGGNet,self).__init__()\n        \n        self.inchannel = inchannel\n        self.numclass = numclass\n        self.conv_later = self.create_conv_layers(VGG16)\n        self.fcs= nn.Sequential(\n            nn.Linear(512*7*7,4096),\n            nn.ReLU(),\n            nn.Dropout(p=0.5),\n            nn.Linear(4096,4096),\n            nn.ReLU(),\n            nn.Dropout(p=0.5),\n            nn.Linear(4096,self.numclass),\n        )\n        \n        \n    def forward(self, x):\n        x=self.conv_later(x)\n        x= x.reshape(x.shape[0],-1)\n        x=self.fcs(x)\n        return x\n    \n    \n    def create_conv_layers(self, architectire):\n        layers= []\n        inchannel = self.inchannel\n        \n        for x in architectire:\n            if type(x) == int:\n                outchannel = x\n                layers+=[\n                    nn.Conv2d(\n                        in_channels = inchannel,\n                        out_channels=outchannel,\n                        kernel_size= (3,3),\n                        stride = (1,1),\n                        padding = (1,1),\n                        ),\n                    nn.BatchNorm2d(x),# Barch norm orginally indtroduce in 2016 however VGG introduced in 2014\n                    nn.ReLU(),                    \n                    ]\n                inchannel = outchannel\n                \n            else:\n                layers+=[nn.MaxPool2d(kernel_size=(2,2),stride=(2,2))]\n                \n        return nn.Sequential(*layers)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.778234Z","iopub.status.idle":"2024-04-15T07:34:16.778581Z","shell.execute_reply.started":"2024-04-15T07:34:16.778412Z","shell.execute_reply":"2024-04-15T07:34:16.778429Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   4. Data Prepration and Training VGG16\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Data_Prepration_and_Training_VGG16\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"def train(model, loss_func, optimizer, reduce_lr, num_epochs=10):\n    start_time = time.time()\n    \n    with TemporaryDirectory() as temp_dir:\n        dir_path = os.path.join(temp_dir, 'best_model_weights.pt')\n        best_acc = 0\n        best_loss = 0\n        \n        for epoch in range(num_epochs):\n            print('epoch {0} started:\\n'.format(epoch))\n            print('-' * 30)\n            \n            for phase in ['train', 'eval']:\n                if phase == 'train':\n                    model.train()\n                    \n                else:\n                    model.eval()\n                \n                running_loss = 0\n                running_acc = 0\n                \n                for inputs, labels in dataLoader[phase]:\n                    inputs = inputs.to(device)\n                    labels = labels.to(device)\n                    optimizer.zero_grad()\n                    \n                    with torch.set_grad_enabled(mode= (phase=='train') ):\n                        # Forward\n                        outputs = model(inputs)\n                        loss = loss_func(outputs, labels)\n                        _, preds = torch.max(outputs, 1)\n                        \n                        # Backward\n                        if phase == 'train':\n                            loss.backward()\n                            optimizer.step()     \n                            \n                    running_loss += loss.item() * inputs.size(0)\n                    running_acc += torch.sum(preds == labels.data)\n            \n            if phase == 'train':\n                reduce_lr.step()\n                \n            epoch_loss = running_loss / dataset_size[phase]\n            epoch_acc = running_acc / dataset_size[phase]\n            print(f'Phase {phase}:\\n   Loss : {epoch_loss}\\n   Accuracy : {epoch_acc}')\n            \n            if phase == 'eval' and epoch_loss < best_loss:\n                best_acc = epoch_acc\n                best_loss = epoch_loss\n                torch.save(model.state_dict(), dir_path)\n            print()\n        \n        print(f\"Training is completed in {time.time() - start_time}\\n\")\n#         print(f\"The best model:\\n   Loss : {best_loss}\\n   Accuracy : {best_acc}\")\n#         model.load_state_dict(torch.load(dir_path))\n\n        return model\n\n\nclass CustomDataset(Dataset):\n    def __init__(self, root_dir, transform=None):\n        self.root_dir = root_dir\n        self.image_files = os.listdir(root_dir)\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.image_files)\n\n    def __getitem__(self, idx):\n        image_path = os.path.join(self.root_dir, self.image_files[idx])\n        image = Image.open(image_path)\n\n        if self.transform is not None:\n            image = self.transform(image)\n\n        return image, []\n\n    \n# using gradient checkpointing techniques, we can checkpoint specific parts of your computation graph and reduce the overall memory consumption.\ndef forward_pass(model, inputs):\n    return model(inputs)\n\n\ndef predict(model, state:str):\n    states = {\n        'test': paths['test'],\n        'eval': os.path.join(paths['train'], 'eval')\n    }\n    device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    model.to(device)\n    optimizer = optim.SGD(model.parameters(), lr=.001, momentum=.9)\n    \n    def implementation(dataLoader):\n        actual_labels = []\n        predicted_labels = []\n        for inputs, labels in dataLoader:\n            inputs = inputs.to(device)\n            if labels != []:\n                labels = labels.to(device)\n                actual_labels.extend(labels)\n            \n            model.eval()\n            with torch.no_grad(): # avoid storing gradients\n                # Apply checkpointing to the forward pass\n                outputs = checkpoint(forward_pass, model, inputs)\n            _, preds = torch.max(outputs, 1)\n            predicted_labels.extend(preds)\n                \n        return predicted_labels, actual_labels\n\n    if state.lower() == 'test':\n        data_transforms = transforms.Compose([\n        transforms.Resize(227),\n        transforms.ToTensor(),\n        transforms.Normalize(mean=[.485, .456, .406], std=[.229, .224, .225])\n        ])\n        dataset = CustomDataset(states[state], transform=data_transforms)\n        dataLoader_test = torch.utils.data.DataLoader(dataset=dataset, num_workers=4, batch_size=64, shuffle=True)\n        predicted_labels, actual_labels = implementation(dataLoader_test)\n        return predicted_labels, actual_labels\n        \n    elif state.lower() == 'eval':\n        data_transform = transforms.Compose([\n                transforms.Resize(227),\n                transforms.ToTensor(),\n                transforms.Normalize(mean=[.485, .456, .406], std=[.229, .224, .225])\n            ])\n        dataset = datasets.ImageFolder(root=os.path.join(states[state]), transform=data_transform)\n        dataLoader = torch.utils.data.DataLoader(dataset=dataset, num_workers=4, batch_size=64, shuffle=True)\n        predicted_labels, actual_labels = implementation(dataLoader)\n        return predicted_labels, actual_labels\n    \n    else:\n        raise ValueError(f\"Expected test or eval, not {state} for the state parameter!\")\n        \n    \ndef imshow(tensor):\n    plt.figure(figsize=(12, 12))\n    img = np.transpose(tensor.numpy(), axes=(1, 2, 0)) # tensor : (3, 227, 227) --> image : (227, 227, 3)\n    \n    # remove the Train Phase normalization from images\n    mean = np.array([.485, .456, .406])\n    std = np.array([.229, .224, .225])\n    img = (mean + img) * std\n    \n    # normal pixels between 0 and 1\n    img = np.clip(a=img, a_min=0, a_max=1)\n    plt.imshow(img)\n    plt.pause(20)\n\n\ndef get_dataset_size_and_labels(dataset):\n    return {x : len(dataset[x]) for x in ['train', 'eval']}, {len(dataset[x].classes) : dataset[x].classes for x in ['train', 'eval']}\n\n\ndef save_data(data, file_name):\n    with open(os.path.join('/kaggle', 'working', file_name), 'wb') as f:\n        pickle.dump(data, f)\n        f.close()\n    \ndef load_data(file_name):\n    return pickle.load(open(os.path.join('/kaggle', 'working', file_name), 'rb'))","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.780100Z","iopub.status.idle":"2024-04-15T07:34:16.780445Z","shell.execute_reply.started":"2024-04-15T07:34:16.780277Z","shell.execute_reply":"2024-04-15T07:34:16.780294Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"if __name__ == \"__main__\":\n    # 1. Implement the AlexNet deep neural network \n    device = \"cuda:0\" if torch.cuda.is_available() else \"cpu\"\n    model = VGGNet().to(device)\n    \n    # 2. Data Prepration\n    # determine transformations for train and test data \n    data_transform = {\n        \"train\" : transforms.Compose([\n            transforms.RandomResizedCrop(size=227),\n            transforms.RandomVerticalFlip(),\n            transforms.ToTensor(),\n            transforms.Normalize(mean=[.485, .456, .406], std=[.229, .224, .225])\n        ]),\n        \n        \"eval\" : transforms.Compose([\n            transforms.Resize(227),\n            transforms.ToTensor(),\n            transforms.Normalize(mean=[.485, .456, .406], std=[.229, .224, .225])\n        ])\n    }\n    # apply transformations on train and test data after being loaded on CPU or GPU\n    root = os.path.join('/kaggle', 'working', 'cifar-10')\n    transformed_dataset = {\n        x: datasets.ImageFolder(root=os.path.join(root, 'train', x), transform=data_transform[x]) for x in ['train', 'eval']\n    }\n    # create a continuous flow of batches and determine how data should load on CPU or GPU\n    # image : (227, 227, 3) --> tensor : (3, 227, 227) --> batch : (500, 3, 227, 227)\n    dataLoader = {\n        x: torch.utils.data.DataLoader(dataset=transformed_dataset[x], num_workers=4, batch_size=64, shuffle=True) for x in ['train', 'eval']\n    }\n    \n    dataset_size, labels = get_dataset_size_and_labels(transformed_dataset)\n    # print(dataset_size)\n    # print(labels)\n    \n    # show the images of the first bacth\n#     inputs, lables = next(iter(dataLoader['train']))\n#     tensor = torchvision.utils.make_grid(inputs)\n#     imshow(tensor)\n    \n    # 3. Training VGG16\n    loss = nn.CrossEntropyLoss()\n    optimizer = optim.SGD(model.parameters(), lr=.001, momentum=.9)\n    reduce_lr = optim.lr_scheduler.StepLR(optimizer=optimizer, step_size=5, gamma=.1)\n    trained_model = train(model, loss, optimizer, reduce_lr, num_epochs=30) # fit the AlexNet NN\n    save_data(trained_model, 'trained_model')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.781327Z","iopub.status.idle":"2024-04-15T07:34:16.781712Z","shell.execute_reply.started":"2024-04-15T07:34:16.781496Z","shell.execute_reply":"2024-04-15T07:34:16.781513Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   5. Evaluating VGG16 and Predicting Labels\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Evaluating_VGG16_and_Predicting_Labels\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"trained_model = load_data('trained_model')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.783600Z","iopub.status.idle":"2024-04-15T07:34:16.783990Z","shell.execute_reply.started":"2024-04-15T07:34:16.783821Z","shell.execute_reply":"2024-04-15T07:34:16.783837Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transformed_dataset['train'].class_to_idx","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.785359Z","iopub.status.idle":"2024-04-15T07:34:16.785745Z","shell.execute_reply.started":"2024-04-15T07:34:16.785535Z","shell.execute_reply":"2024-04-15T07:34:16.785552Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def convert_to_idx(pred_tensors):\n    preds = [pred_tensor.cpu().numpy().tolist() for pred_tensor in pred_tensors]\n    return preds","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.787147Z","iopub.status.idle":"2024-04-15T07:34:16.787499Z","shell.execute_reply.started":"2024-04-15T07:34:16.787331Z","shell.execute_reply":"2024-04-15T07:34:16.787347Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_labels, actual_labels = predict(trained_model,'eval')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.788892Z","iopub.status.idle":"2024-04-15T07:34:16.789237Z","shell.execute_reply.started":"2024-04-15T07:34:16.789071Z","shell.execute_reply":"2024-04-15T07:34:16.789088Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_labels = convert_to_idx(predicted_labels)\nactual_labels = convert_to_idx(actual_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.790840Z","iopub.status.idle":"2024-04-15T07:34:16.791189Z","shell.execute_reply.started":"2024-04-15T07:34:16.791021Z","shell.execute_reply":"2024-04-15T07:34:16.791038Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\n    classification_report(\n    y_true=actual_labels,\n    y_pred=predicted_labels,\n    target_names=transformed_dataset['train'].class_to_idx.keys()\n    )\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.792086Z","iopub.status.idle":"2024-04-15T07:34:16.792425Z","shell.execute_reply.started":"2024-04-15T07:34:16.792257Z","shell.execute_reply":"2024-04-15T07:34:16.792274Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"conf_mat = confusion_matrix(\n    y_true=actual_labels,\n    y_pred=predicted_labels\n)\n\nplt.figure(figsize=(8, 8))\nsns.heatmap(\n    conf_mat,\n    cmap='Oranges',\n    annot=True,\n    cbar=False,\n    fmt='.3g'\n);\n\n\nxlocs, _ = plt.xticks()\nplt.xticks(\n    ticks=xlocs,\n    labels=transformed_dataset['train'].class_to_idx.keys(),\n    rotation=30\n);\n\nylocs, _ = plt.yticks()\nplt.yticks(\n    ticks=ylocs,\n    labels=transformed_dataset['train'].class_to_idx.keys(),\n    rotation=360\n);","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.794329Z","iopub.status.idle":"2024-04-15T07:34:16.794745Z","shell.execute_reply.started":"2024-04-15T07:34:16.794529Z","shell.execute_reply":"2024-04-15T07:34:16.794547Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds, _ = predict(trained_model,'test')","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.796552Z","iopub.status.idle":"2024-04-15T07:34:16.796934Z","shell.execute_reply.started":"2024-04-15T07:34:16.796763Z","shell.execute_reply":"2024-04-15T07:34:16.796780Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds[:10]","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.798333Z","iopub.status.idle":"2024-04-15T07:34:16.798822Z","shell.execute_reply.started":"2024-04-15T07:34:16.798557Z","shell.execute_reply":"2024-04-15T07:34:16.798578Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = convert_to_idx(preds)\npreds[:10]","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.800132Z","iopub.status.idle":"2024-04-15T07:34:16.800482Z","shell.execute_reply.started":"2024-04-15T07:34:16.800314Z","shell.execute_reply":"2024-04-15T07:34:16.800331Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def idx_to_class(preds):\n    li = []\n    map_preds = {0 : 'airplane',\n                 1 : 'automobile',\n                 2 : 'bird',\n                 3 : 'cat',\n                 4 : 'deer',\n                 5 : 'dog',\n                 6 : 'frog',\n                 7 : 'horse',\n                 8 : 'ship',\n                 9 : 'truck'\n                }\n    for pred in preds:\n        if pred in map_preds.keys():\n            li.append(map_preds[pred])\n    return li","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.802001Z","iopub.status.idle":"2024-04-15T07:34:16.802366Z","shell.execute_reply.started":"2024-04-15T07:34:16.802188Z","shell.execute_reply":"2024-04-15T07:34:16.802206Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = idx_to_class(preds)\npreds[:10]","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.803753Z","iopub.status.idle":"2024-04-15T07:34:16.804093Z","shell.execute_reply.started":"2024-04-15T07:34:16.803928Z","shell.execute_reply":"2024-04-15T07:34:16.803944Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"length = len(preds)\nlength","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.805305Z","iopub.status.idle":"2024-04-15T07:34:16.805669Z","shell.execute_reply.started":"2024-04-15T07:34:16.805479Z","shell.execute_reply":"2024-04-15T07:34:16.805495Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   6. Preprocessing and Training ResNet50\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Preprocessing_and_Training_ResNet50\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"batch_tfms = [*aug_transforms(), Normalize.from_stats(*imagenet_stats)]\ndls=ImageDataLoaders.from_folder(\n    path=paths['train'],\n    train='train',\n    valid='eval',\n    item_tfms=Resize(224),\n    batch_tfms=batch_tfms,bs=64\n)\nlearn = vision_learner(dls, resnet50, metrics=accuracy, wd=0.01)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:40:58.364619Z","iopub.execute_input":"2024-04-15T07:40:58.365025Z","iopub.status.idle":"2024-04-15T07:41:04.248898Z","shell.execute_reply.started":"2024-04-15T07:40:58.364993Z","shell.execute_reply":"2024-04-15T07:41:04.247900Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:41:07.026903Z","iopub.execute_input":"2024-04-15T07:41:07.027862Z","iopub.status.idle":"2024-04-15T07:41:32.823857Z","shell.execute_reply.started":"2024-04-15T07:41:07.027823Z","shell.execute_reply":"2024-04-15T07:41:32.822678Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.fit_one_cycle(3, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:41:49.603321Z","iopub.execute_input":"2024-04-15T07:41:49.603790Z","iopub.status.idle":"2024-04-15T07:50:47.694355Z","shell.execute_reply.started":"2024-04-15T07:41:49.603751Z","shell.execute_reply":"2024-04-15T07:50:47.693193Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:50:50.140381Z","iopub.execute_input":"2024-04-15T07:50:50.140810Z","iopub.status.idle":"2024-04-15T07:51:14.717966Z","shell.execute_reply.started":"2024-04-15T07:50:50.140771Z","shell.execute_reply":"2024-04-15T07:51:14.716820Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.fit_one_cycle(5, lr_max=slice(1e-6,3e-4))","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:53:52.384318Z","iopub.execute_input":"2024-04-15T07:53:52.385302Z","iopub.status.idle":"2024-04-15T08:12:36.614237Z","shell.execute_reply.started":"2024-04-15T07:53:52.385259Z","shell.execute_reply":"2024-04-15T08:12:36.613239Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T08:12:49.455506Z","iopub.execute_input":"2024-04-15T08:12:49.456865Z","iopub.status.idle":"2024-04-15T08:13:11.986720Z","shell.execute_reply.started":"2024-04-15T08:12:49.456811Z","shell.execute_reply":"2024-04-15T08:13:11.985691Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   7. Predicting Labels using ResNet50\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Predicting_Labels_using_ResNet50\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"test_img_paths = [os.path.join(paths['test'], file) for file in os.listdir(paths['test'])]\ntest_dl = learn.dls.test_dl(test_img_paths)\nclass_score, y =learn.get_preds(dl=test_dl)\nclass_score = np.argmax(class_score, axis=1)\nclassScore = class_score.tolist()","metadata":{"execution":{"iopub.status.busy":"2024-04-15T08:14:18.269051Z","iopub.execute_input":"2024-04-15T08:14:18.269439Z","iopub.status.idle":"2024-04-15T08:22:59.496118Z","shell.execute_reply.started":"2024-04-15T08:14:18.269405Z","shell.execute_reply":"2024-04-15T08:22:59.494951Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.dls.vocab","metadata":{"execution":{"iopub.status.busy":"2024-04-15T08:26:40.715880Z","iopub.execute_input":"2024-04-15T08:26:40.716733Z","iopub.status.idle":"2024-04-15T08:26:40.723727Z","shell.execute_reply.started":"2024-04-15T08:26:40.716697Z","shell.execute_reply":"2024-04-15T08:26:40.722623Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predicted_labels = idx_to_class(classScore)\nprint(classScore[:10])\nprint(predicted_labels[:10])","metadata":{"execution":{"iopub.status.busy":"2024-04-15T08:26:59.875519Z","iopub.execute_input":"2024-04-15T08:26:59.876233Z","iopub.status.idle":"2024-04-15T08:26:59.927502Z","shell.execute_reply.started":"2024-04-15T08:26:59.876201Z","shell.execute_reply":"2024-04-15T08:26:59.926134Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ImageId = [ (''.join(filter(str.isdigit, name))) for name in os.listdir(paths['test'])]\nsubmission_df = pd.DataFrame({\n    'id' : ImageId,\n    'label' : predicted_labels\n})\nsubmission_df.head()\nsubmission_df.to_csv('/kaggle/working/submission_df.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-15T07:34:16.821916Z","iopub.status.idle":"2024-04-15T07:34:16.822268Z","shell.execute_reply.started":"2024-04-15T07:34:16.822093Z","shell.execute_reply":"2024-04-15T07:34:16.822110Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<div style=\"color:white;\n            display:fill;\n            border-radius:5px;\n            background-color:#44479b;\n            font-size:110%;\n            font-family:Verdana;\n            letter-spacing:0.5px;\n            text-align:center\">\n  <h1 id=\"Loading Data\" style=\"padding: 10px; color:white; text-align:center;\">\n   8. Conclusion\n    <a class=\"anchor-link\" href=\"https://www.kaggle.com/amirhoseinsedaghati/image-classification-using-vgg16-and-resnet50#Conclusion\">¶</a>\n  </h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px;\n           text-align:left\">\n    \nUsing ResNet50, we can acheive a reliable and accurate image classifier with respect to VGG16 because it is a modern naural network and avoid vanishing gradient using skip connections commonly encountered in deep neural networks.<br>\n\n<br>If you find this notebook valuable, your support by giving an upvote would be greatly appreciated. THANK YOU :)\n      \n</div>\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}