vdl_scratch.py 2.6 KB
Newer Older
Y
Yan Chunwei 已提交
1
#!/user/bin/env python
Y
Yan Chunwei 已提交
2
import math
Y
Yan Chunwei 已提交
3
import os
Y
Yan Chunwei 已提交
4
import random
Y
Yan Chunwei 已提交
5
import subprocess
Y
Yan Chunwei 已提交
6

Y
Yan Chunwei 已提交
7 8
import numpy as np
from PIL import Image
Y
Yan Chunwei 已提交
9 10
from scipy.stats import norm
from visualdl import ROOT, LogWriter
Y
Yan Chunwei 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23

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.
Y
Yan Chunwei 已提交
24 25 26 27 28 29 30
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)
Y
Yan Chunwei 已提交
31 32 33

# create histogram
with logw.mode('train') as logger:
Y
Yan Chunwei 已提交
34 35
    histogram = logger.histogram("scratch/histogram", num_buckets=200)
    for step in range(1, 100):
Y
Yan Chunwei 已提交
36
        histogram.add_record(step,
Y
Yan Chunwei 已提交
37 38 39 40
                             np.random.normal(
                                 0.1 + step * 0.001,
                                 200. / (100 + step),
                                 size=1000))
Y
Yan Chunwei 已提交
41 42 43 44 45
# 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'))
Y
Yan Chunwei 已提交
46
    dog_jpg = dog_jpg.resize(np.array(dog_jpg.size) / 2)
Y
Yan Chunwei 已提交
47 48 49 50 51 52 53 54 55 56 57 58
    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]

Y
Yan Chunwei 已提交
59 60
            # a more efficient way to sample images
            idx = image.is_sample_taken() # check whether this image will be taken by reservoir sampling
Y
Yan Chunwei 已提交
61 62 63 64
            if idx >= 0:
                data = np.array(
                    dog_jpg.crop((left_x, left_y, right_x,
                                  right_y))).flatten()
Y
Yan Chunwei 已提交
65
                # add this image to log
Y
Yan Chunwei 已提交
66 67 68 69 70 71 72 73 74
                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()