Visualize Model Predictions with Weights & Biases
This article explores how to visualize a model's predictions using Weights & Biases, including images, videos, audio, tables, HTML, and more.
Created on February 18|Last edited on November 3
Comment
In this article, we'll look at how to visualize a model's predictions with Weights & Biases – images, videos, audio, tables, HTML, metrics, plots, 3d objects and point clouds.
Table of Contents
Metrics
Calling wandb.log(dict) logs the keys and values of the dictionary passed in and associates the values with a step.
Example
# Initialize runwandb.init()# Log multiple metricswandb.log({'accuracy':0.9,'epoch':5})# Log metrics in a loopfor price in apple ['close']:wandb.log({"Stock Price":price})
Run set
1
Plots
You can pass a matplotlib pyplot or figure object into wandb.log
By default we'll convert the plot into a plotly plot. If you want to explicitly log the plot as an image, you can pass the plot into wandb.Image. We also accept directly logging plotly charts.
Example
import matplotlib.pyplot as pltfibonacci = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]plt.plot(fibonacci)plt.ylabel('some interesting numbers')# Initialize runwandb.init()# Log plot objectwandb.log({"plot":plot})
Run set
1
Images
If the image is a NumPy array, we assume it's grayscale if the last dimension is 1, RGB if it's 3, and RGBA if it's 4.
If the array contains floats we convert them to ints between 0 and 255. You can specify a mode manually or just supply a PIL.Image. It's recommended to log fewer than 50 images per step. Click "Create Visualization" to customize the image gallery.
Example
from matplotlib import pyplot as pltpath_to_img = "../input/kernel-files/cafe.jpg"im = plt.imread(path_to_img)# Initialize runwandb.init()# Log image(s)wandb.log({"img":[wandb.Image(im,caption="Cafe")]})
Run set
1
Videos
If the video is a NumPy array, we assume the dimensions are: time, channels, width, and height. By default, we create a 4 fps gif image (ffmpeg and the moviepy python library is required when passing NumPy objects).
Supported formats are "gif", "mp4", "webm", and "ogg". If you pass a string to wandb.Video we assert the file exists and is a supported format before uploading to Weights & Biases. Passing a BytesIO object will create a tempfile with the specified format as the extension. On the W&B runs page, you will see your videos in the Media section.
Example
path_to_video = "../input/kernel-files/openai-gym.mp4# Initialize runwandb.init()# Log video(s)wandb.log({"video":wandb.Video(path_to_video, fps=4, format="gif")})
Run set
1
Audio
The maximum number of audio clips that can be logged per step is 100.
Example
from scipy.io import wavfilepath_to_audio = "../input/kernel-files/piano.wav"# Initialize runwandb.init()# Log audio filewandb.log({"examples":[wandb.Audio(path_to_audio, caption="Piano", sample_rate=32)]})
Run set
1
3D Objects
We support logging 3D file types in three different formats: `glTF, glb, obj`. The 3D files will be viewable on the run page upon completion of your run.
Example
```python
path_to_obj = "../input/kernel-files/wolf.obj"# Initialize runwandb.init()# Log 3D objectivewandb.log({"3d_object":wandb.Object3D(open(path_to_obj))})
Run set
1
Point Clouds
Point Clouds logging has currently has two modes.
Logging a single set of points representing an object , useful for representing datasets like ShapeNet(Example Report).
Along with a new beta release LiDAR scene renderer.
Logging a set of points is as simple as passing in a NumPy array containing your coordinates and the desired colors for the points.
Three different shapes of NumPy arrays are supported for flexible color schemes, supporting common ML us
- [[x, y, z], ...] nx3
- [[x, y, z, c], ...] nx4 | cis a category in the range [1, 14] (Useful for segmentation)
- [[x, y, z, r, g, b], ...] nx6 | r,g,b are values in the range [0,255]for red, green, and blue color channels.
Example
# Initialize runwandb.init()# Log points and boxeswandb.log({"point_scene": wandb.Object3D({"type": "lidar/beta","points": np.array([[0.4, 1, 1.3],[1, 1, 1],[1.2, 1, 1.2]]),"boxes": np.array([{"corners": [[0,0,0],[0,1,0],[0,0,1],[1,0,0],[1,1,0],[0,1,1],[1,0,1],[1,1,1]],"label": "Box","color": [123,321,111],},{"corners": [[0,0,0],[0,2,0],[0,0,2],[2,0,0],[2,2,0],[0,2,2],[2,0,2],[2,2,2]],"label": "Box-2","color": [111,321,0],}]),})})
Run set
1
HTML
Custom HTML can be logged at any key, this exposes an HTML panel on the run page. By default we inject default styles, you can disable default styles by passing inject=False
Example
path_to_html = "../input/kernel-files/some_html.html"# Initialize runwandb.init()# Log plot HTML# As a filewandb.log({"custom_file": wandb.Html(open(path_to_html))})# Inlinewandb.log({"custom_string": wandb.Html('<a href="https://mysite">Link</a>')})# Disable default styleswandb.log({"custom_file": wandb.Html(open("some.html"), inject=False)})
Run set
1
Incremental Logging
If you want to log to a single history step from lots of different places in your code you can pass a step-index to wandb.log() as shown in the example below.
wandb.log(dict) accepts a few keyword arguments:
- step — Step to associate the log with (see Incremental Logging)
- commit — If true, increments the step associated with the log(default: true)
Example
# Initialize runwandb.init()# Log the metric to specific stepsfor step, price in enumerate(apple['close']):# Specify stepswandb.log({"Stock Price": price}, step=step)# Initialize a new runwandb.init(project="visualize-metrics", name="incremental_logging")# Log many metrics to the same stepfor close, date in zip(apple['close'], apple['date']):# Log multiple metrics to the same stepwandb.log({"Stock Price": close}, commit=False)# Somewhere else when I'm ready to report this step:wandb.log({'Another Metric': close*10})
Run set
2
As long as you keep passing the same value for step, W&B will collect the keys and values from each call in one unified dictionary. As soon you call wandb.log() with a different value for step than the previous one, W&B will write all the collected keys and values to the history, and start collection over again.
Note that this means you should only use this with consecutive values for step: 0, 1, 2, .... This feature doesn't let you write to absolutely any history step that you'd like, only the "current" one and the "next" one.
You can also set commit=False in wandb.log to accumulate metrics, just be sure to call wandb.log without the commit flag to persist the metrics.
Add a comment
Thank for sharing
Reply
Thanks for explaining how to log matplotlib plots.
Reply
This looks so cool! Thanks for sharing!
Reply
Iterate on AI agents and models faster. Try Weights & Biases today.