Giter Club home page Giter Club logo

neural-art-tf's Introduction

"Neural Art" in TensorFlow

An implementation of "A neural algorithm of Artistic style" in TensorFlow, for

  • Introductory, hackable demos for TensorFlow, and
  • Demonstrating the use of importing various Caffe cnn models (VGG and illustration2vec) in TF.

In this work, I put effort in putting the code simple as possible, for being a good introductory code to TF. For this reason, I also implemented very basic uses of TensorBoard (the visualizer). I also aimed on demonstrating the use of importing various Caffe models from *.caffemodel files into TensorFlow, especially models that seemed not to be imported by anybody yet in TF (as far as I know). Based on https://github.com/ethereon/caffe-tensorflow, I modified the importer so that it can import illustration2vec (http://illustration2vec.net/), which is another CNN available as a Caffe model. Using different CNNs yields different results, which reflects the characteristics of the model.

In the Neural Art problem setting, the weights of the CNN are fixed, and the input image into the CNN is the only "trainable" variable, making the code easy to understand (the optimized/trained image is the output image). I hope this example serves as a good introduction to TensorFlow as well as for entertainment purposes.

(VGG, default settings, 70 iterations)

(illustration2vec, width=500, beta=10000, 100 iterations)

##Usage

Step 0: Prepare the Caffe model

First, download either the VGG model or the illustration2vec model (*.caffemodel), along with the prototxt (*.prototxt):

Then, convert the model to a binary format recognizable to TensorFlow:

python ./kaffe/kaffe.py [path.prototxt] [path.caffemodel] [output-path]

Note that Caffe is not required for conversion.

The converter included in this repo (all code inside ./kaffe) is a modified version of (an old version of) https://github.com/ethereon/caffe-tensorflow . The converter is modified to be capable of handling the illusration2vec neural network. Since the newer version of the converter requires preprocessing with the Caffe framework for old-format Caffe models (at the time of writing), I have included the converter which is based on the older code, which is capable of handling old-format Caffe models.

Step 1: Neural Art

python neural-art-tf.py

Running neural-art-tf.py without options yields the default settings and input images. Available options are:

  • -m, --model: Model type - Use vgg or i2v
  • -mp, --modelpath: Model file path - The path to the converted Caffe model in Step 0
  • -c, --content: Content image path
  • -s, --style: Style image path
  • -w, --width: Output image width
  • -i, --iters: Number of iterations
  • -a, --alpha: alpha (content weight)
  • -b, --beta: beta (style weight)

For example:

python neural-art-tf.py -m vgg -mp ./vgg -c ./images/sd.jpg -s ./images/style.jpg -w 800

You can view the progress on tensorboard by running

tensorboard --logdir=/tmp/na-logs

References

neural-art-tf's People

Contributors

hi-king avatar woodrush 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  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

neural-art-tf's Issues

Converting model on Windows doesn't work.

On Windows 10 and using TF 0.12, using Python 3.5.2 to run python ./kaffe/kaffe.py and the illust2vec model and prototext gives me this error:

  File "./kaffe/kaffe.py", line 14
    print "scope:", key
                 ^
SyntaxError: Missing parentheses in call to 'print'

Running it with Python 2 instead doesn't let me import Tensorflow since on Windows only Py3.5 can be used.

I fixed the print statement manually, and got a new and exciting error:

WARNING: PyCaffe not found!
Falling back to protocol buffer implementation.
This may take a couple of minutes.
Traceback (most recent call last):
  File "./kaffe/kaffe.py", line 29, in load
    self.load_using_caffe()
  File "./kaffe/kaffe.py", line 37, in load_using_caffe
    import caffe
ImportError: No module named 'caffe'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\google\protobuf\internal\python_message.py", line 1087, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\google\protobuf\internal\python_message.py", line 1109, in InternalParse
    (tag_bytes, new_pos) = local_ReadTag(buffer, pos)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\google\protobuf\internal\decoder.py", line 181, in ReadTag
    while six.indexbytes(buffer, pos) & 0x80:
TypeError: unsupported operand type(s) for &: 'str' and 'int'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./kaffe/kaffe.py", line 104, in <module>
    main()
  File "./kaffe/kaffe.py", line 101, in main
    CaffeDataReader(def_path, data_path).dump(dst_path)
  File "./kaffe/kaffe.py", line 25, in __init__
    self.load()
  File "./kaffe/kaffe.py", line 34, in load
    self.load_using_pb()
  File "./kaffe/kaffe.py", line 43, in load_using_pb
    import caffepb
  File "C:\Users\User\Documents\tensorFlowExp\neural-art-tf\kaffe\caffepb.py", line 712, in <module>
    options=_descriptor._ParseOptions(descriptor_pb2.FieldOptions(), '\020\001')),
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\google\protobuf\descriptor.py", line 869, in _ParseOptions
    message.ParseFromString(string)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\google\protobuf\message.py", line 185, in ParseFromString
    self.MergeFromString(serialized)
  File "C:\Users\User\AppData\Local\Programs\Python\Python35\lib\site-packages\google\protobuf\internal\python_message.py", line 1093, in MergeFromString
    raise message_mod.DecodeError('Truncated message.')
google.protobuf.message.DecodeError: Truncated message.

Your docs specifically say Caffe isn't required, but this error message is misleading in that it pretends that it is (checking the code reveals that it isn't). Seems that the load_using_pb() function is also throwing an error, presumably due to Python 3.

AttributeError: Can't pickle local object 'CaffeDataReader.dump.<locals>.convert'

I install caffe and tensorflow for python3.5, and run the kaffe.py to convert data. However, it throws an error when saving data.

  File "kaffe/kaffe.py", line 109, in <module>
    main()
  File "kaffe/kaffe.py", line 106, in main
    CaffeDataReader(def_path, data_path).dump(dst_path)
  File "kaffe/kaffe.py", line 97, in dump
    np.save(open(dst_path, 'wb'), dict(params))
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/npyio.py", line 509, in save
    pickle_kwargs=pickle_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/numpy/lib/format.py", line 576, in write_array
    pickle.dump(array, fp, protocol=2, **pickle_kwargs)
AttributeError: Can't pickle local object 'CaffeDataReader.dump.<locals>.convert'

How should I fix it?

No module named 'caffe'

Hello, I run python3 kaffe/kaffe.py '*.prototxt' '*.caffemodel' . and got an error "ImportError: No module named 'caffe'". Does it mean I have to install the caffe?

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.