Create Gifs From Images Logged to Weights & Biases
In this article, we explore how to log images during training using Weights & Biases and turn them into gifs showing your model improving over time visually.
Created on June 7|Last edited on January 27
Comment
Gifs are the lifeblood of the internet, and we want more of them. Combine that with the satisfaction of seeing machine learning models improve over time, and we have a winning formula.
In this post, I'll show you how to create slick gifs of your models' training using W&B.
Table of Contents
TL;DRExamplesHow This Works Log the Images During TrainingDownload the Images With the W&B APICombine Your Images With PillowFinally, Share It With the World!Conclusion
TL;DR
- Log your images to W&B during training.
- After training, download them with the W&B API (don't worry, it's actually an easy-to-use Python wrapper).
- Create the gif with Pillow.
Or... just create your gifs using Google Colab ⤵️
Examples


How This Works
I'll dive into the details of this little script so you can learn something alongside having your cool gifs, but the TL;DR is that I'm logging the images like
wandb.log({'examles': wandb.Image(image)}) and then I'm using the wandb.Api to download them after training. Finally, I put them all together into gifs using pillow.
Log the Images During Training
You may know that you can log images (and lots more!) to W&B during training. When you do that, you can see the latest images logged in your workspace and get a slider to see earlier images.
You can see an example of the code and the produced slider below:
images = [PIL.Image.fromarray(image) for image in image_array]wandb.log({"examples": [wandb.Image(image) for image in images]}
Download the Images With the W&B API
The W&B API is a Python wrapper you can use to get data from W&B.
import wandbapi = wandb.Api()run = api.run("<entity>/<project>/<run_id>")
For this to work, we're using run.files() to download the files and run.scan_history (which gets all your W&B logs) to check whether the images were logged as an array or on their own. Images logged on their own would get their own slider, and images logged as an array share a slider.
Get Your Run Path From Your Run Overview.
Navigate to your run by clicking the run name within your project.

Navigate to the overview of your run and copy the run path.

Save the Images Locally
We can use the wandb.file() iterator to download the files like:
for file in run.files():if file.name.endswith('.png'):file.download()
Combine Your Images With Pillow
After downloading the images from run.files(), we can pass them to Image.open from PIL. We can then call save on the first frame and pass the remaining frames to append_frames.
from PIL import Imagedef images_to_gif(image_fnames, fname):image_fnames.sort(key=lambda x: int(x.name.split('_')[-2])) #sort by stepframes = [Image.open(image) for image in image_fnames]frame_one = frames[0]frame_one.save(f'{fname}.gif', format="GIF", append_images=frames,save_all=True, duration=DURATION, loop=0)
Bonus: to convert them to MP4, we can use ffmpeg
w, h = frames[0].sizecmd = f"ffmpeg -loglevel error -i {f'{fname}.gif'} -vcodec libx264 -crf 25 -pix_fmt yuv420p {f'{fname}.mp4'}"os.system(cmd)if not os.path.exists(f'{fname}.mp4'):print(f"Failed to create mp4 file.")
Finally, Share It With the World!
I mean, why else would you be creating these gifs!?
Conclusion
In this post, we've seen how to create gifs of your models' output while training.
To do this, we:
- Logged the images during training with wandb.log and wandb.Image
- Used the W&B API (specifically wandb.files()) to download files that were logged to W&B
- Combined them into gifs with PIL.image
Thanks for reading! Please share your gifs with us @weights_biases on Twitter! We'd love to see them!
Add a comment
Iterate on AI agents and models faster. Try Weights & Biases today.