{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":31236,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\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","trusted":true,"execution":{"iopub.status.busy":"2025-12-17T15:23:08.64254Z","iopub.execute_input":"2025-12-17T15:23:08.642708Z","iopub.status.idle":"2025-12-17T15:23:54.942112Z","shell.execute_reply.started":"2025-12-17T15:23:08.64269Z","shell.execute_reply":"2025-12-17T15:23:54.941418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nfor root, dirs, files in os.walk(\"/kaggle/input/rsna-pneumonia-detection-challenge\"):\n    print(root)\n    print(files[:5])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:42:52.06718Z","iopub.execute_input":"2025-12-17T16:42:52.06795Z","iopub.status.idle":"2025-12-17T16:43:22.292228Z","shell.execute_reply.started":"2025-12-17T16:42:52.067916Z","shell.execute_reply":"2025-12-17T16:43:22.291408Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q transformers datasets accelerate supervision pydicom albumentations\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:43:27.43612Z","iopub.execute_input":"2025-12-17T16:43:27.436984Z","iopub.status.idle":"2025-12-17T16:43:31.643645Z","shell.execute_reply.started":"2025-12-17T16:43:27.436947Z","shell.execute_reply":"2025-12-17T16:43:31.642908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q transformers datasets evaluate albumentations\n!pip install -q accelerate supervision\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:43:32.61124Z","iopub.execute_input":"2025-12-17T16:43:32.612111Z","iopub.status.idle":"2025-12-17T16:43:39.723678Z","shell.execute_reply.started":"2025-12-17T16:43:32.612068Z","shell.execute_reply":"2025-12-17T16:43:39.722459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport pydicom\nfrom tqdm import tqdm\n\nin_dir = \"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images\"\nout_dir = \"/kaggle/working/train_images\"\nos.makedirs(out_dir, exist_ok=True)\n\nfor file in tqdm(os.listdir(in_dir)):\n    ds = pydicom.dcmread(os.path.join(in_dir, file))\n    img = ds.pixel_array\n    img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX).astype(\"uint8\")\n    cv2.imwrite(f\"{out_dir}/{file.replace('.dcm','.png')}\", img)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T15:30:26.004308Z","iopub.execute_input":"2025-12-17T15:30:26.005161Z","iopub.status.idle":"2025-12-17T15:44:40.560371Z","shell.execute_reply.started":"2025-12-17T15:30:26.005123Z","shell.execute_reply":"2025-12-17T15:44:40.559691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport json\n\ndf = pd.read_csv(\"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\")\n\ncoco = {\n    \"images\": [],\n    \"annotations\": [],\n    \"categories\": [{\"id\":1, \"name\":\"pneumonia\"}],\n}\n\nann_id = 1\nimage_id_map = {}\n\nfor idx, row in df.iterrows():\n    pid = row.patientId\n    if pid not in image_id_map:\n        image_id_map[pid] = len(image_id_map) + 1\n        coco[\"images\"].append({\n            \"id\": image_id_map[pid],\n            \"file_name\": f\"{pid}.png\",\n            \"height\": 1024,\n            \"width\": 1024\n        })\n    if row.Target == 1:\n        x,y,w,h = row.x, row.y, row.width, row.height\n        coco[\"annotations\"].append({\n            \"id\": ann_id,\n            \"image_id\": image_id_map[pid],\n            \"category_id\": 1,\n            \"bbox\": [x, y, w, h],\n            \"area\": w*h,\n            \"iscrowd\": 0\n        })\n        ann_id += 1\n\nwith open(\"/kaggle/working/rsna_train_coco.json\",\"w\") as f:\n    json.dump(coco, f)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:43:49.715069Z","iopub.execute_input":"2025-12-17T16:43:49.715442Z","iopub.status.idle":"2025-12-17T16:43:51.511642Z","shell.execute_reply.started":"2025-12-17T16:43:49.715404Z","shell.execute_reply":"2025-12-17T16:43:51.510612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import DetrImageProcessor\n\nfeature_extractor = DetrImageProcessor.from_pretrained(\"facebook/detr-resnet-50\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:44:47.656284Z","iopub.execute_input":"2025-12-17T16:44:47.656966Z","iopub.status.idle":"2025-12-17T16:44:47.770874Z","shell.execute_reply.started":"2025-12-17T16:44:47.656933Z","shell.execute_reply":"2025-12-17T16:44:47.770119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q transformers datasets evaluate\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:44:57.238444Z","iopub.execute_input":"2025-12-17T16:44:57.239197Z","iopub.status.idle":"2025-12-17T16:45:00.701668Z","shell.execute_reply.started":"2025-12-17T16:44:57.239166Z","shell.execute_reply":"2025-12-17T16:45:00.700772Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import DetrImageProcessor\n\nfeature_extractor = DetrImageProcessor.from_pretrained(\"facebook/detr-resnet-50\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T16:45:05.2531Z","iopub.execute_input":"2025-12-17T16:45:05.25354Z","iopub.status.idle":"2025-12-17T16:45:05.37051Z","shell.execute_reply.started":"2025-12-17T16:45:05.253504Z","shell.execute_reply":"2025-12-17T16:45:05.369922Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --upgrade transformers\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:00.39984Z","iopub.execute_input":"2025-12-17T18:08:00.400175Z","iopub.status.idle":"2025-12-17T18:08:15.729076Z","shell.execute_reply.started":"2025-12-17T18:08:00.400147Z","shell.execute_reply":"2025-12-17T18:08:15.728128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\ntrain_images_path = \"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images\"\ntrain_labels_path = \"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\"\n\nprint(os.listdir(train_images_path)[:5])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:20.916879Z","iopub.execute_input":"2025-12-17T18:08:20.917716Z","iopub.status.idle":"2025-12-17T18:08:20.936053Z","shell.execute_reply.started":"2025-12-17T18:08:20.917668Z","shell.execute_reply":"2025-12-17T18:08:20.935232Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#dont run\nimport pydicom\nimport cv2\nfrom tqdm import tqdm\n\nout_dir = \"/kaggle/working/train_images\"\nos.makedirs(out_dir, exist_ok=True)\n\nfor file in tqdm(os.listdir(train_images_path)):\n    ds = pydicom.dcmread(os.path.join(train_images_path, file))\n    img = ds.pixel_array\n    # Normalize pixel values to 0-255\n    img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX).astype(\"uint8\")\n    cv2.imwrite(os.path.join(out_dir, file.replace(\".dcm\", \".png\")), img)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T17:19:50.398278Z","iopub.execute_input":"2025-12-17T17:19:50.398893Z","iopub.status.idle":"2025-12-17T17:33:53.792632Z","shell.execute_reply.started":"2025-12-17T17:19:50.398864Z","shell.execute_reply":"2025-12-17T17:33:53.791716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport json\n\ndf = pd.read_csv(train_labels_path)\n\ncoco = {\n    \"images\": [],\n    \"annotations\": [],\n    \"categories\": [{\"id\": 1, \"name\": \"pneumonia\"}]\n}\n\nann_id = 1\nimage_id_map = {}\n\nfor idx, row in df.iterrows():\n    pid = row.patientId\n    if pid not in image_id_map:\n        image_id_map[pid] = len(image_id_map) + 1\n        coco[\"images\"].append({\n            \"id\": image_id_map[pid],\n            \"file_name\": f\"{pid}.png\",\n            \"height\": 1024,\n            \"width\": 1024\n        })\n    if row.Target == 1:  # pneumonia present\n        x, y, w, h = row.x, row.y, row.width, row.height\n        coco[\"annotations\"].append({\n            \"id\": ann_id,\n            \"image_id\": image_id_map[pid],\n            \"category_id\": 1,\n            \"bbox\": [x, y, w, h],\n            \"area\": w * h,\n            \"iscrowd\": 0\n        })\n        ann_id += 1\n\n# Save COCO JSON\ncoco_json_path = \"/kaggle/working/rsna_train_coco.json\"\nwith open(coco_json_path, \"w\") as f:\n    json.dump(coco, f)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:26.844739Z","iopub.execute_input":"2025-12-17T18:08:26.845079Z","iopub.status.idle":"2025-12-17T18:08:28.639068Z","shell.execute_reply.started":"2025-12-17T18:08:26.845051Z","shell.execute_reply":"2025-12-17T18:08:28.638271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from datasets import load_dataset\n\ncoco_json_path = \"/kaggle/working/rsna_train_coco.json\"\n\ndataset = load_dataset(\n    \"json\",  # use \"json\" for local JSON files\n    data_files={\"train\": coco_json_path}\n)\n\nprint(dataset)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:34.449789Z","iopub.execute_input":"2025-12-17T18:08:34.450144Z","iopub.status.idle":"2025-12-17T18:08:34.696246Z","shell.execute_reply.started":"2025-12-17T18:08:34.450113Z","shell.execute_reply":"2025-12-17T18:08:34.695425Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset[\"train\"][0][\"images\"]       # is a list of dicts\ndataset[\"train\"][0][\"annotations\"]  # is a list of dicts\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:38.011308Z","iopub.execute_input":"2025-12-17T18:08:38.011615Z","iopub.status.idle":"2025-12-17T18:08:38.727266Z","shell.execute_reply.started":"2025-12-17T18:08:38.011589Z","shell.execute_reply":"2025-12-17T18:08:38.726495Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from datasets import Dataset\n\n# The first (and only) row\nrow = dataset[\"train\"][0]\n\nimages = row[\"images\"]         # list of image dicts\nannotations = row[\"annotations\"]  # list of annotation dicts\n\n# Map image_id to annotations\nann_map = {}\nfor ann in annotations:\n    img_id = ann[\"image_id\"]\n    if img_id not in ann_map:\n        ann_map[img_id] = []\n    ann_map[img_id].append(ann)\n\n# Create one row per image\nrows = []\nfor img in images:\n    img_id = img[\"id\"]\n    rows.append({\n        \"file_name\": img[\"file_name\"],\n        \"height\": img[\"height\"],\n        \"width\": img[\"width\"],\n        \"annotations\": ann_map.get(img_id, [])\n    })\n\n# Convert to proper Dataset\ndataset = Dataset.from_list(rows)\nprint(dataset)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:48.789916Z","iopub.execute_input":"2025-12-17T18:08:48.790506Z","iopub.status.idle":"2025-12-17T18:08:49.40447Z","shell.execute_reply.started":"2025-12-17T18:08:48.790477Z","shell.execute_reply":"2025-12-17T18:08:49.403599Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nfrom PIL import Image\nimport os\nimport numpy as np\n\nimages_folder = \"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images\"\nresized_folder = \"/kaggle/working/train_images_512\"\nos.makedirs(resized_folder, exist_ok=True)\n\nfor i, file_name in enumerate(os.listdir(images_folder)):\n    if i % 100 == 0:\n        print(f\"Processing {i}/{len(os.listdir(images_folder))} images\")\n    img_path = os.path.join(images_folder, file_name)\n    ds = pydicom.dcmread(img_path)\n    img = ds.pixel_array\n    img = Image.fromarray(img).convert(\"RGB\")\n    img = img.resize((512, 512))\n    img.save(os.path.join(resized_folder, file_name.replace(\".dcm\", \".png\")))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:08:55.084579Z","iopub.execute_input":"2025-12-17T18:08:55.084889Z","iopub.status.idle":"2025-12-17T18:44:04.854683Z","shell.execute_reply.started":"2025-12-17T18:08:55.08486Z","shell.execute_reply":"2025-12-17T18:44:04.854058Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport json\n\ncsv_path = \"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv\"\ndf = pd.read_csv(csv_path)\n\n# Only keep rows with pneumonia\ndf = df[df['Target'] == 1]\n\nimages = []\nannotations = []\ncategories = [{\"id\": 1, \"name\": \"pneumonia\"}]\n\nimage_id_map = {}\nann_id = 1\n\nfor idx, row in df.iterrows():\n    file_name = row['patientId'] + \".png\"\n    if file_name not in image_id_map:\n        img_id = len(image_id_map) + 1\n        image_id_map[file_name] = img_id\n        images.append({\n            \"id\": img_id,\n            \"file_name\": file_name,\n            \"height\": 512,\n            \"width\": 512\n        })\n    else:\n        img_id = image_id_map[file_name]\n    \n    x = row['x']\n    y = row['y']\n    w = row['width']\n    h = row['height']\n\n    annotations.append({\n        \"id\": ann_id,\n        \"image_id\": img_id,\n        \"category_id\": 1,\n        \"bbox\": [x, y, w, h],\n        \"iscrowd\": 0\n    })\n    ann_id += 1\n\ncoco_json = {\n    \"images\": images,\n    \"annotations\": annotations,\n    \"categories\": categories\n}\n\nwith open(\"/kaggle/working/train_annotations.json\", \"w\") as f:\n    json.dump(coco_json, f)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:52:35.127681Z","iopub.execute_input":"2025-12-17T18:52:35.128551Z","iopub.status.idle":"2025-12-17T18:52:35.741453Z","shell.execute_reply.started":"2025-12-17T18:52:35.12851Z","shell.execute_reply":"2025-12-17T18:52:35.740659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from datasets import load_dataset\n\ndataset = load_dataset(\n    \"json\",\n    data_files={\"train\": \"/kaggle/working/train_annotations.json\"},\n)\ndataset\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:52:50.991076Z","iopub.execute_input":"2025-12-17T18:52:50.9916Z","iopub.status.idle":"2025-12-17T18:52:51.191835Z","shell.execute_reply.started":"2025-12-17T18:52:50.991567Z","shell.execute_reply":"2025-12-17T18:52:51.191295Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nfrom transformers import DetrImageProcessor\n\nfeature_extractor = DetrImageProcessor.from_pretrained(\"facebook/detr-resnet-50\")\n\nimages_folder = \"/kaggle/working/train_images_512\"\n\ndef gen():\n    row = dataset[\"train\"][0]  # your JSON contains all data in first row\n    for img in row[\"images\"]:\n        file_name = img[\"file_name\"]\n        img_path = os.path.join(images_folder, file_name)\n        image = np.array(Image.open(img_path).convert(\"RGB\"))\n\n        anns = [a for a in row[\"annotations\"] if a[\"image_id\"] == img[\"id\"]]\n        boxes = [a[\"bbox\"] for a in anns]\n        labels = [a[\"category_id\"] for a in anns]\n\n        yield {\n            \"pixel_values\": image,\n            \"labels\": {\"boxes\": boxes, \"class_labels\": labels}\n        }\n\nfrom datasets import Dataset\nhf_dataset = Dataset.from_generator(gen)\nhf_dataset\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:53:11.517445Z","iopub.execute_input":"2025-12-17T18:53:11.517955Z","iopub.status.idle":"2025-12-17T18:55:10.307586Z","shell.execute_reply.started":"2025-12-17T18:53:11.517926Z","shell.execute_reply":"2025-12-17T18:55:10.306726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import DetrForObjectDetection\n\nnum_classes = 2  # background + pneumonia\n\nmodel = DetrForObjectDetection.from_pretrained(\n    \"facebook/detr-resnet-50\",\n    num_labels=num_classes,\n    ignore_mismatched_sizes=True\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:55:37.073961Z","iopub.execute_input":"2025-12-17T18:55:37.074708Z","iopub.status.idle":"2025-12-17T18:55:46.600678Z","shell.execute_reply.started":"2025-12-17T18:55:37.074678Z","shell.execute_reply":"2025-12-17T18:55:46.600074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from transformers import TrainingArguments, Trainer\n\ntraining_args = TrainingArguments(\n    output_dir=\"./detr_rsna\",\n    per_device_train_batch_size=2,\n    learning_rate=2e-5,\n    num_train_epochs=12,\n    save_strategy=\"epoch\",\n    logging_steps=50\n)\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=hf_dataset,\n    tokenizer=feature_extractor\n)\n\ntrainer.train()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T18:56:17.506415Z","iopub.execute_input":"2025-12-17T18:56:17.50674Z","iopub.status.idle":"2025-12-17T19:38:52.38765Z","shell.execute_reply.started":"2025-12-17T18:56:17.506712Z","shell.execute_reply":"2025-12-17T19:38:52.386613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport torch\nfrom transformers import DetrForObjectDetection, DetrImageProcessor, TrainingArguments, Trainer\nfrom datasets import load_dataset\nfrom PIL import Image\nimport numpy as np\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\nprint(\"Using device:\", device)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T19:41:43.66909Z","iopub.execute_input":"2025-12-17T19:41:43.66967Z","iopub.status.idle":"2025-12-17T19:41:43.674801Z","shell.execute_reply.started":"2025-12-17T19:41:43.669637Z","shell.execute_reply":"2025-12-17T19:41:43.674057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"feature_extractor = DetrImageProcessor.from_pretrained(\"facebook/detr-resnet-50\")\n\nmodel = DetrForObjectDetection.from_pretrained(\n    \"facebook/detr-resnet-50\",\n    num_labels=2,  # your dataset has 2 classes\n    ignore_mismatched_sizes=True  # allows different number of classes\n).to(device)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T19:42:00.278778Z","iopub.execute_input":"2025-12-17T19:42:00.279501Z","iopub.status.idle":"2025-12-17T19:42:01.113852Z","shell.execute_reply.started":"2025-12-17T19:42:00.279472Z","shell.execute_reply":"2025-12-17T19:42:01.113243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"coco_json_path = \"/kaggle/working/train_annotations.json\"  # make sure this exists\n\nhf_dataset = load_dataset(\n    \"json\",\n    data_files={\"train\": coco_json_path},\n    field=\"images\"\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T19:42:24.548804Z","iopub.execute_input":"2025-12-17T19:42:24.549562Z","iopub.status.idle":"2025-12-17T19:42:24.785583Z","shell.execute_reply.started":"2025-12-17T19:42:24.54953Z","shell.execute_reply":"2025-12-17T19:42:24.784963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nfrom transformers import DetrImageProcessor\n\nfeature_extractor = DetrImageProcessor.from_pretrained(\"facebook/detr-resnet-50\")\n\nimages_folder = \"/kaggle/working/train_images_512\"\n\ndef gen():\n    row = dataset[\"train\"][0]  # your JSON contains all data in first row\n    for img in row[\"images\"]:\n        file_name = img[\"file_name\"]\n        img_path = os.path.join(images_folder, file_name)\n        image = np.array(Image.open(img_path).convert(\"RGB\"))\n\n        anns = [a for a in row[\"annotations\"] if a[\"image_id\"] == img[\"id\"]]\n        boxes = [a[\"bbox\"] for a in anns]\n        labels = [a[\"category_id\"] for a in anns]\n\n        yield {\n            \"pixel_values\": image,\n            \"labels\": {\"boxes\": boxes, \"class_labels\": labels}\n        }\n\nfrom datasets import Dataset\nhf_dataset = Dataset.from_generator(gen)\nhf_dataset\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T19:44:01.880137Z","iopub.execute_input":"2025-12-17T19:44:01.880451Z","iopub.status.idle":"2025-12-17T19:44:01.997447Z","shell.execute_reply.started":"2025-12-17T19:44:01.880424Z","shell.execute_reply":"2025-12-17T19:44:01.99672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"training_args = TrainingArguments(\n    output_dir=\"./detr_rsna\",\n    per_device_train_batch_size=2,\n    learning_rate=2e-5,\n    num_train_epochs=3,      # start with 3 for testing\n    save_strategy=\"epoch\",\n    logging_steps=50,\n    report_to=[],            # disable W&B\n    fp16=True                # use mixed precision if GPU supports\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T19:44:17.13185Z","iopub.execute_input":"2025-12-17T19:44:17.132588Z","iopub.status.idle":"2025-12-17T19:44:17.171959Z","shell.execute_reply.started":"2025-12-17T19:44:17.132548Z","shell.execute_reply":"2025-12-17T19:44:17.171288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=hf_dataset,\n    tokenizer=feature_extractor\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-17T19:44:34.227509Z","iopub.execute_input":"2025-12-17T19:44:34.22783Z","iopub.status.idle":"2025-12-17T19:44:34.244781Z","shell.execute_reply.started":"2025-12-17T19:44:34.2278Z","shell.execute_reply":"2025-12-17T19:44:34.243932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}