使用 patch_size=14 的 VIT 时, DPTDecoder 无法正常工作
作者: schwobr创建于 2026年4月20日更新于 2026年4月20日
Hi, 我想在特定子任务上使用 DPT 与微调的 ViT-H/14 模型, 但是当向模型提供 224x224 的图像批量时, 就出现了运行时错误: RuntimeError: The size of tensor a (16) must match the size of tensor b (32) at non-singleton dimension 3。 我做了进一步的调查, 发现上采样似乎没有按照预期工作, 我的特征维度被计算为 8x8, 16x16, x16x16, 48x48。 正是在这个时候, 我注意到, 缩放系数是使用一种 奇怪的公式 计算的: scale_factors = [stride / 2 ** (i + 2) for i, stride in enumerate(encoder_output_strides)]。 对于不是 2 的幂的步长, 这从逻辑上导致了非整数结果, 从而产生了这种意想不到的行为。 问题在于, 从 16x16 的图块到输入大小的 1/32 (这里是 7) 是不可能的, 使用步长。 也许应该使用与步长最近的 2 的幂来计算缩放系数? 例如:
approx_strides = [2 ** np.round(np.log2(stride)) for stride in encoder_output_strides)]
scale_factors = [stride / 2 ** (i + 2) for i, stride in enumerate(approx_strides)]或者在图块大小为 14 时添加一个特殊条件, 因为这是 ViT 的唯一例外。 我可以根据首选方案写一个 PR。
内容来源: qubvel-org/segmentation_models.pytorch