test_dataset.py 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#   Copyright (c) 2019 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.
import os
import time
import unittest
import sys
import logging
import random
import copy
21 22 23 24
# add python path of PadleDetection to sys.path
parent_path = os.path.abspath(os.path.join(__file__, *(['..'] * 4)))
if parent_path not in sys.path:
    sys.path.append(parent_path)
25

26
from ppdet.data.parallel_map import ParallelMap
27 28


29
class MemorySource(object):
30 31
    """ memory data source for testing
    """
32

33 34 35 36 37 38 39
    def __init__(self, samples):
        self._epoch = -1

        self._pos = -1
        self._drained = False
        self._samples = samples

40 41 42 43 44 45
    def __iter__(self):
        return self

    def __next__(self):
        return self.next()

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    def next(self):
        if self._epoch < 0:
            self.reset()

        if self._pos >= self.size():
            self._drained = True
            raise StopIteration("no more data in " + str(self))
        else:
            sample = copy.deepcopy(self._samples[self._pos])
            self._pos += 1
            return sample

    def reset(self):
        if self._epoch < 0:
            self._epoch = 0
        else:
            self._epoch += 1

        self._pos = 0
        self._drained = False
        random.shuffle(self._samples)

    def size(self):
        return len(self._samples)

    def drained(self):
        assert self._epoch >= 0, "the first epoch has not started yet"
        return self._pos >= self.size()

    def epoch_id(self):
        return self._epoch


class TestDataset(unittest.TestCase):
    """Test cases for ppdet.data.dataset
    """

    @classmethod
    def setUpClass(cls):
        """ setup
        """
        pass

    @classmethod
    def tearDownClass(cls):
        """ tearDownClass """
        pass

    def test_next(self):
        """ test next
        """
        samples = list(range(10))
        mem_sc = MemorySource(samples)

        for i, d in enumerate(mem_sc):
            self.assertTrue(d in samples)

    def test_transform_with_abnormal_worker(self):
        """ test dataset transform with abnormally exit process
        """
106 107
        samples = list(range(20))
        mem_sc = MemorySource(samples)
108

109
        def _worker(sample):
110 111 112 113 114
            if sample == 3:
                sys.exit(1)

            return 2 * sample

115
        test_worker = ParallelMap(
K
Kaipeng Deng 已提交
116
            mem_sc, _worker, worker_num=2, use_process=True, memsize='2M')
117 118

        ct = 0
119
        for i, d in enumerate(test_worker):
120 121 122 123 124 125 126 127
            ct += 1
            self.assertTrue(d / 2 in samples)

        self.assertEqual(len(samples) - 1, ct)

    def test_transform_with_delay_worker(self):
        """ test dataset transform with delayed process
        """
128 129
        samples = list(range(20))
        mem_sc = MemorySource(samples)
130

131
        def _worker(sample):
132 133 134 135 136
            if sample == 3:
                time.sleep(30)

            return 2 * sample

137
        test_worker = ParallelMap(
K
Kaipeng Deng 已提交
138
            mem_sc, _worker, worker_num=2, use_process=True, memsize='2M')
139 140

        ct = 0
141
        for i, d in enumerate(test_worker):
142 143 144 145 146 147 148 149 150
            ct += 1
            self.assertTrue(d / 2 in samples)

        self.assertEqual(len(samples), ct)


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