From 7b3e837bcf64a3b01b8fb5365457724d8bf6f454 Mon Sep 17 00:00:00 2001 From: Yan Chunwei Date: Mon, 15 Jan 2018 14:53:27 +0800 Subject: [PATCH] Feature/add scratch demo (#140) --- README.md | 7 ++-- demo/vdl_scratch.py | 64 +++++++++++++++++++++++++++++++++ setup.py | 6 ++-- visualdl/python/test_storage.py | 4 +-- visualdl/server/lib.py | 2 +- visualdl/server/visualDL | 1 + 6 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 demo/vdl_scratch.py diff --git a/README.md b/README.md index 3eb2d5c2..e59a747e 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ python setup.py bdist_wheel pip install --upgrade dist/visualdl-0.0.1-py2-none-any.whl ``` - -### Step 3: run +### Run a demo from scratch ``` -visualDL --logdir= --port=8888 +vdl_scratch.py +visualDL --logdir=scratch_log ``` +that will start a server locally. diff --git a/demo/vdl_scratch.py b/demo/vdl_scratch.py new file mode 100644 index 00000000..c315e222 --- /dev/null +++ b/demo/vdl_scratch.py @@ -0,0 +1,64 @@ +#!/user/bin/env python +import os +from visualdl import LogWriter, ROOT +import subprocess +from scipy.stats import norm +import numpy as np +import random +from PIL import Image + +logdir = './scratch_log' + +logw = LogWriter(logdir, sync_cycle=30) + +# create scalars in mode train and test. +with logw.mode('train') as logger: + scalar0 = logger.scalar("scratch/scalar") + +with logw.mode('test') as logger: + scalar1 = logger.scalar("scratch/scalar") + +# add scalar records. +for step in range(200): + scalar0.add_record(step, step * 1. / 200) + scalar1.add_record(step, 1. - step * 1. / 200) + +# create histogram +with logw.mode('train') as logger: + histogram = logger.histogram("scratch/histogram", num_buckets=100) + for step in range(100): + histogram.add_record(step, + np.random.normal(0.1 + step * 0.01, size=1000)) + +# create image +with logw.mode("train") as logger: + image = logger.image("scratch/dog", 4, + 1) # randomly sample 4 images one pass + dog_jpg = Image.open(os.path.join(ROOT, 'python/dog.jpg')) + shape = [dog_jpg.size[1], dog_jpg.size[0], 3] + + for pass_ in xrange(4): + image.start_sampling() + for sample in xrange(10): + # randomly crop a demo image. + target_shape = [100, 100, 3] # width, height, channels(3 for RGB) + left_x = random.randint(0, shape[1] - target_shape[1]) + left_y = random.randint(0, shape[0] - target_shape[0]) + right_x = left_x + target_shape[1] + right_y = left_y + target_shape[0] + + idx = image.is_sample_taken() + # a more efficient way to sample images is + if idx >= 0: + data = np.array( + dog_jpg.crop((left_x, left_y, right_x, + right_y))).flatten() + image.set_sample(idx, target_shape, data) + # you can also just write followig codes, it is more clear, but need to + # process image even if it will not be sampled. + # data = np.array( + # dog_jpg.crop((left_x, left_y, right_x, + # right_y))).flatten() + # image.add_sample(shape, data) + + image.finish_sampling() diff --git a/setup.py b/setup.py index 2a456f20..599c5dca 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ def readlines(name): VERSION_NUMBER = read('VERSION_NUMBER') LICENSE = readlines('LICENSE')[0].strip() -install_requires = ['Flask', 'numpy', 'Pillow', 'protobuf'] +install_requires = ['Flask', 'numpy', 'Pillow', 'protobuf', 'scipy'] execute_requires = ['npm', 'node', 'bash'] @@ -94,7 +94,7 @@ setup( install_requires=install_requires, package_data={'visualdl.server': datas, 'visualdl':['core.so'], - 'visualdl.python':['core.so']}, + 'visualdl.python':['core.so', 'dog.jpg']}, packages=packages, - scripts=['visualdl/server/visualDL'], + scripts=['visualdl/server/visualDL', 'demo/vdl_scratch.py'], cmdclass=cmdclass) diff --git a/visualdl/python/test_storage.py b/visualdl/python/test_storage.py index 7c82fbbd..8277e30b 100644 --- a/visualdl/python/test_storage.py +++ b/visualdl/python/test_storage.py @@ -81,9 +81,7 @@ class StorageTest(unittest.TestCase): with self.reader.mode("train") as reader: image_writer.start_sampling() - index = image_writer.is_sample_taken() - image_writer.set_sample(index, shape, list(origin_data)) - image_writer.finish_sampling() + image_writer.add_sample(shape, list(origin_data)) # read and check whether the original image will be displayed image_reader = reader.image(tag) diff --git a/visualdl/server/lib.py b/visualdl/server/lib.py index c801e1fe..d41cc4dd 100644 --- a/visualdl/server/lib.py +++ b/visualdl/server/lib.py @@ -150,7 +150,7 @@ def get_histogram_tags(storage): return get_tags(storage, 'histogram') -def get_histogram(storage, mode, tag, num_samples=200): +def get_histogram(storage, mode, tag, num_samples=100): with storage.mode(mode) as reader: histogram = reader.histogram(tag) res = [] diff --git a/visualdl/server/visualDL b/visualdl/server/visualDL index a3537764..a7fb9a9e 100644 --- a/visualdl/server/visualDL +++ b/visualdl/server/visualDL @@ -100,6 +100,7 @@ def index(): @app.route('/static/') def serve_static(filename): + print 'serve static ', filename return send_from_directory( os.path.join(server_path, static_file_path), filename) -- GitLab