Skip to content

Logging images#

You can log either a single image or a series of images.

Supported data formats include:

  • Image file formats, such as PNG, JPEG, JPG, GIF, BMP and WebP
  • Matplotlib, Seaborn, and PIL figures
  • NumPy arrays
  • TensorFlow and PyTorch tensors

Logging a single image#

Use the upload() method to log an image file from disk:

run["train/bounding_boxes"].upload("bboxes.png")

Logging a series of images#

append() replaces log()

As of neptune-client 0.16.14, append() and extend() are the preferred methods for logging series of values.

You can upgrade your installation with pip install -U neptune-client or continue using log().

To log a series of images, use the append() method.

Each append() call appends an image to the series, so you want to use it inside a loop.

In the app, you can display and navigate the logged images in a gallery view.

Viewing a series of images in Neptune

Passing a path to an image file#

If you supply a file path as a string, use the File constructor.

from neptune.types import File

for name in misclassified_images_names:
    y_pred = ...
    y_true = ...
    run["misclassified_imgs"].append(
        File(f"{name}.png"),
        desc=f"Prediction: {y_pred}\nActual: {y_true}",
    )

Logging Matplotlib, Seaborn, and PIL figures#

You can log Matplotlib, Seaborn, and PIL figures as regular images.

When you log a figure object directly (instead of a path to a file), you don't need the File constructor.

To upload a Matplotlib figure (matplotlib.figure.Figure) as an image:

# Import matplotlib
import matplotlib.pyplot as plt

# Generate figure
fig = plt.figure(figsize=(7, 9))
...

# Log figure to run
run["matplotlib-fig"].upload(fig)

To log a series of Matplotlib images, use append() inside a loop:

for epoch in range(params["iterations"]):
    plt_fig = get_histogram()
    run["train/distribution"].append(plt_fig)

See example in Neptune →

To upload a Searborn figure, access its .figure property:

# Import Seaborn
import seaborn as sns

# Generate chart
seaborn_fig = ...

# Convert Seaborn object to Matplotlib format (matplotlib.Figure)
figure = seaborn_fig.figure

# Log figure to run
run["seaborn-img"].upload(figure)

To log a series of Seaborn figures, use append() inside a loop:

for epoch in range(params["iterations"]):
    seaborn_fig = ...
    figure = seaborn_fig.figure
    run["train/distribution"].append(figure)

See example in Neptune →

To upload a PIL image from memory:

image1 = Image.open("representation-learning.jpg")
image2 = Image.open("RL-agents-1.jpg")

# Log image to run
run["representation_learning"].upload(image1)
run["reinforcement_learning"].upload(image2)

You can also log a series of PIL images:

for epoch in range(images_nr):
    pil_image = ...
    run["train/distribution"].append(pil_image)

See example in Neptune →

Logging the step, epoch or additional metadata#

You can set a custom index with the step argument:

run["train/distribution"].append(plt_histogram, step=epoch)

You can also log a name and description for the image:

# Log data sample
for plt_image, class_name in data_sample:
    run["data/sample"].append(plt_image, name=class_name)

# Log predictions with class probabilities
for image, y_pred in zip(x_test_sample, y_test_sample_pred):
    description = "\n".join(
        [f"class {i}: {pred}" for i, pred in enumerate(y_pred)]
    )
    run["train/predictions"].append(image, description=description)