How to create image artifacts and upload with sigopt.log_image()

This guide demonstrates the four types of data you can upload as a Training Run artifact.

1. Filepath

If a string is provided then this argument will be treated like a filesystem path and the image will be opened and uploaded. The image type will be inferred from the file extension. To follow the code snippet please fetch sigopt_logo.png here.

import sigopt
filepath = "./sigopt_logo.png"
run_context = sigopt.create_run(name= "test logging images")
run_context.log_image(image=filepath, name= "retro SigOpt logo”)

2. PIL.Image.Image

If a PIL Image is provided then it will be converted to PNG and uploaded.

This snippet will change the white pixels in the image to purple, save the data to the PIL Image frame, and upload it to the run context.

import PIL
im_frame = PIL.Image.open(filepath)
im_frame_color = im_frame.convert("RGB")
im_data = im_frame_color.getdata()
new_im_data = []
for item in im_data:
  if item[0] > 0:
    new_image.append((110,50,170))
  else:
    new_image.append(item)
im_frame_color.putdata(new_image)
run_context.log_image(image=im_frame_color, name= "purple letters logo”)

3. numpy.ndarray

If a numpy array is provided then the values will be clamped to the range [0, 255] and then cast to unsigned 8-bit integers. The resulting array will be converted to PNG and then uploaded.

In this snippet we reuse the PIL Image frame to log a grayscale, 2D numpy array.

import numpy
run_context.log_image(image=256*(numpy.array(im_frame)>0), name="B&W logo")

4. matplotlib.figure.Figure

If a matplotlib Figure is provided then it will be converted to SVG and uploaded.

Here we log a matplotlib Figure by counting the number of white pixels in each column of the 2D numpy array and plotting the value on the y axis vs the column position.

import matplotlib.pyplot as plt
import matplotlib.axes as axes
pixel_counts = numpy.sum((numpy.array(im_frame)>0), axis=0)
fig = plt.figure()
new_plot = fig.add_subplot(111)
new_plot.plot(pixel_counts, "b--”)
run_context.log_image(image=fig, name="Pixel counts for columns")
1 Like