Title: Deep dream: Visualizing every layer of GoogLeNet - PyImageSearch

URL Source: https://pyimagesearch.com/2015/08/03/deep-dream-visualizing-every-layer-of-googlenet/

Published Time: 2015-08-03T14:00:34+00:00

Markdown Content:
A few weeks ago I introduced [bat-country](https://pyimagesearch.com/2015/07/06/bat-country-an-extendible-lightweight-python-package-for-deep-dreaming-with-caffe-and-convolutional-neural-networks/), my implementation of a **lightweight, extendible, easy to use Python package for deep dreaming and inceptionism**.

The reception of the library was very good, so I decided that it would be interesting to do a follow up post — but instead of generating some really trippy images like on the [Twitter #deepdream stream](https://twitter.com/search?q=%23deepdream&src=tyah), I thought it would be more captivating to instead _**visualize every layer of GoogLeNet using bat-country**_.

![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

Below follows my Python script to load an image, loop over every layer of the network, and then write each output image to file:

\# import the necessary packages
from \_\_future\_\_ import print\_function
from batcountry import BatCountry
from PIL import Image
import numpy as np
import argparse
import warnings
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add\_argument("-b", "--base-model", required=True, help="base model path")
ap.add\_argument("-i", "--image", help="path to image file")
ap.add\_argument("-o", "--output", help="path to output directory")
args = ap.parse\_args()

# filter warnings, initialize bat country, and grab the layer names of
# the CNN
warnings.filterwarnings("ignore")
bc = BatCountry(args.base\_model)
layers = bc.layers()

# extract the filename and extension of the input image
filename = args.image\[args.image.rfind("/") + 1:\]
(filename, ext) = filename.split(".")

# loop over the layers
for (i, layer) in enumerate(layers):
	# perform visualizing using the current layer
	print("\[INFO\] processing layer \`{}\` {}/{}".format(layer, i + 1, len(layers)))

	try:
		# pass the image through the network
		image = bc.dream(np.float32(Image.open(args.image)), end=layer,
			verbose=False)

		# draw the layer name on the image
		image = cv2.cvtColor(image, cv2.COLOR\_RGB2BGR)
		cv2.putText(image, layer, (5, image.shape\[0\] - 10),
			cv2.FONT\_HERSHEY\_SIMPLEX, 0.95, (0, 255, 0), 2)

		# construct the output path and write the image to file
		p = "{}/{}\_{}.{}".format(args.output, filename, str(i + 1).zfill(4), ext)
		cv2.imwrite(p, image)

	except KeyError, e:
		# the current layer can not be used
		print("\[ERROR\] cannot use layer \`{}\`".format(layer))

# perform housekeeping
bc.cleanup()

This script requires three command line arguments: the `--base-model` directory where our Caffe model lives, the path to our input `--image` , and finally the `--output` directory where our images will be stored after being passed through the network.

As you’ll also see, I am using a `try/except` block to catch any layers that cannot be used for visualization.

Below is the image that I inputted to the network:

[![Image 2: Figure 1: The iconic input image of Dr. Grant and the T-Rex from Jurassic Park.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20338'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/jp.jpg)

**Figure 1:** The iconic input image of Dr. Grant and the T-Rex from Jurassic Park.

I then executed the Python script using the following command:

$ python visualize\_layers.py \\
 	--base-model $CAFFE\_ROOT/caffe/models/bvlc\_googlenet \\
 	--image images/jp.jpg --output output/jp

And the visualization process will kick off. I generated my results on an [Amazon EC2 g2.2xlarge instance](https://pyimagesearch.com/2014/10/13/deep-learning-amazon-ec2-gpu-python-nolearn/) with GPU support enabled so the script finished up within 30 minutes.

You can see a .gif of _**all layer visualizations**_ below:

[![Image 3: Figure 2: Visualizing every layer of GoogLeNet using bat-country.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20450%20254'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/visualizing-all-googlenet-layers.gif)

**Figure 2:** Visualizing every layer of GoogLeNet using bat-country.

The .gif is pretty large at **9.6mb**, so give it a few seconds to load, especially if you are on a slow connection.

In the meantime, here are some of my favorite layers:

[![Image 4: Figure 2: This is my far my favorite one of the bunch. The lower layers of the network reflect edge-like regions in the input image.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20338'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/jp_0007.jpg)

**Figure 3:** This is my far my favorite one of the bunch. The lower layers of the network reflect edge-like regions in the input image.

[![Image 5: Figure 3: The inception_3a/3x3 layer also products a nice effect.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20338'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/jp_0016.jpg)

**Figure 4:** The inception\_3a/3×3 layer also products a nice effect.

[![Image 6: Figure 4: The same goes for the inception_3b/3x3_reduce layer.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20338'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/jp_0029.jpg)

**Figure 5:** The same goes for the inception\_3b/3x3\_reduce layer.

[![Image 7: Figure 4: This one I found amusing -- it seems that Dr. Grant has developed a severe case of dog-ass.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20338'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/jp_0062.jpg)

**Figure 6:** This one I found amusing — it seems that Dr. Grant has developed a severe case of dog-ass.

[![Image 8: Figure 6: Eventually, our Dr. Grant and T-Rex have morphed into something else entirely.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20338'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/jp_0096.jpg)

**Figure 7:** Eventually, our Dr. Grant and T-Rex have morphed into something else entirely.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

This blog post was a quick “just for fun” tutorial on visualizing every layer of a CNN using the [bat-country](https://pyimagesearch.com/2015/07/06/bat-country-an-extendible-lightweight-python-package-for-deep-dreaming-with-caffe-and-convolutional-neural-networks/) library. It also served as a good demonstration on how to use the `bat-country`  library.

If you haven’t had a chance to play around with deep dreaming or inceptionism, [definitely give the original post on bat-country a read](https://pyimagesearch.com/2015/07/06/bat-country-an-extendible-lightweight-python-package-for-deep-dreaming-with-caffe-and-convolutional-neural-networks/) — I think you’ll find it amusing and enjoyable.

See you next week!

![Image 9](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!

![Image 10](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20240%20240'%3E%3C/svg%3E)

#### **About the Author**

Hi there, I’m Adrian Rosebrock, PhD. All too often I see developers, students, and researchers wasting their time, studying the wrong things, and generally struggling to get started with Computer Vision, Deep Learning, and OpenCV. I created this website to show you what I believe is the best possible way to get your start.