From 0ed88c4be607c0275c4310e8a3c449d1ba60e0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Barbero=20Jim=C3=A9nez?= Date: Thu, 26 Oct 2017 19:21:13 +0200 Subject: [PATCH] Added bug with outfolder Changed gatys default parameters to weight 5, iters 500 Added tile overlap parameter, with default value 100 pixels --- README.md | 17 ++++++++++++++--- entrypoint.py | 15 ++++++++++++--- neuralstyle/algorithms.py | 17 ++++++++++------- tests/algorithms_tests.py | 2 +- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d514868..d7540de 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/entrypoint.py b/entrypoint.py index ce0be97..438af9b 100644 --- a/entrypoint.py +++ b/entrypoint.py @@ -15,7 +15,7 @@ --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) @@ -23,6 +23,7 @@ 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. @@ -42,6 +43,7 @@ def main(argv=None): weights = None stylescales = None tilesize = None + tileoverlap = None otherparams = [] # Gather parameters @@ -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] @@ -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) @@ -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: diff --git a/neuralstyle/algorithms.py b/neuralstyle/algorithms.py index 7ff810f..5da17c4 100644 --- a/neuralstyle/algorithms.py +++ b/neuralstyle/algorithms.py @@ -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": { @@ -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(): @@ -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 = [] @@ -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() @@ -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") diff --git a/tests/algorithms_tests.py b/tests/algorithms_tests.py index 7c5da32..c3249c5 100644 --- a/tests/algorithms_tests.py +++ b/tests/algorithms_tests.py @@ -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)