__init__.py 3.9 KB
Newer Older
F
Felix 已提交
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
W
WuHaobo 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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.
F
Felix 已提交
14 15 16 17
import copy
import paddle
import numpy as np
from paddle.io import DistributedBatchSampler, BatchSampler, DataLoader
W
WuHaobo 已提交
18

F
Felix 已提交
19 20
from ppcls.utils import logger

21
from . import dataloader
F
Felix 已提交
22 23 24
from . import imaug
from . import samplers
# dataset
25 26 27 28
from .dataloader.imagenet_dataset import ImageNetDataset
from .dataloader.multilabel_dataset import MultiLabelDataset
from .dataloader.common_dataset import create_operators
from .dataloader.vehicle_dataset import CompCars, VeriWild
F
Felix 已提交
29

F
Felix 已提交
30 31 32 33 34
# sampler
from .samplers import DistributedRandomIdentitySampler

from .preprocess import transform

D
dongshuilong 已提交
35

F
Felix 已提交
36
def build_dataloader(config, mode, device, seed=None):
D
dongshuilong 已提交
37 38
    assert mode in ['Train', 'Eval', 'Test'
                    ], "Mode should be Train, Eval or Test."
F
Felix 已提交
39 40
    # build dataset
    config_dataset = config[mode]['dataset']
41
    config_dataset = copy.deepcopy(config_dataset)
F
Felix 已提交
42 43 44 45 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
    dataset_name = config_dataset.pop('name')
    if 'batch_transform_ops' in config_dataset:
        batch_transform = config_dataset.pop('batch_transform_ops')
    else:
        batch_transform = None

    dataset = eval(dataset_name)(**config_dataset)

    logger.info("build dataset({}) success...".format(dataset))

    # build sampler
    config_sampler = config[mode]['sampler']
    if "name" not in config_sampler:
        batch_sampler = None
        batch_size = config_sampler["batch_size"]
        drop_last = config_sampler["drop_last"]
        shuffle = config_sampler["shuffle"]
    else:
        sampler_name = config_sampler.pop("name")
        batch_sampler = eval(sampler_name)(dataset, **config_sampler)

    logger.info("build batch_sampler({}) success...".format(batch_sampler))

    # build batch operator
    def mix_collate_fn(batch):
        batch = transform(batch, batch_ops)
        # batch each field
        slots = []
        for items in batch:
            for i, item in enumerate(items):
                if len(slots) < len(items):
                    slots.append([item])
                else:
                    slots[i].append(item)
        return [np.stack(slot, axis=0) for slot in slots]

    if isinstance(batch_transform, list):
        batch_ops = create_operators(batch_transform)
        batch_collate_fn = mix_collate_fn
    else:
D
dongshuilong 已提交
82
        batch_collate_fn = None
F
Felix 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    # build dataloader
    config_loader = config[mode]['loader']
    num_workers = config_loader["num_workers"]
    use_shared_memory = config_loader["use_shared_memory"]

    if batch_sampler is None:
        data_loader = DataLoader(
            dataset=dataset,
            places=device,
            num_workers=num_workers,
            return_list=True,
            use_shared_memory=use_shared_memory,
            batch_size=batch_size,
            shuffle=shuffle,
            drop_last=drop_last,
            collate_fn=batch_collate_fn)
    else:
        data_loader = DataLoader(
            dataset=dataset,
            places=device,
            num_workers=num_workers,
            return_list=True,
            use_shared_memory=use_shared_memory,
            batch_sampler=batch_sampler,
            collate_fn=batch_collate_fn)

    logger.info("build data_loader({}) success...".format(data_loader))
D
dongshuilong 已提交
111

112
    return data_loader
D
dongshuilong 已提交
113 114


F
Felix 已提交
115 116 117 118 119 120 121 122 123
'''
# TODO: fix the format
def build_dataloader(config, mode, device, seed=None):
    from . import reader
    from .reader import Reader
    dataloader = Reader(config, mode=mode, places=device)()
    return dataloader

'''