test_reader_reset.py 3.4 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

J
Jiabin Yang 已提交
15
import os
16

17
os.environ['CPU_NUM'] = str(1)
18 19 20 21 22
import unittest

import numpy as np

import paddle
F
fengjiayi 已提交
23
import paddle.fluid as fluid
24
from paddle.fluid import compiler
F
fengjiayi 已提交
25 26 27 28 29


class TestReaderReset(unittest.TestCase):
    def prepare_data(self):
        def fake_data_generator():
30
            for n in range(self.total_ins_num):
F
fengjiayi 已提交
31 32
                yield np.ones(self.ins_shape) * n, n

33
        return fake_data_generator
F
fengjiayi 已提交
34 35 36 37 38

    def setUp(self):
        self.use_cuda = fluid.core.is_compiled_with_cuda()
        self.ins_shape = [3]
        self.batch_size = 5
39 40
        self.batch_num = 20
        self.total_ins_num = self.batch_size * self.batch_num
F
fengjiayi 已提交
41 42 43 44 45 46 47 48
        self.test_pass_num = 100
        self.prepare_data()

    def main(self, with_double_buffer):
        main_prog = fluid.Program()
        startup_prog = fluid.Program()

        with fluid.program_guard(main_prog, startup_prog):
G
GGBond8488 已提交
49 50 51 52 53
            image = paddle.static.data(
                name='image', shape=[-1] + self.ins_shape, dtype='float32'
            )
            label = paddle.static.data(
                name='label', shape=[-1, 1], dtype='int64'
54
            )
55 56 57 58
            data_reader_handle = fluid.io.PyReader(
                feed_list=[image, label],
                capacity=16,
                iterable=False,
59 60
                use_double_buffer=with_double_buffer,
            )
F
fengjiayi 已提交
61 62 63 64 65 66
            fetch_list = [image.name, label.name]

        place = fluid.CUDAPlace(0) if self.use_cuda else fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(startup_prog)

67
        data_reader_handle.decorate_sample_list_generator(
68 69
            paddle.batch(self.prepare_data(), batch_size=self.batch_size)
        )
70

71
        train_cp = compiler.CompiledProgram(main_prog).with_data_parallel(
72 73
            places=[place]
        )
74 75

        batch_id = 0
F
fengjiayi 已提交
76
        pass_count = 0
77 78
        while pass_count < self.test_pass_num:
            data_reader_handle.start()
F
fengjiayi 已提交
79
            try:
80
                while True:
81 82 83
                    data_val, label_val = exe.run(
                        train_cp, fetch_list=fetch_list, return_numpy=True
                    )
84
                    ins_num = data_val.shape[0]
85 86 87
                    broadcasted_label = np.ones(
                        (ins_num,) + tuple(self.ins_shape)
                    ) * label_val.reshape((ins_num, 1))
88 89
                    self.assertEqual(data_val.all(), broadcasted_label.all())
                    batch_id += 1
F
fengjiayi 已提交
90
            except fluid.core.EOFException:
91
                data_reader_handle.reset()
F
fengjiayi 已提交
92
                pass_count += 1
93 94 95
                self.assertEqual(pass_count * self.batch_num, batch_id)

        self.assertEqual(pass_count, self.test_pass_num)
F
fengjiayi 已提交
96 97 98 99 100 101 102 103

    def test_all(self):
        self.main(with_double_buffer=False)
        self.main(with_double_buffer=True)


if __name__ == '__main__':
    unittest.main()