Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed rfdetr/assets/results.png
Binary file not shown.
Binary file removed rfdetr/assets/test.jpg
Binary file not shown.
Binary file removed rfdetr/assets/test_annotated.jpg
Binary file not shown.
9 changes: 8 additions & 1 deletion rfdetr/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
from rfdetr import RFDETRBase
import torch
import os
from rfdetr.util.logger import get_logger

logger = get_logger()

def download_dataset(rf_project: roboflow.Project, dataset_version: int):
versions = rf_project.versions()
Expand All @@ -34,7 +37,7 @@ def download_dataset(rf_project: roboflow.Project, dataset_version: int):

def train_from_rf_project(rf_project: roboflow.Project, dataset_version: int):
location = download_dataset(rf_project, dataset_version)
print(location)
logger.info(f"Using dataset from location: {location}")
rf_detr = RFDETRBase()
device_supports_cuda = torch.cuda.is_available()
rf_detr.train(
Expand All @@ -46,6 +49,7 @@ def train_from_rf_project(rf_project: roboflow.Project, dataset_version: int):

def train_from_coco_dir(coco_dir: str):
rf_detr = RFDETRBase()
device_supports_cuda = torch.cuda.is_available()
rf_detr.train(
dataset_dir=coco_dir,
epochs=1,
Expand All @@ -63,6 +67,7 @@ def trainer():
args = parser.parse_args()

if args.coco_dir is not None:
logger.info(f"Training from COCO directory: {args.coco_dir}")
train_from_coco_dir(args.coco_dir)
return

Expand All @@ -74,9 +79,11 @@ def trainer():
)

if args.workspace is not None:
logger.info(f"Using Roboflow project: {args.workspace}/{args.project_name}")
rf = roboflow.Roboflow(api_key=args.api_key)
project = rf.workspace(args.workspace).project(args.project_name)
else:
logger.info("No project specified, using first project from RF100VL")
projects = get_rf100vl_projects(api_key=args.api_key)
project = projects[0].rf_project

Expand Down
104 changes: 52 additions & 52 deletions rfdetr/datasets/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import torchvision

import rfdetr.datasets.transforms as T
from rfdetr.util.logger import get_logger

logger = get_logger()


def compute_multi_scale_scales(resolution, expanded_scales=False):
Expand Down Expand Up @@ -118,7 +121,7 @@ def make_coco_transforms(image_set, resolution, multi_scale=False, expanded_scal
if multi_scale:
# scales = [448, 512, 576, 640, 704, 768, 832, 896]
scales = compute_multi_scale_scales(resolution, expanded_scales)
print(scales)
logger.info(f"Using multi-scale training with scales: {scales}")

if image_set == 'train':
return T.Compose([
Expand Down Expand Up @@ -162,7 +165,7 @@ def make_coco_transforms_square_div_64(image_set, resolution, multi_scale=False,
if multi_scale:
# scales = [448, 512, 576, 640, 704, 768, 832, 896]
scales = compute_multi_scale_scales(resolution, expanded_scales)
print(scales)
logger.info(f"Using multi-scale training with square resize and scales: {scales}")

if image_set == 'train':
return T.Compose([
Expand Down Expand Up @@ -193,58 +196,55 @@ def make_coco_transforms_square_div_64(image_set, resolution, multi_scale=False,

def build(image_set, args, resolution):
root = Path(args.coco_path)
assert root.exists(), f'provided COCO path {root} does not exist'
mode = 'instances'
PATHS = {
"train": (root / "train2017", root / "annotations" / f'{mode}_train2017.json'),
"val": (root / "val2017", root / "annotations" / f'{mode}_val2017.json'),
"test": (root / "test2017", root / "annotations" / f'image_info_test-dev2017.json'),
}

img_folder, ann_file = PATHS[image_set.split("_")[0]]

try:
square_resize = args.square_resize
except:
square_resize = False

try:
square_resize_div_64 = args.square_resize_div_64
except:
square_resize_div_64 = False


if square_resize_div_64:
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms_square_div_64(image_set, resolution, multi_scale=args.multi_scale, expanded_scales=args.expanded_scales))
else:
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms(image_set, resolution, multi_scale=args.multi_scale, expanded_scales=args.expanded_scales))
return dataset
if not root.exists():
logger.error(f"COCO path {root} does not exist")
raise FileNotFoundError(f"COCO path {root} does not exist")

mode = 'instances'
PATHS = {
"train": (root / "train2017", root / "annotations" / f'{mode}_train2017.json'),
"val": (root / "val2017", root / "annotations" / f'{mode}_val2017.json'),
"test": (root / "test2017", root / "annotations" / f'image_info_test-dev2017.json'),
}

img_folder, ann_file = PATHS[image_set.split("_")[0]]
square_resize_div_64 = getattr(args, 'square_resize_div_64', False)

if square_resize_div_64:
logger.info(f"Building COCO {image_set} dataset with square resize at resolution {resolution}")
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms_square_div_64(image_set, resolution, multi_scale=args.multi_scale, expanded_scales=args.expanded_scales))
else:
logger.info(f"Building COCO {image_set} dataset at resolution {resolution}")
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms(image_set, resolution, multi_scale=args.multi_scale, expanded_scales=args.expanded_scales))
return dataset
except Exception as e:
logger.error(f"Failed to build COCO dataset: {e}")
raise

def build_roboflow(image_set, args, resolution):
root = Path(args.dataset_dir)
assert root.exists(), f'provided Roboflow path {root} does not exist'
mode = 'instances'
PATHS = {
"train": (root / "train", root / "train" / "_annotations.coco.json"),
"val": (root / "valid", root / "valid" / "_annotations.coco.json"),
"test": (root / "test", root / "test" / "_annotations.coco.json"),
}

img_folder, ann_file = PATHS[image_set.split("_")[0]]

try:
square_resize = args.square_resize
except:
square_resize = False

try:
square_resize_div_64 = args.square_resize_div_64
except:
square_resize_div_64 = False


if square_resize_div_64:
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms_square_div_64(image_set, resolution, multi_scale=args.multi_scale))
else:
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms(image_set, resolution, multi_scale=args.multi_scale))
return dataset
if not root.exists():
logger.error(f"Roboflow dataset path {root} does not exist")
raise FileNotFoundError(f"Roboflow dataset path {root} does not exist")

PATHS = {
"train": (root / "train", root / "train" / "_annotations.coco.json"),
"val": (root / "valid", root / "valid" / "_annotations.coco.json"),
"test": (root / "test", root / "test" / "_annotations.coco.json"),
}

img_folder, ann_file = PATHS[image_set.split("_")[0]]
square_resize_div_64 = getattr(args, 'square_resize_div_64', False)

if square_resize_div_64:
logger.info(f"Building Roboflow {image_set} dataset with square resize at resolution {resolution}")
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms_square_div_64(image_set, resolution, multi_scale=args.multi_scale, expanded_scales=args.expanded_scales))
else:
logger.info(f"Building Roboflow {image_set} dataset at resolution {resolution}")
dataset = CocoDetection(img_folder, ann_file, transforms=make_coco_transforms(image_set, resolution, multi_scale=args.multi_scale, expanded_scales=args.expanded_scales))
Comment on lines +241 to +246
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a bug in the existing implementation that is stopping expanded_scales from getting propagated to the dataset when training on roboflow data. you can see it clearly here. this change fixes that bug, which will likely increase performance. however, it'll also slow down training a bit and increase memory usage a bit. not sure that's what we want. worth discussing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isaacrob-roboflow what's your suggestion here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to keep the bug for now but comment that it exists, and then run a proper ablation so we know if it's worth including?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@isaacrob-roboflow That would be great! I’d really like to get this PR merged soon, since it touches a lot of areas and keeps running into conflicts. Plus, I’d rather not merge it right before the release — it increases the risk that something might go wrong.

Would you like me to add a TODO comment on line 239?

TODO: There is a bug in the current implementation that prevents `expanded_scales` from being propagated to the dataset when training on Roboflow data.

Alternatively, we could create a GitHub issue to track the problem. To be honest, I’d prefer the second option.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do both and tag the issue in the comment?

return dataset
except Exception as e:
logger.error(f"Failed to build Roboflow dataset: {e}")
raise
2 changes: 1 addition & 1 deletion rfdetr/datasets/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import PIL
import numpy as np

try:
from collections.abc import Sequence
except Exception:
from collections import Sequence
from numbers import Number
import torch
import torchvision.transforms as T
# from detectron2.data import transforms as DT
import torchvision.transforms.functional as F

from rfdetr.util.box_ops import box_xyxy_to_cxcywh
Expand Down
23 changes: 7 additions & 16 deletions rfdetr/deploy/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,25 @@
on the device.
"""
import argparse
import copy
import contextlib
import datetime
import copy
import json
import os
import os.path as osp
import random
import time
import ast
from pathlib import Path
from collections import namedtuple, OrderedDict

from pycocotools.cocoeval import COCOeval
from pycocotools.coco import COCO
import pycocotools.mask as mask_util

import numpy as np
from PIL import Image
import onnxruntime as nxrun
import pycuda.driver as cuda
import tensorrt as trt
import torch
from torch.utils.data import DataLoader, DistributedSampler
import torchvision.transforms as T
import torchvision.transforms.functional as F
import tqdm

import pycuda.driver as cuda
import pycuda.autoinit
import onnxruntime as nxrun
import tensorrt as trt
from PIL import Image
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval


def parser_args():
Expand Down
Loading