-
Notifications
You must be signed in to change notification settings - Fork 21
/
deep_dream_test.py
executable file
·50 lines (42 loc) · 1.74 KB
/
deep_dream_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
"""Test/benchmark deep_dream.py."""
from pathlib import Path
import logging
import time
import click
from PIL import Image
import deep_dream as dd
from deep_dream import tile_worker
import utils
utils.setup_traceback()
logger = logging.getLogger(__name__)
handler = logging.StreamHandler(dd.stream)
handler.setFormatter(utils.ColorFormatter())
logging.basicConfig(level=logging.INFO, handlers=[handler])
tile_worker.logger.setLevel(logging.DEBUG)
@click.command()
@click.option('--cpu-workers', default=0, help='The number of CPU workers to start.')
@click.option('--gpus', type=utils.List(int, 'integer'), default='',
help='The CUDA device IDs to use.')
@click.option('--max-tile-size', default=512, help='The maximum dimension of a tile.')
def main(cpu_workers=None, gpus=None, max_tile_size=None):
"""Test/benchmark deep_dream.py."""
pwd = Path(__file__).parent
cnn = dd.CNN(dd.GOOGLENET_BVLC, cpu_workers=cpu_workers, gpus=gpus)
input_img = Image.open(str(pwd/'kodim/img0022.jpg')).resize((1536, 1024), Image.LANCZOS)
print('Input image classes:')
for category in cnn.classify(input_img, 5):
print('%.3f %s' % category)
cnn.dream(input_img, 'inception_3a/3x3', min_size=129, n=1, max_tile_size=max_tile_size)
time_0 = time.perf_counter()
img = cnn.dream(input_img, 'inception_3a/3x3', min_size=129, n=10, step_size=1,
max_tile_size=max_tile_size)
time_1 = time.perf_counter()
print('Input image classes:')
for category in cnn.classify(img, 5):
print('%.3f %s' % category)
print('Time taken: %.3f s' % (time_1-time_0))
dd.to_image(img).save('test_output.png')
print('Saved to test_output.png.')
if __name__ == '__main__':
main()