|
| 1 | +# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import cv2 |
| 17 | +import unittest |
| 18 | +import numpy as np |
| 19 | + |
| 20 | +import paddle |
| 21 | +import paddle.fluid as fluid |
| 22 | +import paddle.fluid.core as core |
| 23 | +from paddle.utils.download import get_path_from_url |
| 24 | +from paddle.vision.datasets import DatasetFolder |
| 25 | +from paddle.vision.ops import image_decode_random_crop, image_resize, \ |
| 26 | + random_flip, mirror_normalize |
| 27 | +from paddle.vision.reader import file_label_reader |
| 28 | + |
| 29 | +import test_data_pipeline_static |
| 30 | +from test_data_pipeline_static import DATASET_HOME, DATASET_URL, \ |
| 31 | + DATASET_MD5, IMAGE_NUM |
| 32 | + |
| 33 | +DATASET_HOME = os.path.expanduser("~/.cache/paddle/datasets") |
| 34 | +DATASET_URL = "https://paddlemodels.cdn.bcebos.com/ImageNet_stub.tar" |
| 35 | +DATASET_MD5 = "c7110519124a433901cf005a4a91b607" |
| 36 | +IMAGE_NUM = 100 |
| 37 | + |
| 38 | + |
| 39 | +class TestDataPipelineDynamicCase1( |
| 40 | + test_data_pipeline_static.TestDataPipelineStaticCase1): |
| 41 | + def test_output(self): |
| 42 | + # NOTE: only supoort CUDA kernel currently |
| 43 | + if not core.is_compiled_with_cuda(): |
| 44 | + return |
| 45 | + |
| 46 | + data = self.reader() |
| 47 | + |
| 48 | + image = data['image'].numpy() |
| 49 | + assert image.shape[0] == self.batch_size |
| 50 | + assert image.shape[1] == 3 |
| 51 | + assert image.shape[2] == self.target_size |
| 52 | + assert image.shape[3] == self.target_size |
| 53 | + assert image.dtype == np.float32 |
| 54 | + |
| 55 | + restore_image = image * self.std_np + self.mean_np |
| 56 | + assert np.all(restore_image > -1.) |
| 57 | + assert np.all(restore_image < 256.) |
| 58 | + |
| 59 | + label = data['label'].numpy() |
| 60 | + assert label.shape[0] == self.batch_size |
| 61 | + assert label.dtype == np.int64 |
| 62 | + assert np.all(label >= 0) |
| 63 | + assert np.all(label <= 1) |
| 64 | + |
| 65 | + |
| 66 | +class TestDataPipelineDynamicCase2(TestDataPipelineDynamicCase1): |
| 67 | + def setUp(self): |
| 68 | + self.data_root = get_path_from_url(DATASET_URL, DATASET_HOME, |
| 69 | + DATASET_MD5) |
| 70 | + |
| 71 | + self.num_epoches = 1 |
| 72 | + self.batch_size = 16 |
| 73 | + self.num_threads = 4 |
| 74 | + self.host_memory_padding = 0 |
| 75 | + self.device_memory_padding = 0 |
| 76 | + |
| 77 | + self.shuffle = True |
| 78 | + self.drop_last = True |
| 79 | + self.calc_iter_info() |
| 80 | + |
| 81 | + self.target_size = 128 |
| 82 | + self.flip_prob = 0.5 |
| 83 | + self.mean = [123.675, 116.28, 103.53] |
| 84 | + self.std = [58.395, 57.120, 57.375] |
| 85 | + |
| 86 | + self.mean_np = np.array(self.mean).reshape([1, 3, 1, 1]) |
| 87 | + self.std_np = np.array(self.std).reshape([1, 3, 1, 1]) |
| 88 | + |
| 89 | + self.build_reader() |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + unittest.main() |
0 commit comments