|
1
|
|
|
|
|
2
|
|
|
import os |
|
3
|
|
|
import click |
|
4
|
|
|
|
|
5
|
|
|
from .disk_operations import Disk |
|
6
|
|
|
from .styling_observer import StylingObserver |
|
7
|
|
|
from .algorithm import NSTAlgorithm, AlogirthmParameters |
|
8
|
|
|
from .nst_tf_algorithm import NSTAlgorithmRunner |
|
9
|
|
|
from .image import ImageFactory, ImageProcessingConfig |
|
10
|
|
|
from .algorithm_progress import NSTAlgorithmProgress |
|
11
|
|
|
from .termination_condition.termination_condition import TerminationConditionFacility |
|
12
|
|
|
from .termination_condition_adapter import TerminationConditionAdapterFactory |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def get_vgg_verydeep_19_model(): |
|
16
|
|
|
try: |
|
17
|
|
|
return os.environ['AA_VGG_19'] |
|
18
|
|
|
except KeyError: |
|
19
|
|
|
file_path = os.path.join(os.getcwd(), 'imagenet-vgg-verydeep-19.mat') |
|
20
|
|
|
if os.path.exists(file_path): |
|
21
|
|
|
return file_path |
|
22
|
|
|
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'imagenet-vgg-verydeep-19.mat') |
|
23
|
|
|
if os.path.exists(file_path): |
|
24
|
|
|
return file_path |
|
25
|
|
|
raise NoImageModelSpesifiedError('No pretrained image model found. ' |
|
26
|
|
|
'Please download it and set the AA_VGG_19 environment variable with the' |
|
27
|
|
|
'path where ou stored the model (*.mat file), to indicate to wher to ' |
|
28
|
|
|
'locate and load it') |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
class NoImageModelSpesifiedError(Exception): pass |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
@click.command() |
|
35
|
|
|
@click.argument('content_image') |
|
36
|
|
|
@click.argument('style_image') |
|
37
|
|
|
@click.option('--interactive', '-i', type=bool, default=True) |
|
38
|
|
|
@click.option('--iterations', '-it', type=int, default=100) |
|
39
|
|
|
@click.option('--location', '-l', type=str, default='.') |
|
40
|
|
|
def cli(content_image, style_image, interactive, iterations, location): |
|
41
|
|
|
|
|
42
|
|
|
IMAGE_MODEL_PATH = get_vgg_verydeep_19_model() |
|
43
|
|
|
TERMINATION_CONDITION = 'max-iterations' |
|
44
|
|
|
STYLE_LAYERS = [ |
|
45
|
|
|
('conv1_1', 0.2), |
|
46
|
|
|
('conv2_1', 0.2), |
|
47
|
|
|
('conv3_1', 0.2), |
|
48
|
|
|
('conv4_1', 0.2), |
|
49
|
|
|
('conv5_1', 0.2), |
|
50
|
|
|
] |
|
51
|
|
|
|
|
52
|
|
|
image_factory = ImageFactory(Disk.load_image) |
|
53
|
|
|
|
|
54
|
|
|
# for now we have hardcoded the config to receive 300 x 400 images with 3 color channels |
|
55
|
|
|
image_process_config = ImageProcessingConfig.from_image_dimensions() |
|
56
|
|
|
|
|
57
|
|
|
termination_condition = TerminationConditionFacility.create(TERMINATION_CONDITION, iterations) |
|
58
|
|
|
termination_condition_adapter = TerminationConditionAdapterFactory.create(TERMINATION_CONDITION, termination_condition) |
|
59
|
|
|
print(f' -- Termination Condition: {termination_condition}') |
|
60
|
|
|
|
|
61
|
|
|
algorithm_parameters = AlogirthmParameters( |
|
62
|
|
|
image_factory.from_disk(content_image), |
|
63
|
|
|
image_factory.from_disk(style_image), |
|
64
|
|
|
IMAGE_MODEL_PATH, |
|
65
|
|
|
STYLE_LAYERS, |
|
66
|
|
|
termination_condition_adapter, |
|
67
|
|
|
location, |
|
68
|
|
|
) |
|
69
|
|
|
|
|
70
|
|
|
algorithm = NSTAlgorithm(algorithm_parameters, image_process_config) |
|
71
|
|
|
|
|
72
|
|
|
algorithm_runner = NSTAlgorithmRunner.default(algorithm, image_factory.image_processor.noisy) |
|
73
|
|
|
|
|
74
|
|
|
algorithm_progress = NSTAlgorithmProgress({}) |
|
75
|
|
|
styling_observer = StylingObserver(Disk.save_image) |
|
76
|
|
|
|
|
77
|
|
|
algorithm_runner.progress_subject.add( |
|
78
|
|
|
algorithm_progress, |
|
79
|
|
|
termination_condition_adapter, |
|
80
|
|
|
) |
|
81
|
|
|
algorithm_runner.peristance_subject.add( |
|
82
|
|
|
styling_observer |
|
83
|
|
|
) |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
algorithm_runner.run() |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
if __name__ == '__main__': |
|
90
|
|
|
cli() |
|
91
|
|
|
|