test_dataset.py 3.8 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
from ppdet.utils.check import enable_static_mode
28 29


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

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

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

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

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

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 106
    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
        """
107 108
        samples = list(range(20))
        mem_sc = MemorySource(samples)
109

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

            return 2 * sample

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

        ct = 0
120
        for i, d in enumerate(test_worker):
121 122 123 124 125 126 127 128
            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
        """
129 130
        samples = list(range(20))
        mem_sc = MemorySource(samples)
131

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

            return 2 * sample

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

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

        self.assertEqual(len(samples), ct)


if __name__ == '__main__':
150
    enable_static_mode()
151 152
    logging.basicConfig()
    unittest.main()