{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pydicom\nimport cv2\nfrom pathlib import Path\nimport pandas as pd\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:17:50.40351Z","iopub.execute_input":"2025-04-21T05:17:50.403776Z","iopub.status.idle":"2025-04-21T05:17:51.836816Z","shell.execute_reply.started":"2025-04-21T05:17:50.403756Z","shell.execute_reply":"2025-04-21T05:17:51.836232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_train = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:17:53.352115Z","iopub.execute_input":"2025-04-21T05:17:53.35293Z","iopub.status.idle":"2025-04-21T05:17:53.425889Z","shell.execute_reply.started":"2025-04-21T05:17:53.352893Z","shell.execute_reply":"2025-04-21T05:17:53.425152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_train.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:17:56.448032Z","iopub.execute_input":"2025-04-21T05:17:56.448626Z","iopub.status.idle":"2025-04-21T05:17:56.475429Z","shell.execute_reply.started":"2025-04-21T05:17:56.448602Z","shell.execute_reply":"2025-04-21T05:17:56.474856Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_train = labels_train.drop_duplicates('patientId')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:17:59.271531Z","iopub.execute_input":"2025-04-21T05:17:59.272138Z","iopub.status.idle":"2025-04-21T05:17:59.290058Z","shell.execute_reply.started":"2025-04-21T05:17:59.272111Z","shell.execute_reply":"2025-04-21T05:17:59.289199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_train.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:18:01.750475Z","iopub.execute_input":"2025-04-21T05:18:01.750794Z","iopub.status.idle":"2025-04-21T05:18:01.761668Z","shell.execute_reply.started":"2025-04-21T05:18:01.750773Z","shell.execute_reply":"2025-04-21T05:18:01.760812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels_train.patientId.iloc[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:18:04.116607Z","iopub.execute_input":"2025-04-21T05:18:04.116892Z","iopub.status.idle":"2025-04-21T05:18:04.122022Z","shell.execute_reply.started":"2025-04-21T05:18:04.116872Z","shell.execute_reply":"2025-04-21T05:18:04.121301Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Root_path = Path('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images')\nSave_path = Path('/kaggle/working/Processed')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:18:06.302979Z","iopub.execute_input":"2025-04-21T05:18:06.303274Z","iopub.status.idle":"2025-04-21T05:18:06.307185Z","shell.execute_reply.started":"2025-04-21T05:18:06.303253Z","shell.execute_reply":"2025-04-21T05:18:06.306379Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axis = plt.subplots(3,3,figsize=(9,9))\n\nc = 0\n\nfor i in range(3):\n    for j in range(3):\n        patient_id = labels_train.patientId.iloc[c]\n        dcm_path = Root_path/patient_id\n        dcm_path = dcm_path.with_suffix('.dcm')\n        dcm = pydicom.dcmread(dcm_path).pixel_array\n\n        label = labels_train['Target'].iloc[c]\n        axis[i][j].imshow(dcm,cmap='bone')\n        axis[i][j].set_title(label)\n\n        c += 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T04:57:50.660593Z","iopub.execute_input":"2025-04-21T04:57:50.661016Z","iopub.status.idle":"2025-04-21T04:57:53.333132Z","shell.execute_reply.started":"2025-04-21T04:57:50.660981Z","shell.execute_reply":"2025-04-21T04:57:53.331982Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!rm -rf /kaggle/working/Processed","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-19T14:36:36.580995Z","iopub.execute_input":"2025-04-19T14:36:36.581342Z","iopub.status.idle":"2025-04-19T14:36:36.709474Z","shell.execute_reply.started":"2025-04-19T14:36:36.581316Z","shell.execute_reply":"2025-04-19T14:36:36.708145Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sums = 0\nsums_squared = 0\n\nfor c,patient_id in enumerate(tqdm(labels_train.patientId)):\n    patient_id = labels_train.patientId.iloc[c]\n    dcm_path = Root_path/patient_id\n    dcm_path = dcm_path.with_suffix('.dcm')\n    dcm = pydicom.dcmread(dcm_path).pixel_array/255\n\n    dcm_arr = cv2.resize(dcm,(224,224)).astype(np.float16)\n    label = labels_train.Target.iloc[c]    \n    train_or_val = 'train' if c < 24000 else 'val'\n\n\n    current_save_path = Save_path/train_or_val/str(label)\n    current_save_path.mkdir(parents = True,exist_ok = True)\n    np.save(current_save_path/patient_id,dcm_arr)\n\n    norm = 224*224\n    if train_or_val == 'train':\n        sums += np.sum(dcm_arr)/norm\n        sums_squared += (dcm_arr**2).sum()/norm\n      \n      ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:18:22.365001Z","iopub.execute_input":"2025-04-21T05:18:22.365743Z","iopub.status.idle":"2025-04-21T05:25:02.184933Z","shell.execute_reply.started":"2025-04-21T05:18:22.365716Z","shell.execute_reply":"2025-04-21T05:25:02.184171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean = sums / 24000\nstd = np.sqrt((sums_squared / 24000) - mean**2)\n\nprint(f'mean:{mean},std:{std}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:03.436297Z","iopub.execute_input":"2025-04-21T05:25:03.436582Z","iopub.status.idle":"2025-04-21T05:25:03.441173Z","shell.execute_reply.started":"2025-04-21T05:25:03.436561Z","shell.execute_reply":"2025-04-21T05:25:03.440462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torchmetrics\nimport pytorch_lightning as pl\nfrom pytorch_lightning.callbacks import ModelCheckpoint\nfrom pytorch_lightning.loggers import TensorBoardLogger\nimport torch\nimport torchvision\nfrom torchvision import transforms","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:23.941308Z","iopub.execute_input":"2025-04-21T05:25:23.94173Z","iopub.status.idle":"2025-04-21T05:25:23.945701Z","shell.execute_reply.started":"2025-04-21T05:25:23.941711Z","shell.execute_reply":"2025-04-21T05:25:23.94498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_path(path):\n    return np.load(path).astype(np.float32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:26.077234Z","iopub.execute_input":"2025-04-21T05:25:26.077489Z","iopub.status.idle":"2025-04-21T05:25:26.08103Z","shell.execute_reply.started":"2025-04-21T05:25:26.077473Z","shell.execute_reply":"2025-04-21T05:25:26.08035Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform_train = transforms.Compose([transforms.ToTensor(),\n        transforms.Normalize(0.4904,0.24795),\n        transforms.RandomAffine(degrees=(-5,5),scale=(0.9,1.1),translate=(0,0.05)),\n        transforms.RandomResizedCrop((224,224),scale=(0.35,1))])\n\ntransform_val = transforms.Compose([transforms.ToTensor(),\n        transforms.Normalize(0.49,0.248)])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:28.605909Z","iopub.execute_input":"2025-04-21T05:25:28.606513Z","iopub.status.idle":"2025-04-21T05:25:28.61123Z","shell.execute_reply.started":"2025-04-21T05:25:28.606487Z","shell.execute_reply":"2025-04-21T05:25:28.610658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = torchvision.datasets.DatasetFolder('/kaggle/working/Processed/train/',loader=load_path, extensions='npy',transform=transform_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:31.043443Z","iopub.execute_input":"2025-04-21T05:25:31.044106Z","iopub.status.idle":"2025-04-21T05:25:31.101522Z","shell.execute_reply.started":"2025-04-21T05:25:31.044076Z","shell.execute_reply":"2025-04-21T05:25:31.10083Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_dataset = torchvision.datasets.DatasetFolder('/kaggle/working/Processed/val/',loader=load_path, extensions='npy',transform=transform_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:33.209584Z","iopub.execute_input":"2025-04-21T05:25:33.209845Z","iopub.status.idle":"2025-04-21T05:25:33.220202Z","shell.execute_reply.started":"2025-04-21T05:25:33.209826Z","shell.execute_reply":"2025-04-21T05:25:33.219213Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-19T14:49:03.463241Z","iopub.execute_input":"2025-04-19T14:49:03.463624Z","iopub.status.idle":"2025-04-19T14:49:03.470872Z","shell.execute_reply.started":"2025-04-19T14:49:03.4636Z","shell.execute_reply":"2025-04-19T14:49:03.469563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-19T14:49:05.511223Z","iopub.execute_input":"2025-04-19T14:49:05.511604Z","iopub.status.idle":"2025-04-19T14:49:05.519061Z","shell.execute_reply.started":"2025-04-19T14:49:05.51158Z","shell.execute_reply":"2025-04-19T14:49:05.51797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"picture, label = train_dataset[2]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:09:36.456203Z","iopub.execute_input":"2025-04-21T05:09:36.456548Z","iopub.status.idle":"2025-04-21T05:09:36.643425Z","shell.execute_reply.started":"2025-04-21T05:09:36.456522Z","shell.execute_reply":"2025-04-21T05:09:36.64262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"picture","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:09:38.376766Z","iopub.execute_input":"2025-04-21T05:09:38.377339Z","iopub.status.idle":"2025-04-21T05:09:38.41239Z","shell.execute_reply.started":"2025-04-21T05:09:38.377309Z","shell.execute_reply":"2025-04-21T05:09:38.411311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(picture[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:09:49.553723Z","iopub.execute_input":"2025-04-21T05:09:49.554199Z","iopub.status.idle":"2025-04-21T05:09:49.838492Z","shell.execute_reply.started":"2025-04-21T05:09:49.554171Z","shell.execute_reply":"2025-04-21T05:09:49.837304Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f\"Тип изображения: {type(picture)}\")         \nprint(f\"Форма изображения: {picture.shape}\")       \nprint(f\"Тип метки: {label}\")                       \nprint(f\"Минимум: {picture.min():.4f}, Максимум: {picture.max():.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:09:53.262607Z","iopub.execute_input":"2025-04-21T05:09:53.263547Z","iopub.status.idle":"2025-04-21T05:09:53.270849Z","shell.execute_reply.started":"2025-04-21T05:09:53.263513Z","shell.execute_reply":"2025-04-21T05:09:53.269554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axis = plt.subplots(2,2,figsize=(10,10))\n\nfor i in range(2):\n    for j in range(2):\n        rand_size = np.random.randint(0,24000)\n        picture,label = train_dataset[rand_size]\n        axis[i][j].imshow(picture[0],cmap=('bone'))\n        axis[i][j].set_title(label)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:09:55.656995Z","iopub.execute_input":"2025-04-21T05:09:55.657379Z","iopub.status.idle":"2025-04-21T05:09:56.85316Z","shell.execute_reply.started":"2025-04-21T05:09:55.65735Z","shell.execute_reply":"2025-04-21T05:09:56.852135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch_size = 64","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:42.579643Z","iopub.execute_input":"2025-04-21T05:25:42.580198Z","iopub.status.idle":"2025-04-21T05:25:42.58317Z","shell.execute_reply.started":"2025-04-21T05:25:42.580173Z","shell.execute_reply":"2025-04-21T05:25:42.582579Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(dataset=train_dataset,\n                                           batch_size=batch_size,\n                                           shuffle=True,\n                                           num_workers=4,\n                                           pin_memory=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:44.832585Z","iopub.execute_input":"2025-04-21T05:25:44.833127Z","iopub.status.idle":"2025-04-21T05:25:44.836934Z","shell.execute_reply.started":"2025-04-21T05:25:44.8331Z","shell.execute_reply":"2025-04-21T05:25:44.836225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_loader = torch.utils.data.DataLoader(dataset=val_dataset,\n                                           batch_size=batch_size,\n                                           shuffle=True,\n                                           num_workers=4,\n                                           pin_memory=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:47.369902Z","iopub.execute_input":"2025-04-21T05:25:47.370489Z","iopub.status.idle":"2025-04-21T05:25:47.373928Z","shell.execute_reply.started":"2025-04-21T05:25:47.370467Z","shell.execute_reply":"2025-04-21T05:25:47.373264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torchvision.models.resnet34()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:25:54.709719Z","iopub.execute_input":"2025-04-21T05:25:54.710292Z","iopub.status.idle":"2025-04-21T05:25:55.082766Z","shell.execute_reply.started":"2025-04-21T05:25:54.710267Z","shell.execute_reply":"2025-04-21T05:25:55.081962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class PneumoniaModel(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n\n        \n        self.model = torchvision.models.resnet34(pretrained=False)\n        \n        \n        self.model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)\n        \n        \n        self.model.fc = torch.nn.Linear(in_features=512, out_features=1, bias=True)\n        \n        \n        self.optimizer = torch.optim.AdamW(self.model.parameters(), lr=1e-4)\n        self.loss_fn = torch.nn.BCEWithLogitsLoss(pos_weight=torch.tensor([3.0]))\n        \n        \n        self.train_acc = torchmetrics.Accuracy(task=\"binary\")\n        self.val_acc = torchmetrics.Accuracy(task=\"binary\")\n\n    def forward(self, data):\n        pred = self.model(data)\n        return pred\n\n    def training_step(self, batch, batch_idx):\n        x_ray, label = batch\n        label = label.float()\n        pred = self(x_ray)[:, 0]\n        loss = self.loss_fn(pred, label)\n\n        self.log(\"Train Loss\", loss)\n        self.log(\"Step Train ACC\", self.train_acc(torch.sigmoid(pred), label.int()))\n\n        return loss\n\n    def on_train_epoch_end(self):\n        self.log(\"Train ACC\", self.train_acc.compute())\n        self.train_acc.reset()\n\n    def validation_step(self, batch, batch_idx):\n        x_ray, label = batch\n        label = label.float()\n        pred = self(x_ray)[:, 0]\n        loss = self.loss_fn(pred, label)\n\n        self.log(\"Val Loss\", loss)\n        self.log(\"Step Val ACC\", self.val_acc(torch.sigmoid(pred), label.int()))\n\n    def on_validation_epoch_end(self):\n        self.log(\"Val ACC\", self.val_acc.compute())\n        self.val_acc.reset()\n\n    def configure_optimizers(self):\n        return self.optimizer\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:19:57.655216Z","iopub.execute_input":"2025-04-21T06:19:57.655854Z","iopub.status.idle":"2025-04-21T06:19:57.664668Z","shell.execute_reply.started":"2025-04-21T06:19:57.655832Z","shell.execute_reply":"2025-04-21T06:19:57.663735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = PneumoniaModel()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:20:02.933298Z","iopub.execute_input":"2025-04-21T06:20:02.933561Z","iopub.status.idle":"2025-04-21T06:20:03.233609Z","shell.execute_reply.started":"2025-04-21T06:20:02.933541Z","shell.execute_reply":"2025-04-21T06:20:03.233075Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.optim.lr_scheduler import ReduceLROnPlateau","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T05:10:32.396096Z","iopub.execute_input":"2025-04-21T05:10:32.396409Z","iopub.status.idle":"2025-04-21T05:10:32.401728Z","shell.execute_reply.started":"2025-04-21T05:10:32.396389Z","shell.execute_reply":"2025-04-21T05:10:32.400422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pytorch_lightning.callbacks import TQDMProgressBar\nfrom pytorch_lightning import Trainer\n\n\nmodel = PneumoniaModel()\n\ncheckpoint_callback = ModelCheckpoint(\n    monitor=\"Val Loss\",     \n    dirpath=\"my_checkpoints\",\n    filename=\"pneumonia-{epoch:02d}-{val_loss:.2f}\",\n    save_top_k=1,\n    mode=\"min\"\n)\n\ntrainer = Trainer(\n    max_epochs=10,\n    callbacks=[checkpoint_callback, TQDMProgressBar(refresh_rate=1)],\n    accelerator=\"gpu\"  \n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:20:09.122406Z","iopub.execute_input":"2025-04-21T06:20:09.122671Z","iopub.status.idle":"2025-04-21T06:20:09.503222Z","shell.execute_reply.started":"2025-04-21T06:20:09.122653Z","shell.execute_reply":"2025-04-21T06:20:09.502474Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer.fit(model, train_dataloaders=train_loader, val_dataloaders=val_loader)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:20:12.644201Z","iopub.execute_input":"2025-04-21T06:20:12.644777Z","iopub.status.idle":"2025-04-21T06:41:25.427274Z","shell.execute_reply.started":"2025-04-21T06:20:12.644753Z","shell.execute_reply":"2025-04-21T06:41:25.42665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import re\n\ndef extract_epoch(filename):\n    match = re.search(r\"epoch=(\\d+)\", filename)\n    return int(match.group(1)) if match else -1\n\n\nbest_ckpt = max(checkpoints, key=extract_epoch)\nbest_path = os.path.join(checkpoint_dir, best_ckpt)\n\nprint(\"Загружаем:\", best_ckpt)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:41:51.659237Z","iopub.execute_input":"2025-04-21T06:41:51.660002Z","iopub.status.idle":"2025-04-21T06:41:51.665234Z","shell.execute_reply.started":"2025-04-21T06:41:51.65998Z","shell.execute_reply":"2025-04-21T06:41:51.664531Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ckpt_dir = '/kaggle/working/lightning_logs/version_0/checkpoints'\ncheckpoints = [f for f in os.listdir(ckpt_dir) if f.endswith(\".ckpt\")]\nprint(checkpoints)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:46:57.673795Z","iopub.execute_input":"2025-04-21T06:46:57.674474Z","iopub.status.idle":"2025-04-21T06:46:57.678727Z","shell.execute_reply.started":"2025-04-21T06:46:57.67445Z","shell.execute_reply":"2025-04-21T06:46:57.678176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"full_paths = [os.path.join(ckpt_dir, f) for f in checkpoints]\nfor path in full_paths:\n    print(path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:47:03.486112Z","iopub.execute_input":"2025-04-21T06:47:03.486371Z","iopub.status.idle":"2025-04-21T06:47:03.490719Z","shell.execute_reply.started":"2025-04-21T06:47:03.486338Z","shell.execute_reply":"2025-04-21T06:47:03.489919Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from torch.utils.data import random_split\n\nval_size = int(0.5 * len(val_dataset))  \ntest_size = len(val_dataset) - val_size\n\nval_subset, test_subset = random_split(val_dataset, [val_size, test_size])\n\nval_loader = torch.utils.data.DataLoader(val_subset, batch_size=32, shuffle=False)\ntest_loader = torch.utils.data.DataLoader(test_subset, batch_size=32, shuffle=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T07:29:36.023784Z","iopub.execute_input":"2025-04-21T07:29:36.024084Z","iopub.status.idle":"2025-04-21T07:29:36.029709Z","shell.execute_reply.started":"2025-04-21T07:29:36.024061Z","shell.execute_reply":"2025-04-21T07:29:36.029092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T06:47:53.906362Z","iopub.execute_input":"2025-04-21T06:47:53.906672Z","iopub.status.idle":"2025-04-21T06:47:53.910648Z","shell.execute_reply.started":"2025-04-21T06:47:53.906649Z","shell.execute_reply":"2025-04-21T06:47:53.909775Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import (\n    classification_report,\n    confusion_matrix,\n    roc_curve,\n    roc_auc_score,\n    accuracy_score,\n    ConfusionMatrixDisplay\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T07:40:47.637987Z","iopub.execute_input":"2025-04-21T07:40:47.638308Z","iopub.status.idle":"2025-04-21T07:40:47.642259Z","shell.execute_reply.started":"2025-04-21T07:40:47.638288Z","shell.execute_reply":"2025-04-21T07:40:47.641647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = PneumoniaModel.load_from_checkpoint(\"/kaggle/working/lightning_logs/version_0/checkpoints/epoch=9-step=3750.ckpt\")\nmodel.eval()\nmodel.to(device)\n\n\ny_true = []\ny_pred = []\ny_pred_proba = []\n\nwith torch.no_grad():\n    for images, labels in test_loader:\n        images, labels = images.to(device), labels.to(device)\n        \n        outputs = model(images).squeeze()\n        probs = torch.sigmoid(outputs)\n\n        y_true.extend(labels.cpu().numpy())\n        y_pred_proba.extend(probs.cpu().numpy())\n        y_pred.extend((probs > 0.5).int().cpu().numpy())\n\n# ==== Метрики ====\naccuracy = accuracy_score(y_true, y_pred)\nprint(f\"✅ Accuracy: {accuracy:.4f}\\n\")\n\nprint(\"📊 Classification Report:\")\nprint(classification_report(y_true, y_pred, digits=4))\n\nprint(\"🧱 Confusion Matrix:\")\ncm = confusion_matrix(y_true, y_pred)\nprint(cm)\n\n# ==== ROC и AUC ====\nfpr, tpr, _ = roc_curve(y_true, y_pred_proba)\nauc = roc_auc_score(y_true, y_pred_proba)\n\nplt.figure(figsize=(8, 6))\nplt.plot(fpr, tpr, label=f'ROC curve (AUC = {auc:.2f})')\nplt.plot([0, 1], [0, 1], linestyle='--', color='gray')\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.title('ROC Curve')\nplt.legend()\nplt.grid(True)\nplt.show()\n\n# ==== Confusion Matrix ====\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=[\"Normal\", \"Pneumonia\"])\ndisp.plot(cmap=\"Blues\")\nplt.title(\"Confusion Matrix\")\nplt.grid(False)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-21T07:40:52.870728Z","iopub.execute_input":"2025-04-21T07:40:52.871292Z","iopub.status.idle":"2025-04-21T07:40:59.110118Z","shell.execute_reply.started":"2025-04-21T07:40:52.871268Z","shell.execute_reply":"2025-04-21T07:40:59.109504Z"}},"outputs":[],"execution_count":null}]}