Skip to content
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

add FCOS #4961

Merged
merged 94 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 84 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
791d938
add fcos
xiaohu2015 Nov 16, 2021
c9a00c1
update fcos
xiaohu2015 Nov 16, 2021
6c31577
Merge branch 'pytorch:main' into main
xiaohu2015 Nov 18, 2021
e82f825
add giou_loss
xiaohu2015 Nov 18, 2021
391b8c9
add BoxLinearCoder for FCOS
xiaohu2015 Nov 18, 2021
b01f145
add full code for FCOS
xiaohu2015 Nov 18, 2021
a4eeb59
add giou loss
xiaohu2015 Nov 18, 2021
61e946d
add fcos
xiaohu2015 Nov 18, 2021
997f189
add __all__
xiaohu2015 Nov 18, 2021
19ede68
Merge branch 'main' into main
xiaohu2015 Nov 18, 2021
793b1db
Fixing lint
zhiqwang Nov 18, 2021
a683060
Merge pull request #1 from o295/main
xiaohu2015 Nov 18, 2021
3cf91e3
Fixing lint in giou_loss.py
zhiqwang Nov 18, 2021
1f67950
Merge pull request #2 from o295/main
xiaohu2015 Nov 19, 2021
5d75049
Merge branch 'pytorch:main' into main
xiaohu2015 Nov 19, 2021
c7a7d52
Add typing annotation to fcos
zhiqwang Nov 19, 2021
42b147d
Add trained checkpoints
zhiqwang Nov 19, 2021
ff2d78a
Use partial to replace lambda
zhiqwang Nov 19, 2021
d4c08d3
Minor fixes to docstrings
zhiqwang Nov 19, 2021
edb2a1a
Merge pull request #3 from o295/main
xiaohu2015 Nov 19, 2021
c464249
Apply ufmt format
zhiqwang Nov 19, 2021
8955e77
Merge pull request #4 from o295/main
xiaohu2015 Nov 19, 2021
293e6b9
Fixing docstrings
zhiqwang Nov 19, 2021
4cbce22
Fixing jit scripting
zhiqwang Nov 19, 2021
b444d21
Minor fixes to docstrings
zhiqwang Nov 19, 2021
ac8d062
Fixing jit scripting
zhiqwang Nov 19, 2021
795a550
Merge pull request #5 from o295/main
xiaohu2015 Nov 19, 2021
7b1c73f
Ignore mypy in fcos
zhiqwang Nov 19, 2021
35702fd
Merge pull request #6 from o295/main
xiaohu2015 Nov 19, 2021
06e7ea8
Fixing trained checkpoints
zhiqwang Nov 19, 2021
635a189
Merge pull request #7 from o295/main
xiaohu2015 Nov 19, 2021
47feb9c
Merge branch 'main' into main
xiaohu2015 Nov 19, 2021
d348da3
Fixing unit-test of jit script
zhiqwang Nov 19, 2021
1802ca2
Fixing docstrings
zhiqwang Nov 19, 2021
63c97e8
Merge pull request #8 from o295/main
xiaohu2015 Nov 19, 2021
9286ccd
Merge branch 'pytorch:main' into main
xiaohu2015 Nov 23, 2021
9f5034d
Add test/expect/ModelTester.test_fcos_resnet50_fpn_expect.pkl
zhiqwang Nov 26, 2021
332af3b
Merge pull request #9 from o295/main
xiaohu2015 Nov 26, 2021
cfe224c
Fixing test_detection_model_trainable_backbone_layers
zhiqwang Nov 26, 2021
9fada56
Merge pull request #10 from o295/main
xiaohu2015 Nov 26, 2021
36cb20b
Merge branch 'pytorch:main' into main
xiaohu2015 Nov 26, 2021
a729e57
Update test_fcos_resnet50_fpn_expect.pkl
zhiqwang Nov 26, 2021
a3caf0c
Merge pull request #11 from o295/main
xiaohu2015 Nov 26, 2021
570b147
Merge branch 'pytorch:main' into main
xiaohu2015 Nov 26, 2021
64c8613
Merge branch 'pytorch:main' into main
xiaohu2015 Dec 5, 2021
719c776
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 9, 2022
c2c7a7e
rename stride to box size
xiaohu2015 Jan 9, 2022
7a830e1
remove TODO and fix some typo
xiaohu2015 Jan 10, 2022
b75d07b
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 10, 2022
90efd29
merge some code for better
xiaohu2015 Jan 10, 2022
0e6039b
impove the comments
xiaohu2015 Jan 10, 2022
a0907f9
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 11, 2022
4f9f3ef
remove decode and encode of BoxLinearCoder
xiaohu2015 Jan 11, 2022
db2e89b
remove some unnecessary hints
xiaohu2015 Jan 11, 2022
f5643ac
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 12, 2022
d03f03a
use the default value in detectron2.
xiaohu2015 Jan 12, 2022
56896e5
update doc
xiaohu2015 Jan 12, 2022
fab2748
Add unittest for BoxLinearCoder
zhiqwang Jan 13, 2022
80c3e64
Add types in FCOS
zhiqwang Jan 13, 2022
cb1f8e2
Add docstring for BoxLinearCoder
zhiqwang Jan 13, 2022
56b131f
Minor fix for the docstring
zhiqwang Jan 13, 2022
ba328c0
Merge pull request #12 from o295/main
xiaohu2015 Jan 13, 2022
11cc5d9
update doc
xiaohu2015 Jan 13, 2022
e497021
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 14, 2022
78e27da
Update fcos_resnet50_fpn_coco pretained weights url
jdsgomes Jan 17, 2022
32ec6ba
Update torchvision/models/detection/fcos.py
xiaohu2015 Jan 18, 2022
0e9bd78
Update torchvision/models/detection/fcos.py
xiaohu2015 Jan 18, 2022
4faf24a
Update torchvision/models/detection/fcos.py
xiaohu2015 Jan 18, 2022
868fe54
Update torchvision/models/detection/fcos.py
xiaohu2015 Jan 18, 2022
a09507c
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 18, 2022
54b12c8
Add FCOS model documentation
jdsgomes Jan 18, 2022
6862094
Merge branch 'main' of github.com:xiaohu2015/vision into main
jdsgomes Jan 18, 2022
589a3b1
Fix typo in FCOS documentation
jdsgomes Jan 18, 2022
a4db0dd
Add fcos to the prototype builder
zhiqwang Jan 19, 2022
50bf19b
Capitalize COCO_V1
zhiqwang Jan 19, 2022
688f4d2
Fix params of fcos
zhiqwang Jan 20, 2022
d4e04cf
Merge pull request #13 from zhiqwang/fcos-prototype-api
xiaohu2015 Jan 20, 2022
c75067f
Merge branch 'pytorch:main' into main
xiaohu2015 Jan 20, 2022
f01dcf6
fix bug for partial
xiaohu2015 Jan 20, 2022
9085461
Fixing docs indentation
zhiqwang Jan 20, 2022
9b25135
Merge pull request #14 from zhiqwang/fix-doc-indentation
xiaohu2015 Jan 20, 2022
ebbd772
Fixing docs format in giou_loss
zhiqwang Jan 20, 2022
d8ed195
Adopt Reference for GIoU Loss
zhiqwang Jan 20, 2022
b125f66
Merge pull request #15 from zhiqwang/fix-lint
xiaohu2015 Jan 20, 2022
b0b0892
Rename giou_loss to generalized_box_iou_loss
jdsgomes Jan 20, 2022
8c24ac3
remove overwrite_eps
jdsgomes Jan 20, 2022
bd3e262
Update AP test values
jdsgomes Jan 20, 2022
e452cfe
Minor fixes for the docs
zhiqwang Jan 20, 2022
e0af329
Minor fixes for the docs
zhiqwang Jan 20, 2022
c17a6e8
Merge pull request #17 from zhiqwang/remove_overwrite_eps
xiaohu2015 Jan 20, 2022
10be35d
Update torchvision/models/detection/fcos.py
xiaohu2015 Jan 20, 2022
4922635
Update torchvision/prototype/models/detection/fcos.py
xiaohu2015 Jan 20, 2022
6bae6c1
Merge branch 'main' into main
jdsgomes Jan 20, 2022
6840a7d
Merge branch 'main' into main
jdsgomes Jan 21, 2022
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
12 changes: 12 additions & 0 deletions docs/source/models.rst
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ The models subpackage contains definitions for the following model
architectures for detection:

- `Faster R-CNN <https://arxiv.org/abs/1506.01497>`_
- `FCOS <https://arxiv.org/abs/1904.01355>`_
- `Mask R-CNN <https://arxiv.org/abs/1703.06870>`_
- `RetinaNet <https://arxiv.org/abs/1708.02002>`_
- `SSD <https://arxiv.org/abs/1512.02325>`_
Expand Down Expand Up @@ -639,6 +640,7 @@ Network box AP mask AP keypoint AP
Faster R-CNN ResNet-50 FPN 37.0 - -
Faster R-CNN MobileNetV3-Large FPN 32.8 - -
Faster R-CNN MobileNetV3-Large 320 FPN 22.8 - -
FCOS ResNet-50 FPN 39.1 - -
RetinaNet ResNet-50 FPN 36.4 - -
SSD300 VGG16 25.1 - -
SSDlite320 MobileNetV3-Large 21.3 - -
Expand Down Expand Up @@ -699,6 +701,7 @@ Network train time (s / it) test time (s / it)
Faster R-CNN ResNet-50 FPN 0.2288 0.0590 5.2
Faster R-CNN MobileNetV3-Large FPN 0.1020 0.0415 1.0
Faster R-CNN MobileNetV3-Large 320 FPN 0.0978 0.0376 0.6
FCOS ResNet-50 FPN 0.1450 0.0539 3.3
RetinaNet ResNet-50 FPN 0.2514 0.0939 4.1
SSD300 VGG16 0.2093 0.0744 1.5
SSDlite320 MobileNetV3-Large 0.1773 0.0906 1.5
Expand All @@ -718,6 +721,15 @@ Faster R-CNN
torchvision.models.detection.fasterrcnn_mobilenet_v3_large_fpn
torchvision.models.detection.fasterrcnn_mobilenet_v3_large_320_fpn

FCOS
----

.. autosummary::
:toctree: generated/
:template: function.rst

torchvision.models.detection.fcos_resnet50_fpn


RetinaNet
---------
Expand Down
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ ignore_errors = True

ignore_errors = True

[mypy-torchvision.models.detection.fcos]

ignore_errors = True
datumbox marked this conversation as resolved.
Show resolved Hide resolved

[mypy-torchvision.ops.*]

ignore_errors = True
Expand Down
7 changes: 7 additions & 0 deletions references/detection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ torchrun --nproc_per_node=8 train.py\
--lr-steps 16 22 --aspect-ratio-group-factor 3
```

### FCOS ResNet-50 FPN
```
torchrun --nproc_per_node=8 train.py\
--dataset coco --model fcos_resnet50_fpn --epochs 26\
--lr-steps 16 22 --aspect-ratio-group-factor 3 --lr 0.01 --amp
```

### RetinaNet
```
torchrun --nproc_per_node=8 train.py\
Expand Down
Binary file not shown.
12 changes: 12 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def _check_input_backprop(model, inputs):
"retinanet_resnet50_fpn": lambda x: x[1],
"ssd300_vgg16": lambda x: x[1],
"ssdlite320_mobilenet_v3_large": lambda x: x[1],
"fcos_resnet50_fpn": lambda x: x[1],
}


Expand Down Expand Up @@ -274,6 +275,13 @@ def _check_input_backprop(model, inputs):
"max_size": 224,
"input_shape": (3, 224, 224),
},
"fcos_resnet50_fpn": {
"num_classes": 2,
"score_thresh": 0.05,
"min_size": 224,
"max_size": 224,
"input_shape": (3, 224, 224),
},
"maskrcnn_resnet50_fpn": {
"num_classes": 10,
"min_size": 224,
Expand Down Expand Up @@ -325,6 +333,10 @@ def _check_input_backprop(model, inputs):
"max_trainable": 6,
"n_trn_params_per_layer": [96, 99, 138, 200, 239, 257, 266],
},
"fcos_resnet50_fpn": {
"max_trainable": 5,
"n_trn_params_per_layer": [54, 64, 83, 96, 106, 107],
},
}


Expand Down
13 changes: 13 additions & 0 deletions test/test_models_detection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ def test_balanced_positive_negative_sampler(self):
assert neg[0].sum() == 3
assert neg[0][0:6].sum() == 3

def test_box_linear_coder(self):
box_coder = _utils.BoxLinearCoder(normalize_by_size=True)
# Generate a random 10x4 boxes tensor, with coordinates < 50.
boxes = torch.rand(10, 4) * 50
boxes.clamp_(min=1.0) # tiny boxes cause numerical instability in box regression
boxes[:, 2:] += boxes[:, :2]

proposals = torch.tensor([0, 0, 101, 101] * 10).reshape(10, 4).float()

rel_codes = box_coder.encode_single(boxes, proposals)
pred_boxes = box_coder.decode_single(rel_codes, boxes)
torch.allclose(proposals, pred_boxes)

@pytest.mark.parametrize("train_layers, exp_froz_params", [(0, 53), (1, 43), (2, 24), (3, 11), (4, 1), (5, 0)])
def test_resnet_fpn_backbone_frozen_layers(self, train_layers, exp_froz_params):
# we know how many initial layers and parameters of the network should
Expand Down
1 change: 1 addition & 0 deletions torchvision/models/detection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .retinanet import *
from .ssd import *
from .ssdlite import *
from .fcos import *
77 changes: 77 additions & 0 deletions torchvision/models/detection/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,83 @@ def decode_single(self, rel_codes: Tensor, boxes: Tensor) -> Tensor:
return pred_boxes


class BoxLinearCoder:
"""
The linear box-to-box transform defined in FCOS. The transformation is parameterized
by the distance from the center of (square) src box to 4 edges of the target box.
"""

def __init__(self, normalize_by_size: bool = True) -> None:
xiaohu2015 marked this conversation as resolved.
Show resolved Hide resolved
"""
Args:
normalize_by_size (bool): normalize deltas by the size of src (anchor) boxes.
jdsgomes marked this conversation as resolved.
Show resolved Hide resolved
"""
self.normalize_by_size = normalize_by_size

def encode_single(self, reference_boxes: Tensor, proposals: Tensor) -> Tensor:
xiaohu2015 marked this conversation as resolved.
Show resolved Hide resolved
"""
Encode a set of proposals with respect to some reference boxes

Args:
reference_boxes (Tensor): reference boxes
proposals (Tensor): boxes to be encoded
jdsgomes marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Tensor: the encoded relative box offsets that can be used to
decode the boxes.
"""
# get the center of reference_boxes
reference_boxes_ctr_x = 0.5 * (reference_boxes[:, 0] + reference_boxes[:, 2])
reference_boxes_ctr_y = 0.5 * (reference_boxes[:, 1] + reference_boxes[:, 3])

# get box regression transformation deltas
target_l = reference_boxes_ctr_x - proposals[:, 0]
target_t = reference_boxes_ctr_y - proposals[:, 1]
target_r = proposals[:, 2] - reference_boxes_ctr_x
target_b = proposals[:, 3] - reference_boxes_ctr_y

targets = torch.stack((target_l, target_t, target_r, target_b), dim=1)
if self.normalize_by_size:
reference_boxes_w = reference_boxes[:, 2] - reference_boxes[:, 0]
reference_boxes_h = reference_boxes[:, 3] - reference_boxes[:, 1]
reference_boxes_size = torch.stack(
(reference_boxes_w, reference_boxes_h, reference_boxes_w, reference_boxes_h), dim=1
)
targets = targets / reference_boxes_size

return targets

def decode_single(self, rel_codes: Tensor, boxes: Tensor) -> Tensor:
xiaohu2015 marked this conversation as resolved.
Show resolved Hide resolved
"""
From a set of original boxes and encoded relative box offsets,
get the decoded boxes.

Args:
rel_codes (Tensor): encoded boxes
boxes (Tensor): reference boxes.
jdsgomes marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Tensor: the predicted boxes with the encoded relative box offsets.
"""

boxes = boxes.to(rel_codes.dtype)

ctr_x = 0.5 * (boxes[:, 0] + boxes[:, 2])
ctr_y = 0.5 * (boxes[:, 1] + boxes[:, 3])
if self.normalize_by_size:
boxes_w = boxes[:, 2] - boxes[:, 0]
boxes_h = boxes[:, 3] - boxes[:, 1]
boxes_size = torch.stack((boxes_w, boxes_h, boxes_w, boxes_h), dim=1)
rel_codes = rel_codes * boxes_size

pred_boxes1 = ctr_x - rel_codes[:, 0]
pred_boxes2 = ctr_y - rel_codes[:, 1]
pred_boxes3 = ctr_x + rel_codes[:, 2]
pred_boxes4 = ctr_y + rel_codes[:, 3]
pred_boxes = torch.stack((pred_boxes1, pred_boxes2, pred_boxes3, pred_boxes4), dim=1)
return pred_boxes


class Matcher:
"""
This class assigns to each predicted "element" (e.g., a box) a ground-truth
Expand Down
Loading