Skip to content

Commit

Permalink
Loading model file from script directory, resizing output images t
Browse files Browse the repository at this point in the history
o same size.
  • Loading branch information
alexjc committed Apr 17, 2016
1 parent 6afaf1a commit cd5109a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
3 changes: 1 addition & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The ``doodle.py`` script generates an image by using three or four images as inp
Examples & Usage
================

Note the ``--device`` argument that lets you specify which GPU or CPU to use. For the samples above, here are the performance results:
The main script is called ``doodle.py``, which you can run with Python 3.4+. The ``--device`` argument that lets you specify which GPU or CPU to use. For the samples above, here are the performance results:

* **GPU Rendering** — Assuming you have CUDA setup and enough on-board RAM, the process should complete in 3 to 8 minutes, even with twice the iteration count.
* **CPU Rendering** — This will take hours and hours, even up to 12h on older hardware. To match quality it'd take twice the time. Do multiple runs in parallel!
Expand Down Expand Up @@ -68,7 +68,6 @@ If you want to transfer the style given a source style with annotations, and a t
To perform regular style transfer without semantic annotations, simply delete or rename the files with the semantic maps. The photo is originally by `Seth Johnson <http://sethjohnson.tumblr.com/post/655063019/this-was-a-project-for-an-art-history-class-turns>`_, and the concept for this style transfer by `Kyle McDonald <https://twitter.com/kcimc>`_.


.. image:: docs/Portraits_example.jpg


Expand Down
Binary file modified docs/Textures_example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 16 additions & 12 deletions doodle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Copyright (c) 2016, Alex J. Champandard.
#
# Research and development sponsored by the nucl.ai Conference!
#
# Research and Development sponsored by the nucl.ai Conference!
# http://events.nucl.ai/
# July 18-20, 2016 in Vienna/Austria.
#
Expand Down Expand Up @@ -148,13 +148,14 @@ def load_data(self):
"""Open the serialized parameters from a pre-trained network, and load them into the model created.
"""

if not os.path.exists('vgg19_conv.pkl.bz2'):
vgg19_file = os.path.join(os.path.dirname(__file__), 'vgg19_conv.pkl.bz2')
if not os.path.exists(vgg19_file):
print("{}ERROR: Model file with pre-trained convolution layers not found. Download here:{}\n"\
"https://github.com/alexjc/neural-doodle/releases/download/v0.0/vgg19_conv.pkl.bz2{}\n"\
.format(ansi.RED_B, ansi.RED, ansi.ENDC))
sys.exit(-1)

data = pickle.load(bz2.open('vgg19_conv.pkl.bz2', 'rb'))
data = pickle.load(bz2.open(vgg19_file, 'rb'))
params = lasagne.layers.get_all_param_values(self.network['main'])
lasagne.layers.set_all_param_values(self.network['main'], data[:len(params)])

Expand Down Expand Up @@ -184,9 +185,9 @@ def finalize_image(self, image, resolution):
to disk -- shuffling dimensions as appropriate.
"""

image = image.reshape(resolution)[::-1]
image = np.swapaxes(np.swapaxes(image, 0, 1), 1, 2)
return np.clip(image, 0, 255).astype('uint8')
image = np.swapaxes(np.swapaxes(image[::-1], 0, 1), 1, 2)
image = np.clip(image, 0, 255).astype('uint8')
return scipy.misc.imresize(image, resolution, interp='bicubic')


#----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -454,8 +455,9 @@ def evaluate(self, Xn):

# Dump the image to disk if requested by the user.
if args.save_every and self.frame % args.save_every == 0:
resolution = self.content_image.shape[1:]
image = scipy.misc.toimage(self.model.finalize_image(Xn, resolution), cmin=0, cmax=255)
frame = Xn.reshape(self.content_image.shape[1:])
resolution = self.content_img_original.shape
image = scipy.misc.toimage(self.model.finalize_image(frame, resolution), cmin=0, cmax=255)
image.save('frames/%04d.png'%self.frame)

# Print more information to the console every few iterations.
Expand Down Expand Up @@ -514,7 +516,7 @@ def run(self):
bounds = [int(i) for i in args.seed_range.split(':')]
Xn = np.random.uniform(bounds[0], bounds[1], shape + (3,)).astype(np.float32)
if args.seed == 'previous':
Xn = scipy.misc.imresize(Xn[0], shape)
Xn = scipy.misc.imresize(Xn[0], shape, interp='bicubic')
Xn = Xn.transpose((2, 0, 1))[np.newaxis]

# Optimization algorithm needs min and max bounds to prevent divergence.
Expand All @@ -527,7 +529,7 @@ def run(self):
Xn.astype(np.float64).flatten(),
bounds=data_bounds,
factr=0.0, pgtol=0.0, # Disable automatic termination, set low threshold.
m=4, # Maximum correlations kept in memory by algorithm.
m=5, # Maximum correlations kept in memory by algorithm.
maxfun=args.iterations-1, # Limit number of calls to evaluate().
iprint=-1) # Handle our own logging of information.
except OverflowError:
Expand All @@ -539,7 +541,9 @@ def run(self):
args.seed = 'previous'
resolution = self.content_image.shape
Xn = Xn.reshape(resolution)
scipy.misc.toimage(self.model.finalize_image(Xn, resolution[1:]), cmin=0, cmax=255).save(args.output)

output = self.model.finalize_image(Xn[0], self.content_img_original.shape)
scipy.misc.toimage(output, cmin=0, cmax=255).save(args.output)


if __name__ == "__main__":
Expand Down

0 comments on commit cd5109a

Please sign in to comment.