Skip to content

Commit

Permalink
Merge pull request #16 from albarji/bugfix/output
Browse files Browse the repository at this point in the history
Fixed bug with outfolder + configurable overlap + some minor improvements
  • Loading branch information
albarji authored Oct 26, 2017
2 parents 1abb9b9 + 0ed88c4 commit 28901be
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ of having a local structure such as
styles/
picasso.png
vangogh.png
results/
something.md
applying the *vangogh.png* style to the *docker.png* image amounts to

Expand All @@ -59,6 +61,14 @@ applying the *vangogh.png* style to the *docker.png* image amounts to
You can provide several content and style images, in which case all cross-combinations will be generated.

nvidia-docker run --rm -v $(pwd):/images albarji/neural-style --content contents/docker.png contents/whatever.jpg --style styles/vangogh.png styles/picasso.png

By default all generated images are saved into the container **/images**. When running the commands above the results
will get saved into the host local folder (through the mounted volume). If you want to locate the generated images at a
different folder you can use the --output parameters. Note that, similarly to the --content and --style arguments,
the path provided in --output will be regarded as relative to the /images folder. For instance, using the local
structure from the example above, saving the generated images in the "results" folder will require the following:

nvidia-docker run --rm -v $(pwd):/images albarji/neural-style --content contents/docker.png --style styles/vangogh.png --output results

### Fine tuning the results

Expand Down Expand Up @@ -105,10 +115,11 @@ as smaller in the rendered image.
#### Style weight

Gatys algorithm allows to adjust the amount of style imposed over the content image, by means of the --sw parameter.
By default a value of **10** is used, meaning the importance of the style is 10 times the importance of the content.
Smaller weight values result in the transfer of colors, while higher values transfer textures and details of the style
By default a value of **5** is used, meaning the importance of the style is 5 times the importance of the content.
Smaller weight values result in the transfer of colors, while higher values transfer textures and even objects of the
style.

If several weight values can be provided, all combinations will be generated. For instance, to generate the same
If several weight values are provided, all combinations will be generated. For instance, to generate the same
style transfer with three different weights, use

nvidia-docker run --rm -v $(pwd):/images albarji/neural-style --content contents/docker.png --style styles/vangogh.png --sw 5 10 20
Expand Down
15 changes: 12 additions & 3 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
--style STYLE_IMAGES: file or files with the styles to transfer
--output OUTPUT_FOLDER: name of the output folder in which to save results
--size SIZE: size of the output image. Default: content image size
--sw STYLE_WEIGHT (default 10): weight or list of weights of the style over the content, in range (0, inf)
--sw STYLE_WEIGHT (default 5): weight or list of weights of the style over the content, in range (0, inf)
--ss STYLE_SCALE (default 1.0): scaling or list of scaling factors for the style images
--alg ALGORITHM: style-transfer algorithm to use. Must be one of the following:
gatys Highly detailed transfer, slow processing times (default)
chen-schmidt Fast patch-based style transfer
chen-schmidt-inverse Even faster aproximation to chen-schmidt through the use of an inverse network
--tilesize TILE_SIZE: maximum size of each tile in the style transfer.
If your GPU runs out of memory you should try reducing this value. Default: 400
--tileoverlap TILE_OVERLAP: overlap of tiles in the style transfer, measured in pixels. Default: 100
Additionally provided parameters are carried on to the underlying algorithm.
Expand All @@ -42,6 +43,7 @@ def main(argv=None):
weights = None
stylescales = None
tilesize = None
tileoverlap = None
otherparams = []

# Gather parameters
Expand All @@ -56,7 +58,7 @@ def main(argv=None):
i += len(styles) + 1
# Other general parameters
elif argv[i] == "--output":
savefolder = argv[i+1]
savefolder = "/images/" + argv[i+1]
i += 2
elif argv[i] == "--alg":
alg = argv[i+1]
Expand All @@ -73,6 +75,9 @@ def main(argv=None):
elif argv[i] == "--tilesize":
tilesize = int(argv[i+1])
i += 2
elif argv[i] == "--tileoverlap":
tileoverlap = int(argv[i+1])
i += 2
# Help
elif argv[i] == "--help":
print(HELP)
Expand All @@ -94,7 +99,11 @@ def main(argv=None):
LOGGER.info("\tAlgorithm = %s" % alg)
LOGGER.info("\tStyle weights = %s" % str(weights))
LOGGER.info("\tStyle scales = %s" % str(stylescales))
styletransfer(contents, styles, savefolder, size, alg, weights, stylescales, tilesize, algparams=otherparams)
LOGGER.info("\tSize = %s" % str(size))
LOGGER.info("\tTile size = %s" % str(tilesize))
LOGGER.info("\tTile overlap = %s" % str(tileoverlap))
styletransfer(contents, styles, savefolder, size, alg, weights, stylescales, tilesize, tileoverlap,
algparams=otherparams)
return 1

except Exception:
Expand Down
17 changes: 10 additions & 7 deletions neuralstyle/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"-content_weight", "100",
"-save_iter", "10000",
"-proto_file", "/app/neural-style/models/VGG_ILSVRC_19_layers_deploy.prototxt",
"-model_file", "/app/neural-style/models/VGG_ILSVRC_19_layers.caffemodel"
"-model_file", "/app/neural-style/models/VGG_ILSVRC_19_layers.caffemodel",
"-num_iterations", "500"
]
},
"chen-schmidt": {
Expand All @@ -47,7 +48,7 @@


def styletransfer(contents, styles, savefolder, size=None, alg="gatys", weights=None, stylescales=None,
maxtilesize=400, algparams=None):
maxtilesize=400, tileoverlap=100, algparams=None):
"""General style transfer routine over multiple sets of options"""
# Check arguments
if alg not in ALGORITHMS.keys():
Expand All @@ -60,11 +61,13 @@ def styletransfer(contents, styles, savefolder, size=None, alg="gatys", weights=
weights = [None]
else:
if weights is None:
weights = [10.0]
weights = [5.0]
if stylescales is None:
stylescales = [1.0]
if maxtilesize is None:
maxtilesize = 400
if tileoverlap is None:
tileoverlap = 100
if algparams is None:
algparams = []

Expand All @@ -77,11 +80,11 @@ def styletransfer(contents, styles, savefolder, size=None, alg="gatys", weights=
stylescale=scale, algparams=algparams)
# Else use a tiling strategy
else:
neuraltile(content=content, style=style, outfile=outfile, size=size, maxtilesize=maxtilesize, alg=alg,
weight=weight, stylescale=scale, algparams=algparams)
neuraltile(content=content, style=style, outfile=outfile, size=size, maxtilesize=maxtilesize,
overlap=tileoverlap, alg=alg, weight=weight, stylescale=scale, algparams=algparams)


def styletransfer_single(content, style, outfile, size=None, alg="gatys", weight=10.0, stylescale=1.0, algparams=None):
def styletransfer_single(content, style, outfile, size=None, alg="gatys", weight=5.0, stylescale=1.0, algparams=None):
"""General style transfer routine over a single set of options"""
workdir = TemporaryDirectory()

Expand All @@ -108,7 +111,7 @@ def styletransfer_single(content, style, outfile, size=None, alg="gatys", weight
mergealpha(algfile, alphafile, outfile)


def neuraltile(content, style, outfile, size=None, maxtilesize=400, overlap=50, alg="gatys", weight=10.0,
def neuraltile(content, style, outfile, size=None, maxtilesize=400, overlap=100, alg="gatys", weight=5.0,
stylescale=1.0, algparams=None):
"""Strategy to generate a high resolution image by running style transfer on overlapping image tiles"""
LOGGER.info("Starting tiling strategy")
Expand Down
2 changes: 1 addition & 1 deletion tests/algorithms_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_neuraltile():
tmpdir = TemporaryDirectory()
content = CONTENTS + "avila-walls.jpg"
outfile = tmpdir.name + "/tiled.png"
neuraltile(content, STYLES + "cubism.jpg", outfile, alg="chen-schmidt-inverse", maxtilesize=400)
neuraltile(content, STYLES + "cubism.jpg", outfile, alg="chen-schmidt-inverse", maxtilesize=400, overlap=100)
assert shape(outfile) == shape(content)


Expand Down

0 comments on commit 28901be

Please sign in to comment.