test_flat_map.py 2.0 KB
Newer Older
J
jiangzhiwen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# Copyright 2020 Huawei Technologies Co., Ltd
#
# 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 numpy as np
import mindspore.dataset as ds

DATA_FILE = "../data/dataset/test_flat_map/images1.txt"
INDEX_FILE = "../data/dataset/test_flat_map/image_index.txt"


def test_flat_map_1():
    '''
    DATA_FILE records the path of image folders, load the images from them.
    '''
    import mindspore.dataset.transforms.nlp.utils as nlp

    def flat_map_func(x):
H
hesham 已提交
29
        data_dir = x[0].item().decode('utf8')
J
jiangzhiwen 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        d = ds.ImageFolderDatasetV2(data_dir)
        return d

    data = ds.TextFileDataset(DATA_FILE)
    data = data.flat_map(flat_map_func)

    count = 0
    for d in data:
        assert isinstance(d[0], np.ndarray)
        count += 1
    assert count == 52


def test_flat_map_2():
    '''
    Flatten 3D structure data
    '''
    import mindspore.dataset.transforms.nlp.utils as nlp

    def flat_map_func_1(x):
H
hesham 已提交
50
        data_dir = x[0].item().decode('utf8')
J
jiangzhiwen 已提交
51 52 53 54
        d = ds.ImageFolderDatasetV2(data_dir)
        return d

    def flat_map_func_2(x):
H
hesham 已提交
55
        text_file = x[0].item().decode('utf8')
J
jiangzhiwen 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        d = ds.TextFileDataset(text_file)
        d = d.flat_map(flat_map_func_1)
        return d

    data = ds.TextFileDataset(INDEX_FILE)
    data = data.flat_map(flat_map_func_2)

    count = 0
    for d in data:
        assert isinstance(d[0], np.ndarray)
        count += 1
    assert count == 104


if __name__ == "__main__":
    test_flat_map_1()
    test_flat_map_2()