{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":3649,"databundleVersionId":46718}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install py7zr -q\nimport py7zr\n\n# Extracting train.7z\nwith py7zr.SevenZipFile('/kaggle/input/competitions/cifar-10/train.7z', mode='r') as z:\n    z.extractall('/kaggle/working/')\n\n# Extracting test.7z\nwith py7zr.SevenZipFile('/kaggle/input/competitions/cifar-10/test.7z', mode='r') as z:\n    z.extractall('/kaggle/working/')\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:05:58.795150Z","iopub.execute_input":"2026-03-03T18:05:58.795434Z","iopub.status.idle":"2026-03-03T18:08:42.866136Z","shell.execute_reply.started":"2026-03-03T18:05:58.795402Z","shell.execute_reply":"2026-03-03T18:08:42.865427Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/working ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:42.867710Z","iopub.execute_input":"2026-03-03T18:08:42.867929Z","iopub.status.idle":"2026-03-03T18:08:43.013325Z","shell.execute_reply.started":"2026-03-03T18:08:42.867906Z","shell.execute_reply":"2026-03-03T18:08:43.012664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/working/train | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:43.014531Z","iopub.execute_input":"2026-03-03T18:08:43.014757Z","iopub.status.idle":"2026-03-03T18:08:43.249305Z","shell.execute_reply.started":"2026-03-03T18:08:43.014732Z","shell.execute_reply":"2026-03-03T18:08:43.248439Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/working/test | head -n 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:43.250676Z","iopub.execute_input":"2026-03-03T18:08:43.251242Z","iopub.status.idle":"2026-03-03T18:08:44.131868Z","shell.execute_reply.started":"2026-03-03T18:08:43.251214Z","shell.execute_reply":"2026-03-03T18:08:44.131090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import models\nimport torchvision.transforms as T\nimport cv2\nfrom PIL import Image\nimport glob\nimport pandas as pd\n\ndevice = 'cuda'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:44.133242Z","iopub.execute_input":"2026-03-03T18:08:44.133902Z","iopub.status.idle":"2026-03-03T18:08:51.756275Z","shell.execute_reply.started":"2026-03-03T18:08:44.133870Z","shell.execute_reply":"2026-03-03T18:08:51.755520Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/competitions/cifar-10/trainLabels.csv')\nlabel_conversion = ['airplane',  \n'automobile', \n'bird', \n'cat', \n'deer', \n'dog', \n'frog', \n'horse', \n'ship', \n'truck']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:51.757197Z","iopub.execute_input":"2026-03-03T18:08:51.757555Z","iopub.status.idle":"2026-03-03T18:08:51.783954Z","shell.execute_reply.started":"2026-03-03T18:08:51.757532Z","shell.execute_reply":"2026-03-03T18:08:51.783437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_conversion.index('cat')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:51.785810Z","iopub.execute_input":"2026-03-03T18:08:51.786062Z","iopub.status.idle":"2026-03-03T18:08:51.791688Z","shell.execute_reply.started":"2026-03-03T18:08:51.786042Z","shell.execute_reply":"2026-03-03T18:08:51.791083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class MyDataset(Dataset):\n    def __init__(self, file_paths, is_test=False):\n        self.is_test = is_test\n        self.file_paths = file_paths\n\n        self.transform = T.Compose([\n            T.ToTensor(),        # ПРЕВРАЩАЕТ PIL Image в Tensor (КРИТИЧНО!)\n            T.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Стандарт для цветных картинок\n        ])\n\n    def __len__(self):\n        return len(self.file_paths)\n\n    def __getitem__(self, idx):\n        path = self.file_paths[idx]\n        image = Image.open(path).convert('RGB')\n        image = self.transform(image)\n        img_id = int(path.split('/')[-1].split('.')[0])\n        \n        if self.is_test:\n            return image, img_id\n            \n        img_id = int(path.split('/')[-1].split('.')[0])\n        label = train_labels[train_labels['id'] == img_id]['label']\n        return image, label_conversion.index(label.values[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:51.792585Z","iopub.execute_input":"2026-03-03T18:08:51.792811Z","iopub.status.idle":"2026-03-03T18:08:51.801765Z","shell.execute_reply.started":"2026-03-03T18:08:51.792792Z","shell.execute_reply":"2026-03-03T18:08:51.801197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_files = glob.glob('/kaggle/working/train/*.png')\ntest_files = glob.glob('/kaggle/working/test/*.png')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:51.802680Z","iopub.execute_input":"2026-03-03T18:08:51.802933Z","iopub.status.idle":"2026-03-03T18:08:52.351954Z","shell.execute_reply.started":"2026-03-03T18:08:51.802914Z","shell.execute_reply":"2026-03-03T18:08:52.351166Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader = DataLoader(MyDataset(train_files), batch_size=32, shuffle=True)\ntest_loader = DataLoader(MyDataset(test_files, is_test=True), batch_size=32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:52.352912Z","iopub.execute_input":"2026-03-03T18:08:52.353177Z","iopub.status.idle":"2026-03-03T18:08:52.357471Z","shell.execute_reply.started":"2026-03-03T18:08:52.353145Z","shell.execute_reply":"2026-03-03T18:08:52.356662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = models.resnet18(weights=None)\nmodel.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1, stride=1)\nmodel.fc = nn.Linear(model.fc.in_features, len(label_conversion))\nmodel.to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:52.358315Z","iopub.execute_input":"2026-03-03T18:08:52.358642Z","iopub.status.idle":"2026-03-03T18:08:52.940729Z","shell.execute_reply.started":"2026-03-03T18:08:52.358613Z","shell.execute_reply":"2026-03-03T18:08:52.939973Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)\ncriterion = nn.CrossEntropyLoss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:52.941702Z","iopub.execute_input":"2026-03-03T18:08:52.942077Z","iopub.status.idle":"2026-03-03T18:08:52.946321Z","shell.execute_reply.started":"2026-03-03T18:08:52.942046Z","shell.execute_reply":"2026-03-03T18:08:52.945663Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from time import time\nfor _ in range(3):\n    start = time()\n    model.train()\n    for img, label in train_loader:\n        img, label = img.to(device), label.to(device)\n        optimizer.zero_grad()\n        loss = criterion(model(img), label)\n        loss.backward()\n        optimizer.step()\n    print(_, time() - start)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:08:52.947227Z","iopub.execute_input":"2026-03-03T18:08:52.947517Z","iopub.status.idle":"2026-03-03T18:11:59.652027Z","shell.execute_reply.started":"2026-03-03T18:08:52.947490Z","shell.execute_reply":"2026-03-03T18:11:59.651213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = []\nmodel.eval()\nstart = time()\nwith torch.no_grad():\n    for img, idx in test_loader:\n        img = img.to(device)\n        output = model(img)\n        predicted_classes = output.argmax(dim=1).cpu().numpy()\n        \n        for i in range(len(predicted_classes)):\n            preds.append({\n                'id': idx[i].item(), \n                'label': label_conversion[predicted_classes[i]]\n            })\n\ndf_preds = pd.DataFrame(preds)\nprint(time() - start)\nprint(df_preds.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:11:59.653686Z","iopub.execute_input":"2026-03-03T18:11:59.653930Z","iopub.status.idle":"2026-03-03T18:15:08.686826Z","shell.execute_reply.started":"2026-03-03T18:11:59.653908Z","shell.execute_reply":"2026-03-03T18:15:08.686059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(df_preds)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:15:08.687942Z","iopub.execute_input":"2026-03-03T18:15:08.688550Z","iopub.status.idle":"2026-03-03T18:15:08.693172Z","shell.execute_reply.started":"2026-03-03T18:15:08.688526Z","shell.execute_reply":"2026-03-03T18:15:08.692418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_preds.to_csv('cifar_subm.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-03T18:15:08.694088Z","iopub.execute_input":"2026-03-03T18:15:08.695270Z","iopub.status.idle":"2026-03-03T18:15:08.924605Z","shell.execute_reply.started":"2026-03-03T18:15:08.695250Z","shell.execute_reply":"2026-03-03T18:15:08.923862Z"}},"outputs":[],"execution_count":null}]}