Skip to content

Commit

Permalink
wrap up production python packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
boromir674 committed Nov 10, 2021
1 parent a7d57eb commit 515b923
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
37 changes: 32 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions src/artificial_artwork/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,36 @@
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()
@click.argument('content_image')
@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),
Expand Down
6 changes: 2 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 515b923

Please sign in to comment.