{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":19989,"databundleVersionId":1160143,"sourceType":"competition"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install -U albumentations\n!pip install -U lightning\n!pip install -U optuna optuna-integration","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-31T15:50:28.901468Z","iopub.execute_input":"2024-10-31T15:50:28.901759Z","iopub.status.idle":"2024-10-31T15:51:08.616068Z","shell.execute_reply.started":"2024-10-31T15:50:28.901726Z","shell.execute_reply":"2024-10-31T15:51:08.614959Z"},"trusted":true,"collapsed":true,"jupyter":{"outputs_hidden":true}},"outputs":[{"name":"stdout","text":"Requirement already satisfied: albumentations in /opt/conda/lib/python3.10/site-packages (1.4.17)\nCollecting albumentations\n  Downloading albumentations-1.4.20-py3-none-any.whl.metadata (32 kB)\nRequirement already satisfied: numpy>=1.24.4 in /opt/conda/lib/python3.10/site-packages (from albumentations) (1.26.4)\nRequirement already satisfied: scipy>=1.10.0 in /opt/conda/lib/python3.10/site-packages (from albumentations) (1.14.1)\nRequirement already satisfied: PyYAML in /opt/conda/lib/python3.10/site-packages (from albumentations) (6.0.2)\nRequirement already satisfied: pydantic>=2.7.0 in /opt/conda/lib/python3.10/site-packages (from albumentations) (2.9.2)\nCollecting albucore==0.0.19 (from albumentations)\n  Downloading albucore-0.0.19-py3-none-any.whl.metadata (5.2 kB)\nRequirement already satisfied: eval-type-backport in /opt/conda/lib/python3.10/site-packages (from albumentations) (0.2.0)\nRequirement already satisfied: opencv-python-headless>=4.9.0.80 in /opt/conda/lib/python3.10/site-packages (from albumentations) (4.10.0.84)\nCollecting stringzilla>=3.10.4 (from albucore==0.0.19->albumentations)\n  Downloading stringzilla-3.10.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl.metadata (79 kB)\n\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m79.8/79.8 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n\u001b[?25hRequirement already satisfied: annotated-types>=0.6.0 in /opt/conda/lib/python3.10/site-packages (from pydantic>=2.7.0->albumentations) (0.7.0)\nRequirement already satisfied: pydantic-core==2.23.4 in /opt/conda/lib/python3.10/site-packages (from pydantic>=2.7.0->albumentations) (2.23.4)\nRequirement already satisfied: typing-extensions>=4.6.1 in /opt/conda/lib/python3.10/site-packages (from pydantic>=2.7.0->albumentations) (4.12.2)\nDownloading albumentations-1.4.20-py3-none-any.whl (225 kB)\n\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m225.8/225.8 kB\u001b[0m \u001b[31m10.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n\u001b[?25hDownloading albucore-0.0.19-py3-none-any.whl (11 kB)\nDownloading stringzilla-3.10.7-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl (291 kB)\n\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m291.4/291.4 kB\u001b[0m \u001b[31m20.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n\u001b[?25hInstalling collected packages: stringzilla, albucore, albumentations\n  Attempting uninstall: albucore\n    Found existing installation: albucore 0.0.17\n    Uninstalling albucore-0.0.17:\n      Successfully uninstalled albucore-0.0.17\n  Attempting uninstall: albumentations\n    Found existing installation: albumentations 1.4.17\n    Uninstalling albumentations-1.4.17:\n      Successfully uninstalled albumentations-1.4.17\nSuccessfully installed albucore-0.0.19 albumentations-1.4.20 stringzilla-3.10.7\nCollecting lightning\n  Downloading lightning-2.4.0-py3-none-any.whl.metadata (38 kB)\nRequirement already satisfied: PyYAML<8.0,>=5.4 in /opt/conda/lib/python3.10/site-packages (from lightning) (6.0.2)\nRequirement already satisfied: fsspec<2026.0,>=2022.5.0 in /opt/conda/lib/python3.10/site-packages (from fsspec[http]<2026.0,>=2022.5.0->lightning) (2024.6.1)\nRequirement already satisfied: lightning-utilities<2.0,>=0.10.0 in /opt/conda/lib/python3.10/site-packages (from lightning) (0.11.7)\nRequirement already satisfied: packaging<25.0,>=20.0 in /opt/conda/lib/python3.10/site-packages (from lightning) (21.3)\nRequirement already satisfied: torch<4.0,>=2.1.0 in /opt/conda/lib/python3.10/site-packages (from lightning) (2.4.0)\nRequirement already satisfied: torchmetrics<3.0,>=0.7.0 in /opt/conda/lib/python3.10/site-packages (from lightning) (1.4.2)\nRequirement already satisfied: tqdm<6.0,>=4.57.0 in /opt/conda/lib/python3.10/site-packages (from lightning) (4.66.4)\nRequirement already satisfied: typing-extensions<6.0,>=4.4.0 in /opt/conda/lib/python3.10/site-packages (from lightning) (4.12.2)\nRequirement already satisfied: pytorch-lightning in /opt/conda/lib/python3.10/site-packages (from lightning) (2.4.0)\nRequirement already satisfied: aiohttp!=4.0.0a0,!=4.0.0a1 in /opt/conda/lib/python3.10/site-packages (from fsspec[http]<2026.0,>=2022.5.0->lightning) (3.9.5)\nRequirement already satisfied: setuptools in /opt/conda/lib/python3.10/site-packages (from lightning-utilities<2.0,>=0.10.0->lightning) (70.0.0)\nRequirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /opt/conda/lib/python3.10/site-packages (from packaging<25.0,>=20.0->lightning) (3.1.2)\nRequirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from torch<4.0,>=2.1.0->lightning) (3.15.1)\nRequirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch<4.0,>=2.1.0->lightning) (1.13.3)\nRequirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch<4.0,>=2.1.0->lightning) (3.3)\nRequirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch<4.0,>=2.1.0->lightning) (3.1.4)\nRequirement already satisfied: numpy>1.20.0 in /opt/conda/lib/python3.10/site-packages (from torchmetrics<3.0,>=0.7.0->lightning) (1.26.4)\nRequirement already satisfied: aiosignal>=1.1.2 in /opt/conda/lib/python3.10/site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (1.3.1)\nRequirement already satisfied: attrs>=17.3.0 in /opt/conda/lib/python3.10/site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (23.2.0)\nRequirement already satisfied: frozenlist>=1.1.1 in /opt/conda/lib/python3.10/site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (1.4.1)\nRequirement already satisfied: multidict<7.0,>=4.5 in /opt/conda/lib/python3.10/site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (6.0.5)\nRequirement already satisfied: yarl<2.0,>=1.0 in /opt/conda/lib/python3.10/site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (1.9.4)\nRequirement already satisfied: async-timeout<5.0,>=4.0 in /opt/conda/lib/python3.10/site-packages (from aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (4.0.3)\nRequirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.10/site-packages (from jinja2->torch<4.0,>=2.1.0->lightning) (2.1.5)\nRequirement already satisfied: mpmath<1.4,>=1.1.0 in /opt/conda/lib/python3.10/site-packages (from sympy->torch<4.0,>=2.1.0->lightning) (1.3.0)\nRequirement already satisfied: idna>=2.0 in /opt/conda/lib/python3.10/site-packages (from yarl<2.0,>=1.0->aiohttp!=4.0.0a0,!=4.0.0a1->fsspec[http]<2026.0,>=2022.5.0->lightning) (3.7)\nDownloading lightning-2.4.0-py3-none-any.whl (810 kB)\n\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m811.0/811.0 kB\u001b[0m \u001b[31m14.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n\u001b[?25hInstalling collected packages: lightning\nSuccessfully installed lightning-2.4.0\nRequirement already satisfied: optuna in /opt/conda/lib/python3.10/site-packages (4.0.0)\nCollecting optuna-integration\n  Downloading optuna_integration-4.0.0-py3-none-any.whl.metadata (11 kB)\nRequirement already satisfied: alembic>=1.5.0 in /opt/conda/lib/python3.10/site-packages (from optuna) (1.13.3)\nRequirement already satisfied: colorlog in /opt/conda/lib/python3.10/site-packages (from optuna) (6.8.2)\nRequirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (from optuna) (1.26.4)\nRequirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.10/site-packages (from optuna) (21.3)\nRequirement already satisfied: sqlalchemy>=1.3.0 in /opt/conda/lib/python3.10/site-packages (from optuna) (2.0.30)\nRequirement already satisfied: tqdm in /opt/conda/lib/python3.10/site-packages (from optuna) (4.66.4)\nRequirement already satisfied: PyYAML in /opt/conda/lib/python3.10/site-packages (from optuna) (6.0.2)\nRequirement already satisfied: Mako in /opt/conda/lib/python3.10/site-packages (from alembic>=1.5.0->optuna) (1.3.5)\nRequirement already satisfied: typing-extensions>=4 in /opt/conda/lib/python3.10/site-packages (from alembic>=1.5.0->optuna) (4.12.2)\nRequirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /opt/conda/lib/python3.10/site-packages (from packaging>=20.0->optuna) (3.1.2)\nRequirement already satisfied: greenlet!=0.4.17 in /opt/conda/lib/python3.10/site-packages (from sqlalchemy>=1.3.0->optuna) (3.0.3)\nRequirement already satisfied: MarkupSafe>=0.9.2 in /opt/conda/lib/python3.10/site-packages (from Mako->alembic>=1.5.0->optuna) (2.1.5)\nDownloading optuna_integration-4.0.0-py3-none-any.whl (96 kB)\n\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m96.9/96.9 kB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n\u001b[?25hInstalling collected packages: optuna-integration\nSuccessfully installed optuna-integration-4.0.0\n","output_type":"stream"}],"execution_count":1},{"cell_type":"code","source":"import os\nimport optuna\nimport torch\nimport torch.optim as optim\nfrom torchvision import models, transforms\nfrom torch.utils.data import Dataset, DataLoader\nfrom optuna.integration import PyTorchLightningPruningCallback\nimport lightning.pytorch as pl\nfrom PIL import Image\nimport pandas as pd\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-10-31T15:51:08.617494Z","iopub.execute_input":"2024-10-31T15:51:08.61784Z","iopub.status.idle":"2024-10-31T15:51:17.136825Z","shell.execute_reply.started":"2024-10-31T15:51:08.617806Z","shell.execute_reply":"2024-10-31T15:51:17.136041Z"},"trusted":true},"outputs":[],"execution_count":2},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/global-wheat-detection\"\nTRAIN_IMG_DIR = os.path.join(DATA_DIR, \"train\")        # Directory containing training images\nTRAIN_ANNOTATIONS = os.path.join(DATA_DIR, \"train.csv\")  # Path to the CSV file with annotations, this ","metadata":{"execution":{"iopub.status.busy":"2024-10-31T15:51:17.137837Z","iopub.execute_input":"2024-10-31T15:51:17.138253Z","iopub.status.idle":"2024-10-31T15:51:17.14284Z","shell.execute_reply.started":"2024-10-31T15:51:17.138221Z","shell.execute_reply":"2024-10-31T15:51:17.142006Z"},"trusted":true},"outputs":[],"execution_count":3},{"cell_type":"code","source":"# Import necessary libraries\nimport json\nimport torch\nimport pandas as pd\nfrom torchvision.models.detection import fasterrcnn_resnet50_fpn\nfrom transformers import DetrForObjectDetection\nimport albumentations as A\nfrom albumentations.pytorch.transforms import ToTensorV2\nfrom torch.utils.data import DataLoader, Dataset\nimport matplotlib.pyplot as plt\nimport pytorch_lightning as pl\nfrom pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping\nfrom optuna.integration import PyTorchLightningPruningCallback\nimport optuna\n\n# Separate augmentation strategies\nIMAGENET_MEAN = [0.485, 0.456, 0.406]\nIMAGENET_STD = [0.229, 0.224, 0.225]\n\ndef get_augmentations():\n    return {\n        \"train\": A.Compose([\n            A.HorizontalFlip(p=0.5),\n            A.RandomBrightnessContrast(p=0.2),\n            A.HueSaturationValue(p=0.2),\n            A.RandomRotate90(p=0.5),\n            A.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),\n            ToTensorV2(),\n        ]),\n        \"val\": A.Compose([\n            A.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),\n            ToTensorV2(),\n        ])\n    }\n\n# Unified and automated pipeline class\nclass AutomatedObjectDetectionPipeline(pl.LightningModule):\n    def __init__(self, config):\n        super().__init__()\n        self.config = config\n        self.model = self.initialize_model()\n        self.validation_losses = []\n\n    def initialize_model(self):\n        if self.config['model'] == 'faster_rcnn':\n            model = fasterrcnn_resnet50_fpn(pretrained=True)\n            in_features = model.roi_heads.box_predictor.cls_score.in_features\n            model.roi_heads.box_predictor = torch.nn.Linear(in_features, self.config['num_classes'])\n        elif self.config['model'] == 'detr':\n            model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-50', num_labels=self.config['num_classes'])\n        else:\n            raise ValueError(f\"Unsupported model: {self.config['model']}\")\n        return model\n\n    def forward(self, images, targets=None):\n        return self.model(images, targets)\n\n    def training_step(self, batch, batch_idx):\n        images, targets = batch\n        loss_dict = self.model(images, targets)\n        loss = sum(loss for loss in loss_dict.values())\n        self.log(\"train_loss\", loss, on_step=True, on_epoch=True, prog_bar=True)\n        return loss\n\n    def validation_step(self, batch, batch_idx):\n        images, targets = batch\n        loss_dict = self.model(images, targets)\n        val_loss = sum(loss for loss in loss_dict.values())\n        self.log(\"val_loss\", val_loss, on_step=False, on_epoch=True, prog_bar=True)\n        return val_loss\n\n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=self.config['lr'])\n        scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)\n        return [optimizer], [scheduler]\n\n    def visualize_predictions(self, images, predictions):\n        \"\"\"Visualize predictions with bounding boxes.\"\"\"\n        for img, pred in zip(images, predictions):\n            plt.figure(figsize=(10, 10))\n            plt.imshow(img.permute(1, 2, 0).cpu().numpy())\n            for box, score in zip(pred['boxes'], pred['scores']):\n                if score > 0.5:  # Threshold for visualization\n                    x_min, y_min, x_max, y_max = box.cpu().numpy()\n                    plt.gca().add_patch(plt.Rectangle((x_min, y_min), x_max - x_min, y_max - y_min,\n                                                      edgecolor='red', facecolor='none', lw=2))\n            plt.axis('off')\n            plt.show()\n\n# Dataset and DataLoader module\nclass ObjectDetectionDataModule(pl.LightningDataModule):\n    def __init__(self, img_dir, annotations_file, batch_size=16, augmentations=None):\n        super().__init__()\n        self.img_dir = img_dir\n        self.annotations_file = annotations_file\n        self.batch_size = batch_size\n        self.augmentations = augmentations or {}\n\n    def setup(self, stage=None):\n        self.train_dataset = ExampleDataset(self.img_dir, self.annotations_file, transforms=self.augmentations.get(\"train\"))\n        self.val_dataset = ExampleDataset(self.img_dir, self.annotations_file, transforms=self.augmentations.get(\"val\"))\n\n    def train_dataloader(self):\n        return DataLoader(self.train_dataset, batch_size=self.batch_size, shuffle=True, collate_fn=self.collate_fn)\n\n    def val_dataloader(self):\n        return DataLoader(self.val_dataset, batch_size=self.batch_size, shuffle=False, collate_fn=self.collate_fn)\n\n    @staticmethod\n    def collate_fn(batch):\n        images, targets = zip(*batch)\n        return list(images), list(targets)\n\n# Example dataset preparation\nclass ExampleDataset(Dataset):\n    def __init__(self, img_dir, annotations_file, transforms):\n        self.img_dir = img_dir\n        self.df = pd.read_csv(annotations_file)\n        self.transforms = transforms\n        self.imgs = self.df['image_id'].unique()\n\n    def __len__(self):\n        return len(self.imgs)\n\n    def __getitem__(self, idx):\n        image_id = self.imgs[idx]\n        img_path = f\"{self.img_dir}/{image_id}.jpg\"\n        image = plt.imread(img_path)\n        records = self.df[self.df['image_id'] == image_id]\n        boxes = [list(map(float, row['bbox'][1:-1].split(','))) for _, row in records.iterrows()]\n        target = {\n            \"boxes\": torch.tensor(boxes, dtype=torch.float32),\n            \"labels\": torch.ones((len(boxes),), dtype=torch.int64)\n        }\n\n        if self.transforms:\n            augmented = self.transforms(image=image)\n            image = augmented['image']\n\n        return image, target\n\n# Load configuration from JSON file\nwith open('config.json', 'r') as f:\n    config = json.load(f)\n\n# Function to train and evaluate models\ndef train_and_evaluate(model_name):\n    config['model'] = model_name\n    pipeline = AutomatedObjectDetectionPipeline(config)\n    data_module = ObjectDetectionDataModule(\n        img_dir=config['img_dir'],\n        annotations_file=config['annotations_file'],\n        batch_size=config['batch_size'],\n        augmentations=get_augmentations()\n    )\n\n    checkpoint_callback = ModelCheckpoint(monitor=\"val_loss\", save_top_k=1, mode=\"min\", filename=f\"{model_name}_best_model\")\n    trainer = pl.Trainer(max_epochs=config['max_epochs'], callbacks=[\n        checkpoint_callback,\n        EarlyStopping(monitor=\"val_loss\", patience=config['patience'])\n    ])\n    trainer.fit(pipeline, datamodule=data_module)\n\n    return trainer.callback_metrics[\"val_loss\"].item(), checkpoint_callback.best_model_path\n\n# Train and compare models\nresults = {}\nfor model_name in [\"faster_rcnn\", \"detr\"]:\n    print(f\"Training and evaluating model: {model_name}\")\n    val_loss, best_model_path = train_and_evaluate(model_name)\n    results[model_name] = {\"val_loss\": val_loss, \"best_model_path\": best_model_path}\n\n# Select the best model\nbest_model = min(results, key=lambda x: results[x][\"val_loss\"])\nprint(f\"Best model: {best_model} with validation loss: {results[best_model]['val_loss']}\")\nprint(f\"Best model path: {results[best_model]['best_model_path']}\")\n\n# Load the best model for visualization or inference\nconfig['model'] = best_model\npipeline = AutomatedObjectDetectionPipeline(config)\npipeline.load_state_dict(torch.load(results[best_model]['best_model_path']))\npipeline.eval()\n\ndef test_and_visualize():\n    data_module = ObjectDetectionDataModule(\n        img_dir=config['img_dir'],\n        annotations_file=config['annotations_file'],\n        batch_size=config['batch_size'],\n        augmentations=get_augmentations()\n    )\n    data_module.setup()\n    test_loader = data_module.val_dataloader()\n    images, targets = next(iter(test_loader))\n    with torch.no_grad():\n        predictions = pipeline(images)\n    pipeline.visualize_predictions(images, predictions)\n\ntest_and_visualize()\n","metadata":{"execution":{"iopub.status.busy":"2024-10-31T15:52:53.718716Z","iopub.execute_input":"2024-10-31T15:52:53.719193Z","iopub.status.idle":"2024-10-31T16:13:23.944103Z","shell.execute_reply.started":"2024-10-31T15:52:53.719147Z","shell.execute_reply":"2024-10-31T16:13:23.94318Z"},"trusted":true},"outputs":[{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n  warnings.warn(\n/opt/conda/lib/python3.10/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=FasterRCNN_ResNet50_FPN_Weights.COCO_V1`. You can also use `weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT` to get the most up-to-date weights.\n  warnings.warn(msg)\nDownloading: \"https://download.pytorch.org/models/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth\" to /root/.cache/torch/hub/checkpoints/fasterrcnn_resnet50_fpn_coco-258fb6c6.pth\n100%|██████████| 160M/160M [00:00<00:00, 192MB/s]  \n/opt/conda/lib/python3.10/site-packages/lightning/fabric/connector.py:571: `precision=16` is supported for historical reasons but its usage is discouraged. Please set your precision to 16-mixed instead!\nINFO: Trainer will use only 1 of 2 GPUs because it is running inside an interactive / notebook environment. You may try to set `Trainer(devices=2)` but please note that multi-GPU inside interactive / notebook environments is considered experimental and unstable. Your mileage may vary.\nINFO: Using 16bit Automatic Mixed Precision (AMP)\nINFO: GPU available: True (cuda), used: True\nINFO: TPU available: False, using: 0 TPU cores\nINFO: HPU available: False, using: 0 HPUs\n/opt/conda/lib/python3.10/site-packages/lightning/pytorch/callbacks/model_checkpoint.py:654: Checkpoint directory /kaggle/working exists and is not empty.\nINFO: LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1]\nINFO: \n  | Name  | Type       | Params | Mode \n---------------------------------------------\n0 | model | FasterRCNN | 41.3 M | train\n---------------------------------------------\n41.1 M    Trainable params\n222 K     Non-trainable params\n41.3 M    Total params\n165.197   Total estimated model params size (MB)\n189       Modules in train mode\n0         Modules in eval mode\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Sanity Checking: |          | 0/? [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":""}},"metadata":{}},{"name":"stderr","text":"/opt/conda/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.\n  self.pid = os.fork()\n/opt/conda/lib/python3.10/site-packages/lightning/pytorch/utilities/data.py:78: Trying to infer the `batch_size` from an ambiguous collection. The batch size we found is 3. To avoid any miscalculations, use `self.log(..., batch_size=batch_size)`.\n/opt/conda/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.\n  self.pid = os.fork()\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Training: |          | 0/? [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4e23dc4d5eee45ed993fded924740402"}},"metadata":{}},{"name":"stdout","text":"Step 0, Loss: 4.0303754806518555\nStep 50, Loss: 1.7943534851074219\nStep 100, Loss: 1.5380109548568726\nStep 150, Loss: 1.7049401998519897\nStep 200, Loss: 1.6941463947296143\nStep 250, Loss: 1.6187926530838013\nStep 300, Loss: 1.5799098014831543\nStep 350, Loss: 1.4260823726654053\nStep 400, Loss: 1.754647135734558\nStep 450, Loss: 1.8305633068084717\nStep 500, Loss: 1.6969661712646484\nStep 550, Loss: 1.6697561740875244\nStep 600, Loss: 0.8715195059776306\nStep 650, Loss: 1.9416823387145996\nStep 700, Loss: 1.4699360132217407\nStep 750, Loss: 1.8104166984558105\nStep 800, Loss: 1.201194167137146\n","output_type":"stream"},{"name":"stderr","text":"/opt/conda/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.\n  self.pid = os.fork()\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Validation: |          | 0/? [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":""}},"metadata":{}},{"name":"stderr","text":"/opt/conda/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.\n  self.pid = os.fork()\n","output_type":"stream"},{"name":"stdout","text":"Step 0, Loss: 1.705870270729065\nStep 50, Loss: 1.712477445602417\nStep 100, Loss: 1.657142162322998\nStep 150, Loss: 1.7291221618652344\nStep 200, Loss: 1.4629276990890503\nStep 250, Loss: 1.51841139793396\nStep 300, Loss: 1.4584643840789795\nStep 350, Loss: 1.3783345222473145\nStep 400, Loss: 1.7994228601455688\nStep 450, Loss: 1.7493528127670288\nStep 500, Loss: 1.6277891397476196\nStep 550, Loss: 1.6414051055908203\nStep 600, Loss: 1.0368993282318115\nStep 650, Loss: 1.7997113466262817\nStep 700, Loss: 1.5226051807403564\nStep 750, Loss: 1.7679415941238403\nStep 800, Loss: 1.3197801113128662\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"Validation: |          | 0/? [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":""}},"metadata":{}},{"name":"stderr","text":"INFO: `Trainer.fit` stopped: `max_epochs=2` reached.\n","output_type":"stream"}],"execution_count":6},{"cell_type":"code","source":"import torch\nfrom torchvision import models, transforms\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\n\n# Load model\nmodel = models.detection.fasterrcnn_resnet50_fpn(pretrained=False)\nin_features = model.roi_heads.box_predictor.cls_score.in_features\nmodel.roi_heads.box_predictor = models.detection.faster_rcnn.FastRCNNPredictor(in_features, 2)\nmodel.load_state_dict(torch.load(\"/kaggle/working/fasterrcnn_resnet50_fpn.pth\"))\nmodel.eval()\n\n# Load and preprocess image\nimage_path = \"/kaggle/input/global-wheat-detection/test/f5a1f0358.jpg\"\nimage = Image.open(image_path).convert(\"RGB\")\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n])\ninput_image = transform(image).unsqueeze(0)\n\n# Make predictions\nwith torch.no_grad():\n    predictions = model(input_image)\n\n# Visualize results\nfig, ax = plt.subplots(1, figsize=(12, 12))\nax.imshow(image)\nfor box, score in zip(predictions[0]['boxes'], predictions[0]['scores']):\n    if score > 0.25:  # Threshold for visualization\n        x_min, y_min, x_max, y_max = box\n        rect = patches.Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, linewidth=2, edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}