|
1 | 1 | #!/user/bin/env python |
| 2 | +import math |
2 | 3 | import os |
3 | | -from visualdl import LogWriter, ROOT |
| 4 | +import random |
4 | 5 | import subprocess |
5 | | -from scipy.stats import norm |
| 6 | + |
6 | 7 | import numpy as np |
7 | | -import random |
8 | 8 | from PIL import Image |
| 9 | +from scipy.stats import norm |
| 10 | +from visualdl import ROOT, LogWriter |
9 | 11 |
|
10 | 12 | logdir = './scratch_log' |
11 | 13 |
|
|
19 | 21 | scalar1 = logger.scalar("scratch/scalar") |
20 | 22 |
|
21 | 23 | # 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) |
25 | 31 |
|
26 | 32 | # create histogram |
27 | 33 | 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): |
30 | 36 | 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)) |
33 | 41 | # create image |
34 | 42 | with logw.mode("train") as logger: |
35 | 43 | image = logger.image("scratch/dog", 4, |
36 | 44 | 1) # randomly sample 4 images one pass |
37 | 45 | dog_jpg = Image.open(os.path.join(ROOT, 'python/dog.jpg')) |
| 46 | + dog_jpg = dog_jpg.resize(np.array(dog_jpg.size) / 2) |
38 | 47 | shape = [dog_jpg.size[1], dog_jpg.size[0], 3] |
39 | 48 |
|
40 | 49 | for pass_ in xrange(4): |
|
47 | 56 | right_x = left_x + target_shape[1] |
48 | 57 | right_y = left_y + target_shape[0] |
49 | 58 |
|
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 |
52 | 61 | if idx >= 0: |
53 | 62 | data = np.array( |
54 | 63 | dog_jpg.crop((left_x, left_y, right_x, |
55 | 64 | right_y))).flatten() |
| 65 | + # add this image to log |
56 | 66 | image.set_sample(idx, target_shape, data) |
57 | 67 | # you can also just write followig codes, it is more clear, but need to |
58 | 68 | # process image even if it will not be sampled. |
|
0 commit comments