From edb1963308e3d39e8c2c9850bc3af41dd6950785 Mon Sep 17 00:00:00 2001 From: F-G Fernandez <26927750+frgfm@users.noreply.github.com> Date: Sun, 23 Jun 2024 19:36:08 +0200 Subject: [PATCH] style(ruff): fix lint --- .github/verify_labels.py | 4 +--- holocron/models/classification/sknet.py | 4 +--- holocron/models/classification/tridentnet.py | 4 +--- holocron/models/detection/yolo.py | 4 +--- holocron/models/detection/yolov2.py | 4 +--- holocron/models/detection/yolov4.py | 3 +-- holocron/models/segmentation/unet.py | 6 ++---- holocron/models/segmentation/unet3p.py | 3 +-- holocron/nn/functional.py | 8 ++------ holocron/nn/modules/activation.py | 6 ++---- holocron/nn/modules/attention.py | 3 +-- holocron/nn/modules/conv.py | 4 +--- holocron/nn/modules/downsample.py | 9 +++------ holocron/nn/modules/lambda_layer.py | 3 +-- references/clean_checkpoint.py | 4 +--- 15 files changed, 20 insertions(+), 49 deletions(-) diff --git a/.github/verify_labels.py b/.github/verify_labels.py index 08ba8aa4a..34ef6e630 100644 --- a/.github/verify_labels.py +++ b/.github/verify_labels.py @@ -82,9 +82,7 @@ def parse_args(): ) parser.add_argument("pr", type=int, help="PR number") - args = parser.parse_args() - - return args + return parser.parse_args() if __name__ == "__main__": diff --git a/holocron/models/classification/sknet.py b/holocron/models/classification/sknet.py index 183d954de..2eaf4137c 100644 --- a/holocron/models/classification/sknet.py +++ b/holocron/models/classification/sknet.py @@ -110,9 +110,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: b, m, c = paths.shape[:3] z = self.sa(paths.sum(dim=1)).view(b, m, c, 1, 1) attention_factors = torch.softmax(z, dim=1) - out = (attention_factors * paths).sum(dim=1) - - return out + return (attention_factors * paths).sum(dim=1) class SKBottleneck(_ResBlock): diff --git a/holocron/models/classification/tridentnet.py b/holocron/models/classification/tridentnet.py index c10df0db2..25f354a14 100644 --- a/holocron/models/classification/tridentnet.py +++ b/holocron/models/classification/tridentnet.py @@ -41,7 +41,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: dilations = [1] * self.num_branches if self.dilation[0] == 1 else [1 + idx for idx in range(self.num_branches)] # Use shared weight to apply the convolution - out = torch.cat( + return torch.cat( [ F.conv2d( _x, @@ -57,8 +57,6 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: 1, ) - return out - class Tridentneck(_ResBlock): expansion: int = 4 diff --git a/holocron/models/detection/yolo.py b/holocron/models/detection/yolo.py index d1c82f6b3..e8a9e936c 100644 --- a/holocron/models/detection/yolo.py +++ b/holocron/models/detection/yolo.py @@ -339,9 +339,7 @@ def _format_outputs(self, x: Tensor) -> Tuple[Tensor, Tensor, Tensor]: def _forward(self, x: Tensor) -> Tensor: out = self.backbone(x) out = self.block4(out) - out = self.classifier(out) - - return out + return self.classifier(out) def forward( self, x: Tensor, target: Optional[List[Dict[str, Tensor]]] = None diff --git a/holocron/models/detection/yolov2.py b/holocron/models/detection/yolov2.py index 594033d00..6ee2f6600 100644 --- a/holocron/models/detection/yolov2.py +++ b/holocron/models/detection/yolov2.py @@ -208,9 +208,7 @@ def _forward(self, x: Tensor) -> Tensor: out = torch.cat((passthrough, out), 1) out = self.block6(out) - out = self.head(out) - - return out + return self.head(out) def forward( self, x: Union[Tensor, List[Tensor], Tuple[Tensor, ...]], target: Optional[List[Dict[str, Tensor]]] = None diff --git a/holocron/models/detection/yolov4.py b/holocron/models/detection/yolov4.py index 955cae3f5..11a5838f6 100644 --- a/holocron/models/detection/yolov4.py +++ b/holocron/models/detection/yolov4.py @@ -628,7 +628,7 @@ def forward( y3 = self.yolo3(o3, target) if not self.training: - detections = [ + return [ { "boxes": torch.cat((det1["boxes"], det2["boxes"], det3["boxes"]), dim=0), "scores": torch.cat((det1["scores"], det2["scores"], det3["scores"]), dim=0), @@ -636,7 +636,6 @@ def forward( } for det1, det2, det3 in zip(y1, y2, y3) ] - return detections return {k: y1[k] + y2[k] + y3[k] for k in y1} diff --git a/holocron/models/segmentation/unet.py b/holocron/models/segmentation/unet.py index 89c36790f..2df1cf9d5 100644 --- a/holocron/models/segmentation/unet.py +++ b/holocron/models/segmentation/unet.py @@ -223,8 +223,7 @@ def forward(self, x: Tensor) -> Tensor: x = decoder(xs.pop(), x) # Classifier - x = self.classifier(x) - return x + return self.classifier(x) class UBlock(nn.Module): @@ -368,8 +367,7 @@ def forward(self, x: Tensor) -> Tensor: x = self.upsample(x) # Classifier - x = self.classifier(x) - return x + return self.classifier(x) def _unet(arch: str, pretrained: bool, progress: bool, **kwargs: Any) -> UNet: diff --git a/holocron/models/segmentation/unet3p.py b/holocron/models/segmentation/unet3p.py index dc1d90d3a..f616f19cd 100644 --- a/holocron/models/segmentation/unet3p.py +++ b/holocron/models/segmentation/unet3p.py @@ -155,8 +155,7 @@ def forward(self, x: Tensor) -> Tensor: xs[idx] = self.decoder[idx](xs[:idx], xs[idx], xs[idx + 1 :]) # Classifier - x = self.classifier(xs[0]) - return x + return self.classifier(xs[0]) def _unet(arch: str, pretrained: bool, progress: bool, **kwargs: Any) -> nn.Module: diff --git a/holocron/nn/functional.py b/holocron/nn/functional.py index 3f0245ab0..41cfb0ee9 100644 --- a/holocron/nn/functional.py +++ b/holocron/nn/functional.py @@ -133,9 +133,7 @@ def concat_downsample2d(x: Tensor, scale_factor: int) -> Tensor: # N * C * H * W --> N * C * (H/scale_factor) * scale_factor * (W/scale_factor) * scale_factor x = x.view(b, c, h // scale_factor, scale_factor, w // scale_factor, scale_factor) x = x.permute(0, 3, 5, 1, 2, 4).contiguous() - x = x.view(b, int(c * scale_factor**2), h // scale_factor, w // scale_factor) - - return x + return x.view(b, int(c * scale_factor**2), h // scale_factor, w // scale_factor) def z_pool(x: Tensor, dim: int) -> Tensor: @@ -364,9 +362,7 @@ def _xcorr2d( h = floor((h + (2 * padding[0]) - (dilation[0] * (weight.shape[-2] - 1)) - 1) / stride[0] + 1) w = floor((w + (2 * padding[1]) - (dilation[1] * (weight.shape[-1] - 1)) - 1) / stride[1] + 1) - x = x.view(-1, weight.shape[0], h, w) - - return x + return x.view(-1, weight.shape[0], h, w) def _convNd(x: Tensor, weight: Tensor) -> Tensor: diff --git a/holocron/nn/modules/activation.py b/holocron/nn/modules/activation.py index 539f1683f..5bbd6ba0c 100644 --- a/holocron/nn/modules/activation.py +++ b/holocron/nn/modules/activation.py @@ -22,8 +22,7 @@ def __init__(self, inplace: bool = False) -> None: self.inplace = inplace def extra_repr(self) -> str: - inplace_str = "inplace=True" if self.inplace else "" - return inplace_str + return "inplace=True" if self.inplace else "" class HardMish(_Activation): @@ -80,5 +79,4 @@ def __init__(self, in_channels: int, kernel_size: int = 3) -> None: def forward(self, x: Tensor) -> Tensor: out = self.conv(x) out = self.bn(out) - x = torch.max(x, out) - return x + return torch.max(x, out) diff --git a/holocron/nn/modules/attention.py b/holocron/nn/modules/attention.py index e01fb6637..1475c34c7 100644 --- a/holocron/nn/modules/attention.py +++ b/holocron/nn/modules/attention.py @@ -74,5 +74,4 @@ def forward(self, x: Tensor) -> Tensor: x_h = cast(Tensor, self.h_branch(x)) x_w = cast(Tensor, self.w_branch(x)) - out = (x_c + x_h + x_w) / 3 - return out + return (x_c + x_h + x_w) / 3 diff --git a/holocron/nn/modules/conv.py b/holocron/nn/modules/conv.py index fb588a2b1..192b80188 100644 --- a/holocron/nn/modules/conv.py +++ b/holocron/nn/modules/conv.py @@ -496,6 +496,4 @@ def forward(self, x: Tensor) -> Tensor: # Multiply-Add operation # --> (N, C, H // s, W // s) - out = (kernel * x_unfolded).sum(dim=3).view(*x.shape[:2], *kernel.shape[-2:]) - - return out + return (kernel * x_unfolded).sum(dim=3).view(*x.shape[:2], *kernel.shape[-2:]) diff --git a/holocron/nn/modules/downsample.py b/holocron/nn/modules/downsample.py index a7f766a5e..db1e2eedf 100644 --- a/holocron/nn/modules/downsample.py +++ b/holocron/nn/modules/downsample.py @@ -74,8 +74,7 @@ def forward(self, x: Tensor) -> Tensor: return x.view(x.size(0), x.size(1), -1).mean(-1).view(x.size(0), x.size(1), 1, 1) def extra_repr(self) -> str: - inplace_str = "flatten=True" if self.flatten else "" - return inplace_str + return "flatten=True" if self.flatten else "" class GlobalMaxPool2d(nn.Module): @@ -97,13 +96,11 @@ def forward(self, x: Tensor) -> Tensor: return x.view(x.size(0), x.size(1), -1).max(-1).values.view(x.size(0), x.size(1), 1, 1) def extra_repr(self) -> str: - inplace_str = "flatten=True" if self.flatten else "" - return inplace_str + return "flatten=True" if self.flatten else "" def get_padding(kernel_size: int, stride: int = 1, dilation: int = 1) -> int: - padding = ((stride - 1) + dilation * (kernel_size - 1)) // 2 - return padding + return ((stride - 1) + dilation * (kernel_size - 1)) // 2 class BlurPool2d(nn.Module): diff --git a/holocron/nn/modules/lambda_layer.py b/holocron/nn/modules/lambda_layer.py index a0dd4e02d..edc4717b7 100644 --- a/holocron/nn/modules/lambda_layer.py +++ b/holocron/nn/modules/lambda_layer.py @@ -105,5 +105,4 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Y = Yc + Yp # B x (H * W) x num_heads x dim_v -> B x (num_heads * dim_v) x H x W - out = Y.permute(0, 2, 3, 1).reshape(b, self.num_heads * v.shape[2], h, w) - return out + return Y.permute(0, 2, 3, 1).reshape(b, self.num_heads * v.shape[2], h, w) diff --git a/references/clean_checkpoint.py b/references/clean_checkpoint.py index 6400172c6..9d3e30120 100644 --- a/references/clean_checkpoint.py +++ b/references/clean_checkpoint.py @@ -27,9 +27,7 @@ def parse_args(): parser.add_argument("checkpoint", type=str, help="path to the training checkpoint") parser.add_argument("outfile", type=str, help="model") - args = parser.parse_args() - - return args + return parser.parse_args() if __name__ == "__main__":