From 515b923d6dc019e110d31389a501f8232a6c903e Mon Sep 17 00:00:00 2001 From: Konstantinos Date: Wed, 10 Nov 2021 04:09:08 +0200 Subject: [PATCH] wrap up production python packaging --- README.rst | 37 ++++++++++++++++++++++++++++++----- src/artificial_artwork/cli.py | 25 +++++++++++++++++++---- tox.ini | 6 ++---- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/README.rst b/README.rst index 5495bd5..190b723 100644 --- a/README.rst +++ b/README.rst @@ -70,24 +70,51 @@ Sample commands to install NST CLI using a terminal: Usage ----- + +Download the Vgg-Verydeep-19 pretrained `model` from https://mega.nz/file/i5xDWI4Y. + +Exctract the model (weights and layer architecture). + +For example use `tar -xvf imagenet-vgg-verydeep-19.tar` to extract in the current directory. + +Indicate to the program where to find the model: + +:: + + export AA_VGG_19=$PWD/imagenet-vgg-verydeep-19.mat + +We have included one 'content' and one 'style' image in the source repository, to facilitate testing. +You can use these images to quickly try running the program. + +For example, you can get the code with `git clone git@github.com:boromir674/neural-style-transfer.git`, +then `cd neural-style-transfer`. + Assuming you have installed using a symbolic link in your PATH (as shown above), or if you are still -operating withing your virtual environment, -then a sample example of using the cli program would be: +operating withing your virtual environment, then you can create artificial artwork with the following command. +The algorithm will apply the style to the content iteratively. +It will iterate 100 times. :: - neural-style-transfer tests/data/canoe_water.jpg tests/data/./tests/data/blue-red-w400-h300.jpg + # Create a directory where to store the artificial artwork + mkdir nst_output + + # Run a Neural Style Algorithm for 100 iterations and store output to nst_output directory + neural-style-transfer tests/data/canoe_water.jpg tests/data/blue-red-w400-h300.jpg --location nst_output + Note we are using as 'content' and 'style' images jpg files included in the distribution (artificial-artwork package). We are using a photo of a canoe on water and an abstract painting with prevalence of blue and red color shades. Also note that to demonstrate quicker, both images have been already resized to just 400 pixels of width and 300 of height each. +Navigating to `nst_output` you can find multiple image files generated from running the algorithm. Each file corresponds to the +image generated on a different iteration while running the algorithm. The bigger the iteration the more "style" has been applied. + +Check out your artificial artwork! -:: - xdg-open .. |circleci| image:: https://img.shields.io/circleci/build/github/boromir674/neural-style-transfer/master?logo=circleci diff --git a/src/artificial_artwork/cli.py b/src/artificial_artwork/cli.py index db3f60c..24300d9 100644 --- a/src/artificial_artwork/cli.py +++ b/src/artificial_artwork/cli.py @@ -10,7 +10,25 @@ from .algorithm_progress import NSTAlgorithmProgress from .termination_condition.termination_condition import TerminationConditionFacility from .termination_condition_adapter import TerminationConditionAdapterFactory -from .model_loader import load_vgg_model + + +def get_vgg_verydeep_19_model(): + try: + return os.environ['AA_VGG_19'] + except KeyError: + file_path = os.path.join(os.getcwd(), 'imagenet-vgg-verydeep-19.mat') + if os.path.exists(file_path): + return file_path + file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'imagenet-vgg-verydeep-19.mat') + if os.path.exists(file_path): + return file_path + raise NoImageModelSpesifiedError('No pretrained image model found. ' + 'Please download it and set the AA_VGG_19 environment variable with the' + 'path where ou stored the model (*.mat file), to indicate to wher to ' + 'locate and load it') + + +class NoImageModelSpesifiedError(Exception): pass @click.command() @@ -18,11 +36,10 @@ @click.argument('style_image') @click.option('--interactive', '-i', type=bool, default=True) @click.option('--iterations', '-it', type=int, default=100) -@click.option('--location', '-l', type=str, default='nst_output') +@click.option('--location', '-l', type=str, default='.') def cli(content_image, style_image, interactive, iterations, location): - # my_dir = os.path.dirname(os.path.realpath(__file__)) - IMAGE_MODEL_PATH = os.path.join('/', 'data', 'repos', 'neural-style-transfer', 'imagenet-vgg-verydeep-19.mat') + IMAGE_MODEL_PATH = get_vgg_verydeep_19_model() TERMINATION_CONDITION = 'max-iterations' STYLE_LAYERS = [ ('conv1_1', 0.2), diff --git a/tox.ini b/tox.ini index 2dee72b..8c7db1a 100644 --- a/tox.ini +++ b/tox.ini @@ -83,9 +83,7 @@ deps = mypy skip_install = true commands = - mypy {posargs} --follow-imports skip --install-types {toxinidir}/src/neural_style_transfer - ; mypy {posargs} --install-types {toxinidir}/src/neural_style_transfer/utils - ; mypy {posargs} --follow-imports skip --install-types {toxinidir}/src/neural_style_transfer/disk_operations.py + mypy {posargs} --follow-imports skip --install-types {toxinidir}/src/{env:PY_PACKAGE} ## PYTHON PACKAGING @@ -252,7 +250,7 @@ description = Generate UML (class and package) diagrams by inspecting the code. setenv = {[testenv]setenv} # include dirs to pythonpath to solve issue of inspect lib failing with for some relative imports - PYTHONPATH={toxinidir}/src/neural_style_transfer:{toxinidir}/src/neural_style_transfer/utils + PYTHONPATH={toxinidir}/src/{env:PY_PACKAGE}:{toxinidir}/src/{env:PY_PACKAGE}/utils UML_DIAGRAMS=uml-diagrams deps = click