/
dlearn.py
executable file
·64 lines (51 loc) · 1.48 KB
/
dlearn.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#! /usr/bin/env python2.7
"""Learn some high-level features for images like these:
http://www.robots.ox.ac.uk/~vgg/data/flowers/17/
"""
from os.path import basename, join
import click
from logbook import Logger, StderrHandler
import pandas as pd
from sklearn.decomposition import dict_learning_online
from skimage.data import imread
from skimage.transform import resize
log = Logger(basename(__file__))
IMG_NROW = 200
IMG_NCOL = 220
N_ITER = 10
BATCH_SIZE = 1
def prep(image_name):
bname = basename(image_name)
log.info('reading {}'.format(bname))
image = imread(image_name, as_grey=True)
log.info('resizing {}'.format(bname))
return resize(image, (IMG_NROW, IMG_NCOL))
@click.command()
@click.option(
'--output',
type=click.File('wb'),
default='-')
@click.argument(
'image_files',
nargs=-1,
type=click.Path(exists=True))
def main(output, image_files):
log.info('starting with {} image files'.format(len(image_files)))
images = (prep(i) for i in image_files)
log.info('starting online dictionary learning')
D = None
for image in images:
D = dict_learning_online(
image,
dict_init=D,
n_components=2000,
verbose=True,
n_jobs=-1,
n_iter=N_ITER,
batch_size=BATCH_SIZE,
return_code=False)
output.write(pd.DataFrame(D).to_csv())
log.info('done')
if __name__ == '__main__':
with StderrHandler().applicationbound():
main()