提交 926e65b5 编写于 作者: S superjom

add image server

上级 0a332d47
import numpy as np
import re
import storage
from PIL import Image
from tempfile import NamedTemporaryFile
def get_scalar_tags(storage, mode):
result = {}
print 'modes', storage.modes()
for mode in storage.modes():
result[mode] = {}
reader = storage.as_mode(mode)
for tag in reader.tags('scalar'):
result[mode][tag] = {
'displayName': reader.scalar(tag).caption(),
'description': "",
}
return result
def get_image_tags(storage, mode):
result = {}
print 'modes', storage.modes()
for mode in storage.modes():
reader = storage.as_mode(mode)
print 'tags', reader.tags('image')
result[mode] = {}
for tag in reader.tags('image'):
image = reader.image(tag)
if image.num_samples() == 1:
result[mode][tag] = {
'displayName': reader.scalar(tag).caption(),
'description': "",
'samples': 1,
}
else:
for i in xrange(image.num_samples()):
result[mode][tag + '/%d' % i] = {
'displayName': reader.scalar(tag).caption(),
'description': "",
'samples': 1,
}
return result
def get_image_tag_steps(storage, mode, tag):
# remove suffix '/x'
res = re.search(r".*/([0-9]+$)", tag)
if res:
tag = tag[:tag.rfind('/')]
reader = storage.as_mode(mode)
image = reader.image(tag)
# TODO(ChunweiYan) make max_steps a config
max_steps = 10
res = []
steps = []
if image.num_records() > max_steps:
span = int(image.num_records() / max_steps)
steps = [image.num_records() - i * span - 1 for i in xrange(max_steps)]
steps = [i for i in reversed(steps)]
steps[0] = max(steps[0], 0)
else:
steps = [i for i in xrange(image.num_records())]
for step in steps:
res.append({
'wall_time': image.timestamp(step),
'step': step,
})
return res
def get_invididual_image(storage, mode, tag, index):
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)
data = image.data(offset, index)
shape = image.shape(offset, index)
# print data
# print shape
data = np.array(data, dtype='uint8').reshape(shape)
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')
tags = get_image_tags(reader, 'train')
tags = get_image_tag_steps(reader, 'train', 'layer1/layer2/image0/0')
print 'image step tags', tags
image = get_invididual_image(reader, "train", 'layer1/layer2/image0/0', 2)
print image
#!/bin/bash
set -ex
export PYTHONPATH="/home/superjom/project/VisualDL/build/visualdl/logic:/home/superjom/project/VisualDL/visualdl/python"
python storage_mock.py
#!/bin/bash
set -ex
export PYTHONPATH="$(pwd)/..:/home/superjom/project/VisualDL/build/visualdl/logic:/home/superjom/project/VisualDL/visualdl/python"
python visual_dl.py --logdir ./tmp/mock --host 172.23.233.68
import storage
import numpy as np
import unittest
import random
import time
dir = "./tmp/mock"
writer = storage.StorageWriter(dir, sync_cycle=20)
train_writer = writer.as_mode("train")
test_writer = writer.as_mode("test")
train_scalar = train_writer.scalar("model/scalar/min")
test_scalar = test_writer.scalar("model/scalar/min")
train_scalar1 = train_writer.scalar("model/scalar/max")
test_scalar1 = test_writer.scalar("model/scalar/max")
for i in range(100):
train_scalar.add_record(i, random.random())
train_scalar1.add_record(i, random.random())
#time.sleep(1)
if i % 10 == 0:
test_scalar.add_record(i, random.random())
test_scalar1.add_record(i, random.random())
def add_image(mode):
writer_ = writer.as_mode(mode)
tag = "layer1/layer2/image0"
image_writer = writer_.image(tag, 10)
num_passes = 25
num_samples = 100
shape = [10, 10, 3]
for pass_ in xrange(num_passes):
image_writer.start_sampling()
for ins in xrange(num_samples):
index = image_writer.is_sample_taken()
if index != -1:
data = np.random.random(shape) * 256
data = np.ndarray.flatten(data)
image_writer.set_sample(index, shape, list(data))
image_writer.finish_sampling()
add_image("train")
add_image("test")
#!/bin/bash
set -ex
export PYTHONPATH="$(pwd)/..:/home/superjom/project/VisualDL/build/visualdl/logic:/home/superjom/project/VisualDL/visualdl/python"
python lib.py
...@@ -2,18 +2,20 @@ ...@@ -2,18 +2,20 @@
""" """
import json import json
import os import os
import re
import sys import sys
from optparse import OptionParser from optparse import OptionParser
from flask import Flask, redirect from flask import Flask, redirect
from flask import request from flask import request
from flask import send_from_directory from flask import send_from_directory, send_file
from flask import Response from flask import Response
from visualdl.log import logger from visualdl.log import logger
import visualdl.mock.data as mock_data import visualdl.mock.data as mock_data
import visualdl.mock.tags as mock_tags import visualdl.mock.tags as mock_tags
import storage import storage
import lib
app = Flask(__name__, static_url_path="") app = Flask(__name__, static_url_path="")
...@@ -87,7 +89,7 @@ def logdir(): ...@@ -87,7 +89,7 @@ def logdir():
@app.route('/data/runs') @app.route('/data/runs')
def runs(): def runs():
is_debug = bool(request.args.get('debug')) modes = storage.modes()
result = gen_result(0, "", ["train", "test"]) result = gen_result(0, "", ["train", "test"])
return Response(json.dumps(result), mimetype='application/json') return Response(json.dumps(result), mimetype='application/json')
...@@ -95,20 +97,18 @@ def runs(): ...@@ -95,20 +97,18 @@ def runs():
@app.route("/data/plugin/scalars/tags") @app.route("/data/plugin/scalars/tags")
def tags(): def tags():
is_debug = bool(request.args.get('debug')) is_debug = bool(request.args.get('debug'))
tag = request.args.get('tag')
if is_debug: if is_debug:
result = mock_tags.data() result = mock_tags.data()
else: else:
result = {} result = lib.get_scalar_tags(storage, mode)
print 'modes', storage.modes() print 'tags', result
for mode in storage.modes(): result = gen_result(0, "", result)
result[mode] = {} return Response(json.dumps(result), mimetype='application/json')
reader = storage.as_mode(mode)
for tag in reader.tags("scalar"):
result[mode][tag] = { @app.route("/data/plugin/images/tags")
'displayName': reader.scalar(tag).caption(), def tags():
'description': "" result = lib.get_image_tags(storage, mode)
}
print 'tags', result print 'tags', result
result = gen_result(0, "", result) result = gen_result(0, "", result)
return Response(json.dumps(result), mimetype='application/json') return Response(json.dumps(result), mimetype='application/json')
...@@ -135,6 +135,29 @@ def scalars(): ...@@ -135,6 +135,29 @@ def scalars():
return Response(json.dumps(result), mimetype='application/json') return Response(json.dumps(result), mimetype='application/json')
@app.route('/data/plugin/images/images')
def images():
run = request.args.get('run')
tag = request.args.get('tag')
res = lib.gen_image_tag_steps(storage, mode, tag)
return Response(json.dumps(result), mimetype='application/json')
@app.route('/data/plugin/images/individualImage')
def individual_image():
run = request.args.get('run')
tag = request.args.get('tag') # include a index
index = request.args.get('index') # index of step
offset = 0
imagefile = lib.get_invididual_image(storage, mode, tag)
response = send_file(
imagefile, as_attachment=True, attachment_filename='img.png')
return response
if __name__ == '__main__': if __name__ == '__main__':
logger.info(" port=" + str(options.port)) logger.info(" port=" + str(options.port))
app.run(debug=False, host=options.host, port=options.port) app.run(debug=False, host=options.host, port=options.port)
...@@ -91,6 +91,8 @@ PYBIND11_PLUGIN(core) { ...@@ -91,6 +91,8 @@ PYBIND11_PLUGIN(core) {
py::class_<cp::ImageReader>(m, "ImageReader") py::class_<cp::ImageReader>(m, "ImageReader")
.def("caption", &cp::ImageReader::caption) .def("caption", &cp::ImageReader::caption)
.def("num_records", &cp::ImageReader::num_records) .def("num_records", &cp::ImageReader::num_records)
.def("num_samples", &cp::ImageReader::num_samples)
.def("timestamp", &cp::ImageReader::timestamp)
.def("data", &cp::ImageReader::data) .def("data", &cp::ImageReader::data)
.def("shape", &cp::ImageReader::shape); .def("shape", &cp::ImageReader::shape);
......
...@@ -49,6 +49,10 @@ template class ScalarReader<double>; ...@@ -49,6 +49,10 @@ template class ScalarReader<double>;
void Image::StartSampling() { void Image::StartSampling() {
step_ = writer_.AddRecord(); step_ = writer_.AddRecord();
time_t time = std::time(nullptr);
step_.SetTimeStamp(time);
// resize record // resize record
for (int i = 0; i < num_samples_; i++) { for (int i = 0; i < num_samples_; i++) {
step_.AddData<value_t>(); step_.AddData<value_t>();
...@@ -110,6 +114,16 @@ void Image::SetSample(int index, ...@@ -110,6 +114,16 @@ void Image::SetSample(int index,
meta.SetMulti(shape); meta.SetMulti(shape);
} }
std::string ImageReader::caption() {
CHECK_EQ(reader_.captions().size(), 1);
auto caption = reader_.captions().front();
if (Reader::TagMatchMode(caption, mode_)) {
return Reader::GenReadableTag(mode_, caption);
}
string::TagDecode(caption);
return caption;
}
std::vector<ImageReader::value_t> ImageReader::data(int step, int index) { std::vector<ImageReader::value_t> ImageReader::data(int step, int index) {
auto record = reader_.record(step); auto record = reader_.record(step);
auto entry = record.data<value_t>(index); auto entry = record.data<value_t>(index);
......
...@@ -199,19 +199,15 @@ struct ImageReader { ...@@ -199,19 +199,15 @@ struct ImageReader {
ImageReader(const std::string& mode, TabletReader tablet) ImageReader(const std::string& mode, TabletReader tablet)
: reader_(tablet), mode_{mode} {} : reader_(tablet), mode_{mode} {}
std::string caption() { std::string caption();
CHECK_EQ(reader_.captions().size(), 1);
auto caption = reader_.captions().front();
if (Reader::TagMatchMode(caption, mode_)) {
return Reader::GenReadableTag(mode_, caption);
}
string::TagDecode(caption);
return caption;
}
// number of steps. // number of steps.
int num_records() { return reader_.total_records(); } int num_records() { return reader_.total_records(); }
int num_samples() { return reader_.num_samples(); }
int64_t timestamp(int step) { return reader_.record(step).timestamp(); }
std::vector<value_t> data(int step, int index); std::vector<value_t> data(int step, int index);
std::vector<shape_t> shape(int step, int index); std::vector<shape_t> shape(int step, int index);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册