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
        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())):
S
superjom 已提交
54 55 56
                    caption = tag if image.num_samples() <= 1 else '%s/%d'%(tag, i)
                    result[mode][caption] = {
                        'displayName': caption,
S
superjom 已提交
57 58 59 60 61 62 63
                        'description': "",
                        'samples': 1,
                    }
    return result


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

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

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


S
superjom 已提交
97
def get_invididual_image(storage, mode, tag, step_index):
S
superjom 已提交
98 99 100 101 102 103 104 105
    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 已提交
106 107 108
    record = image.record(step_index, offset)

    data = np.array(record.data(), dtype='uint8').reshape(record.shape())
S
superjom 已提交
109 110 111 112 113 114 115 116 117
    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 已提交
118
    tags = get_image_tags(reader)
S
superjom 已提交
119 120

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

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