Logging Pytorch Images with wandb.Table()

I am trying to log images during training process, I followed info on “PyTorch | Weights & Biases Documentation” this page.

My codes looks like this:

image_table = wandb.Table()
    lr_images_list = []
    rc_images_list = []
    hr_images_list = []
    transform_2PIL = tvt.Compose([tvt.ToPILImage(mode='L')])
    for index in range(recon_images.size(0)):
        lr_images_list.append(transform_2PIL(low_res_images[index, :, :, :]))
        rc_images_list.append(transform_2PIL(recon_images[index, :, :, :]))
        hr_images_list.append(transform_2PIL(high_res_images[index, :, :, :]))

    image_table.add_column("Low Res Image", lr_images_list)
    image_table.add_column("Reconstruction Image", rc_images_list)
    image_table.add_column("High resolution Image", hr_images_list)
    image_table.add_column("Index of Batch", step_in_batch)
    image_table.add_column("Global Step", global_step)
    # Log your Table to W&B
    wandb.log({f"Result for {step_in_batch} at {global_step}": image_table})

But as soon as image_table.add_column("Low Res Image", lr_images_list) executes, it outputs:

Traceback (most recent call last):
  File "D:\Program Files\JetBrains\PyCharm 2020.3.5\plugins\python\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "C:\Users\kousa\.conda\envs\UDPET_GPU\Lib\site-packages\wandb\data_types.py", line 774, in add_column
    assert is_first_col or len(data) == len(
AssertionError: Expected length 0, found 4

low_res_images is a pytorch tensor, I am trying to turn them back to PIL and then log them in wandb, however, i couldn’t get it to run.

I also tried something like:

image_table = wandb.Table(columns=["Low Res Image", "Reconstruction Image","High Resolution Image",
                                   "Index of Batch", "Global Step", "Index in Batch"])
for index in range(recon_images.size(0)):
    image_table.add_data(transform_2PIL(low_res_images[index, :, :, :]),
                         transform_2PIL(recon_images[index, :, :, :]),
                         transform_2PIL(high_res_images[index, :, :, :]),
wandb.log({f"Result for {step_in_batch} at {global_step}": image_table})

But it gives TypeError: Object of type Image is not JSON serializable.

What went wrong?
Any insight is appreciated!

Hi Zhang,

Thanks for getting in touch! To make sure I understand fully and have the full picture may I please ask for the following info:

  1. What version of wandb are you utilizing?
  2. Where does the method recon_imagescome from? Is this internal to Pytorch or a custom function?
  3. It may also be useful to have a look at the output.log file and the debug.log and debug-internal.log files from the ./wandb/run-date_time-runid/ folder (located in files and logs subfolders) if you have access to the working directory where the experiment is running.

Let me know when you can. I look forward to working with you!


Hi ZHANG, 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!