|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- # Copyright 2021 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.
- # ==============================================================================
- """
- Test LSUN dataset operators
- """
- import pytest
-
- import mindspore.dataset as ds
- import mindspore.dataset.vision.c_transforms as vision
- from mindspore import log as logger
-
- DATA_DIR = "../data/dataset/testLSUN"
-
-
- def test_lsun_basic():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case basic")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 4
-
-
- def test_lsun_num_samples():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case num_samples")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, num_samples=10, num_parallel_workers=2)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 4
-
- random_sampler = ds.RandomSampler(num_samples=3, replacement=True)
- data1 = ds.LSUNDataset(DATA_DIR, num_parallel_workers=2, sampler=random_sampler)
-
- num_iter = 0
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- num_iter += 1
-
- assert num_iter == 3
-
- random_sampler = ds.RandomSampler(num_samples=3, replacement=False)
- data1 = ds.LSUNDataset(DATA_DIR, num_parallel_workers=2, sampler=random_sampler)
-
- num_iter = 0
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- num_iter += 1
-
- assert num_iter == 3
-
-
- def test_lsun_num_shards():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case numShards")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, num_shards=2, shard_id=1)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 2
-
-
- def test_lsun_shard_id():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case withShardID")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, num_shards=2, shard_id=0)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 2
-
-
- def test_lsun_no_shuffle():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case noShuffle")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, shuffle=False)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 4
-
-
- def test_lsun_extra_shuffle():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case extra_shuffle")
- # define parameters
- repeat_count = 2
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, shuffle=True)
- data1 = data1.shuffle(buffer_size=5)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 8
-
-
- def test_lsun_decode():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case decode")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, decode=True)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 4
-
-
- def test_sequential_sampler():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case SequentialSampler")
- # define parameters
- repeat_count = 1
- # apply dataset operations
- sampler = ds.SequentialSampler(num_samples=10)
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", sampler=sampler)
- data1 = data1.repeat(repeat_count)
- result = []
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- result.append(item["label"])
- num_iter += 1
-
- assert num_iter == 2
- logger.info("Result: {}".format(result))
-
-
- def test_random_sampler():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case RandomSampler")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- sampler = ds.RandomSampler()
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", sampler=sampler)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 2
-
-
- def test_distributed_sampler():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case DistributedSampler")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- sampler = ds.DistributedSampler(2, 1)
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", sampler=sampler)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 1
-
-
- def test_pk_sampler():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case PKSampler")
- # define parameters
- repeat_count = 1
-
- # apply dataset operations
- sampler = ds.PKSampler(1)
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", sampler=sampler)
- data1 = data1.repeat(repeat_count)
-
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 2
-
-
- def test_chained_sampler():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case Chained Sampler - Random and Sequential, with repeat")
-
- # Create chained sampler, random and sequential
- sampler = ds.RandomSampler()
- child_sampler = ds.SequentialSampler()
- sampler.add_child(child_sampler)
- # Create LSUNDataset with sampler
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", sampler=sampler)
-
- data1 = data1.repeat(count=3)
-
- # Verify dataset size
- data1_size = data1.get_dataset_size()
- logger.info("dataset size is: {}".format(data1_size))
- assert data1_size == 6
-
- # Verify number of iterations
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 6
-
-
- def test_lsun_test_dataset():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case usage")
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, usage="test", num_samples=8)
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 1
-
-
- def test_lsun_valid_dataset():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case usage")
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, usage="valid", num_samples=8)
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 2
-
-
- def test_lsun_train_dataset():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case usage")
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", num_samples=8)
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 2
-
-
- def test_lsun_all_dataset():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case usage")
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, usage="all", num_samples=8)
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 4
-
-
- def test_lsun_classes():
- """
- Feature: LSUN
- Description: test classes of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case usage")
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, usage="train", classes=["bedroom"], num_samples=8)
- num_iter = 0
- # each data is a dictionary
- for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 1
-
-
- def test_lsun_zip():
- """
- Feature: LSUN
- Description: test basic usage of LSUN
- Expectation: the dataset is as expected
- """
- logger.info("Test Case zip")
- # define parameters
- repeat_count = 2
-
- # apply dataset operations
- data1 = ds.LSUNDataset(DATA_DIR, num_samples=10)
- data2 = ds.LSUNDataset(DATA_DIR, num_samples=10)
-
- data1 = data1.repeat(repeat_count)
- # rename dataset2 for no conflict
- data2 = data2.rename(input_columns=["image", "label"], output_columns=["image1", "label1"])
- data3 = ds.zip((data1, data2))
-
- num_iter = 0
- # each data is a dictionary
- for item in data3.create_dict_iterator(num_epochs=1, output_numpy=True):
- # in this example, each dictionary has keys "image" and "label"
- logger.info("image is {}".format(item["image"]))
- logger.info("label is {}".format(item["label"]))
- num_iter += 1
-
- logger.info("Number of data in data1: {}".format(num_iter))
- assert num_iter == 4
-
-
- def test_lsun_exception():
- """
- Feature: LSUN
- Description: test error cases for LSUN
- Expectation: throw exception correctly
- """
- logger.info("Test lsun exception")
-
- error_msg_1 = "sampler and shuffle cannot be specified at the same time"
- with pytest.raises(RuntimeError, match=error_msg_1):
- ds.LSUNDataset(DATA_DIR, shuffle=False, sampler=ds.PKSampler(3))
-
- error_msg_2 = "sampler and sharding cannot be specified at the same time"
- with pytest.raises(RuntimeError, match=error_msg_2):
- ds.LSUNDataset(DATA_DIR, sampler=ds.PKSampler(3), num_shards=2, shard_id=0)
-
- error_msg_3 = "num_shards is specified and currently requires shard_id as well"
- with pytest.raises(RuntimeError, match=error_msg_3):
- ds.LSUNDataset(DATA_DIR, num_shards=10)
-
- error_msg_4 = "shard_id is specified but num_shards is not"
- with pytest.raises(RuntimeError, match=error_msg_4):
- ds.LSUNDataset(DATA_DIR, shard_id=0)
-
- error_msg_5 = "Input shard_id is not within the required interval"
- with pytest.raises(ValueError, match=error_msg_5):
- ds.LSUNDataset(DATA_DIR, num_shards=5, shard_id=-1)
- with pytest.raises(ValueError, match=error_msg_5):
- ds.LSUNDataset(DATA_DIR, num_shards=5, shard_id=5)
- with pytest.raises(ValueError, match=error_msg_5):
- ds.LSUNDataset(DATA_DIR, num_shards=2, shard_id=5)
-
- error_msg_6 = "num_parallel_workers exceeds"
- with pytest.raises(ValueError, match=error_msg_6):
- ds.LSUNDataset(DATA_DIR, shuffle=False, num_parallel_workers=0)
- with pytest.raises(ValueError, match=error_msg_6):
- ds.LSUNDataset(DATA_DIR, shuffle=False, num_parallel_workers=256)
- with pytest.raises(ValueError, match=error_msg_6):
- ds.LSUNDataset(DATA_DIR, shuffle=False, num_parallel_workers=-2)
-
- error_msg_7 = "Argument shard_id"
- with pytest.raises(TypeError, match=error_msg_7):
- ds.LSUNDataset(DATA_DIR, num_shards=2, shard_id="0")
-
-
-
- def test_lsun_exception_map():
- """
- Feature: LSUN
- Description: test error cases for LSUN
- Expectation: throw exception correctly
- """
- logger.info("Test lsun exception map")
- def exception_func(item):
- raise Exception("Error occur!")
-
- def exception_func2(image, label):
- raise Exception("Error occur!")
-
- try:
- data = ds.LSUNDataset(DATA_DIR)
- data = data.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
- for _ in data.__iter__():
- pass
- assert False
- except RuntimeError as e:
- assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
-
- try:
- data = ds.LSUNDataset(DATA_DIR)
- data = data.map(operations=exception_func2,
- input_columns=["image", "label"],
- output_columns=["image", "label", "label1"],
- column_order=["image", "label", "label1"],
- num_parallel_workers=1)
- for _ in data.__iter__():
- pass
- assert False
- except RuntimeError as e:
- assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
-
- try:
- data = ds.LSUNDataset(DATA_DIR)
- data = data.map(operations=vision.Decode(), input_columns=["image"], num_parallel_workers=1)
- data = data.map(operations=exception_func, input_columns=["image"], num_parallel_workers=1)
- for _ in data.__iter__():
- pass
- assert False
- except RuntimeError as e:
- assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
-
-
- if __name__ == '__main__':
- test_lsun_basic()
- test_lsun_num_samples()
- test_sequential_sampler()
- test_random_sampler()
- test_distributed_sampler()
- test_pk_sampler()
- test_lsun_num_shards()
- test_lsun_shard_id()
- test_lsun_no_shuffle()
- test_lsun_extra_shuffle()
- test_lsun_decode()
- test_lsun_test_dataset()
- test_lsun_valid_dataset()
- test_lsun_train_dataset()
- test_lsun_all_dataset()
- test_lsun_classes()
- test_lsun_zip()
- test_lsun_exception()
- test_lsun_exception_map()
|