From 44112347aaf69cb59c7311a1a1d5b9bbc6426b59 Mon Sep 17 00:00:00 2001 From: Pavel Bartsits <48913536+cxnt@users.noreply.github.com> Date: Thu, 16 Oct 2025 20:08:18 +0400 Subject: [PATCH] Fix UnboundLocalError in RepNCSPELAN4 when csp_type != 'csp2' ## Description This PR fixes an `UnboundLocalError` in the `RepNCSPELAN4` class that occurs when `csp_type` parameter is set to any value other than `'csp2'`. ## Problem The current implementation in `hybrid_encoder.py` has a conditional assignment issue: ```python if csp_type == 'csp2': CSPLayer = CSPLayer2 self.cv2 = nn.Sequential(CSPLayer(...), ...) # CSPLayer used here ``` When `csp_type != 'csp2'`, the local variable `CSPLayer` is never assigned, but Python still treats it as a local variable due to the assignment in the `if` block. This prevents falling back to the global `CSPLayer` class and raises: --- engine/deim/hybrid_encoder.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/engine/deim/hybrid_encoder.py b/engine/deim/hybrid_encoder.py index 77a7472..388cef8 100644 --- a/engine/deim/hybrid_encoder.py +++ b/engine/deim/hybrid_encoder.py @@ -200,9 +200,11 @@ def __init__(self, c1, c2, c3, c4, n=3, self.c = c3//2 self.cv1 = ConvNormLayer_fuse(c1, c3, 1, 1, bias=bias, act=act) if csp_type == 'csp2': - CSPLayer = CSPLayer2 - self.cv2 = nn.Sequential(CSPLayer(c3//2, c4, n, 1, bias=bias, act=act, bottletype=VGGBlock), ConvNormLayer_fuse(c4, c4, 3, 1, bias=bias, act=act)) - self.cv3 = nn.Sequential(CSPLayer(c4, c4, n, 1, bias=bias, act=act, bottletype=VGGBlock), ConvNormLayer_fuse(c4, c4, 3, 1, bias=bias, act=act)) + CSPLayerType = CSPLayer2 + else: + CSPLayerType = CSPLayer + self.cv2 = nn.Sequential(CSPLayerType(c3//2, c4, n, 1, bias=bias, act=act, bottletype=VGGBlock), ConvNormLayer_fuse(c4, c4, 3, 1, bias=bias, act=act)) + self.cv3 = nn.Sequential(CSPLayerType(c4, c4, n, 1, bias=bias, act=act, bottletype=VGGBlock), ConvNormLayer_fuse(c4, c4, 3, 1, bias=bias, act=act)) self.cv4 = ConvNormLayer_fuse(c3+(2*c4), c2, 1, 1, bias=bias, act=act) def forward_chunk(self, x):