未验证 提交 7b3e837b 编写于 作者: Y Yan Chunwei 提交者: GitHub

Feature/add scratch demo (#140)

上级 6f02954f
......@@ -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=<some log> --port=8888
vdl_scratch.py
visualDL --logdir=scratch_log
```
that will start a server locally.
#!/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()
......@@ -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)
......@@ -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)
......
......@@ -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 = []
......
......@@ -100,6 +100,7 @@ def index():
@app.route('/static/<path:filename>')
def serve_static(filename):
print 'serve static ', filename
return send_from_directory(
os.path.join(server_path, static_file_path), filename)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册