|
| 1 | +import pprint |
| 2 | +import re |
| 3 | +import urllib |
| 4 | +from tempfile import NamedTemporaryFile |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +from PIL import Image |
| 8 | + |
| 9 | +import storage |
| 10 | + |
| 11 | + |
| 12 | +def get_modes(storage): |
| 13 | + return storage.modes() |
| 14 | + |
| 15 | + |
| 16 | +def get_scalar_tags(storage, mode): |
| 17 | + result = {} |
| 18 | + for mode in storage.modes(): |
| 19 | + reader = storage.as_mode(mode) |
| 20 | + tags = reader.tags('scalar') |
| 21 | + if tags: |
| 22 | + result[mode] = {} |
| 23 | + for tag in tags: |
| 24 | + result[mode][tag] = { |
| 25 | + 'displayName': reader.scalar(tag).caption(), |
| 26 | + 'description': "", |
| 27 | + } |
| 28 | + return result |
| 29 | + |
| 30 | + |
| 31 | +def get_scalar(storage, mode, tag): |
| 32 | + reader = storage.as_mode(mode) |
| 33 | + scalar = reader.scalar(tag) |
| 34 | + |
| 35 | + records = scalar.records() |
| 36 | + ids = scalar.ids() |
| 37 | + timestamps = scalar.timestamps() |
| 38 | + |
| 39 | + result = zip(timestamps, ids, records) |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def get_image_tags(storage): |
| 44 | + result = {} |
| 45 | + |
| 46 | + for mode in storage.modes(): |
| 47 | + reader = storage.as_mode(mode) |
| 48 | + tags = reader.tags('image') |
| 49 | + if tags: |
| 50 | + result[mode] = {} |
| 51 | + for tag in tags: |
| 52 | + image = reader.image(tag) |
| 53 | + for i in xrange(max(1, image.num_samples())): |
| 54 | + caption = tag if image.num_samples() <= 1 else '%s/%d'%(tag, i) |
| 55 | + result[mode][caption] = { |
| 56 | + 'displayName': caption, |
| 57 | + 'description': "", |
| 58 | + 'samples': 1, |
| 59 | + } |
| 60 | + return result |
| 61 | + |
| 62 | + |
| 63 | +def get_image_tag_steps(storage, mode, tag): |
| 64 | + print 'image_tag_steps,mode,tag:', mode, tag |
| 65 | + # remove suffix '/x' |
| 66 | + res = re.search(r".*/([0-9]+$)", tag) |
| 67 | + sample_index = 0 |
| 68 | + origin_tag = tag |
| 69 | + if res: |
| 70 | + tag = tag[:tag.rfind('/')] |
| 71 | + sample_index = int(res.groups()[0]) |
| 72 | + |
| 73 | + reader = storage.as_mode(mode) |
| 74 | + image = reader.image(tag) |
| 75 | + res = [] |
| 76 | + |
| 77 | + for step_index in range(image.num_records()): |
| 78 | + record = image.record(step_index, sample_index) |
| 79 | + shape = record.shape() |
| 80 | + assert shape, "%s,%s" % (mode, tag) |
| 81 | + query = urllib.urlencode({ |
| 82 | + 'sample': 0, |
| 83 | + 'index': step_index, |
| 84 | + 'tag': origin_tag, |
| 85 | + 'run': mode, |
| 86 | + }) |
| 87 | + res.append({ |
| 88 | + 'height': shape[0], |
| 89 | + 'width': shape[1], |
| 90 | + 'step': record.step_id(), |
| 91 | + 'wall_time': image.timestamp(step_index), |
| 92 | + 'query': query, |
| 93 | + }) |
| 94 | + return res |
| 95 | + |
| 96 | + |
| 97 | +def get_invididual_image(storage, mode, tag, step_index): |
| 98 | + reader = storage.as_mode(mode) |
| 99 | + res = re.search(r".*/([0-9]+$)", tag) |
| 100 | + # remove suffix '/x' |
| 101 | + if res: |
| 102 | + offset = int(res.groups()[0]) |
| 103 | + tag = tag[:tag.rfind('/')] |
| 104 | + |
| 105 | + image = reader.image(tag) |
| 106 | + record = image.record(step_index, offset) |
| 107 | + |
| 108 | + data = np.array(record.data(), dtype='uint8').reshape(record.shape()) |
| 109 | + tempfile = NamedTemporaryFile(mode='w+b', suffix='.png') |
| 110 | + with Image.fromarray(data) as im: |
| 111 | + im.save(tempfile) |
| 112 | + tempfile.seek(0, 0) |
| 113 | + return tempfile |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + reader = storage.StorageReader('./tmp/mock') |
| 118 | + tags = get_image_tags(reader) |
| 119 | + |
| 120 | + tags = get_image_tag_steps(reader, 'train', 'layer1/layer2/image0/0') |
| 121 | + pprint.pprint(tags) |
| 122 | + |
| 123 | + image = get_invididual_image(reader, "train", 'layer1/layer2/image0/0', 2) |
| 124 | + print image |
0 commit comments