You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Jupyter与Google Colab同确定性算法同输入输出不一致求助

超像素掩码边界超像素块检测结果不一致问题

环境信息

  • 本地PyTorch版本:1.12.1
  • Colab PyTorch版本:1.12.1+cu113

掩码图片

掩码图片

测试数据

测试张量文件存储在共享文件夹中

算法代码

import torch
torch.use_deterministic_algorithms(True)
torch.manual_seed(42)

test = torch.load(SUPERPIXEL_EXAMPLE)
def flatten(L):
    for item in L:
        try:
            yield from flatten(item)
        except TypeError:
            yield item

def get_outer_values(superpixel_mask, flatten_list_fct=flatten):
    superpixel_mask_refined = superpixel_mask.clone()
    outer_superpixel_rows, outer_superpixel_cols, outer_all = [], [], []
    tuples = torch.nonzero(superpixel_mask_refined)
    rows = torch.unique(tuples[:,0])
    columns = torch.unique(tuples[:,1])
    # scan over rows 
    for i in rows:
        current_row = superpixel_mask_refined[i, :]
        unique_non_zeroed_row = torch.unique(current_row[current_row.nonzero(as_tuple=True)], sorted=False)
        first_superpixel = unique_non_zeroed_row[-1]
        last_superpixel = unique_non_zeroed_row[0]
        outer_superpixel_rows.append(first_superpixel.item())
        outer_superpixel_rows.append(last_superpixel.item())
    print(f"Outer sp rows {torch.unique(torch.Tensor(outer_superpixel_rows))}")
    # scan over columns
    for i in columns:
        current_column = superpixel_mask_refined[:, i]
        unique_non_zeroed_column = torch.unique(current_column[current_column.nonzero(as_tuple=True)], sorted=False)
        first_superpixel = unique_non_zeroed_column[-1]
        last_superpixel = unique_non_zeroed_column[0]
        outer_superpixel_cols.append(first_superpixel.item())
        outer_superpixel_cols.append(last_superpixel.item())
    print(f"Outer sp cols {torch.unique(torch.Tensor(outer_superpixel_cols))}")
    outer_all.append(outer_superpixel_cols)
    outer_all.append(outer_superpixel_rows)
    outer_all = flatten(outer_all)
    result = list(set(outer_all))
    result.sort()
    print(f"All outer values are: {result}")
get_outer_values(test)

Colab输出结果

Outer sp rows tensor([102., 113., 119., 120., 124., 140., 145., 158., 159., 163., 182., 201.,
        212., 218., 229., 232., 236., 244., 248., 259., 261., 262.])
Outer sp cols tensor([102., 112., 113., 118., 119., 120., 121., 122., 124., 132., 145., 159.,
        212., 232., 236., 259., 261., 262., 263., 264.])
All outer values are: [102, 112, 113, 118, 119, 120, 121, 122, 124, 132, 140, 145, 158, 159, 163, 182, 201, 212, 218, 229, 232, 236, 244, 248, 259, 261, 262, 263, 264]

本地运行输出结果

Outer sp rows tensor([102., 112., 113., 118., 119., 120., 124., 126., 135., 140., 145., 157.,
        158., 159., 160., 163., 177., 182., 193., 198., 199., 201., 212., 218.,
        219., 226., 229., 232., 234., 236., 244., 247., 248., 259., 261., 262.,
        263.])
Outer sp cols tensor([102., 112., 113., 118., 119., 120., 121., 122., 124., 126., 132., 138.,
        140., 142., 145., 153., 157., 159., 160., 166., 178., 181., 182., 199.,
        201., 202., 207., 212., 219., 226., 232., 233., 235., 236., 247., 248.,
        249., 251., 261., 262., 263., 264.])
All outer values are: [102, 112, 113, 118, 119, 120, 121, 122, 124, 126, 132, 135, 138, 140, 142, 145, 153, 157, 158, 159, 160, 163, 166, 177, 178, 181, 182, 193, 198, 199, 201, 202, 207, 212, 218, 219, 226, 229, 232, 233, 234, 235, 236, 244, 247, 248, 249, 251, 259, 261, 262, 263, 264]

结果差异的超像素编号

[135, 138, 142, 153, 157, 160, 166, 177, 178, 181, 193, 198, 199, 202, 207, 219, 226, 233, 234, 235, 247, 249, 251, 126]

问题说明

我需要从超像素掩码中提取位于边界的超像素块,但在本地Jupyter和Google Colab运行同一算法、使用同一输入时,得到了不同结果,其中Colab的结果是正确的。已经定位到问题出在上述get_outer_values函数中,但无法找出结果不一致的原因,希望得到解答。

内容的提问来源于stack exchange,提问作者MGroeg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 10:05:22