def population_render_transparency(x, invert_colours=False, b=None):
  """Render image from patches with transparancy.

  Renders patches with transparency using black as the transparent colour.
  Args:
    x: tensor of transformed RGB image patches of shape [S, B, 5, H, W].
    invert_colours: Invert all RGB values.
    b: optional tensor of background RGB image of shape [S, 3, H, W].
  Returns:
    Tensor of rendered RGB images of shape [S, 3, H, W].
  """
  # Sum the RGB patches [S, B, 3, H, W] as [S, 3, H, W].
  x = x[:, :, :3, :, :] * x[:, :, 3:4, :, :]
  y = x[:, :, :3, :, :].sum(1)
  if invert_colours:
    y[:, :3, :, :] = 1.0 - y[:, :3, :, :]
  # Add backgrounds [S, 3, H, W].
  if b is not None:
    b = b.cuda() if x.is_cuda else b.cpu()
    y = (y + b).clamp(0., 1.)
  return y.clamp(0., 1.).permute(0, 2, 3, 1)