Title: Generating art with guided deep dreaming. - PyImageSearch

URL Source: https://pyimagesearch.com/2015/07/13/generating-art-with-guided-deep-dreaming/

Published Time: 2015-07-13T14:00:15+00:00

Markdown Content:
[![Image 1: van_gogh_animated](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20244'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/van_gogh_animated.gif)

One of the main benefits of the [bat-country Python package](https://pyimagesearch.com/2015/07/06/bat-country-an-extendible-lightweight-python-package-for-deep-dreaming-with-caffe-and-convolutional-neural-networks/) for deep dreaming and visualization is its ease of use, extensibility, and customization.

And let me tell you, that customization _**really came in handy last Friday**_ when the Google Research team [released an update to their deep dream work](https://plus.google.com/+ResearchatGoogle/posts/NoBnPqwq3wh), demonstrating a method to “guide” your input images to visualize the features of a target image.

The results were quite impressive — _so I decided to port the functionality to bat-country._

Truth be told, it only took ~20 minutes from start-to-finish to get the code together. I honestly spent more time running the Python scripts to gather example images and updating the documentation than I did updating the codebase.

The secret to this quick turnaround is the extensibility of the `BatCountry` class where nearly _every function and every method can be overridden and extended_.

Want to change how each image is pre-processed or post-processed? No problem. Define your own custom processor and pass it in. Want to change the objective function? Again, just define your own objective and you’re good to go.

In fact, defining your own custom objective function is the the _exact route_ I took when extending `bat-country` . I simply defined a new objective function, allowing the step function to be further customized, and we’re done!

In the remainder of this blog post we’ll play around with the new [bat-country](https://github.com/jrosebr1/bat-country/) update to perform guided dreaming — _**and even use it to generate our own art using guided deep dreaming!**_

Last Friday the Google Research team posted an update to their deep dream working demonstrating it was possible to _guide_ your dreaming process by supplying a _seed image_. This method passes your input image through the network in a similar manner, but this time using your seed image to guide and influence the output.

Using `bat-country` , it’s just as easy to perform guided dreaming as deep dreaming. Here’s some quick sample code:

\# import the necessary packages
from batcountry import BatCountry
from PIL import Image
import numpy as np
import argparse

# 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("-l", "--layer", type=str, default="inception\_4c/output",
	help="layer of CNN to use")
ap.add\_argument("-i", "--image", required=True, help="path to base image")
ap.add\_argument("-g", "--guide-image", required=True, help="path to guide image")
ap.add\_argument("-o", "--output", required=True, help="path to output image")
args = ap.parse\_args()

# we can't stop here...
bc = BatCountry(args.base\_model)
features = bc.prepare\_guide(Image.open(args.guide\_image), end=args.layer)
image = bc.dream(np.float32(Image.open(args.image)), end=args.layer,
	iter\_n=20, objective\_fn=BatCountry.guided\_objective,
	objective\_features=features)
bc.cleanup()

# write the output image to file
result = Image.fromarray(np.uint8(image))
result.save(args.output)

If you don’t already have `bat-country` installed, either pull down the code from the [GitHub repo](https://github.com/jrosebr1/bat-country/) or use pip to install it on your system: `pip install bat-country` or `pip install --upgrade bat-country` . You’ll also need a working installation of [Caffe](http://caffe.berkeleyvision.org/).

What’s nice about this approach is that we can “guide” what the output image looks like. Here I use a seed image of Vincent van Gogh’s _Starry Night_ and apply to an image of clouds:

[![Image 2: Figure 1: An example of applying guided dreaming using Starry Night and a cloud image.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20168'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/starry_night_example.jpg)

**Figure 1:** An example of applying guided dreaming using _Starry Night_ and a cloud image.

As you can see, the output cloud image after applying guided dreaming appears to mimic many of the brush strokes of Van Gogh’s painting.

Which got me thinking — **what would happen if I took some well-known paintings from extremely famous artists such as _Andy Warhol_, _MC Escher_, _Pablo Picasso_, _Jackson Pollock_, and _Vincent van Gogh_ and used them as inputs and guides to each other?**

What would the results look like? Would the artistic style of each painting be transferred to the other?

In order to test this out, I collected images of the following pieces of work:

*   Andy Warhol – _[Gun](https://pyimagesearch.com/wp-content/uploads/2015/07/andy_warhol_gun.jpg)_
*   Andy Warhol – _[Marilyn Monroe](https://pyimagesearch.com/wp-content/uploads/2015/07/andy_warhol_marilyn_mmonroe.jpg)_
*   Jackson Pollock – _[Energy Made Visibile](https://pyimagesearch.com/wp-content/uploads/2015/07/jackson_pollock_energy_made_visible.jpg)_
*   MC Escher – _[Sky and Water I](https://pyimagesearch.com/wp-content/uploads/2015/07/mc_escher_sky_and_water_i.jpg)_
*   MC Escher – _[Mosaic II](https://pyimagesearch.com/wp-content/uploads/2015/07/mc_escher_mosaic_ii.jpg)_
*   Pablo Picasso – _[Guernica](https://pyimagesearch.com/wp-content/uploads/2015/07/pablo_picasso_guernica.jpg)_
*   Vincent van Gogh – _[Wheat Field with Cypresses](https://pyimagesearch.com/wp-content/uploads/2015/07/van_gogh_wheat_field_with_cypresses.jpg)_

And then I passed each of them through the `demo_guided.py` script detailed above.

Generating art with deep dreaming
---------------------------------

Overall, the results look really fantastic. I’m especially pleased with how _Wheat Field with Cypresses_ and _Guernica_ turned out when used as input images and the others paintings as guided images.

Below you can find some of my favorite images:

Vincent van Gogh – Wheat Field with Cypresses
---------------------------------------------

[![Image 3: Figure 3: Vincent van Gogh's Wheat Field with Cypresses guided using Warhol's Marilyn Monroe.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20244'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/van_gogh_08.jpg)

**Figure 2:** Vincent van Gogh’s _Wheat Field with Cypresses_ guided using Warhol’s _Marilyn Monroe_. Notice how eyes are now present in the sky.

[![Image 4: Vincent van Gogh's Wheat Field with Cypresses guided using Jackson Pollock's Energy Made Visible.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20244'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/van_gogh_09.jpg)

**Figure 3:** Vincent van Gogh’s _Wheat Field with Cypresses_ guided using Jackson Pollock’s _Energy Made Visible_.

[![Image 5: Figure 5: Vincent van Gogh's Wheat Field with Cypresses guided using Picasso's Guernica.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20244'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/van_gogh_12.jpg)

**Figure 4:** Vincent van Gogh’s _Wheat Field with Cypresses_ guided using Picasso’s _Guernica_.

Pablo Picasso – Guernica
------------------------

[![Image 6: Figure X: Pablo Picasso's Guernica guided with MC Escher's Sky and Water I.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20174'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/pablo_picasso_guernica_05.jpg)

**Figure 5:** Pablo Picasso’s _Guernica_ guided with MC Escher’s _Mosaic II_.

[![Image 7: Figure X: Pablo Picasso – Guernica's guided with Jackson Pollock's Energy Made Visible.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20174'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/pablo_picasso_guernica_03.jpg)

**Figure 6:** Pablo Picasso’s _Guernica_ guided with Jackson Pollock’s _Energy Made Visible_.

[![Image 8: Figure X: Pablo Picasso Guernica's guided with Andy Warhol's Marilyn Monroe.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20174'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/pablo_picasso_guernica_02.jpg)

**Figure 7:** Pablo Picasso’s _Guernica_ guided with Andy Warhol’s _Marilyn Monroe_.

MC Escher – Mosaic II
---------------------

[![Image 9: Figure X: MC Esher's Mosaic II guided by Jackson Pollock's Energy Made Visible.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20221'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/mc_escher_mosaic_ii_03.jpg)

**Figure 8:** MC Esher’s _Mosaic II_ guided by Jackson Pollock’s _Energy Made Visible_.

[![Image 10: Figure X: MC Esher's Mosaic II guided by Vincent van Gogh's Wheat Field with Cypresses.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20221'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/mc_escher_mosaic_ii_06.jpg)

**Figure 9:** MC Esher’s _Mosaic II_ guided by Vincent van Gogh’s _Wheat Field with Cypresses_.

Andy Warhol – Marilyn Monroe
----------------------------

[![Image 11: Figure 2: Andy Warhol's Marilyn Monroe guided using MC Escher's Mosaic II.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20300'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/andy_warhol_marilyn_mmonroe_04.jpg)

**Figure 10:** Andy Warhol’s _Marilyn Monroe_ guided using MC Escher’s _Sky and Water I_.

[![Image 12: Figure 3: Andy Warhol's Marilyn Monroe guided using Picasso's Guernica.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20300'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/07/andy_warhol_marilyn_mmonroe_05.jpg)

**Figure 11:** Andy Warhol’s _Marilyn Monroe_ guided using Picasso’s _Guernica_.

### 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
-------

In this blog post I reviewed the updates to the [bat-country package](https://github.com/jrosebr1/bat-country/) will allow for _**guided deep dreaming**_ by supplying two images: an input image that will be passed through the network, and a seed image that the network will use to “guide” the output.

I then took the updated code and used it to generate art from famous works, such as Jackson Pollock’s _Energy Made Visible_, Pablo Picasso’s _Guernica_, and Vincent van Gogh’s _Wheat Field with Cypresses_.

Definitely consider installing the `bat-country` package on your system and giving deep dreaming a try! It’s strangely addictive (and not to mention, a lot of fun) to play around and generate your own images.

Finally, if you’re interested in deep learning, deep dreaming, and computer vision, think about signing up for the PyImageSearch Newsletter by entering your email address in the form below. I send out regular updates on the PyImageSearch blog, each filled with actionable, real-world computer vision projects.

See you next time!

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

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.