{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[],"toc_visible":true},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport torchvision\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nimport torch.optim.lr_scheduler as lr_scheduler\nfrom torch.optim.lr_scheduler import _LRScheduler\nimport torch.utils.data as data\n\nimport torchvision.transforms as transforms\nimport torchvision.datasets as datasets\nimport torchvision.models as models\n\nfrom sklearn import decomposition\nfrom sklearn import manifold\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.metrics import ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom PIL import Image\n\nimport copy\nfrom collections import namedtuple\nimport os\nimport random\nimport shutil\nimport time\nimport pandas as pd\nfrom skimage import io, transform\n\nfrom torchvision import datasets\nfrom torchvision.datasets import ImageFolder\nfrom torch.utils.data import DataLoader\nfrom torch.utils.data import Dataset\n\nfrom tempfile import TemporaryDirectory\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:32.047621Z","iopub.execute_input":"2024-04-09T04:38:32.048555Z","iopub.status.idle":"2024-04-09T04:38:41.355403Z","shell.execute_reply.started":"2024-04-09T04:38:32.048517Z","shell.execute_reply":"2024-04-09T04:38:41.354554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:50.145292Z","iopub.execute_input":"2024-04-09T04:38:50.145791Z","iopub.status.idle":"2024-04-09T04:38:50.177687Z","shell.execute_reply.started":"2024-04-09T04:38:50.145755Z","shell.execute_reply":"2024-04-09T04:38:50.176685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:50.680313Z","iopub.execute_input":"2024-04-09T04:38:50.680681Z","iopub.status.idle":"2024-04-09T04:38:50.687720Z","shell.execute_reply.started":"2024-04-09T04:38:50.680652Z","shell.execute_reply":"2024-04-09T04:38:50.686792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 1234\n\nrandom.seed(SEED)\nnp.random.seed(SEED)\ntorch.manual_seed(SEED)\ntorch.cuda.manual_seed(SEED)\ntorch.backends.cudnn.deterministic = True","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:53.287696Z","iopub.execute_input":"2024-04-09T04:38:53.288101Z","iopub.status.idle":"2024-04-09T04:38:53.296146Z","shell.execute_reply.started":"2024-04-09T04:38:53.288071Z","shell.execute_reply":"2024-04-09T04:38:53.295259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_map = {\n    0: \"affenpinscher\",\n    1: \"afghan_hound\",\n    2: \"african_hunting_dog\",\n    3: \"airedale\",\n    4: \"american_staffordshire_terrier\",\n    5: \"appenzeller\",\n    6: \"australian_terrier\",\n    7: \"basenji\",\n    8: \"basset\",\n    9: \"beagle\",\n    10: \"bedlington_terrier\",\n    11: \"bernese_mountain_dog\",\n    12: \"black-and-tan_coonhound\",\n    13: \"blenheim_spaniel\",\n    14: \"bloodhound\",\n    15: \"bluetick\",\n    16: \"border_collie\",\n    17: \"border_terrier\",\n    18: \"borzoi\",\n    19: \"boston_bull\",\n    20: \"bouvier_des_flandres\",\n    21: \"boxer\",\n    22: \"brabancon_griffon\",\n    23: \"briard\",\n    24: \"brittany_spaniel\",\n    25: \"bull_mastiff\",\n    26: \"cairn\",\n    27: \"cardigan\",\n    28: \"chesapeake_bay_retriever\",\n    29: \"chihuahua\",\n    30: \"chow\",\n    31: \"clumber\",\n    32: \"cocker_spaniel\",\n    33: \"collie\",\n    34: \"curly-coated_retriever\",\n    35: \"dandie_dinmont\",\n    36: \"dhole\",\n    37: \"dingo\",\n    38: \"doberman\",\n    39: \"english_foxhound\",\n    40: \"english_setter\",\n    41: \"english_springer\",\n    42: \"entlebucher\",\n    43: \"eskimo_dog\",\n    44: \"flat-coated_retriever\",\n    45: \"french_bulldog\",\n    46: \"german_shepherd\",\n    47: \"german_short-haired_pointer\",\n    48: \"giant_schnauzer\",\n    49: \"golden_retriever\",\n    50: \"gordon_setter\",\n    51: \"great_dane\",\n    52: \"great_pyrenees\",\n    53: \"greater_swiss_mountain_dog\",\n    54: \"groenendael\",\n    55: \"ibizan_hound\",\n    56: \"irish_setter\",\n    57: \"irish_terrier\",\n    58: \"irish_water_spaniel\",\n    59: \"irish_wolfhound\",\n    60: \"italian_greyhound\",\n    61: \"japanese_spaniel\",\n    62: \"keeshond\",\n    63: \"kelpie\",\n    64: \"kerry_blue_terrier\",\n    65: \"komondor\",\n    66: \"kuvasz\",\n    67: \"labrador_retriever\",\n    68: \"lakeland_terrier\",\n    69: \"leonberg\",\n    70: \"lhasa\",\n    71: \"malamute\",\n    72: \"malinois\",\n    73: \"maltese_dog\",\n    74: \"mexican_hairless\",\n    75: \"miniature_pinscher\",\n    76: \"miniature_poodle\",\n    77: \"miniature_schnauzer\",\n    78: \"newfoundland\",\n    79: \"norfolk_terrier\",\n    80: \"norwegian_elkhound\",\n    81: \"norwich_terrier\",\n    82: \"old_english_sheepdog\",\n    83: \"otterhound\",\n    84: \"papillon\",\n    85: \"pekinese\",\n    86: \"pembroke\",\n    87: \"pomeranian\",\n    88: \"pug\",\n    89: \"redbone\",\n    90: \"rhodesian_ridgeback\",\n    91: \"rottweiler\",\n    92: \"saint_bernard\",\n    93: \"saluki\",\n    94: \"samoyed\",\n    95: \"schipperke\",\n    96: \"scotch_terrier\",\n    97: \"scottish_deerhound\",\n    98: \"sealyham_terrier\",\n    99: \"shetland_sheepdog\",\n    100: \"shih-tzu\",\n    101: \"siberian_husky\",\n    102: \"silky_terrier\",\n    103: \"soft-coated_wheaten_terrier\",\n    104: \"staffordshire_bullterrier\",\n    105: \"standard_poodle\",\n    106: \"standard_schnauzer\",\n    107: \"sussex_spaniel\",\n    108: \"tibetan_mastiff\",\n    109: \"tibetan_terrier\",\n    110: \"toy_poodle\",\n    111: \"toy_terrier\",\n    112: \"vizsla\",\n    113: \"walker_hound\",\n    114: \"weimaraner\",\n    115: \"welsh_springer_spaniel\",\n    116: \"west_highland_white_terrier\",\n    117: \"whippet\",\n    118: \"wire-haired_fox_terrier\",\n    119: \"yorkshire_terrier\"\n}\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:54.070545Z","iopub.execute_input":"2024-04-09T04:38:54.071177Z","iopub.status.idle":"2024-04-09T04:38:54.085878Z","shell.execute_reply.started":"2024-04-09T04:38:54.071148Z","shell.execute_reply":"2024-04-09T04:38:54.084930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create reverse mapping dictionary\nreverse_labels_map = {v: k for k, v in labels_map.items()}\n\n# Example CSV label names\ncsv_labels = [\"pug\", \"boxer\", \"beagle\", \"pomeranian\", \"labrador_retriever\"]\n\n# Convert CSV label names to numerical labels\nnumerical_labels = [reverse_labels_map[label_name] for label_name in csv_labels]\nprint(numerical_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:55.291183Z","iopub.execute_input":"2024-04-09T04:38:55.291759Z","iopub.status.idle":"2024-04-09T04:38:55.297967Z","shell.execute_reply.started":"2024-04-09T04:38:55.291727Z","shell.execute_reply":"2024-04-09T04:38:55.297032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_name = None\nclass_idx = None\n\nlabels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:57.645448Z","iopub.execute_input":"2024-04-09T04:38:57.646071Z","iopub.status.idle":"2024-04-09T04:38:57.673779Z","shell.execute_reply.started":"2024-04-09T04:38:57.646041Z","shell.execute_reply":"2024-04-09T04:38:57.672870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[\"class_idx\"] = labels[\"breed\"].map(reverse_labels_map)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:57.982599Z","iopub.execute_input":"2024-04-09T04:38:57.982985Z","iopub.status.idle":"2024-04-09T04:38:57.995456Z","shell.execute_reply.started":"2024-04-09T04:38:57.982956Z","shell.execute_reply":"2024-04-09T04:38:57.994570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = labels.drop(columns=[\"breed\"])","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:38:58.543960Z","iopub.execute_input":"2024-04-09T04:38:58.544672Z","iopub.status.idle":"2024-04-09T04:38:58.552843Z","shell.execute_reply.started":"2024-04-09T04:38:58.544642Z","shell.execute_reply":"2024-04-09T04:38:58.551703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:01.119271Z","iopub.execute_input":"2024-04-09T04:39:01.120020Z","iopub.status.idle":"2024-04-09T04:39:01.135077Z","shell.execute_reply.started":"2024-04-09T04:39:01.119979Z","shell.execute_reply":"2024-04-09T04:39:01.134177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n = 0\nimg_name = labels.iloc[n, 0]\nimg_name = img_name+\".jpg\"\nclass_idx = labels.iloc[n, 1:]\n\nprint('Image name: {}'.format(img_name))\nprint('breed: {}'.format(class_idx))","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:01.766003Z","iopub.execute_input":"2024-04-09T04:39:01.766331Z","iopub.status.idle":"2024-04-09T04:39:01.773377Z","shell.execute_reply.started":"2024-04-09T04:39:01.766307Z","shell.execute_reply":"2024-04-09T04:39:01.772246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_landmarks(image, class_name):\n    \"\"\"Show image with class_name\"\"\"\n    plt.imshow(image)\n    print(class_name)\n    plt.title(labels_map[class_name[\"class_idx\"]])\n\nplt.figure()\nshow_landmarks(io.imread(os.path.join('/kaggle/input/dog-breed-identification/train', img_name)),\n               class_idx)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:03.916792Z","iopub.execute_input":"2024-04-09T04:39:03.917555Z","iopub.status.idle":"2024-04-09T04:39:04.359938Z","shell.execute_reply.started":"2024-04-09T04:39:03.917517Z","shell.execute_reply":"2024-04-09T04:39:04.359030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CustomDataset(Dataset):\n    def __init__(self, root_dir, label_file, transform=None):\n        self.root_dir = root_dir\n        self.transform = transform\n        self.labels = {}\n        self.img_labels = pd.read_csv(label_file)\n        self.img_labels[\"class_idx\"] = self.img_labels[\"breed\"].map(reverse_labels_map)\n        self.img_labels = self.img_labels.drop(columns=[\"breed\"])\n        \n    def __len__(self):\n        return len(self.img_labels)\n\n    def __getitem__(self, idx):\n        img_path = os.path.join(self.root_dir, self.img_labels.iloc[idx, 0])\n        img_path = img_path+\".jpg\"\n        image = Image.open(img_path)\n        label = self.img_labels.iloc[idx, 1]\n        if self.transform:\n            image = self.transform(image)\n\n        return image, label\n\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),  # Resize images to 224x224 (you can adjust the size as needed)\n    transforms.ToTensor(),           # Convert the image to a PyTorch tensor\n    # You can add more transformations here if needed (e.g., normalization)\n])\n\nroot = \"/kaggle/input/dog-breed-identification/train\"\nlabel_file = \"/kaggle/input/dog-breed-identification/labels.csv\"\n\ndataset = CustomDataset(root_dir=root, label_file=label_file, transform=transform)\n\nbatch_size = 64\ndata_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:04.361650Z","iopub.execute_input":"2024-04-09T04:39:04.362073Z","iopub.status.idle":"2024-04-09T04:39:04.391079Z","shell.execute_reply.started":"2024-04-09T04:39:04.362041Z","shell.execute_reply":"2024-04-09T04:39:04.390034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_sizes = len(dataset)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:06.400061Z","iopub.execute_input":"2024-04-09T04:39:06.400413Z","iopub.status.idle":"2024-04-09T04:39:06.405098Z","shell.execute_reply.started":"2024-04-09T04:39:06.400383Z","shell.execute_reply":"2024-04-09T04:39:06.404155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_loader","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:06.728531Z","iopub.execute_input":"2024-04-09T04:39:06.728877Z","iopub.status.idle":"2024-04-09T04:39:06.734959Z","shell.execute_reply.started":"2024-04-09T04:39:06.728850Z","shell.execute_reply":"2024-04-09T04:39:06.733884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display image and label.\ntrain_features, train_labels = next(iter(data_loader))\nprint(f\"Feature batch shape: {train_features.size()}\")\nprint(f\"Labels batch shape: {train_labels.size()}\")\nimg = train_features[0].squeeze()\nlabel = train_labels[0]\nimg_np = img.numpy()\nimg_np = np.transpose(img_np, (1, 2, 0))\nplt.imshow(img_np)\nplt.show()\nprint(f\"Label: {labels_map[int(label)]}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:07.097773Z","iopub.execute_input":"2024-04-09T04:39:07.098454Z","iopub.status.idle":"2024-04-09T04:39:08.132706Z","shell.execute_reply.started":"2024-04-09T04:39:07.098421Z","shell.execute_reply":"2024-04-09T04:39:08.131756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.models import resnet50,resnet101","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:10.237757Z","iopub.execute_input":"2024-04-09T04:39:10.238153Z","iopub.status.idle":"2024-04-09T04:39:10.242914Z","shell.execute_reply.started":"2024-04-09T04:39:10.238121Z","shell.execute_reply":"2024-04-09T04:39:10.241883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model50 = resnet50(weights=\"IMAGENET1K_V1\")","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:10.774651Z","iopub.execute_input":"2024-04-09T04:39:10.775492Z","iopub.status.idle":"2024-04-09T04:39:12.091987Z","shell.execute_reply.started":"2024-04-09T04:39:10.775456Z","shell.execute_reply":"2024-04-09T04:39:12.091052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model101 = resnet101(weights=\"IMAGENET1K_V1\")","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:13.679543Z","iopub.execute_input":"2024-04-09T04:39:13.680137Z","iopub.status.idle":"2024-04-09T04:39:15.810275Z","shell.execute_reply.started":"2024-04-09T04:39:13.680110Z","shell.execute_reply":"2024-04-09T04:39:15.809430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_features = model50.fc.in_features\nmodel50.fc = nn.Linear(num_features, 120)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:15.812170Z","iopub.execute_input":"2024-04-09T04:39:15.812649Z","iopub.status.idle":"2024-04-09T04:39:15.820856Z","shell.execute_reply.started":"2024-04-09T04:39:15.812616Z","shell.execute_reply":"2024-04-09T04:39:15.819871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_features = model101.fc.in_features\nmodel101.fc = nn.Linear(num_features, 120)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:15.822031Z","iopub.execute_input":"2024-04-09T04:39:15.822645Z","iopub.status.idle":"2024-04-09T04:39:15.831487Z","shell.execute_reply.started":"2024-04-09T04:39:15.822615Z","shell.execute_reply":"2024-04-09T04:39:15.830695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model50.eval()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:16.943764Z","iopub.execute_input":"2024-04-09T04:39:16.944179Z","iopub.status.idle":"2024-04-09T04:39:16.954877Z","shell.execute_reply.started":"2024-04-09T04:39:16.944145Z","shell.execute_reply":"2024-04-09T04:39:16.953738Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model101.eval()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:39:17.172634Z","iopub.execute_input":"2024-04-09T04:39:17.173234Z","iopub.status.idle":"2024-04-09T04:39:17.185581Z","shell.execute_reply.started":"2024-04-09T04:39:17.173205Z","shell.execute_reply":"2024-04-09T04:39:17.184559Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(model, criterion, optimizer, scheduler, num_epochs=25):\n    since = time.time()\n    train_data= []\n    acc_data = []\n    # Create a temporary directory to save training checkpoints\n    with TemporaryDirectory() as tempdir:\n        best_model_params_path = os.path.join(tempdir, 'best_model_params.pt')\n\n        torch.save(model.state_dict(), best_model_params_path)\n        best_acc = 0.0\n\n        for epoch in range(num_epochs):\n            print(f'Epoch {epoch}/{num_epochs - 1}')\n            print('-' * 10)\n\n            # Each epoch has a training and validation phase\n            for phase in ['train', 'val']:\n                if phase == 'train':\n                    model.train()  # Set model to training mode\n                else:\n                    model.eval()   # Set model to evaluate mode\n\n                running_loss = 0.0\n                running_corrects = 0\n\n                # Iterate over data.\n                for i,(inputs, labels) in enumerate(data_loader):\n                    inputs = inputs.to(device)\n                    labels = labels.to(device)\n\n                    # zero the parameter gradients\n                    optimizer.zero_grad()\n\n                    # forward\n                    # track history if only in train\n                    with torch.set_grad_enabled(phase == 'train'):\n                        outputs = model(inputs)\n                        _, preds = torch.max(outputs, 1)\n                        loss = criterion(outputs, labels)\n\n                        # backward + optimize only if in training phase\n                        if phase == 'train':\n                            loss.backward()\n                            optimizer.step()\n\n                    # statistics\n                    running_loss += loss.item() * inputs.size(0)\n                    running_corrects += torch.sum(preds == labels.data)\n                if phase == 'train':\n                    scheduler.step()\n\n                epoch_loss = running_loss / dataset_sizes\n                epoch_acc = running_corrects.double() / dataset_sizes\n                train_data.append(epoch_loss)\n                acc_data.append(epoch_acc)\n                print(train_data)\n                print(acc_data)\n\n                print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')\n\n                # deep copy the model\n                if phase == 'val' and epoch_acc > best_acc:\n                    best_acc = epoch_acc\n                    torch.save(model.state_dict(), best_model_params_path)\n\n            print()\n\n        time_elapsed = time.time() - since\n        print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')\n        print(f'Best val Acc: {best_acc:4f}')\n\n        # load best model weights\n        model.load_state_dict(torch.load(best_model_params_path))\n    return model, train_data, acc_data","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:51:00.757319Z","iopub.execute_input":"2024-04-09T04:51:00.758013Z","iopub.status.idle":"2024-04-09T04:51:00.772163Z","shell.execute_reply.started":"2024-04-09T04:51:00.757981Z","shell.execute_reply":"2024-04-09T04:51:00.771242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_model(model, num_images=6):\n    was_training = model.training\n    model.eval()\n    images_so_far = 0\n    fig = plt.figure()\n\n    with torch.no_grad():\n        for i, (inputs, labels) in enumerate(data_loader):\n            inputs = inputs.to(device)\n            labels = labels.to(device)\n\n            outputs = model(inputs)\n            _, preds = torch.max(outputs, 1)\n\n            for j in range(inputs.size()[0]):\n                images_so_far += 1\n                print(labels_map[labels.tolist()[j]])\n                ax = plt.subplot(num_images//2, 2, images_so_far)\n                class_idx_pred = preds[j].tolist()\n                ax.set_title(f'predicted: {labels_map[class_idx_pred]}')\n                print(inputs.cpu().data[j].shape)\n                img_np = inputs.cpu().data[j]\n                img_np = img_np.numpy()\n                img_np = np.transpose(img_np, (1, 2, 0))\n                plt.imshow(img_np)\n\n                if images_so_far == num_images:\n                    model.train(mode=was_training)\n                    return\n        model.train(mode=was_training)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:41:47.969920Z","iopub.execute_input":"2024-04-09T04:41:47.970779Z","iopub.status.idle":"2024-04-09T04:41:47.979731Z","shell.execute_reply.started":"2024-04-09T04:41:47.970749Z","shell.execute_reply":"2024-04-09T04:41:47.978763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model50 = model50.to(device)\n\ncriterion = nn.CrossEntropyLoss()\n\n# Observe that all parameters are being optimized\noptimizer_ft = optim.SGD(model50.parameters(), lr=0.001, momentum=0.9)\n\n# Decay LR by a factor of 0.1 every 7 epochs\nexp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:41:49.351561Z","iopub.execute_input":"2024-04-09T04:41:49.352396Z","iopub.status.idle":"2024-04-09T04:41:49.543278Z","shell.execute_reply.started":"2024-04-09T04:41:49.352363Z","shell.execute_reply":"2024-04-09T04:41:49.542289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model50, train_data, acc_data= train_model(model50, criterion, optimizer_ft, exp_lr_scheduler,\n                       num_epochs=2)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T05:09:17.113573Z","iopub.execute_input":"2024-04-09T05:09:17.114290Z","iopub.status.idle":"2024-04-09T05:16:35.786634Z","shell.execute_reply.started":"2024-04-09T05:09:17.114261Z","shell.execute_reply":"2024-04-09T05:16:35.785661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_data)\nprint(acc_data[1].tolist())","metadata":{"execution":{"iopub.status.busy":"2024-04-09T04:56:30.188228Z","iopub.execute_input":"2024-04-09T04:56:30.188613Z","iopub.status.idle":"2024-04-09T04:56:30.193721Z","shell.execute_reply.started":"2024-04-09T04:56:30.188584Z","shell.execute_reply":"2024-04-09T04:56:30.192817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Prepare data\ni = 0\nloss = []\nacc = []\nval_loss = []\nval_acc = []\nfor i in range(len(train_data)):\n    print(i)\n    if i%2==0:\n        loss.append(train_data[i])\n        acc.append(acc_data[(i)].tolist())\n        val_loss.append(train_data[i+1])\n        val_acc.append(acc_data[i+1].tolist())\n# Create plot\nprint(acc)\nprint(acc_data)\nplt.plot(acc)\n\n# Add labels and title\nplt.xlabel('epochs')\nplt.ylabel('acc')\nplt.title('acc plot')\nplt.show()  # Use plt.savefig('filename.png') to save the plot instead\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T05:29:48.184619Z","iopub.execute_input":"2024-04-09T05:29:48.185275Z","iopub.status.idle":"2024-04-09T05:29:48.383680Z","shell.execute_reply.started":"2024-04-09T05:29:48.185239Z","shell.execute_reply":"2024-04-09T05:29:48.382782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(loss)\n\n# Add labels and title\nplt.xlabel('epochs')\nplt.ylabel('loss')\nplt.title('loss plot')\nplt.show()  # Use plt.savefig('filename.png') to save the plot instead\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T05:30:22.436494Z","iopub.execute_input":"2024-04-09T05:30:22.437341Z","iopub.status.idle":"2024-04-09T05:30:22.639676Z","shell.execute_reply.started":"2024-04-09T05:30:22.437310Z","shell.execute_reply":"2024-04-09T05:30:22.638763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(val_acc)\n\n# Add labels and title\nplt.xlabel('epochs')\nplt.ylabel('val_acc')\nplt.title('val_acc plot')\nplt.show()  # Use plt.savefig('filename.png') to save the plot instead\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T05:30:40.199353Z","iopub.execute_input":"2024-04-09T05:30:40.200050Z","iopub.status.idle":"2024-04-09T05:30:40.474187Z","shell.execute_reply.started":"2024-04-09T05:30:40.200017Z","shell.execute_reply":"2024-04-09T05:30:40.472884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(val_loss)\n\n# Add labels and title\nplt.xlabel('epochs')\nplt.ylabel('val_loss')\nplt.title('val_loss plot')\nplt.show()  # Use plt.savefig('filename.png') to save the plot instead\n","metadata":{"execution":{"iopub.status.busy":"2024-04-09T05:31:01.686572Z","iopub.execute_input":"2024-04-09T05:31:01.687458Z","iopub.status.idle":"2024-04-09T05:31:01.941897Z","shell.execute_reply.started":"2024-04-09T05:31:01.687422Z","shell.execute_reply":"2024-04-09T05:31:01.940968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_model(model50)","metadata":{"execution":{"iopub.status.busy":"2024-04-07T17:44:12.252195Z","iopub.execute_input":"2024-04-07T17:44:12.252617Z","iopub.status.idle":"2024-04-07T17:44:24.255040Z","shell.execute_reply.started":"2024-04-07T17:44:12.252586Z","shell.execute_reply":"2024-04-07T17:44:24.253655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model101 = model101.to(device)\n\ncriterion = nn.CrossEntropyLoss()\n\n# Observe that all parameters are being optimized\noptimizer_ft = optim.SGD(model101.parameters(), lr=0.001, momentum=0.9)\n\n# Decay LR by a factor of 0.1 every 7 epochs\nexp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-04-07T17:45:40.048764Z","iopub.execute_input":"2024-04-07T17:45:40.049312Z","iopub.status.idle":"2024-04-07T17:45:40.068282Z","shell.execute_reply.started":"2024-04-07T17:45:40.049271Z","shell.execute_reply":"2024-04-07T17:45:40.067219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model101= train_model(model101, criterion, optimizer_ft, exp_lr_scheduler,\n                       num_epochs=10)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_model(model101)","metadata":{"execution":{"iopub.status.busy":"2024-04-07T17:45:54.392131Z","iopub.execute_input":"2024-04-07T17:45:54.393467Z","iopub.status.idle":"2024-04-07T17:46:11.714937Z","shell.execute_reply.started":"2024-04-07T17:45:54.393406Z","shell.execute_reply":"2024-04-07T17:46:11.713583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}