Skip to content

Commit c678924

Browse files
authored
Feature/add scratch demo (#150)
1 parent ec572c0 commit c678924

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

demo/mxnet/mxnet_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import mxnet as mx
1+
import logging
22

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

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

48-
import logging
4949
logging.getLogger().setLevel(logging.DEBUG) # logging to stdout
5050

5151
train_iter = mx.io.NDArrayIter(mnist['train_data'], mnist['train_label'], batch_size, shuffle=True)

demo/paddle/cifar10_image_classification_vgg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import sys
44

5+
import numpy as np
6+
from visualdl import LogWriter
7+
58
import paddle.v2 as paddle
69
import paddle.v2.fluid as fluid
710
import paddle.v2.fluid.framework as framework
8-
from paddle.v2.fluid.param_attr import ParamAttr
911
from paddle.v2.fluid.initializer import NormalInitializer
10-
from visualdl import LogWriter
11-
import numpy as np
12+
from paddle.v2.fluid.param_attr import ParamAttr
1213

1314
logdir = "./tmp"
1415
logwriter = LogWriter(logdir, sync_cycle=10)

demo/vdl_scratch.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/user/bin/env python
2+
import math
23
import os
3-
from visualdl import LogWriter, ROOT
4+
import random
45
import subprocess
5-
from scipy.stats import norm
6+
67
import numpy as np
7-
import random
88
from PIL import Image
9+
from scipy.stats import norm
10+
from visualdl import ROOT, LogWriter
911

1012
logdir = './scratch_log'
1113

@@ -19,22 +21,29 @@
1921
scalar1 = logger.scalar("scratch/scalar")
2022

2123
# add scalar records.
22-
for step in range(200):
23-
scalar0.add_record(step, step * 1. / 200)
24-
scalar1.add_record(step, 1. - step * 1. / 200)
24+
last_record0 = 0.
25+
last_record1 = 0.
26+
for step in range(1, 100):
27+
last_record0 += 0.1 * (random.random() - 0.3)
28+
last_record1 += 0.1 * (random.random() - 0.7)
29+
scalar0.add_record(step, last_record0)
30+
scalar1.add_record(step, last_record1)
2531

2632
# create histogram
2733
with logw.mode('train') as logger:
28-
histogram = logger.histogram("scratch/histogram", num_buckets=100)
29-
for step in range(100):
34+
histogram = logger.histogram("scratch/histogram", num_buckets=200)
35+
for step in range(1, 100):
3036
histogram.add_record(step,
31-
np.random.normal(0.1 + step * 0.01, size=1000))
32-
37+
np.random.normal(
38+
0.1 + step * 0.001,
39+
200. / (100 + step),
40+
size=1000))
3341
# create image
3442
with logw.mode("train") as logger:
3543
image = logger.image("scratch/dog", 4,
3644
1) # randomly sample 4 images one pass
3745
dog_jpg = Image.open(os.path.join(ROOT, 'python/dog.jpg'))
46+
dog_jpg = dog_jpg.resize(np.array(dog_jpg.size) / 2)
3847
shape = [dog_jpg.size[1], dog_jpg.size[0], 3]
3948

4049
for pass_ in xrange(4):
@@ -47,12 +56,13 @@
4756
right_x = left_x + target_shape[1]
4857
right_y = left_y + target_shape[0]
4958

50-
idx = image.is_sample_taken()
51-
# a more efficient way to sample images is
59+
# a more efficient way to sample images
60+
idx = image.is_sample_taken() # check whether this image will be taken by reservoir sampling
5261
if idx >= 0:
5362
data = np.array(
5463
dog_jpg.crop((left_x, left_y, right_x,
5564
right_y))).flatten()
65+
# add this image to log
5666
image.set_sample(idx, target_shape, data)
5767
# you can also just write followig codes, it is more clear, but need to
5868
# process image even if it will not be sampled.

0 commit comments

Comments
 (0)