-
Notifications
You must be signed in to change notification settings - Fork 3
benchmark ssdlite detection pipeline #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,102 @@ | ||
import torch | ||
import pathlib | ||
|
||
from torch.hub import tqdm | ||
|
||
from torchvision import datasets | ||
from torchvision.transforms import functional as F_v1 | ||
|
||
COCO_ROOT = "~/datasets/coco" | ||
|
||
__all__ = ["classification_dataset_builder", "detection_dataset_builder"] | ||
|
||
def classification_dataset_builder(*, input_type, api_version, rng, num_samples): | ||
|
||
def classification_dataset_builder(*, api_version, rng, num_samples): | ||
return [ | ||
F_v1.to_pil_image( | ||
# average size of images in ImageNet | ||
torch.randint(0, 256, (3, 469, 387), dtype=torch.uint8, generator=rng) | ||
torch.randint(0, 256, (3, 469, 387), dtype=torch.uint8, generator=rng), | ||
) | ||
for _ in range(num_samples) | ||
] | ||
|
||
|
||
def detection_dataset_builder(*, api_version, rng, num_samples): | ||
root = pathlib.Path(COCO_ROOT).expanduser().resolve() | ||
image_folder = str(root / "train2017") | ||
annotation_file = str(root / "annotations" / "instances_train2017.json") | ||
if api_version == "v1": | ||
dataset = CocoDetectionV1(image_folder, annotation_file, transforms=None) | ||
elif api_version == "v2": | ||
dataset = datasets.CocoDetection(image_folder, annotation_file) | ||
else: | ||
raise ValueError(f"Got {api_version=}") | ||
|
||
dataset = _coco_remove_images_without_annotations(dataset) | ||
|
||
idcs = torch.randperm(len(dataset), generator=rng)[:num_samples].tolist() | ||
print(f"Caching {num_samples} ({idcs[:3]} ... {idcs[-3:]}) COCO samples") | ||
return [dataset[idx] for idx in tqdm(idcs)] | ||
|
||
|
||
# everything below is copy-pasted from | ||
# https://github.com/pytorch/vision/blob/main/references/detection/coco_utils.py | ||
|
||
import torch | ||
import torchvision | ||
|
||
|
||
class CocoDetectionV1(torchvision.datasets.CocoDetection): | ||
def __init__(self, img_folder, ann_file, transforms): | ||
super().__init__(img_folder, ann_file) | ||
self._transforms = transforms | ||
|
||
def __getitem__(self, idx): | ||
img, target = super().__getitem__(idx) | ||
image_id = self.ids[idx] | ||
target = dict(image_id=image_id, annotations=target) | ||
if self._transforms is not None: | ||
img, target = self._transforms(img, target) | ||
return img, target | ||
|
||
|
||
def _coco_remove_images_without_annotations(dataset, cat_list=None): | ||
def _has_only_empty_bbox(anno): | ||
return all(any(o <= 1 for o in obj["bbox"][2:]) for obj in anno) | ||
|
||
def _count_visible_keypoints(anno): | ||
return sum(sum(1 for v in ann["keypoints"][2::3] if v > 0) for ann in anno) | ||
|
||
min_keypoints_per_image = 10 | ||
|
||
def _has_valid_annotation(anno): | ||
# if it's empty, there is no annotation | ||
if len(anno) == 0: | ||
return False | ||
# if all boxes have close to zero area, there is no annotation | ||
if _has_only_empty_bbox(anno): | ||
return False | ||
# keypoints task have a slight different criteria for considering | ||
# if an annotation is valid | ||
if "keypoints" not in anno[0]: | ||
return True | ||
# for keypoint detection tasks, only consider valid images those | ||
# containing at least min_keypoints_per_image | ||
if _count_visible_keypoints(anno) >= min_keypoints_per_image: | ||
return True | ||
return False | ||
|
||
if not isinstance(dataset, torchvision.datasets.CocoDetection): | ||
raise TypeError( | ||
f"This function expects dataset of type torchvision.datasets.CocoDetection, instead got {type(dataset)}" | ||
) | ||
ids = [] | ||
for ds_idx, img_id in enumerate(dataset.ids): | ||
ann_ids = dataset.coco.getAnnIds(imgIds=img_id, iscrowd=None) | ||
anno = dataset.coco.loadAnns(ann_ids) | ||
if cat_list: | ||
anno = [obj for obj in anno if obj["category_id"] in cat_list] | ||
if _has_valid_annotation(anno): | ||
ids.append(ds_idx) | ||
|
||
dataset = torch.utils.data.Subset(dataset, ids) | ||
return dataset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is this needed?