Giter VIP home page Giter VIP logo

bat-country's Introduction

bat-country


We can't stop here, this is bat country. — Raoul Duke

The bat-country package is an easy to use, highly extendible, lightweight Python module for inceptionism and deep dreaming with Convolutional Neural Networks and Caffe. My contributions here are honestly pretty minimal. All the real research has been done by the Google Research Team — I'm simply taking the IPython Notebook, turning it into a Python module, while keeping in mind the importance of extensibility, such as custom step functions.

If you're looking for a more advanced CNN visualization tool, check out Justin Johnson's cnn-vis library.

Installation

The bat-country packages requires Caffe, an open-source CNN implementation from Berkeley, to be already installed on your system. I detail the steps required to get Caffe up and running on your system in the official bat-country release post. An excellent alternative is to use the Docker image provided by Tomasz of Vision.ai. Using the Docker image will get you up and running quite painlessly.

After you have Caffe setup and working, bat-country is a breeze to install. Just use pip:

$ pip install bat-country

You can also install bat-country by pulling down the repository and setup.py:

$ git clone https://github.com/jrosebr1/bat-country.git
$ pip install -r requirements.txt
$ python setup.py install

A simple example

As I mentioned, one of the goals of bat-country is simplicity. Provided you have already installed Caffe and bat-country on your system, it only takes 3 lines of Python code to generate a deep dream/inceptionism image:

# we can't stop here...
bc = BatCountry("caffe/models/bvlc_googlenet")
image = bc.dream(np.float32(Image.open("/path/to/image.jpg")))
bc.cleanup()

After executing this code, you can then take the image returned by the dream method and write it to file:

result = Image.fromarray(np.uint8(image))
result.save("/path/to/output.jpg")

And that's it!

For more information on bat-country, along with more code examples, head over to the the official announcement post on the PyImageSearch blog:

http://www.pyimagesearch.com/2015/07/06/bat-country-an-extendible-lightweight-python-package-for-deep-dreaming-with-caffe-and-convolutional-neural-networks/

Guided dreaming

Google has also demonstrated that it's 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 the output.

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

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()

What's nice about this approach is that I "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:

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:

Here's another example, this time using a seed image of an antelope:

You can read more about guided dreaming, plus view more example images here:

http://www.pyimagesearch.com/2015/07/13/generating-art-with-guided-deep-dreaming/

Some visual examples

Below are a few example images generated using the bat-country package:
















bat-country's People

Contributors

jrosebr1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bat-country's Issues

Error running visualize_layers.py

I'm trying to replicate the results from
https://www.pyimagesearch.com/2015/08/03/deep-dream-visualizing-every-layer-of-googlenet/
However, when I get near the end, it crashes, and I'm not quite sure what to do with it.
Bat Country can run single images without difficulty, but doesn't quite make it here.
To note, I am using the bvlc_googlenet model that I got when looking into deepdreamanim.
my command line is:
python visualize_layers.py --base-model /root/deepdream/caffe/models/bvlc_googlenet --image g1.jpg --output bvlc_googlenet

The crash looks like:

[INFO] processing layer inception_5b/pool 145/152
[INFO] processing layer inception_5b/pool_proj 146/152
[INFO] processing layer inception_5b/relu_pool_proj 147/152
[ERROR] cannot use layer inception_5b/relu_pool_proj
[INFO] processing layer inception_5b/output 148/152
[INFO] processing layer pool5/7x7_s1 149/152
F0621 18:21:13.746654 2164 blob.cpp:32] Check failed: shape[i] >= 0 (-3 vs. 0)
*** Check failure stack trace: ***
Aborted (core dumped)

I'd appreciate any and all help. Thank you.

Changing the model from GoogLenet

I'm getting some errors when I try to change the model from the default GoogLenet. I'm probably just not understanding the API correctly.

This works:

>>> BatCountry('/caffe-master/models/bvlc_googlenet')

But this doesn't:

>>> BatCountry('/caffe-master/models/bvlc_alexnet')  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "batcountry/batcountry.py", line 34, in __init__
    channel_swap=channels)
  File "/caffe-master/distribute/python/caffe/classifier.py", line 26, in __init__
    caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
RuntimeError: Could not open file /caffe-master/models/bvlc_alexnet/bvlc_googlenet.caffemodel

Are there additional arguments that need to be set when changing the model? I couldn't figure this out from the doc. In demo_bulk.py it seems like the only argument we pass is the path to the model.

PIP install bat-country Breaks tensorflow in virtualenv

issue

Bat-country dependancies are out of date - thus when I ran
pip install bat-country
I got errors when you try to run tensorflow in same env because you have just downgraded all the packages... incl protobuf.

work around

pip uninstall bat-country
pip install --upgrade (all packages that just got downgraded) 
or 
pip install tensorflow (*should* upgrade everything that needs upgrading)

I uninstalled bat-country then re-upgraded the relevant tensorflow packages everything worked again.
then

git clone https://github.com/jrosebr1/bat-country.git
python setup.py install

and it should work...

UnboundLocalError: local variable 'nH' referenced before assignment

Having a spot of trouble with prepare_guide, as described in your example code.

Traceback (most recent call last):
  File "./guided-test.py", line 30, in <module>
    features = bc.prepare_guide(Image.open(guide_image), end=layer)
  File "/usr/local/lib/python2.7/dist-packages/batcountry/batcountry.py", line 185, in prepare_guide
    src.reshape(1, 3, nH, nW)
UnboundLocalError: local variable 'nH' referenced before assignment

Here are the pertinent bits of my script.

bc = BatCountry("/home/username/caffe/models/bvlc_googlenet")

input_image = "/home/username/images/monkey.jpg"
output_image = "/home/username/test-guided.jpg"
guide_image = "/home/username/guides/starry-guide.jpg"

layer = "inception_4c/output"

features = bc.prepare_guide(Image.open(guide_image), end=layer)
image = bc.dream(np.float32(
    Image.open(input_image)),
    end=layer,
    iter_n=20,
    objective_fn=BatCountry.guided_objective,
    objective_features=features,
    )

result = Image.fromarray(np.uint8(image))
result.save(output_image)
input_image = output_image

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.