lib.py 3.2 KB
Newer Older
S
superjom 已提交
1
import pprint
S
superjom 已提交
2
import re
S
superjom 已提交
3
import urllib
S
superjom 已提交
4 5 6 7 8 9
from tempfile import NamedTemporaryFile

import numpy as np
from PIL import Image

import storage
S
superjom 已提交
10 11


S
superjom 已提交
12 13 14 15
def get_modes(storage):
    return storage.modes()


S
superjom 已提交
16 17 18 19
def get_scalar_tags(storage, mode):
    result = {}
    for mode in storage.modes():
        reader = storage.as_mode(mode)
S
superjom 已提交
20 21 22 23 24 25 26 27
        tags = reader.tags('scalar')
        if tags:
            result[mode] = {}
            for tag in tags:
                result[mode][tag] = {
                    'displayName': reader.scalar(tag).caption(),
                    'description': "",
                }
S
superjom 已提交
28 29 30
    return result


S
superjom 已提交
31 32 33 34 35 36 37 38 39 40 41 42
def get_scalar(storage, mode, tag):
    reader = storage.as_mode(mode)
    scalar = reader.scalar(tag)

    records = scalar.records()
    ids = scalar.ids()
    timestamps = scalar.timestamps()

    result = zip(timestamps, ids, records)
    return result


S
superjom 已提交
43
def get_image_tags(storage):
S
superjom 已提交
44 45 46 47
    result = {}

    for mode in storage.modes():
        reader = storage.as_mode(mode)
S
superjom 已提交
48 49 50 51 52 53 54 55 56
        tags = reader.tags('image')
        if tags:
            result[mode] = {}
            for tag in tags:
                image = reader.image(tag)
                for i in xrange(max(1, image.num_samples())):
                    tag = image.caption() if image.num_samples() <= 1 else '%s/%d'%(tag, i)
                    result[mode][tag] = {
                        'displayName': tag,
S
superjom 已提交
57 58 59 60 61 62 63 64 65
                        'description': "",
                        'samples': 1,
                    }
    return result


def get_image_tag_steps(storage, mode, tag):
    # remove suffix '/x'
    res = re.search(r".*/([0-9]+$)", tag)
S
superjom 已提交
66
    sample_index = 0
S
superjom 已提交
67
    origin_tag = tag
S
superjom 已提交
68 69
    if res:
        tag = tag[:tag.rfind('/')]
S
superjom 已提交
70
        sample_index = int(res.groups()[0])
S
superjom 已提交
71 72 73 74

    reader = storage.as_mode(mode)
    image = reader.image(tag)
    res = []
S
superjom 已提交
75

S
superjom 已提交
76 77
    for step_index in range(image.num_records()):
        record = image.record(step_index, sample_index)
S
superjom 已提交
78 79 80
        shape = record.shape()
        query = urllib.urlencode({
            'sample': 0,
S
superjom 已提交
81
            'index': step_index,
S
superjom 已提交
82
            'tag': origin_tag,
S
superjom 已提交
83 84
            'run': mode,
        })
S
superjom 已提交
85
        res.append({
S
superjom 已提交
86 87 88 89 90
            'height': shape[0],
            'width': shape[1],
            'step': record.step_id(),
            'wall_time': image.timestamp(step_index),
            'query': query,
S
superjom 已提交
91 92 93 94
        })
    return res


S
superjom 已提交
95
def get_invididual_image(storage, mode, tag, step_index):
S
superjom 已提交
96 97 98 99 100 101 102 103
    reader = storage.as_mode(mode)
    res = re.search(r".*/([0-9]+$)", tag)
    # remove suffix '/x'
    if res:
        offset = int(res.groups()[0])
        tag = tag[:tag.rfind('/')]

    image = reader.image(tag)
S
superjom 已提交
104 105 106
    record = image.record(step_index, offset)

    data = np.array(record.data(), dtype='uint8').reshape(record.shape())
S
superjom 已提交
107 108 109 110 111 112 113 114 115
    tempfile = NamedTemporaryFile(mode='w+b', suffix='.png')
    with Image.fromarray(data) as im:
        im.save(tempfile)
    tempfile.seek(0, 0)
    return tempfile


if __name__ == '__main__':
    reader = storage.StorageReader('./tmp/mock')
S
superjom 已提交
116
    tags = get_image_tags(reader)
S
superjom 已提交
117 118

    tags = get_image_tag_steps(reader, 'train', 'layer1/layer2/image0/0')
S
superjom 已提交
119
    pprint.pprint(tags)
S
superjom 已提交
120 121 122

    image = get_invididual_image(reader, "train", 'layer1/layer2/image0/0', 2)
    print image