{"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":24800,"databundleVersionId":1831594,"sourceType":"competition"},{"sourceId":428708,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":349459,"modelId":370719}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nfrom PIL import Image\nfrom torchvision import transforms\nimport pydicom\nimport numpy as np\n\n\n# Label map\nlabel_map = {0: \"Normal\", 1: \"Abnormal\"}\n\n# Function to load full model\ndef load_full_model(model_path, device):\n    model = torch.load(model_path, map_location=device, weights_only=False)\n    model.eval()\n    return model\n\n# Convert DICOM image to PIL RGB\ndef dicom_to_pil(dicom_path):\n    ds = pydicom.dcmread(dicom_path)\n    img = ds.pixel_array.astype(np.float32)\n\n    # Normalize to [0, 255]\n    img -= img.min()\n    img /= (img.max() + 1e-6)\n    img *= 255.0\n    img = img.astype(np.uint8)\n\n    return Image.fromarray(img).convert(\"RGB\")\n\n# Predict from DICOM image path\ndef predict_dicom(model, dicom_path, mean, std, device):\n    transform = transforms.Compose([\n        transforms.Resize((256, 256)),\n        transforms.ToTensor(),\n        transforms.Normalize(mean=mean, std=std)\n    ])\n\n    pil_img = dicom_to_pil(dicom_path)\n    img_tensor = transform(pil_img).unsqueeze(0).to(device)\n\n    with torch.no_grad():\n        output = model(img_tensor)\n        pred = torch.argmax(output, dim=1).item()\n\n    return pred","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-08T12:29:07.512703Z","iopub.execute_input":"2025-06-08T12:29:07.513351Z","iopub.status.idle":"2025-06-08T12:29:07.520067Z","shell.execute_reply.started":"2025-06-08T12:29:07.513323Z","shell.execute_reply":"2025-06-08T12:29:07.519382Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#normal\n# \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/00053190460d56c53cc3e57321387478.dicom\"\n#upnormal\n#/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/0061cf6d35e253b6e7f03940592cc35e.dicom\n\n\n# Load model\nmodel_path = \"/kaggle/input/model/tensorflow2/default/1/best_model_full_resnet101_NDL_.pth\"\nmodel_loaded = load_full_model(model_path, DEVICE)\n# Set device\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\n# Normalization values used in training\nmean = [0.54821104, 0.54821104, 0.54821104]\nstd = [0.26668723, 0.26668723, 0.26668723]\n\n# Predict from DICOM\ndicom_path = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/007c1195aab852cff5c20736be26a1ae.dicom\"\npredicted_class = predict_dicom(model_loaded, dicom_path, mean, std, DEVICE)\n\nprint(f\"Prediction: {label_map[predicted_class]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-08T12:31:08.387168Z","iopub.execute_input":"2025-06-08T12:31:08.387846Z","iopub.status.idle":"2025-06-08T12:31:08.932205Z","shell.execute_reply.started":"2025-06-08T12:31:08.387818Z","shell.execute_reply":"2025-06-08T12:31:08.931357Z"}},"outputs":[],"execution_count":null}]}