Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

[论文复现]masked_select 返回错误的值

Open DrRyanHuang opened this issue 2 years ago • 3 comments

bug描述 Describe the Bug

paddle.masked_select 返回错误的值

我本要实现一个这样的需求:

>>> import numpy as np
>>> A = np.arange(6).reshape(3, 2)

>>> A
[[0 1]
 [2 3]
 [4 5]]

>>> mask = np.array([1, 0, 1], dtype="bool")
[ True False  True]

>>> A[mask]
[[0 1]
 [4 5]]

就是为了实现这样的索引操作

在我的代码中是这样实现的:

# Paddle 不支持直接这样索引
# bbox_annotation = bbox_annotation[bbox_annotation[:, -1] != -1]

# Paddle2.3 暂时不支持
# bbox_annotation = paddle.masked_select(bbox_annotation, 
#                                        bbox_annotation[:, -1] != -1)

# Paddle2.3 mask 堆叠2次 stack/repeat 不支持 data_type[bool] ......

mask = bbox_annotation[:, -1] != -1
_, n = bbox_annotation.shape
mask_int = paddle.stack([mask.cast("int")]*n, 
                        axis=1)
mask = mask_int.cast("bool")
bbox_annotation = paddle.masked_select(bbox_annotation,  mask)
bbox_annotation = bbox_annotation.reshape((-1, n))

执行 paddle.masked_select 之前

>>> bbox_annotation
Tensor(shape=[8, 6], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[473.78640747, 219.13960266, 492.85083008, 250.74626160, 144.16235352,
         15.         ],
        [550.75024414, 708.69183350, 630.53839111, 723.44158936, 165.10130310,
         8.          ],
        [433.53927612, 585.07464600, 583.23034668, 616.68127441, 165.10372925,
         8.          ],
        [351.63284302, 430.55313110, 504.85437012, 455.13607788, 148.94572449,
         8.          ],
        [343.86584473, 328.00701904, 458.25244141, 361.72079468, 148.62698364,
         8.          ],
        [347.39630127, 266.19842529, 413.76876831, 283.05529785, 138.87124634,
         8.          ],
        [236.54016113, 193.15188599, 353.04501343, 240.91308594, 174.42779541,
         8.          ],
        [129.21447754, 91.30816650 , 271.84466553, 127.12905884, 158.77893066,
         8.          ]])

>>> bbox_annotation.min()
Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [8.])

>>> mask
Tensor(shape=[8, 6], dtype=bool, place=Place(gpu:0), stop_gradient=True,
       [[True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True]])

执行 paddle.masked_select 之后

>>> bbox_annotation
Tensor(shape=[48], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [-0.07486226,  0.07318814, -0.05083524, -0.07613675,  0.19384325,
        -0.07445700,  0.10021958, -0.06555402, -0.02481044, -0.04359574,
         0.00612496, -0.06837679, -0.06321189, -0.03873237, -0.07528426,
         0.00094453,  0.00943219, -0.01272164, -0.09646113, -0.01505692,
         0.07912542,  0.01428900, -0.00641055, -0.09557207, -0.05254355,
         0.02944359, -0.07232574, -0.13168815, -0.05608154,  0.07785014,
        -0.04577152, -0.09324921, -0.05664594, -0.10593040,  0.04946944,
        -0.07670377, -0.05103868, -0.17420547,  0.18594459, -0.11639674,
        -0.07586578, -0.07297748, -0.00931612, -0.06859001,  0.08021021,
        -0.09340942,  0.03019318, -0.07149952])

没有一个数字对得上,请问这大概是什么原因呢?

其他补充信息 Additional Supplementary Information

No response

DrRyanHuang avatar Aug 09 '22 07:08 DrRyanHuang

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

paddle-bot[bot] avatar Aug 09 '22 07:08 paddle-bot[bot]

你好,我这边develop分支测试没有问题,请问你使用的哪个版本的paddle?

>>> bbox_annotation
Tensor(shape=[8, 6], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[473.78640747, 219.13960266, 492.85083008, 250.74626160, 144.16235352,
         15.         ],
        [550.75024414, 708.69183350, 630.53839111, 723.44158936, 165.10130310,
         8.          ],
        [433.53927612, 585.07464600, 583.23034668, 616.68127441, 165.10372925,
         8.          ],
        [351.63284302, 430.55313110, 504.85437012, 455.13607788, 148.94572449,
         8.          ],
        [343.86584473, 328.00701904, 458.25244141, 361.72079468, 148.62698364,
         8.          ],
        [347.39630127, 266.19842529, 413.76876831, 283.05529785, 138.87124634,
         8.          ],
        [236.54016113, 193.15188599, 353.04501343, 240.91308594, 174.42779541,
         8.          ],
        [129.21447754, 91.30816650 , 271.84466553, 127.12905884, 158.77893066,
         8.          ]])
>>> mask
Tensor(shape=[8, 6], dtype=bool, place=Place(gpu:0), stop_gradient=True,
       [[True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True]])
>>> bbox_annotation_masked = paddle.masked_select(bbox_annotation,  mask)
>>> bbox_annotation_masked
Tensor(shape=[48], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [473.78640747, 219.13960266, 492.85083008, 250.74626160, 144.16235352,
        15.         , 550.75024414, 708.69183350, 630.53839111, 723.44158936,
        165.10130310, 8.          , 433.53927612, 585.07464600, 583.23034668,
        616.68127441, 165.10372925, 8.          , 351.63284302, 430.55313110,
        504.85437012, 455.13607788, 148.94572449, 8.          , 343.86584473,
        328.00701904, 458.25244141, 361.72079468, 148.62698364, 8.          ,
        347.39630127, 266.19842529, 413.76876831, 283.05529785, 138.87124634,
        8.          , 236.54016113, 193.15188599, 353.04501343, 240.91308594,
        174.42779541, 8.          , 129.21447754, 91.30816650 , 271.84466553,
        127.12905884, 158.77893066, 8.          ])

pangyoki avatar Aug 09 '22 12:08 pangyoki

@pangyoki 这是我的主机信息:

{'CUDA_VISIBLE_DEVICES': None,
 'GCC': 'gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0\r',
 'GPU': ['GPU 0: NVIDIA GeForce GTX 1650 Ti'],
 'GPUs used': 1,
 'NVCC': 'Build cuda_11.6.r11.6/compiler.30794723_0',
 'OpenCV': '4.5.5',
 'Paddle compiled with cuda': True,
 'PaddlePaddle': '2.3.0',
 'Python': '3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit '
           '(AMD64)]',
 'cudnn': '8.3',
 'platform': 'Windows-10-10.0.19044-SP0'}

执行代码:

bbox_annotation_masked = [[473.78640747, 219.13960266, 492.85083008, 250.74626160, 144.16235352,
  15.         ],
  [550.75024414, 708.69183350, 630.53839111, 723.44158936, 165.10130310,
  8.          ],
  [433.53927612, 585.07464600, 583.23034668, 616.68127441, 165.10372925,
  8.          ],
  [351.63284302, 430.55313110, 504.85437012, 455.13607788, 148.94572449,
  8.          ],
  [343.86584473, 328.00701904, 458.25244141, 361.72079468, 148.62698364,
  8.          ],
  [347.39630127, 266.19842529, 413.76876831, 283.05529785, 138.87124634,
  8.          ],
  [236.54016113, 193.15188599, 353.04501343, 240.91308594, 174.42779541,
  8.          ],
  [129.21447754, 91.30816650 , 271.84466553, 127.12905884, 158.77893066,
  8.          ]]

import paddle

bbox_annotation_masked = paddle.to_tensor(bbox_annotation_masked)
print(bbox_annotation_masked)
mask = [[True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True]]
mask = paddle.to_tensor(mask)
bbox_annotation_masked = paddle.masked_select(bbox_annotation_masked,  mask)
print(bbox_annotation_masked)
print(paddle.__version__)

输出:

Tensor(shape=[8, 6], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [[473.78640747, 219.13960266, 492.85083008, 250.74626160, 144.16235352,
         15.         ],
        [550.75024414, 708.69183350, 630.53839111, 723.44158936, 165.10130310,
         8.          ],
        [433.53927612, 585.07464600, 583.23034668, 616.68127441, 165.10372925,
         8.          ],
        [351.63284302, 430.55313110, 504.85437012, 455.13607788, 148.94572449,
         8.          ],
        [343.86584473, 328.00701904, 458.25244141, 361.72079468, 148.62698364,
         8.          ],
        [347.39630127, 266.19842529, 413.76876831, 283.05529785, 138.87124634,
         8.          ],
        [236.54016113, 193.15188599, 353.04501343, 240.91308594, 174.42779541,
         8.          ],
        [129.21447754, 91.30816650 , 271.84466553, 127.12905884, 158.77893066,
         8.          ]])
Tensor(shape=[48], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
2.3.0

DrRyanHuang avatar Aug 09 '22 14:08 DrRyanHuang