Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add scratch demo #150

Merged
merged 15 commits into from
Jan 15, 2018
Merged
4 changes: 2 additions & 2 deletions demo/mxnet/mxnet_demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mxnet as mx
import logging

import mxnet as mx
# Here we import LogWriter so that we can write log data while MXNet is training
from visualdl import LogWriter

Expand Down Expand Up @@ -45,7 +46,6 @@ def _callback(param):
# Start to build CNN in MXNet, train MNIST dataset. For more info, check MXNet's official website:
# https://mxnet.incubator.apache.org/tutorials/python/mnist.html

import logging
logging.getLogger().setLevel(logging.DEBUG) # logging to stdout

train_iter = mx.io.NDArrayIter(mnist['train_data'], mnist['train_label'], batch_size, shuffle=True)
Expand Down
7 changes: 4 additions & 3 deletions demo/paddle/cifar10_image_classification_vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import sys

import numpy as np
from visualdl import LogWriter

import paddle.v2 as paddle
import paddle.v2.fluid as fluid
import paddle.v2.fluid.framework as framework
from paddle.v2.fluid.param_attr import ParamAttr
from paddle.v2.fluid.initializer import NormalInitializer
from visualdl import LogWriter
import numpy as np
from paddle.v2.fluid.param_attr import ParamAttr

logdir = "./tmp"
logwriter = LogWriter(logdir, sync_cycle=10)
Expand Down
34 changes: 22 additions & 12 deletions demo/vdl_scratch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/user/bin/env python
import math
import os
from visualdl import LogWriter, ROOT
import random
import subprocess
from scipy.stats import norm

import numpy as np
import random
from PIL import Image
from scipy.stats import norm
from visualdl import ROOT, LogWriter

logdir = './scratch_log'

Expand All @@ -19,22 +21,29 @@
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)
last_record0 = 0.
last_record1 = 0.
for step in range(1, 100):
last_record0 += 0.1 * (random.random() - 0.3)
last_record1 += 0.1 * (random.random() - 0.7)
scalar0.add_record(step, last_record0)
scalar1.add_record(step, last_record1)

# create histogram
with logw.mode('train') as logger:
histogram = logger.histogram("scratch/histogram", num_buckets=100)
for step in range(100):
histogram = logger.histogram("scratch/histogram", num_buckets=200)
for step in range(1, 100):
histogram.add_record(step,
np.random.normal(0.1 + step * 0.01, size=1000))

np.random.normal(
0.1 + step * 0.001,
200. / (100 + step),
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'))
dog_jpg = dog_jpg.resize(np.array(dog_jpg.size) / 2)
shape = [dog_jpg.size[1], dog_jpg.size[0], 3]

for pass_ in xrange(4):
Expand All @@ -47,12 +56,13 @@
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
# a more efficient way to sample images
idx = image.is_sample_taken() # check whether this image will be taken by reservoir sampling
if idx >= 0:
data = np.array(
dog_jpg.crop((left_x, left_y, right_x,
right_y))).flatten()
# add this image to log
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.
Expand Down