Replace AdaptiveAvgPool2d with fixed-kernel AvgPool2d for ONNX compatibility
AdaptiveAvgPool2d with None dimensions can cause issues with some ONNX runtimes. Use AvgPool2d with kernel=(img_h//16, 1) to achieve the same height-to-1 reduction with full ONNX compatibility. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -66,9 +66,11 @@ class LiteCRNN(nn.Module):
|
||||
nn.MaxPool2d(2, 2), # H/2, W/2
|
||||
)
|
||||
|
||||
# 经过 4 次高度 pool: img_h / 16 (如 40 → 2, 不够整除时用自适应)
|
||||
# 用 AdaptiveAvgPool 把高度压到 1
|
||||
self.adaptive_pool = nn.AdaptiveAvgPool2d((1, None)) # (B, 128, 1, W')
|
||||
# 经过 4 次高度 pool 后高度 = img_h/16 (向下取整)
|
||||
# 例: 40→20→10→5→2
|
||||
# 用固定 kernel 的 AvgPool 把剩余高度压到 1 (ONNX 兼容)
|
||||
self._pool_h = img_h // 16 # 40→2, 若有余数也向下取整
|
||||
self.height_pool = nn.AvgPool2d(kernel_size=(self._pool_h, 1))
|
||||
|
||||
# ---- RNN 序列建模 ----
|
||||
self.rnn_input_size = 128
|
||||
@@ -95,7 +97,7 @@ class LiteCRNN(nn.Module):
|
||||
"""
|
||||
# CNN
|
||||
conv = self.cnn(x) # (B, 128, H', W')
|
||||
conv = self.adaptive_pool(conv) # (B, 128, 1, W')
|
||||
conv = self.height_pool(conv) # (B, 128, 1, W')
|
||||
conv = conv.squeeze(2) # (B, 128, W')
|
||||
conv = conv.permute(0, 2, 1) # (B, W', 128) — batch_first 序列
|
||||
|
||||
|
||||
Reference in New Issue
Block a user