test_storage.py 4.3 KB
Newer Older
Q
Qiao Longfei 已提交
1 2
import pprint
import sys
S
superjom 已提交
3 4 5
import unittest

import numpy as np
S
superjom 已提交
6
from PIL import Image
7
from visualdl import LogReader, LogWriter
S
superjom 已提交
8

Y
Yan Chunwei 已提交
9 10
pprint.pprint(sys.path)

S
superjom 已提交
11

S
superjom 已提交
12
class StorageTest(unittest.TestCase):
S
superjom 已提交
13
    def setUp(self):
S
superjom 已提交
14
        self.dir = "./tmp/storage_test"
Y
Yan Chunwei 已提交
15
        self.writer = LogWriter(self.dir, sync_cycle=1).as_mode("train")
S
superjom 已提交
16 17 18

    def test_scalar(self):
        print 'test write'
S
superjom 已提交
19
        scalar = self.writer.scalar("model/scalar/min")
S
superjom 已提交
20
        # scalar.set_caption("model/scalar/min")
S
superjom 已提交
21
        for i in range(10):
S
superjom 已提交
22
            scalar.add_record(i, float(i))
S
superjom 已提交
23

S
superjom 已提交
24
        print 'test read'
Y
Yan Chunwei 已提交
25
        self.reader = LogReader(self.dir)
S
superjom 已提交
26 27 28 29 30
        with self.reader.mode("train") as reader:
            scalar = reader.scalar("model/scalar/min")
            self.assertEqual(scalar.caption(), "train")
            records = scalar.records()
            ids = scalar.ids()
Y
Yan Chunwei 已提交
31 32
            self.assertTrue(
                np.equal(records, [float(i) for i in range(10)]).all())
S
superjom 已提交
33 34 35
            self.assertTrue(np.equal(ids, [float(i) for i in range(10)]).all())
            print 'records', records
            print 'ids', ids
S
superjom 已提交
36

S
superjom 已提交
37 38
    def test_image(self):
        tag = "layer1/layer2/image0"
S
superjom 已提交
39
        image_writer = self.writer.image(tag, 10, 1)
S
superjom 已提交
40 41
        num_passes = 10
        num_samples = 100
42
        shape = [10, 10, 3]
S
superjom 已提交
43 44 45 46

        for pass_ in xrange(num_passes):
            image_writer.start_sampling()
            for ins in xrange(num_samples):
Y
Yan Chunwei 已提交
47 48 49
                data = np.random.random(shape) * 256
                data = np.ndarray.flatten(data)
                image_writer.add_sample(shape, list(data))
S
superjom 已提交
50 51
            image_writer.finish_sampling()

Y
Yan Chunwei 已提交
52
        self.reader = LogReader(self.dir)
S
superjom 已提交
53 54 55 56
        with self.reader.mode("train") as reader:
            image_reader = reader.image(tag)
            self.assertEqual(image_reader.caption(), tag)
            self.assertEqual(image_reader.num_records(), num_passes)
S
superjom 已提交
57

S
superjom 已提交
58 59 60 61
            image_record = image_reader.record(0, 1)
            self.assertTrue(np.equal(image_record.shape(), shape).all())
            data = image_record.data()
            self.assertEqual(len(data), np.prod(shape))
S
superjom 已提交
62

S
superjom 已提交
63 64 65
            image_tags = reader.tags("image")
            self.assertTrue(image_tags)
            self.assertEqual(len(image_tags), 1)
S
superjom 已提交
66

S
superjom 已提交
67 68 69 70 71 72 73 74 75 76 77 78
    def test_check_image(self):
        '''
        check whether the storage will keep image data consistent
        '''
        print 'check image'
        tag = "layer1/check/image1"
        image_writer = self.writer.image(tag, 10, 1)

        image = Image.open("./dog.jpg")
        shape = [image.size[1], image.size[0], 3]
        origin_data = np.array(image.getdata()).flatten()

Y
Yan Chunwei 已提交
79
        self.reader = LogReader(self.dir)
S
superjom 已提交
80
        with self.reader.mode("train") as reader:
S
superjom 已提交
81

S
superjom 已提交
82
            image_writer.start_sampling()
Y
Yan Chunwei 已提交
83
            image_writer.add_sample(shape, list(origin_data))
S
superjom 已提交
84

S
superjom 已提交
85 86 87 88 89
            # read and check whether the original image will be displayed
            image_reader = reader.image(tag)
            image_record = image_reader.record(0, 0)
            data = image_record.data()
            shape = image_record.shape()
S
superjom 已提交
90

S
superjom 已提交
91 92 93 94 95 96 97
            PIL_image_shape = (shape[0] * shape[1], shape[2])
            data = np.array(data, dtype='uint8').reshape(PIL_image_shape)
            print 'origin', origin_data.flatten()
            print 'data', data.flatten()
            image = Image.fromarray(data.reshape(shape))
            # manully check the image and found that nothing wrong with the image storage.
            # image.show()
S
superjom 已提交
98

S
superjom 已提交
99 100 101 102 103 104
    def test_with_syntax(self):
        with self.writer.mode("train") as writer:
            scalar = writer.scalar("model/scalar/average")
            for i in range(10):
                scalar.add_record(i, float(i))

Y
Yan Chunwei 已提交
105
        self.reader = LogReader(self.dir)
S
superjom 已提交
106 107 108 109
        with self.reader.mode("train") as reader:
            scalar = reader.scalar("model/scalar/average")
            self.assertEqual(scalar.caption(), "train")

S
superjom 已提交
110
    def test_modes(self):
Y
Yan Chunwei 已提交
111
        store = LogWriter(self.dir, sync_cycle=1)
S
superjom 已提交
112 113 114 115 116 117 118 119 120 121 122 123

        scalars = []

        for i in range(10):
            with store.mode("mode-%d" % i) as writer:
                scalar = writer.scalar("add/scalar0")
                scalars.append(scalar)

        for scalar in scalars[:-1]:
            for i in range(10):
                scalar.add_record(i, float(i))

S
superjom 已提交
124

S
superjom 已提交
125 126
if __name__ == '__main__':
    unittest.main()