# Segmentation masks not logging

**URL:** <https://community.wandb.ai/t/segmentation-masks-not-logging/4794>\
**Category:** W&B Help\
**Created:** [July 25, 2023, 11:38pm UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794 "2023-07-25T23:38:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![clarkbab](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clarkbab](https://community.wandb.ai/u/clarkbab)\
**Post date:** [July 25, 2023, 11:38pm UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/1 "2023-07-25T23:38:16Z")

</div>

Hi there,

I’m trying to log images and segmentation masks to Wandb, which I’ve had success with in the past. However it seems to have stopped working. The code I’m using (with pytorch-lightning framework) is:

```python
image = Image(
  x_img,
  caption=desc,
  masks={
      'ground_truth': {
          'mask_data': y_img,
          'class_labels': class_labels
      },
      'predictions': {
          'mask_data': y_hat_img,
          'class_labels': class_labels
      }
  }
)
title = f'desc:{desc}:class:{j}:axis:{axis}'
self.logger.log_image(key=title, images=[image])

```

Any ideas what could be going wrong?

Example run: clarkbab/segmenter-miccai-convergence-part-3/runs/2ssrpujc

Thanks!

---

<div class="post-metadata">

**Author:** ![uma-wandb](https://avatars.discourse-cdn.com/v4/letter/u/b77776/32.png) [@uma-wandb](https://community.wandb.ai/u/uma-wandb)\
**Post date:** [July 31, 2023, 4:22pm UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/2 "2023-07-31T16:22:38Z")

</div>

Hey @clarkbab, when you try to log images and segmentation masks, does it error? Or does it not log in the workspace? Any details about how your script stopped working would be super helpful!

Additionally, in the line

```auto
self.logger.log_image(key=title, images=[image])

```

How are you logging the images? Are you using `WandbLogger` from `pytorch_lightning.loggers`? Thank you for providing all these details!

---

<div class="post-metadata">

**Author:** ![clarkbab](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clarkbab](https://community.wandb.ai/u/clarkbab)\
**Post date:** [August 2, 2023, 6:51am UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/3 "2023-08-02T06:51:52Z")

</div>

Hi! There are no errors, it’s just failing silently. Yes that’s correct, I’m using `WandbLogger` from `pytorch_lightning.loggers`.

I am passing `x_img`, `y_img` and `y_hat_img` as numpy arrays and they have the same size and correct datatypes (`float32` for `x_img`, and `bool` for others).

---

<div class="post-metadata">

**Author:** ![clarkbab](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clarkbab](https://community.wandb.ai/u/clarkbab)\
**Post date:** [August 3, 2023, 7:27am UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/4 "2023-08-03T07:27:58Z")

</div>

Hi @uma-wandb,

I’ve found the problem here, it seems that `pytorch_lightning` wraps the `wandb.Image` in another `wandb.Image` ([see here](https://github.com/Lightning-AI/lightning/blob/9d749ec3e67c6fcf86baf5badd7e63c158ea40af/src/lightning/pytorch/loggers/wandb.py#L483C45-L483C50)) which doesn’t preserve the ‘masks’ keyword of the original image. Is this expected behaviour, or should `wandb.Image` preserve the ‘masks’ keyword?

I’ve added some code to reproduce:

```python
import numpy as np
import wandb

# Create image and mask.
image = np.random.normal(size=(100, 100))
mask = np.random.choice([True, False], size=(100, 100))

# Create wandb image.
wandb_image = wandb.Image(image, masks={'mask': { 'mask_data': mask, 'class_labels': {0: 'background', 1: 'foreground'}}})

# This logs both image and mask...
wandb.init(project='test', name='test')
wandb.log({'test': [wandb_image]})

# Create wrapped image and mask.
wrapped_image = wandb.Image(wandb_image)

# This logs the image only...
wandb.log({'test': [wrapped_image]})

```

Thanks!  
Brett

---

<div class="post-metadata">

**Author:** ![uma-wandb](https://avatars.discourse-cdn.com/v4/letter/u/b77776/32.png) [@uma-wandb](https://community.wandb.ai/u/uma-wandb)\
**Post date:** [August 9, 2023, 10:53pm UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/5 "2023-08-09T22:53:02Z")

</div>

@clarkba Yes this behavior is expected. Thank you for sending the line this is referencing - in `.log_image`, we expect to receive the native image as well as the corresponding mask. When wrapping this in another `wandb.Image`, we override all the metadata associated with the image, masks included. For your workflow, I recommend continuing to log the native image + mask rather than a `wandb.Image` to avoid this problem.

---

<div class="post-metadata">

**Author:** ![uma-wandb](https://avatars.discourse-cdn.com/v4/letter/u/b77776/32.png) [@uma-wandb](https://community.wandb.ai/u/uma-wandb)\
**Post date:** [August 14, 2023, 5:30pm UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/6 "2023-08-14T17:30:24Z")

</div>

Hi Brett, since we have not heard back from you we are going to close this request. If you would like to re-open the conversation, please let us know!

---

<div class="post-metadata">

**Author:** ![clarkbab](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clarkbab](https://community.wandb.ai/u/clarkbab)\
**Post date:** [September 18, 2023, 6:03am UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/7 "2023-09-18T06:03:30Z")

</div>

Thanks @uma-wandb,

That makes sense, I’m now using the `pytorch-lightning` ‘log\_image’ method to log native image and masks instead of `wandb.Image`.

Thanks,  
Brett

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [November 17, 2023, 6:03am UTC](https://community.wandb.ai/t/segmentation-masks-not-logging/4794/8 "2023-11-17T06:03:49Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
