{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-21T16:22:01.107356Z","iopub.execute_input":"2023-07-21T16:22:01.107767Z","iopub.status.idle":"2023-07-21T16:22:01.115354Z","shell.execute_reply.started":"2023-07-21T16:22:01.107737Z","shell.execute_reply":"2023-07-21T16:22:01.114346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:22:01.120624Z","iopub.execute_input":"2023-07-21T16:22:01.120901Z","iopub.status.idle":"2023-07-21T16:22:12.98298Z","shell.execute_reply.started":"2023-07-21T16:22:01.120868Z","shell.execute_reply":"2023-07-21T16:22:12.981501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install wandb","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:22:12.985768Z","iopub.execute_input":"2023-07-21T16:22:12.986384Z","iopub.status.idle":"2023-07-21T16:22:25.045849Z","shell.execute_reply.started":"2023-07-21T16:22:12.986341Z","shell.execute_reply":"2023-07-21T16:22:25.0445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport copy\nimport matplotlib.pyplot as plt\n\n###\nfrom PIL import Image\n\n###\nimport torch\n# neural network modules, nn.Linear, nn.Conv2d, loss functions\nimport torch.nn as nn\n# activation functions\nimport torch.nn.functional as F\n# optimizers\nimport torch.optim as optim\n# transformations \nimport torchvision.transforms as transforms\n\nfrom torchvision import models\nimport torchvision\nfrom torch.utils.data import DataLoader, Dataset, Subset\n\n###\nfrom sklearn.model_selection import train_test_split\n\n# device config\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n\n###\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:22:25.047967Z","iopub.execute_input":"2023-07-21T16:22:25.048718Z","iopub.status.idle":"2023-07-21T16:22:25.061081Z","shell.execute_reply.started":"2023-07-21T16:22:25.048674Z","shell.execute_reply":"2023-07-21T16:22:25.059857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport torch\nfrom torch.utils.data import DataLoader, Dataset, ConcatDataset, SubsetRandomSampler\nfrom torchvision.datasets import MNIST\nfrom torchvision import datasets, transforms\nfrom torchvision.models import resnet18\nimport torchvision\nimport torch.nn as nn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nimport cv2\nimport os\nfrom tqdm.notebook import trange, tqdm\nimport albumentations\nfrom timeit import default_timer as timer\nimport glob\nimport py7zr\nfrom sklearn.model_selection import train_test_split\nimport wandb\nfrom torch.nn import functional as F\nimport math\nNO_LABEL = -1\nfrom PIL import Image\nimport itertools\nfrom py7zr import unpack_7zarchive\nimport shutil\nshutil.register_unpack_format('7zip', ['.7z'], unpack_7zarchive)\nimport wandb","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:22:25.066236Z","iopub.execute_input":"2023-07-21T16:22:25.067947Z","iopub.status.idle":"2023-07-21T16:22:25.161734Z","shell.execute_reply.started":"2023-07-21T16:22:25.067911Z","shell.execute_reply":"2023-07-21T16:22:25.15558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"wandb\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:25:09.713718Z","iopub.execute_input":"2023-07-21T16:25:09.71478Z","iopub.status.idle":"2023-07-21T16:25:09.899146Z","shell.execute_reply.started":"2023-07-21T16:25:09.714736Z","shell.execute_reply":"2023-07-21T16:25:09.898056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.login(key= secret_value_0)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:25:09.901303Z","iopub.execute_input":"2023-07-21T16:25:09.901919Z","iopub.status.idle":"2023-07-21T16:25:09.924994Z","shell.execute_reply.started":"2023-07-21T16:25:09.901879Z","shell.execute_reply":"2023-07-21T16:25:09.923575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb.init(\n    # set the wandb project where this run will be logged\n    project=\"mean_teacher\",\n    \n    # track hyperparameters and run metadata\n    config={\n    \"learning_rate\": 0.02,\n    \"architecture\": \"Resnet32x32\",\n    \"dataset\": \"CIFAR-10\",\n    \"epochs\": 15,\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:25:09.925959Z","iopub.execute_input":"2023-07-21T16:25:09.927184Z","iopub.status.idle":"2023-07-21T16:25:47.745427Z","shell.execute_reply.started":"2023-07-21T16:25:09.92715Z","shell.execute_reply":"2023-07-21T16:25:47.744552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.unpack_archive('/kaggle/input/c/cifar-10/train.7z')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:25:47.750509Z","iopub.execute_input":"2023-07-21T16:25:47.753203Z","iopub.status.idle":"2023-07-21T16:26:51.870116Z","shell.execute_reply.started":"2023-07-21T16:25:47.753031Z","shell.execute_reply":"2023-07-21T16:26:51.869174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_label = ['frog', 'truck', 'deer', 'automobile', 'bird', 'horse', 'ship', 'cat', 'dog', 'airplane']\n\nlabel_dict = {}  # Create an empty dictionary\n\nfor index, label in enumerate(temp_label):\n    label_dict[label] = index\n\nprint(label_dict)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:26:51.871706Z","iopub.execute_input":"2023-07-21T16:26:51.872099Z","iopub.status.idle":"2023-07-21T16:26:51.879155Z","shell.execute_reply.started":"2023-07-21T16:26:51.87204Z","shell.execute_reply":"2023-07-21T16:26:51.878119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CifarLoader(Dataset):\n  def __init__(self, root_dir, csv_file = None, transform = None, transform2= None):\n\n    # images directory \n    self.root_dir = root_dir\n    # transformations if any\n    self.transform = transform\n    self.transform2= transform2\n    if csv_file:\n      # read csv file\n      self.df = pd.read_csv(csv_file)\n\n      # {class: lbl}\n      self.annot_dict = {}\n\n      for i, lbl in enumerate(self.df['label'].unique()):\n        self.annot_dict[lbl] = i\n\n      # {lbl: class}\n      self.annot_dict_reversed = {v:k for k,v in self.annot_dict.items()}\n\n      # add a column with decoded labels\n      self.df['encoded'] = self.df['label'].map(self.annot_dict)\n\n    else: \n      # if no csv file provided -> create a df, fill labels with 0's\n      self.df = pd.DataFrame({\n          'id': list(range(1, len(os.listdir(self.root_dir)) + 1)),\n          'label': np.zeros(len(os.listdir(self.root_dir))),\n          'encoded': np.zeros(len(os.listdir(self.root_dir)))\n      })\n\n\n  def __len__(self):\n    # num of records in the df\n    return len(self.df)\n\n  def __getitem__(self, index):\n    # path to the image\n    img_path = os.path.join(self.root_dir, str(self.df.iloc[index, 0]) + '.png')\n    # read image\n    #image = np.array(Image.open(img_path).convert('RGB'))\n    image= cv2.imread(img_path)\n    image= cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n    # get label\n    y_label = torch.tensor(int(self.df.iloc[index, 2]))\n\n    # apply transformations\n    image1= self.transform2(image= image)\n    if self.transform:\n        image1 = self.transform(image)\n        image2= self.transform(image1)\n        \n    \n    \n    \n    return (image1,image2, y_label)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:26:51.881301Z","iopub.execute_input":"2023-07-21T16:26:51.881643Z","iopub.status.idle":"2023-07-21T16:26:51.89519Z","shell.execute_reply.started":"2023-07-21T16:26:51.881612Z","shell.execute_reply":"2023-07-21T16:26:51.894332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{}},{"cell_type":"code","source":"# transform tensors to normalized range form [0, 1] to [-1, 1]\nmean = np.array([0.5, 0.5, 0.5])\nstd = np.array([0.5, 0.5, 0.5])\n\ntr = transforms.Compose([\n        transforms.ToPILImage(),\n        transforms.Resize((32, 32)),  \n        transforms.ToTensor(),        \n        transforms.Normalize(mean, std)\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:26:51.896355Z","iopub.execute_input":"2023-07-21T16:26:51.896961Z","iopub.status.idle":"2023-07-21T16:26:51.911559Z","shell.execute_reply.started":"2023-07-21T16:26:51.896929Z","shell.execute_reply":"2023-07-21T16:26:51.910434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_transforms(image_size=(32, 32)):\n\n    transforms_student = albumentations.Compose([\n        albumentations.HorizontalFlip(p=0.5),\n        albumentations.ImageCompression(quality_lower=99, quality_upper=100),\n        albumentations.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=10, border_mode=0, p=0.7),\n        # albumentations.Resize(image_size, image_size),\n        # albumentations.Cutout(max_h_size=int(image_size * 0.4), max_w_size=int(image_size * 0.4), num_holes=1, p=0.5),\n        albumentations.Normalize()\n    ])\n\n    transforms_val = albumentations.Compose([\n        albumentations.HorizontalFlip(p=0.5),\n        albumentations.ImageCompression(quality_lower=99, quality_upper=100),\n        albumentations.ShiftScaleRotate(shift_limit=0.2, scale_limit=0.2, rotate_limit=10, border_mode=0, p=0.7),\n        # albumentations.Resize(image_size, image_size),\n        # albumentations.Cutout(max_h_size=int(image_size * 0.4), max_w_size=int(image_size * 0.4), num_holes=1, p=0.5),\n        albumentations.Normalize()\n    ])\n    \n\n    return transforms_student, transforms_val","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:26:51.91335Z","iopub.execute_input":"2023-07-21T16:26:51.91369Z","iopub.status.idle":"2023-07-21T16:26:51.92899Z","shell.execute_reply.started":"2023-07-21T16:26:51.91366Z","shell.execute_reply":"2023-07-21T16:26:51.92793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms_train, transforms_val = get_transforms(image_size=(32, 32))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:26:51.930755Z","iopub.execute_input":"2023-07-21T16:26:51.93116Z","iopub.status.idle":"2023-07-21T16:26:51.943729Z","shell.execute_reply.started":"2023-07-21T16:26:51.931129Z","shell.execute_reply":"2023-07-21T16:26:51.942827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = CifarLoader(csv_file = '/kaggle/input/c/cifar-10/trainLabels.csv', \n                      root_dir = './train',\n                      transform = tr,\n                     transform2= transforms_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:26:51.9488Z","iopub.execute_input":"2023-07-21T16:26:51.949222Z","iopub.status.idle":"2023-07-21T16:26:52.011949Z","shell.execute_reply.started":"2023-07-21T16:26:51.949198Z","shell.execute_reply":"2023-07-21T16:26:52.01109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indexes = list(range(len(dataset)))\n\ntrain_indexes, val_indexes = train_test_split(indexes, test_size=0.2)\n\ndata = {}\ndata['train'] = Subset(dataset, train_indexes)\ndata['val'] = Subset(dataset, val_indexes)\n\ndataloader_train = DataLoader(dataset = data['train'], batch_size = 128, shuffle=False)\ndataloader_val = DataLoader(dataset = data['val'], batch_size = 128\n                            , shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:51.198161Z","iopub.execute_input":"2023-07-21T16:44:51.198528Z","iopub.status.idle":"2023-07-21T16:44:51.222398Z","shell.execute_reply.started":"2023-07-21T16:44:51.198497Z","shell.execute_reply":"2023-07-21T16:44:51.22115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def images_show(images, lbls=None):\n  # show only 4 first images from each batch\n  for i in range(4):\n    plt.subplot(1, 4, i + 1)\n    # convert to numpy\n    img = images[i].numpy().transpose((1, 2, 0))\n    # unnormalize\n    img = img * std + mean \n    img = np.clip(img, a_min=0, a_max=1)\n    plt.imshow(img)\n \n    if lbls != None:\n      # decode labels using reversed annotation dictionary\n      plt.title(dataset.annot_dict_reversed[lbls[i].item()])\n    plt.axis('off')\n  plt.tight_layout(pad=0.5)\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:54.849562Z","iopub.execute_input":"2023-07-21T16:44:54.849934Z","iopub.status.idle":"2023-07-21T16:44:54.858233Z","shell.execute_reply.started":"2023-07-21T16:44:54.849902Z","shell.execute_reply":"2023-07-21T16:44:54.857243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MODEL","metadata":{}},{"cell_type":"code","source":"# class ResNet224x224(nn.Module):\n#     def __init__(self, block, layers, channels, groups=1, num_classes=1000, downsample='basic'):\n#         super().__init__()\n#         assert len(layers) == 4\n#         self.downsample_mode = downsample\n#         self.inplanes = 64\n#         self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,\n#                                bias=False)\n#         self.bn1 = nn.BatchNorm2d(self.inplanes)\n#         self.relu = nn.ReLU(inplace=True)\n#         self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)\n#         self.layer1 = self._make_layer(block, channels, groups, layers[0])\n#         self.layer2 = self._make_layer(\n#             block, channels * 2, groups, layers[1], stride=2)\n#         self.layer3 = self._make_layer(\n#             block, channels * 4, groups, layers[2], stride=2)\n#         self.layer4 = self._make_layer(\n#             block, channels * 8, groups, layers[3], stride=2)\n#         self.avgpool = nn.AvgPool2d(7)\n#         self.fc1 = nn.Linear(block.out_channels(\n#             channels * 8, groups), num_classes)\n#         self.fc2 = nn.Linear(block.out_channels(\n#             channels * 8, groups), num_classes)\n\n#         for m in self.modules():\n#             if isinstance(m, nn.Conv2d):\n#                 n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels\n#                 m.weight.data.normal_(0, math.sqrt(2. / n))\n#             elif isinstance(m, nn.BatchNorm2d):\n#                 m.weight.data.fill_(1)\n#                 m.bias.data.zero_()\n\n#     def _make_layer(self, block, planes, groups, blocks, stride=1):\n#         downsample = None\n#         if stride != 1 or self.inplanes != block.out_channels(planes, groups):\n#             if self.downsample_mode == 'basic' or stride == 1:\n#                 downsample = nn.Sequential(\n#                     nn.Conv2d(self.inplanes, block.out_channels(planes, groups),\n#                               kernel_size=1, stride=stride, bias=False),\n#                     nn.BatchNorm2d(block.out_channels(planes, groups)),\n#                 )\n#             elif self.downsample_mode == 'shift_conv':\n#                 downsample = ShiftConvDownsample(in_channels=self.inplanes,\n#                                                  out_channels=block.out_channels(planes, groups))\n#             else:\n#                 assert False\n\n#         layers = []\n#         layers.append(block(self.inplanes, planes, groups, stride, downsample))\n#         self.inplanes = block.out_channels(planes, groups)\n#         for i in range(1, blocks):\n#             layers.append(block(self.inplanes, planes, groups))\n\n#         return nn.Sequential(*layers)\n\n#     def forward(self, x):\n#         x = self.conv1(x)\n#         x = self.bn1(x)\n#         x = self.relu(x)\n#         x = self.maxpool(x)\n#         x = self.layer1(x)\n#         x = self.layer2(x)\n#         x = self.layer3(x)\n#         x = self.layer4(x)\n#         x = self.avgpool(x)\n#         x = x.view(x.size(0), -1)\n#         return self.fc1(x),self.fc2(x)\n    \n# class BottleneckBlock(nn.Module):\n#     @classmethod\n#     def out_channels(cls, planes, groups):\n#         if groups > 1:\n#             return 2 * planes\n#         else:\n#             return 4 * planes\n\n#     def __init__(self, inplanes, planes, groups, stride=1, downsample=None):\n#         super().__init__()\n#         self.relu = nn.ReLU(inplace=True)\n\n#         self.conv_a1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)\n#         self.bn_a1 = nn.BatchNorm2d(planes)\n#         self.conv_a2 = nn.Conv2d(\n#             planes, planes, kernel_size=3, stride=stride, padding=1, bias=False, groups=groups)\n#         self.bn_a2 = nn.BatchNorm2d(planes)\n#         self.conv_a3 = nn.Conv2d(planes, self.out_channels(\n#             planes, groups), kernel_size=1, bias=False)\n#         self.bn_a3 = nn.BatchNorm2d(self.out_channels(planes, groups))\n\n#         self.downsample = downsample\n#         self.stride = stride\n\n#     def forward(self, x):\n#         a, residual = x, x\n\n#         a = self.conv_a1(a)\n#         a = self.bn_a1(a)\n#         a = self.relu(a)\n#         a = self.conv_a2(a)\n#         a = self.bn_a2(a)\n#         a = self.relu(a)\n#         a = self.conv_a3(a)\n#         a = self.bn_a3(a)\n\n#         if self.downsample is not None:\n#             residual = self.downsample(residual)\n\n#         return self.relu(residual + a)\n\n# def get_model(is_mean_teacher=False,device=None):\n#     model=ResNet224x224(BottleneckBlock,\n#                           layers=[3, 8, 36, 3],\n#                           channels=32 * 4,\n#                           groups=32,\n#                           downsample='basic')\n#     model= nn.DataParallel(model)\n#     model = model.to(device)\n    \n#     # Detach params for Exponential Moving Average Model (aka the Mean Teacher).\n#     # We'll manually update these params instead of using backprop.\n#     if is_mean_teacher:\n#         for param in model.parameters():\n#             param.detach_()\n#     return model","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:54.862664Z","iopub.execute_input":"2023-07-21T16:44:54.86345Z","iopub.status.idle":"2023-07-21T16:44:54.87316Z","shell.execute_reply.started":"2023-07-21T16:44:54.863385Z","shell.execute_reply":"2023-07-21T16:44:54.872122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.autograd import Variable, Function","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:54.875057Z","iopub.execute_input":"2023-07-21T16:44:54.875794Z","iopub.status.idle":"2023-07-21T16:44:54.888036Z","shell.execute_reply.started":"2023-07-21T16:44:54.875713Z","shell.execute_reply":"2023-07-21T16:44:54.88712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet32x32(nn.Module):\n    def __init__(self, block, layers, channels, groups=1, num_classes=1000, downsample='basic'):\n        super().__init__()\n        assert len(layers) == 3\n        self.downsample_mode = downsample\n        self.inplanes = 16\n        self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1,\n                               padding=1, bias=False)\n        self.layer1 = self._make_layer(block, channels, groups, layers[0])\n        self.layer2 = self._make_layer(\n            block, channels * 2, groups, layers[1], stride=2)\n        self.layer3 = self._make_layer(\n            block, channels * 4, groups, layers[2], stride=2)\n        self.avgpool = nn.AvgPool2d(8)\n        self.fc1 = nn.Linear(block.out_channels(\n            channels * 4, groups), num_classes)\n        self.fc2 = nn.Linear(block.out_channels(\n            channels * 4, groups), num_classes)\n\n        for m in self.modules():\n            if isinstance(m, nn.Conv2d):\n                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels\n                m.weight.data.normal_(0, math.sqrt(2. / n))\n            elif isinstance(m, nn.BatchNorm2d):\n                m.weight.data.fill_(1)\n                m.bias.data.zero_()\n\n    def _make_layer(self, block, planes, groups, blocks, stride=1):\n        downsample = None\n        if stride != 1 or self.inplanes != block.out_channels(planes, groups):\n            if self.downsample_mode == 'basic' or stride == 1:\n                downsample = nn.Sequential(\n                    nn.Conv2d(self.inplanes, block.out_channels(planes, groups),\n                              kernel_size=1, stride=stride, bias=False),\n                    nn.BatchNorm2d(block.out_channels(planes, groups)),\n                )\n            elif self.downsample_mode == 'shift_conv':\n                downsample = ShiftConvDownsample(in_channels=self.inplanes,\n                                                 out_channels=block.out_channels(planes, groups))\n            else:\n                assert False\n\n        layers = []\n        layers.append(block(self.inplanes, planes, groups, stride, downsample))\n        self.inplanes = block.out_channels(planes, groups)\n        for i in range(1, blocks):\n            layers.append(block(self.inplanes, planes, groups))\n\n        return nn.Sequential(*layers)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.layer1(x)\n        x = self.layer2(x)\n        x = self.layer3(x)\n        x = self.avgpool(x)\n        x = x.view(x.size(0), -1)\n        return self.fc1(x), self.fc2(x)\n\n\ndef conv3x3(in_planes, out_planes, stride=1):\n    \"3x3 convolution with padding\"\n    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,\n                     padding=1, bias=False)\n\nclass ShakeShakeBlock(nn.Module):\n    @classmethod\n    def out_channels(cls, planes, groups):\n        assert groups == 1\n        return planes\n\n    def __init__(self, inplanes, planes, groups, stride=1, downsample=None):\n        super().__init__()\n        assert groups == 1\n        self.conv_a1 = conv3x3(inplanes, planes, stride)\n        self.bn_a1 = nn.BatchNorm2d(planes)\n        self.conv_a2 = conv3x3(planes, planes)\n        self.bn_a2 = nn.BatchNorm2d(planes)\n\n        self.conv_b1 = conv3x3(inplanes, planes, stride)\n        self.bn_b1 = nn.BatchNorm2d(planes)\n        self.conv_b2 = conv3x3(planes, planes)\n        self.bn_b2 = nn.BatchNorm2d(planes)\n\n        self.downsample = downsample\n        self.stride = stride\n\n    def forward(self, x):\n        a, b, residual = x, x, x\n\n        a = F.relu(a, inplace=False)\n        a = self.conv_a1(a)\n        a = self.bn_a1(a)\n        a = F.relu(a, inplace=True)\n        a = self.conv_a2(a)\n        a = self.bn_a2(a)\n\n        b = F.relu(b, inplace=False)\n        b = self.conv_b1(b)\n        b = self.bn_b1(b)\n        b = F.relu(b, inplace=True)\n        b = self.conv_b2(b)\n        b = self.bn_b2(b)\n\n        ab = shake(a, b, training=self.training)\n\n        if self.downsample is not None:\n            residual = self.downsample(x)\n\n        return residual + ab\nclass Shake(Function):\n    @classmethod\n    def forward(cls, ctx, inp1, inp2, training):\n        assert inp1.size() == inp2.size()\n        gate_size = [inp1.size()[0], *itertools.repeat(1, inp1.dim() - 1)]\n        gate = inp1.new(*gate_size)\n        if training:\n            gate.uniform_(0, 1)\n        else:\n            gate.fill_(0.5)\n        return inp1 * gate + inp2 * (1. - gate)\n\n    @classmethod\n    def backward(cls, ctx, grad_output):\n        grad_inp1 = grad_inp2 = grad_training = None\n        gate_size = [grad_output.size()[0], *itertools.repeat(1,\n                                                              grad_output.dim() - 1)]\n        gate = Variable(grad_output.data.new(*gate_size).uniform_(0, 1))\n        if ctx.needs_input_grad[0]:\n            grad_inp1 = grad_output * gate\n        if ctx.needs_input_grad[1]:\n            grad_inp2 = grad_output * (1 - gate)\n        assert not ctx.needs_input_grad[2]\n        return grad_inp1, grad_inp2, grad_training\n\n\ndef shake(inp1, inp2, training=False):\n    return Shake.apply(inp1, inp2, training)\n\n\nclass ShiftConvDownsample(nn.Module):\n    def __init__(self, in_channels, out_channels):\n        super().__init__()\n        self.relu = nn.ReLU(inplace=True)\n        self.conv = nn.Conv2d(in_channels=2 * in_channels,\n                              out_channels=out_channels,\n                              kernel_size=1,\n                              groups=2)\n        self.bn = nn.BatchNorm2d(out_channels)\n\n    def forward(self, x):\n        x = torch.cat((x[:, :, 0::2, 0::2],\n                       x[:, :, 1::2, 1::2]), dim=1)\n        x = self.relu(x)\n        x = self.conv(x)\n        x = self.bn(x)\n        return x\ndef get_model(is_mean_teacher=False,device=None):\n    model=ResNet32x32(ShakeShakeBlock,\n                          layers=[4, 4, 4],\n                            channels=96,\n                            downsample='shift_conv')\n    model= nn.DataParallel(model)\n    model = model.to(device)\n\n    # Detach params for Exponential Moving Average Model (aka the Mean Teacher).\n    # We'll manually update these params instead of using backprop.\n    if is_mean_teacher:\n        for param in model.parameters():\n            param.detach_()\n    return model\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:55.016913Z","iopub.execute_input":"2023-07-21T16:44:55.01767Z","iopub.status.idle":"2023-07-21T16:44:55.056861Z","shell.execute_reply.started":"2023-07-21T16:44:55.017624Z","shell.execute_reply":"2023-07-21T16:44:55.055808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loss function","metadata":{}},{"cell_type":"markdown","source":"        # Costs\n        'consistency_type': 'mse',\n        'consistency_rampup': 5,\n        'consistency': 100.0,\n        'logit_distance_cost': .01,\n        'weight_decay': 2e-4,\n\n        # Optimization\n        'lr_rampup': 0,\n        'base_lr': 0.05,\n        'nesterov': True,","metadata":{}},{"cell_type":"code","source":"def softmax_mse_loss(input_logits, target_logits):\n    \n    assert input_logits.size() == target_logits.size()\n    input_softmax = F.softmax(input_logits, dim=1)\n    target_softmax = F.softmax(target_logits, dim=1)\n    num_classes = input_logits.size()[1]\n    return F.mse_loss(input_softmax, target_softmax, size_average=False) / num_classes\n\n\ndef softmax_kl_loss(input_logits, target_logits):\n    \n    assert input_logits.size() == target_logits.size()\n    input_log_softmax = F.log_softmax(input_logits, dim=1)\n    target_softmax = F.softmax(target_logits, dim=1)\n    return F.kl_div(input_log_softmax, target_softmax, size_average=False)\n\n\ndef symmetric_mse_loss(input1, input2):\n    assert input1.size() == input2.size()\n    num_classes = input1.size()[1]\n    return torch.sum((input1 - input2)**2) / num_classes\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:55.059306Z","iopub.execute_input":"2023-07-21T16:44:55.059674Z","iopub.status.idle":"2023-07-21T16:44:55.072993Z","shell.execute_reply.started":"2023-07-21T16:44:55.059642Z","shell.execute_reply":"2023-07-21T16:44:55.071993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sigmoid_rampup(current, rampup_length):\n  \n    if rampup_length == 0:\n        return 1.0\n    else:\n        current = np.clip(current, 0.0, rampup_length)\n        phase = 1.0 - current / rampup_length\n        return float(np.exp(-5.0 * phase * phase))\n\n\ndef linear_rampup(current, rampup_length):\n   \n    assert current >= 0 and rampup_length >= 0\n    if current >= rampup_length:\n        return 1.0\n    else:\n        return current / rampup_length\n\n\ndef cosine_rampdown(current, rampdown_length):\n   \n    assert 0 <= current <= rampdown_length\n    return float(.5 * (np.cos(np.pi * current / rampdown_length) + 1))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:55.074202Z","iopub.execute_input":"2023-07-21T16:44:55.074595Z","iopub.status.idle":"2023-07-21T16:44:55.087606Z","shell.execute_reply.started":"2023-07-21T16:44:55.074564Z","shell.execute_reply":"2023-07-21T16:44:55.086681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_current_consistency_weight(epoch):\n    return 100.0 * sigmoid_rampup(epoch, 5)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:55.089128Z","iopub.execute_input":"2023-07-21T16:44:55.089832Z","iopub.status.idle":"2023-07-21T16:44:55.099374Z","shell.execute_reply.started":"2023-07-21T16:44:55.089798Z","shell.execute_reply":"2023-07-21T16:44:55.09826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"def update_teacher_params(student, teacher, alpha, global_step):\n    # Use the true average until the exponential average is more correct\n    alpha = min(1 - 1 / (global_step + 1), alpha)\n    for ema_param, param in zip(teacher.parameters(), student.parameters()):\n        ema_param.data.mul_(alpha).add_(1 - alpha, param.data)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:55.102697Z","iopub.execute_input":"2023-07-21T16:44:55.103131Z","iopub.status.idle":"2023-07-21T16:44:55.111606Z","shell.execute_reply.started":"2023-07-21T16:44:55.103099Z","shell.execute_reply":"2023-07-21T16:44:55.110622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(train_loader,student, teacher, optimizer, device, epoch):\n    \n    class_criterion = nn.CrossEntropyLoss(size_average=False, ignore_index=NO_LABEL).cuda()\n    consistency_criterion = softmax_mse_loss\n    student.train()\n    teacher.train()\n    running_loss=0\n    global_step = 0\n    for i, (images1, images2,labels) in tqdm(enumerate(train_loader),total=len(train_loader)):\n        images1 = images1.to(device)\n        images2= images2.to(device)\n        \n        images1= images1.float()\n        images2= images2.float()\n        labels= labels.float()\n        labels = labels.type(torch.LongTensor)\n        labels = labels.to(device)\n         #meta_images = images + torch.rand(images.size()).to(device) * 1.0 + 0.1 # augment gaussian\n            \n        # forward pass\n        student_pred,_ = student(images1)\n        teacher_pred,_= teacher(images2)\n        \n        student_classif, student_consistency = student_pred, student_pred\n#       \n        \n        student_class_loss = class_criterion(student_classif, labels)/(len(images1))\n        consistency_weight = get_current_consistency_weight(epoch)\n        \n        consistency_loss = consistency_weight * consistency_criterion(student_consistency, teacher_pred) / (len(images1))\n        loss = student_class_loss + consistency_loss\n        running_loss += loss.item()\n\n        # backward and optimizer\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        global_step += 1\n        update_teacher_params(student, teacher, 0.995, global_step)\n    epoch_loss = running_loss / len(train_loader)\n\n    return loss, student_class_loss, consistency_loss, consistency_weight, epoch_loss","metadata":{"execution":{"iopub.status.busy":"2023-07-21T16:44:55.113277Z","iopub.execute_input":"2023-07-21T16:44:55.113646Z","iopub.status.idle":"2023-07-21T16:44:55.126712Z","shell.execute_reply.started":"2023-07-21T16:44:55.113605Z","shell.execute_reply":"2023-07-21T16:44:55.125747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def validate(eval_loader, model, epoch,device):\n    class_criterion = nn.CrossEntropyLoss(size_average=False, ignore_index=NO_LABEL).cuda()\n    \n\n    # switch to evaluate mode\n    model.eval()\n  \n    running_loss=0\n    for i, (images1,images2, labels) in enumerate(eval_loader):\n        images1 = images1.to(device)\n      \n        \n        images1= images1.float()\n       \n        labels= labels.float()\n        labels = labels.type(torch.LongTensor)\n        labels = labels.to(device)\n        model_pred,_ = model(images1)\n        valid_loss = class_criterion(model_pred, labels)/(len(images1))\n        running_loss += valid_loss.item()\n    epoch_loss = running_loss / len(eval_loader)\n    wandb.log({\"epoch_loss\": epoch_loss})\n       \n    return model,epoch_loss","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:43:09.403275Z","iopub.execute_input":"2023-07-21T17:43:09.403655Z","iopub.status.idle":"2023-07-21T17:43:09.411901Z","shell.execute_reply.started":"2023-07-21T17:43:09.403623Z","shell.execute_reply":"2023-07-21T17:43:09.410971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_accuracy(model, data_loader, device):\n    correct = 0\n    total = 0\n\n    with torch.no_grad():\n        model.eval()\n        for images1, images2,labels in data_loader:\n            images1 = images1.to(device)\n         \n            images1= images1.float()\n            labels= labels.float()\n            labels = labels.type(torch.LongTensor)\n            labels = labels.to(device)\n            outputs ,_= model(images1)\n            _, predicted = torch.max(outputs.data, 1)\n\n            total += labels.size(0)\n            correct += (predicted == labels).sum().item()\n\n    return 100*(correct/total)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:11:00.912692Z","iopub.execute_input":"2023-07-21T17:11:00.913056Z","iopub.status.idle":"2023-07-21T17:11:00.920675Z","shell.execute_reply.started":"2023-07-21T17:11:00.913024Z","shell.execute_reply":"2023-07-21T17:11:00.91956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_losses(train_acc, valid_acc, train_loss, valid_loss):\n    # change the style of the plots to seaborn\n    plt.style.use('seaborn')\n\n    train_acc = np.array(train_acc)\n    valid_acc = np.array(valid_acc)\n\n    fig, (ax1, ax2) = plt.subplots(1, 2)\n\n    ax1.plot(train_acc, color=\"blue\", label=\"Train_acc\")\n    ax1.plot(valid_acc, color=\"red\", label=\"Validation_acc\")\n    ax1.set(title=\"Acc over epochs\",\n            xlabel=\"Epoch\",\n            ylabel=\"Acc\")\n    ax1.legend()\n\n    ax2.plot(train_loss, color=\"blue\", label=\"Train_loss\")\n    ax2.plot(valid_loss, color=\"red\", label=\"Validation_loss\")\n    ax2.set(title=\"loss over epochs\",\n            xlabel=\"Epoch\",\n            ylabel=\"Loss\")\n    ax2.legend()\n\n    fig.show()\n\n    # change the plot style to default\n    plt.style.use('default')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:11:00.926993Z","iopub.execute_input":"2023-07-21T17:11:00.927359Z","iopub.status.idle":"2023-07-21T17:11:00.936707Z","shell.execute_reply.started":"2023-07-21T17:11:00.927324Z","shell.execute_reply":"2023-07-21T17:11:00.93521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def training_loop(epochs,device,train_loader,valid_loader):\n\n    if not os.path.exists(\"save_model\"):\n        os.mkdir(\"save_model\")\n    student_model = get_model(device=device)\n    teacher_model = get_model(is_mean_teacher=True,device=device)\n    \n    train_losses = []\n    valid_losses = []\n    list_train_acc = []\n    list_val_acc = []\n    optimizer = torch.optim.SGD(student_model.parameters(), 0.05,\n                                momentum=0.9,\n                                weight_decay=2e-4,\n                                nesterov=True)\n\n    for epoch in tqdm(range(0, epochs), total= epochs):\n        # training\n        loss, student_class_loss, consistency_loss, consistency_weights, train_loss= train(train_loader,student_model, teacher_model, optimizer, device, epoch)\n        \n        with torch.no_grad():\n            student_model, valid_loss = validate(valid_loader, student_model, epoch,device)\n        train_acc = get_accuracy(student_model, train_loader, device=device)\n        valid_acc = get_accuracy(student_model, valid_loader, device=device)\n        \n        print('Epochs: {}, Train_loss: {}, Valid_loss: {}, Train_accuracy: {}, Valid_accuracy: {}'.format(\n                    epoch, train_loss, valid_loss, train_acc, valid_acc\n                    ))\n        wandb.log({\"Train_loss\": train_loss, \"Valid_loss\": valid_loss, \"Train_accuracy\": train_acc, \"Valid_accuracy\": valid_acc})\n        list_train_acc.append(train_acc)\n        list_val_acc.append(valid_acc)\n        train_losses.append(train_loss)\n        valid_losses.append(valid_loss)\n        \n        torch.save(student_model.state_dict(), \"save_model/epoch_{}_acc{}.pth\".format(epoch+1, valid_acc))\n    plot_losses(list_train_acc, list_val_acc, train_losses, valid_losses)\n\n    return student_model, optimizer, (train_losses, valid_losses)","metadata":{"execution":{"iopub.status.busy":"2023-07-22T06:59:45.406401Z","iopub.execute_input":"2023-07-22T06:59:45.407406Z","iopub.status.idle":"2023-07-22T06:59:45.427055Z","shell.execute_reply.started":"2023-07-22T06:59:45.407369Z","shell.execute_reply":"2023-07-22T06:59:45.426042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:11:00.955092Z","iopub.execute_input":"2023-07-21T17:11:00.955544Z","iopub.status.idle":"2023-07-21T17:11:00.969351Z","shell.execute_reply.started":"2023-07-21T17:11:00.955513Z","shell.execute_reply":"2023-07-21T17:11:00.968297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student,optimizer,_ = training_loop(15,device,dataloader_train,dataloader_val)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:43:12.802184Z","iopub.execute_input":"2023-07-21T17:43:12.80326Z","iopub.status.idle":"2023-07-21T18:44:56.056936Z","shell.execute_reply.started":"2023-07-21T17:43:12.803217Z","shell.execute_reply":"2023-07-21T18:44:56.055951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model= get_model(device=device)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:50:38.004803Z","iopub.execute_input":"2023-07-21T18:50:38.005207Z","iopub.status.idle":"2023-07-21T18:50:38.4551Z","shell.execute_reply.started":"2023-07-21T18:50:38.005175Z","shell.execute_reply":"2023-07-21T18:50:38.454119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path='/kaggle/working/save_model/epoch_14_acc75.22999999999999.pth'","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:52:59.510215Z","iopub.execute_input":"2023-07-21T18:52:59.510572Z","iopub.status.idle":"2023-07-21T18:52:59.51555Z","shell.execute_reply.started":"2023-07-21T18:52:59.510542Z","shell.execute_reply":"2023-07-21T18:52:59.514515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_state_dict(torch.load(path, map_location=device))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T18:53:00.917966Z","iopub.execute_input":"2023-07-21T18:53:00.918579Z","iopub.status.idle":"2023-07-21T18:53:01.079127Z","shell.execute_reply.started":"2023-07-21T18:53:00.918533Z","shell.execute_reply":"2023-07-21T18:53:01.07814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.uưnpack_archive('/kaggle/input/c/cifar-10/test.7z')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndataset_test = CifarLoader(root_dir = './test',\n                      transform = tr)\n\ndataloader_test = DataLoader(dataset = dataset_test, \n                             batch_size = 128, shuffle=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.eval()\nkey = [\"frog\",\"truck\",\"deer\",\"automobile\",\"bird\",\"horse\",\"ship\",\"cat\",\"dog\",\"airplane\"]\nkey = {i : key[i] for i in range(len(key))}\nres = []\n\n\nwith torch.no_grad():\n    for x in tqdm(test_loader, total= len(test_loader)):\n        output,_ = model(x.to(\"cuda\"))\n        output = torch.argmax(output, dim=1)\n        for i in output:\n            res.append(key[i.item()])\ndf = pd.DataFrame(res)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['id'] = range(1, len(df) + 1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.set_index('id')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=True, header=[\"label\"])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.867654Z","iopub.status.idle":"2023-07-21T17:13:30.868725Z","shell.execute_reply.started":"2023-07-21T17:13:30.868482Z","shell.execute_reply":"2023-07-21T17:13:30.868505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns.values[0] = 'Label'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"submission.csv\", index=True, header=[\"label\"])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.874376Z","iopub.status.idle":"2023-07-21T17:13:30.874852Z","shell.execute_reply.started":"2023-07-21T17:13:30.874619Z","shell.execute_reply":"2023-07-21T17:13:30.874642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_key_by_value(dictionary, target_value):\n    for key, value in dictionary.items():\n        if value == target_value:\n            return key\n    return None ","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.876095Z","iopub.status.idle":"2023-07-21T17:13:30.877243Z","shell.execute_reply.started":"2023-07-21T17:13:30.876973Z","shell.execute_reply":"2023-07-21T17:13:30.876996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr = transforms.Compose([\n        transforms.ToPILImage(),\n        transforms.Resize((224, 224)),  \n        transforms.ToTensor(),        \n        transforms.Normalize(mean, std)\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.878582Z","iopub.status.idle":"2023-07-21T17:13:30.87904Z","shell.execute_reply.started":"2023-07-21T17:13:30.878805Z","shell.execute_reply":"2023-07-21T17:13:30.878828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CifarLoader(Dataset):\n  def __init__(self, root_dir, csv_file = None, transform = None):\n\n    # images directory \n    self.root_dir = root_dir\n    # transformations if any\n    self.transform = transform\n\n    if csv_file:\n      # read csv file\n      self.df = pd.read_csv(csv_file)\n\n      # {class: lbl}\n      self.annot_dict = {}\n\n      for i, lbl in enumerate(self.df['label'].unique()):\n        self.annot_dict[lbl] = i\n\n      # {lbl: class}\n      self.annot_dict_reversed = {v:k for k,v in self.annot_dict.items()}\n\n      # add a column with decoded labels\n      self.df['encoded'] = self.df['label'].map(self.annot_dict)\n\n    else: \n      # if no csv file provided -> create a df, fill labels with 0's\n      self.df = pd.DataFrame({\n          'id': list(range(1, len(os.listdir(self.root_dir)) + 1)),\n          'label': np.zeros(len(os.listdir(self.root_dir))),\n          'encoded': np.zeros(len(os.listdir(self.root_dir)))\n      })\n\n\n  def __len__(self):\n    # num of records in the df\n    return len(self.df)\n\n  def __getitem__(self, index):\n    # path to the image\n    img_path = os.path.join(self.root_dir, str(self.df.iloc[index, 0]) + '.png')\n    # read image\n    image = np.array(Image.open(img_path).convert('RGB'))\n    # get label\n    y_label = torch.tensor(int(self.df.iloc[index, 2]))\n\n    # apply transformations\n    if self.transform:\n      image = self.transform(image)\n    \n    return (image, y_label)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.880801Z","iopub.status.idle":"2023-07-21T17:13:30.881284Z","shell.execute_reply.started":"2023-07-21T17:13:30.881022Z","shell.execute_reply":"2023-07-21T17:13:30.881044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = CifarLoader(csv_file = '/kaggle/input/c/cifar-10/trainLabels.csv', \n                      root_dir = './train',\n                      transform = tr)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.883015Z","iopub.status.idle":"2023-07-21T17:13:30.883536Z","shell.execute_reply.started":"2023-07-21T17:13:30.883281Z","shell.execute_reply":"2023-07-21T17:13:30.883304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = np.array([0.5, 0.5, 0.5])\nstd = np.array([0.5, 0.5, 0.5])\ndef images_show(images, lbls=None):\n  # show only 4 first images from each batch\n  for i in range(4):\n    plt.subplot(1, 4, i + 1)\n    # convert to numpy\n    img = images[i].numpy().transpose((1, 2, 0))\n    # unnormalize\n    img = img * std + mean \n    img = np.clip(img, a_min=0, a_max=1)\n    plt.imshow(img)\n \n    if lbls != None:\n      # decode labels using reversed annotation dictionary\n      plt.title(dataset.annot_dict_reversed[lbls[i].item()])\n    plt.axis('off')\n  plt.tight_layout(pad=0.5)\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.885177Z","iopub.status.idle":"2023-07-21T17:13:30.885644Z","shell.execute_reply.started":"2023-07-21T17:13:30.885404Z","shell.execute_reply":"2023-07-21T17:13:30.885427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dict","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.887905Z","iopub.status.idle":"2023-07-21T17:13:30.889078Z","shell.execute_reply.started":"2023-07-21T17:13:30.888818Z","shell.execute_reply":"2023-07-21T17:13:30.888843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loaders= DataLoader(dataset, batch_size= 16, shuffle= True)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.890293Z","iopub.status.idle":"2023-07-21T17:13:30.891115Z","shell.execute_reply.started":"2023-07-21T17:13:30.890852Z","shell.execute_reply":"2023-07-21T17:13:30.890874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataiter = iter(train_loaders)\ndata = next(dataiter)\n\n\n\nfor i in range(3):\n  images, labels = next(dataiter)\n  images_show(images, labels)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T17:13:30.892665Z","iopub.status.idle":"2023-07-21T17:13:30.893599Z","shell.execute_reply.started":"2023-07-21T17:13:30.893357Z","shell.execute_reply":"2023-07-21T17:13:30.893381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}