Applying masks to image in pytorch

Viewed 61

I have tensor with images, which has shape [b, c, h, w], where

  • b - batch,
  • c - channels,
  • w - width,
  • h - height,

for example tensor has shape [4, 3, 275, 275]; and I have tensor with masks from neural network, which has shape [b, n, h, w], where

  • b - batch,
  • n - num of detections,
  • h,
  • w - height and width respectively.

Also I have list of colors for mask with length equal num of detections, where every element is tuple of tensors with single number like colors[0]=(tensor(0), tensor(0), tensor(0)). How I can apply all colors to images to avoid for loops? Right now I have double loop:

for masks_, img_to_draw_ in zip(masks, img_to_draw):
    for mask, color in zip(masks_, colors_):
        img_to_draw_[:, mask] = color[:, None]
    imgs.append(img_to_draw_)

Maybe exist a way to vectorized this operation? At least the inner loop.

0 Answers
Related