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

Revision models.detection.yolo #851

Merged
merged 32 commits into from
May 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cf1646c
remove under_review decorators
redleaf-kim Aug 2, 2022
fecf88c
add yolo cfg with giou & update related test function
redleaf-kim Aug 2, 2022
b5abc8f
add serveral yolo config & layers function test
redleaf-kim Aug 2, 2022
198ebc1
Merge branch 'Lightning-AI:master' into yolo_review
redleaf-kim Aug 2, 2022
08f17f7
remove unused import & variable
redleaf-kim Aug 3, 2022
db2601a
add type hints
redleaf-kim Aug 9, 2022
fe38bb7
remove and merge duplicated test
redleaf-kim Aug 9, 2022
7da9d4a
improve readability
redleaf-kim Aug 9, 2022
8c3ed4e
Merge remote-tracking branch 'origin/yolo_review' into yolo_review
redleaf-kim Aug 9, 2022
8f69419
Merge branch 'master' into yolo_review
otaj Aug 12, 2022
b25a864
Merge branch 'master' into yolo_review
otaj Sep 15, 2022
9ff86ab
Merge branch 'master' into yolo_review
otaj Sep 16, 2022
17fab64
add catch_warning fixture
Sep 19, 2022
353f119
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2022
a3445ac
fix pytest error; indexing argument will be required to pass in upcom…
redleaf-kim Sep 19, 2022
189346c
fix pytest catch_warnings; MisconfigurationException error
redleaf-kim Sep 19, 2022
a1d97b6
fix pytest error
redleaf-kim Sep 19, 2022
d5b5fb9
Merge remote-tracking branch 'origin/yolo_review' into yolo_review
redleaf-kim Sep 19, 2022
0b4eca4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2022
b52ab5b
Merge branch 'master' into yolo_review
Borda Sep 19, 2022
eb9930e
Fix most obvious CI failings
Sep 19, 2022
fdf38fb
fix test with a missing warning
Sep 19, 2022
a42cdec
resolve accidentally introduced errors
Sep 21, 2022
d534cfa
add catch_warnings
Oct 11, 2022
57c9baf
Merge branch 'master' into yolo_review
Oct 11, 2022
55059f5
Merge branch 'master' into yolo_review
Borda Oct 27, 2022
bf5b360
Apply suggestions from code review
Borda Mar 28, 2023
3ed65fd
Merge branch 'master' into yolo_review
Borda Mar 28, 2023
bd23c27
update mergify team
Borda May 19, 2023
41d2749
Merge branch 'master' into yolo_review
Borda May 19, 2023
0d1c4e7
Merge branch 'master' into yolo_review
Borda May 19, 2023
d758939
Merge branch 'master' into yolo_review
mergify[bot] May 19, 2023
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: 6 additions & 6 deletions pl_bolts/models/detection/yolo/yolo_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _create_layer(config: dict, num_inputs: List[int]) -> Tuple[nn.Module, int]:
return create_func[config["type"]](config, num_inputs)


def _create_convolutional(config, num_inputs):
def _create_convolutional(config: dict, num_inputs: int) -> Tuple[nn.Module, int]:
module = nn.Sequential()

batch_normalize = config.get("batch_normalize", False)
Expand Down Expand Up @@ -206,13 +206,13 @@ def _create_convolutional(config, num_inputs):
return module, config["filters"]


def _create_maxpool(config, num_inputs):
def _create_maxpool(config: dict, num_inputs: int) -> Tuple[nn.Module, int]:
padding = (config["size"] - 1) // 2
module = nn.MaxPool2d(config["size"], config["stride"], padding)
return module, num_inputs[-1]


def _create_route(config, num_inputs):
def _create_route(config: dict, num_inputs: int) -> Tuple[nn.Module, int]:
num_chunks = config.get("groups", 1)
chunk_idx = config.get("group_id", 0)

Expand All @@ -228,17 +228,17 @@ def _create_route(config, num_inputs):
return module, num_outputs


def _create_shortcut(config, num_inputs):
def _create_shortcut(config: dict, num_inputs: int) -> Tuple[nn.Module, int]:
module = yolo_layers.ShortcutLayer(config["from"])
return module, num_inputs[-1]


def _create_upsample(config, num_inputs):
def _create_upsample(config: dict, num_inputs: int) -> Tuple[nn.Module, int]:
module = nn.Upsample(scale_factor=config["stride"], mode="nearest")
return module, num_inputs[-1]


def _create_yolo(config, num_inputs):
def _create_yolo(config: dict, num_inputs: int) -> Tuple[nn.Module, int]:
# The "anchors" list alternates width and height.
anchor_dims = config["anchors"]
anchor_dims = [(anchor_dims[i], anchor_dims[i + 1]) for i in range(0, len(anchor_dims), 2)]
Expand Down
8 changes: 4 additions & 4 deletions pl_bolts/models/detection/yolo/yolo_layers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Dict, List, Optional, Tuple
from typing import Callable, Dict, List, Optional, Tuple, Union

import torch
from pytorch_lightning.utilities.exceptions import MisconfigurationException
Expand Down Expand Up @@ -464,7 +464,7 @@ def _calculate_losses(
class Mish(nn.Module):
"""Mish activation."""

def forward(self, x):
def forward(self, x: Tensor) -> Tensor:
return x * torch.tanh(nn.functional.softplus(x))


Expand All @@ -483,7 +483,7 @@ def __init__(self, source_layers: List[int], num_chunks: int, chunk_idx: int) ->
self.num_chunks = num_chunks
self.chunk_idx = chunk_idx

def forward(self, x, outputs):
def forward(self, x, outputs: List[Union[Tensor, None]]) -> Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this 'x' here!

chunks = [torch.chunk(outputs[layer], self.num_chunks, dim=1)[self.chunk_idx] for layer in self.source_layers]
return torch.cat(chunks, dim=1)

Expand All @@ -500,5 +500,5 @@ def __init__(self, source_layer: int) -> None:
super().__init__()
self.source_layer = source_layer

def forward(self, x, outputs):
def forward(self, x, outputs: List[Union[Tensor, None]]) -> Tensor:
Copy link
Contributor

Choose a reason for hiding this comment

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

We could review why we are passing 'x' to the forward method and doing nothing with it. Seems to be just to keep with the format...

Copy link
Contributor

Choose a reason for hiding this comment

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

@luca-medeiros you're right. RouteLayer and ShortcutLayer do not use x (the output from the previous layer) and calling them is anyway handled as a special case, so the x can be dropped.

return outputs[-1] + outputs[self.source_layer]
5 changes: 3 additions & 2 deletions tests/models/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ def test_fasterrcnn_pyt_module_bbone_train(tmpdir):
trainer.fit(model, train_dl, valid_dl)


def test_yolo(tmpdir):
config_path = Path(TEST_ROOT) / "data" / "yolo.cfg"
@pytest.mark.parametrize("config", [("yolo"), ("yolo_giou")])
def test_yolo(config):
otaj marked this conversation as resolved.
Show resolved Hide resolved
config_path = Path(TEST_ROOT) / "data" / f"{config}.cfg"
config = YOLOConfiguration(config_path)
model = YOLO(config.get_network())

Expand Down
19 changes: 4 additions & 15 deletions tests/models/yolo/unit/test_yolo_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from pathlib import Path

import pytest

from pl_bolts.models.detection.yolo.yolo_config import (
YOLOConfiguration,
_create_convolutional,
_create_maxpool,
_create_shortcut,
_create_upsample,
)
from tests import TEST_ROOT


@pytest.mark.parametrize(
Expand All @@ -22,7 +18,7 @@
],
)
def test_create_convolutional(config):
redleaf-kim marked this conversation as resolved.
Show resolved Hide resolved
conv = _create_convolutional(config, [3])[0]
conv, _ = _create_convolutional(config, [3])

assert conv.conv.out_channels == config["filters"]
assert conv.conv.kernel_size == (config["size"], config["size"])
Expand Down Expand Up @@ -63,7 +59,7 @@ def test_create_convolutional(config):
)
def test_create_maxpool(config):
redleaf-kim marked this conversation as resolved.
Show resolved Hide resolved
pad_size = (config["size"] - 1) // 2
maxpool = _create_maxpool(config, [3])[0]
maxpool, _ = _create_maxpool(config, [3])

assert maxpool.kernel_size == config["size"]
assert maxpool.stride == config["stride"]
Expand All @@ -78,7 +74,7 @@ def test_create_maxpool(config):
],
)
def test_create_shortcut(config):
redleaf-kim marked this conversation as resolved.
Show resolved Hide resolved
shortcut = _create_shortcut(config, [3])[0]
shortcut, _ = _create_shortcut(config, [3])

assert shortcut.source_layer == config["from"]

Expand All @@ -91,13 +87,6 @@ def test_create_shortcut(config):
],
)
def test_create_upsample(config):
redleaf-kim marked this conversation as resolved.
Show resolved Hide resolved
upsample = _create_upsample(config, [3])[0]
upsample, _ = _create_upsample(config, [3])

assert upsample.scale_factor == float(config["stride"])


@pytest.mark.parametrize("config", [("yolo"), ("yolo_giou")])
def test_yolo_config(config):
config_path = Path(TEST_ROOT) / "data" / f"{config}.cfg"
config = YOLOConfiguration(config_path)
config.get_network()