data_feed.py 36.7 KB
Newer Older
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 29
# 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.

from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

import os
import inspect

from ppdet.core.workspace import register, serializable
from ppdet.utils.download import get_dataset_path

from ppdet.data.reader import Reader
# XXX these are for triggering the decorator
from ppdet.data.transform.operators import (
    DecodeImage, MixupImage, NormalizeBox, NormalizeImage, RandomDistort,
    RandomFlipImage, RandomInterpImage, ResizeImage, ExpandImage, CropImage,
W
wangguanzhong 已提交
30
    Permute, MultiscaleTestResize)
31
from ppdet.data.transform.arrange_sample import (
W
wangguanzhong 已提交
32 33
    ArrangeRCNN, ArrangeEvalRCNN, ArrangeTestRCNN, ArrangeSSD, ArrangeEvalSSD,
    ArrangeTestSSD, ArrangeYOLO, ArrangeEvalYOLO, ArrangeTestYOLO)
34 35

__all__ = [
W
wangguanzhong 已提交
36 37 38 39 40 41
    'PadBatch', 'MultiScale', 'RandomShape', 'PadMSTest', 'DataSet',
    'CocoDataSet', 'DataFeed', 'TrainFeed', 'EvalFeed', 'FasterRCNNTrainFeed',
    'MaskRCNNTrainFeed', 'FasterRCNNEvalFeed', 'MaskRCNNEvalFeed',
    'FasterRCNNTestFeed', 'MaskRCNNTestFeed', 'SSDTrainFeed', 'SSDEvalFeed',
    'SSDTestFeed', 'YoloTrainFeed', 'YoloEvalFeed', 'YoloTestFeed',
    'create_reader'
42 43 44
]


45
def _prepare_data_config(feed, args_path):
46 47
    # if `DATASET_DIR` does not exists, search ~/.paddle/dataset for a directory
    # named `DATASET_DIR` (e.g., coco, pascal), if not present either, download
W
wangguanzhong 已提交
48 49
    dataset_home = args_path if args_path else feed.dataset.dataset_dir
    if dataset_home:
50 51
        annotation = getattr(feed.dataset, 'annotation', None)
        image_dir = getattr(feed.dataset, 'image_dir', None)
W
wangguanzhong 已提交
52
        dataset_dir = get_dataset_path(dataset_home, annotation, image_dir)
53 54 55 56
        if annotation:
            feed.dataset.annotation = os.path.join(dataset_dir, annotation)
        if image_dir:
            feed.dataset.image_dir = os.path.join(dataset_dir, image_dir)
57 58 59 60 61 62

    mixup_epoch = -1
    if getattr(feed, 'mixup_epoch', None) is not None:
        mixup_epoch = feed.mixup_epoch

    data_config = {
63 64 65 66 67 68 69 70
        'ANNO_FILE': feed.dataset.annotation,
        'IMAGE_DIR': feed.dataset.image_dir,
        'USE_DEFAULT_LABEL': feed.dataset.use_default_label,
        'IS_SHUFFLE': feed.shuffle,
        'SAMPLES': feed.samples,
        'WITH_BACKGROUND': feed.with_background,
        'MIXUP_EPOCH': mixup_epoch,
        'TYPE': type(feed.dataset).__source__
71
    }
Y
Yang Zhang 已提交
72

73 74 75 76
    if feed.mode == 'TRAIN':
        data_config['CLASS_AWARE_SAMPLING'] = getattr(
            feed, 'class_aware_sampling', False)

K
Kaipeng Deng 已提交
77
    if len(getattr(feed.dataset, 'images', [])) > 0:
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        data_config['IMAGES'] = feed.dataset.images

    return data_config


def create_reader(feed, max_iter=0, args_path=None, my_source=None):
    """
    Return iterable data reader.

    Args:
        max_iter (int): number of iterations.
        my_source (callable): callable function to create a source iterator
            which is used to provide source data in 'ppdet.data.reader'
    """

    # if `DATASET_DIR` does not exists, search ~/.paddle/dataset for a directory
    # named `DATASET_DIR` (e.g., coco, pascal), if not present either, download
    data_config = _prepare_data_config(feed, args_path)
96

W
walloollaw 已提交
97 98 99
    bufsize = getattr(feed, 'bufsize', 10)
    use_process = getattr(feed, 'use_process', False)
    memsize = getattr(feed, 'memsize', '3G')
100 101 102 103
    transform_config = {
        'WORKER_CONF': {
            'bufsize': bufsize,
            'worker_num': feed.num_workers,
W
walloollaw 已提交
104 105
            'use_process': use_process,
            'memsize': memsize
106 107 108 109 110 111 112 113 114 115
        },
        'BATCH_SIZE': feed.batch_size,
        'DROP_LAST': feed.drop_last,
        'USE_PADDED_IM_INFO': feed.use_padded_im_info,
    }

    batch_transforms = feed.batch_transforms
    pad = [t for t in batch_transforms if isinstance(t, PadBatch)]
    rand_shape = [t for t in batch_transforms if isinstance(t, RandomShape)]
    multi_scale = [t for t in batch_transforms if isinstance(t, MultiScale)]
W
wangguanzhong 已提交
116
    pad_ms_test = [t for t in batch_transforms if isinstance(t, PadMSTest)]
117 118 119 120 121 122 123 124 125

    if any(pad):
        transform_config['IS_PADDING'] = True
        if pad[0].pad_to_stride != 0:
            transform_config['COARSEST_STRIDE'] = pad[0].pad_to_stride
    if any(rand_shape):
        transform_config['RANDOM_SHAPES'] = rand_shape[0].sizes
    if any(multi_scale):
        transform_config['MULTI_SCALES'] = multi_scale[0].scales
W
wangguanzhong 已提交
126 127 128 129
    if any(pad_ms_test):
        transform_config['ENABLE_MULTISCALE_TEST'] = True
        transform_config['NUM_SCALE'] = feed.num_scale
        transform_config['COARSEST_STRIDE'] = pad_ms_test[0].pad_to_stride
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    if hasattr(inspect, 'getfullargspec'):
        argspec = inspect.getfullargspec
    else:
        argspec = inspect.getargspec

    ops = []
    for op in feed.sample_transforms:
        op_dict = op.__dict__.copy()
        argnames = [
            arg for arg in argspec(type(op).__init__).args if arg != 'self'
        ]
        op_dict = {k: v for k, v in op_dict.items() if k in argnames}
        op_dict['op'] = op.__class__.__name__
        ops.append(op_dict)
    transform_config['OPS'] = ops

147 148
    return Reader.create(feed.mode, data_config, transform_config, max_iter,
                         my_source)
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193


# XXX batch transforms are only stubs for now, actually handled by `post_map`
@serializable
class PadBatch(object):
    """
    Pad a batch of samples to same dimensions

    Args:
        pad_to_stride (int): pad to multiple of strides, e.g., 32
    """

    def __init__(self, pad_to_stride=0):
        super(PadBatch, self).__init__()
        self.pad_to_stride = pad_to_stride


@serializable
class MultiScale(object):
    """
    Randomly resize image by scale

    Args:
        scales (list): list of int, randomly resize to one of these scales
    """

    def __init__(self, scales=[]):
        super(MultiScale, self).__init__()
        self.scales = scales


@serializable
class RandomShape(object):
    """
    Randomly reshape a batch

    Args:
        sizes (list): list of int, random choose a size from these
    """

    def __init__(self, sizes=[]):
        super(RandomShape, self).__init__()
        self.sizes = sizes


W
wangguanzhong 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207
@serializable
class PadMSTest(object):
    """
    Padding for multi-scale test
 
    Args:
        pad_to_stride (int): pad to multiple of strides, e.g., 32
    """

    def __init__(self, pad_to_stride=0):
        super(PadMSTest, self).__init__()
        self.pad_to_stride = pad_to_stride


208 209 210 211 212 213 214 215 216 217 218 219 220 221
@serializable
class DataSet(object):
    """
    Dataset, e.g., coco, pascal voc

    Args:
        annotation (str): annotation file path
        image_dir (str): directory where image files are stored
        shuffle (bool): shuffle samples
    """
    __source__ = 'RoiDbSource'

    def __init__(self,
                 annotation,
K
Kaipeng Deng 已提交
222
                 image_dir=None,
223 224 225 226 227 228 229 230 231
                 dataset_dir=None,
                 use_default_label=None):
        super(DataSet, self).__init__()
        self.dataset_dir = dataset_dir
        self.annotation = annotation
        self.image_dir = image_dir
        self.use_default_label = use_default_label


K
Kaipeng Deng 已提交
232
COCO_DATASET_DIR = 'dataset/coco'
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
COCO_TRAIN_ANNOTATION = 'annotations/instances_train2017.json'
COCO_TRAIN_IMAGE_DIR = 'train2017'
COCO_VAL_ANNOTATION = 'annotations/instances_val2017.json'
COCO_VAL_IMAGE_DIR = 'val2017'


@serializable
class CocoDataSet(DataSet):
    def __init__(self,
                 dataset_dir=COCO_DATASET_DIR,
                 annotation=COCO_TRAIN_ANNOTATION,
                 image_dir=COCO_TRAIN_IMAGE_DIR):
        super(CocoDataSet, self).__init__(
            dataset_dir=dataset_dir, annotation=annotation, image_dir=image_dir)


K
Kaipeng Deng 已提交
249 250 251 252 253
VOC_DATASET_DIR = 'dataset/voc'
VOC_TRAIN_ANNOTATION = 'train.txt'
VOC_VAL_ANNOTATION = 'val.txt'
VOC_IMAGE_DIR = None
VOC_USE_DEFAULT_LABEL = True
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276


@serializable
class VocDataSet(DataSet):
    __source__ = 'VOCSource'

    def __init__(self,
                 dataset_dir=VOC_DATASET_DIR,
                 annotation=VOC_TRAIN_ANNOTATION,
                 image_dir=VOC_IMAGE_DIR,
                 use_default_label=VOC_USE_DEFAULT_LABEL):
        super(VocDataSet, self).__init__(
            dataset_dir=dataset_dir,
            annotation=annotation,
            image_dir=image_dir,
            use_default_label=use_default_label)


@serializable
class SimpleDataSet(DataSet):
    __source__ = 'SimpleSource'

    def __init__(self,
K
Kaipeng Deng 已提交
277 278 279 280
                 dataset_dir=None,
                 annotation=None,
                 image_dir=None,
                 use_default_label=None):
281 282
        super(SimpleDataSet, self).__init__(
            dataset_dir=dataset_dir, annotation=annotation, image_dir=image_dir)
K
Kaipeng Deng 已提交
283 284 285 286
        self.images = []

    def add_images(self, images):
        self.images.extend(images)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303


@serializable
class DataFeed(object):
    """
    DataFeed encompasses all data loading related settings

    Args:
        dataset (object): a `Dataset` instance
        fields (list): list of data fields needed
        image_shape (list): list of image dims (C, MAX_DIM, MIN_DIM)
        sample_transforms (list): list of sample transformations to use
        batch_transforms (list): list of batch transformations to use
        batch_size (int): number of images per device
        shuffle (bool): if samples should be shuffled
        drop_last (bool): drop last batch if size is uneven
        num_workers (int): number of workers processes (or threads)
W
walloollaw 已提交
304 305 306 307
        bufsize (int): size of queue used to buffer results from workers
        use_process (bool): use process or thread as workers
        memsize (str): size of shared memory used in result queue
                        when 'use_process' is True, default to '3G'
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    """
    __category__ = 'data'

    def __init__(self,
                 dataset,
                 fields,
                 image_shape,
                 sample_transforms=None,
                 batch_transforms=None,
                 batch_size=1,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 with_background=True,
                 num_workers=2,
                 bufsize=10,
                 use_process=False,
W
walloollaw 已提交
325
                 memsize=None,
326 327
                 use_padded_im_info=False,
                 class_aware_sampling=False):
328 329 330 331 332 333 334 335 336 337 338 339 340
        super(DataFeed, self).__init__()
        self.fields = fields
        self.image_shape = image_shape
        self.sample_transforms = sample_transforms
        self.batch_transforms = batch_transforms
        self.batch_size = batch_size
        self.shuffle = shuffle
        self.samples = samples
        self.drop_last = drop_last
        self.with_background = with_background
        self.num_workers = num_workers
        self.bufsize = bufsize
        self.use_process = use_process
W
walloollaw 已提交
341
        self.memsize = memsize
342 343
        self.dataset = dataset
        self.use_padded_im_info = use_padded_im_info
344
        self.class_aware_sampling = class_aware_sampling
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        if isinstance(dataset, dict):
            self.dataset = DataSet(**dataset)


# for custom (i.e., Non-preset) datasets
@register
class TrainFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset,
                 fields,
                 image_shape,
                 sample_transforms=[],
                 batch_transforms=[],
                 batch_size=1,
                 shuffle=True,
                 samples=-1,
                 drop_last=False,
                 with_background=True,
                 num_workers=2,
                 bufsize=10,
W
walloollaw 已提交
367 368
                 use_process=True,
                 memsize=None):
369 370 371 372 373 374 375 376 377 378 379 380 381
        super(TrainFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            with_background=with_background,
            num_workers=num_workers,
            bufsize=bufsize,
W
walloollaw 已提交
382 383
            use_process=use_process,
            memsize=memsize)
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443


@register
class EvalFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset,
                 fields,
                 image_shape,
                 sample_transforms=[],
                 batch_transforms=[],
                 batch_size=1,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 with_background=True,
                 num_workers=2):
        super(EvalFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            with_background=with_background,
            num_workers=num_workers)


@register
class TestFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset,
                 fields,
                 image_shape,
                 sample_transforms=[],
                 batch_transforms=[],
                 batch_size=1,
                 shuffle=False,
                 drop_last=False,
                 with_background=True,
                 num_workers=2):
        super(TestFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            drop_last=drop_last,
            with_background=with_background,
            num_workers=num_workers)


444
# yapf: disable
445 446 447 448 449 450 451 452 453 454
@register
class FasterRCNNTrainFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=CocoDataSet().__dict__,
                 fields=[
                     'image', 'im_info', 'im_id', 'gt_box', 'gt_label',
                     'is_crowd'
                 ],
W
wangguanzhong 已提交
455
                 image_shape=[None, 3, None, None],
456
                 sample_transforms=[
457 458 459 460 461 462 463 464
                     DecodeImage(to_rgb=True),
                     RandomFlipImage(prob=0.5),
                     NormalizeImage(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225],
                                    is_scale=True,
                                    is_channel_first=False),
                     ResizeImage(target_size=800, max_size=1333, interp=1),
                     Permute(to_bgr=False)
465 466 467 468 469 470
                 ],
                 batch_transforms=[PadBatch()],
                 batch_size=1,
                 shuffle=True,
                 samples=-1,
                 drop_last=False,
W
walloollaw 已提交
471
                 bufsize=10,
472
                 num_workers=2,
W
walloollaw 已提交
473
                 use_process=False,
474 475
                 memsize=None,
                 class_aware_sampling=False):
476 477 478 479 480 481 482 483 484 485 486 487 488
        # XXX this should be handled by the data loader, since `fields` is
        # given, just collect them
        sample_transforms.append(ArrangeRCNN())
        super(FasterRCNNTrainFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
W
walloollaw 已提交
489
            bufsize=bufsize,
490
            num_workers=num_workers,
W
walloollaw 已提交
491
            use_process=use_process,
492 493
            memsize=memsize,
            class_aware_sampling=class_aware_sampling)
494 495 496 497 498
        # XXX these modes should be unified
        self.mode = 'TRAIN'


@register
Y
Yang Zhang 已提交
499
class FasterRCNNEvalFeed(DataFeed):
500 501 502
    __doc__ = DataFeed.__doc__

    def __init__(self,
Y
Yang Zhang 已提交
503 504
                 dataset=CocoDataSet(COCO_VAL_ANNOTATION,
                                     COCO_VAL_IMAGE_DIR).__dict__,
W
wangguanzhong 已提交
505 506
                 fields=['image', 'im_info', 'im_id', 'im_shape', 'gt_box',
                         'gt_label', 'is_difficult'],
W
wangguanzhong 已提交
507
                 image_shape=[None, 3, None, None],
508
                 sample_transforms=[
509 510 511 512 513
                     DecodeImage(to_rgb=True),
                     NormalizeImage(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225],
                                    is_scale=True,
                                    is_channel_first=False),
Y
Yang Zhang 已提交
514 515
                     ResizeImage(target_size=800, max_size=1333, interp=1),
                     Permute(to_bgr=False)
516 517 518
                 ],
                 batch_transforms=[PadBatch()],
                 batch_size=1,
Y
Yang Zhang 已提交
519
                 shuffle=False,
520 521 522
                 samples=-1,
                 drop_last=False,
                 num_workers=2,
W
wangguanzhong 已提交
523 524 525 526
                 use_padded_im_info=True,
                 enable_multiscale=False,
                 num_scale=1,
                 enable_aug_flip=False):
W
wangguanzhong 已提交
527
        sample_transforms.append(ArrangeEvalRCNN())
Y
Yang Zhang 已提交
528
        super(FasterRCNNEvalFeed, self).__init__(
529 530 531 532 533 534 535 536 537 538
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
Y
Yang Zhang 已提交
539 540
            use_padded_im_info=use_padded_im_info)
        self.mode = 'VAL'
W
wangguanzhong 已提交
541 542 543
        self.enable_multiscale = enable_multiscale
        self.num_scale = num_scale
        self.enable_aug_flip = enable_aug_flip
544 545 546


@register
Y
Yang Zhang 已提交
547
class FasterRCNNTestFeed(DataFeed):
548 549 550
    __doc__ = DataFeed.__doc__

    def __init__(self,
Y
Yang Zhang 已提交
551 552
                 dataset=SimpleDataSet(COCO_VAL_ANNOTATION,
                                       COCO_VAL_IMAGE_DIR).__dict__,
553
                 fields=['image', 'im_info', 'im_id', 'im_shape'],
W
wangguanzhong 已提交
554
                 image_shape=[None, 3, None, None],
555
                 sample_transforms=[
556 557 558 559 560
                     DecodeImage(to_rgb=True),
                     NormalizeImage(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225],
                                    is_scale=True,
                                    is_channel_first=False),
561 562 563 564 565 566 567 568 569 570
                     Permute(to_bgr=False)
                 ],
                 batch_transforms=[PadBatch()],
                 batch_size=1,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 num_workers=2,
                 use_padded_im_info=True):
        sample_transforms.append(ArrangeTestRCNN())
Y
Yang Zhang 已提交
571 572 573
        if isinstance(dataset, dict):
            dataset = SimpleDataSet(**dataset)
        super(FasterRCNNTestFeed, self).__init__(
574 575 576 577 578 579 580 581 582 583 584
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
            use_padded_im_info=use_padded_im_info)
Y
Yang Zhang 已提交
585
        self.mode = 'TEST'
586 587


Y
Yang Zhang 已提交
588 589 590
# XXX currently use two presets, in the future, these should be combined into a
# single `RCNNTrainFeed`. Mask (and keypoint) should be processed
# automatically if `gt_mask` (or `gt_keypoints`) is in the required fields
591
@register
Y
Yang Zhang 已提交
592
class MaskRCNNTrainFeed(DataFeed):
593 594 595
    __doc__ = DataFeed.__doc__

    def __init__(self,
Y
Yang Zhang 已提交
596 597 598 599 600
                 dataset=CocoDataSet().__dict__,
                 fields=[
                     'image', 'im_info', 'im_id', 'gt_box', 'gt_label',
                     'is_crowd', 'gt_mask'
                 ],
W
wangguanzhong 已提交
601
                 image_shape=[None, 3, None, None],
602
                 sample_transforms=[
603
                     DecodeImage(to_rgb=True),
Y
Yang Zhang 已提交
604
                     RandomFlipImage(prob=0.5, is_mask_flip=True),
605 606 607 608
                     NormalizeImage(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225],
                                    is_scale=True,
                                    is_channel_first=False),
Y
Yang Zhang 已提交
609 610 611 612 613
                     ResizeImage(target_size=800,
                                 max_size=1333,
                                 interp=1,
                                 use_cv2=True),
                     Permute(to_bgr=False, channel_first=True)
614 615 616
                 ],
                 batch_transforms=[PadBatch()],
                 batch_size=1,
Y
Yang Zhang 已提交
617
                 shuffle=True,
618 619 620
                 samples=-1,
                 drop_last=False,
                 num_workers=2,
Y
Yang Zhang 已提交
621 622 623 624
                 use_process=False,
                 use_padded_im_info=False):
        sample_transforms.append(ArrangeRCNN(is_mask=True))
        super(MaskRCNNTrainFeed, self).__init__(
625 626 627 628 629 630 631 632 633 634
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
Y
Yang Zhang 已提交
635 636
            use_process=use_process)
        self.mode = 'TRAIN'
637 638 639 640 641 642 643 644 645 646


@register
class MaskRCNNEvalFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=CocoDataSet(COCO_VAL_ANNOTATION,
                                     COCO_VAL_IMAGE_DIR).__dict__,
                 fields=['image', 'im_info', 'im_id', 'im_shape'],
W
wangguanzhong 已提交
647
                 image_shape=[None, 3, None, None],
648
                 sample_transforms=[
649 650 651 652 653 654 655 656 657 658
                     DecodeImage(to_rgb=True),
                     NormalizeImage(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225],
                                    is_scale=True,
                                    is_channel_first=False),
                     ResizeImage(target_size=800,
                                 max_size=1333,
                                 interp=1,
                                 use_cv2=True),
                     Permute(to_bgr=False, channel_first=True)
659 660 661 662 663 664 665 666
                 ],
                 batch_transforms=[PadBatch()],
                 batch_size=1,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 num_workers=2,
                 use_process=False,
W
wangguanzhong 已提交
667 668 669 670
                 use_padded_im_info=True,
                 enable_multiscale=False,
                 num_scale=1,
                 enable_aug_flip=False):
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
        sample_transforms.append(ArrangeTestRCNN())
        super(MaskRCNNEvalFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
            use_process=use_process,
            use_padded_im_info=use_padded_im_info)
        self.mode = 'VAL'
W
wangguanzhong 已提交
686 687 688
        self.enable_multiscale = enable_multiscale
        self.num_scale = num_scale
        self.enable_aug_flip = enable_aug_flip
689 690 691 692 693 694 695 696 697 698


@register
class MaskRCNNTestFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=SimpleDataSet(COCO_VAL_ANNOTATION,
                                       COCO_VAL_IMAGE_DIR).__dict__,
                 fields=['image', 'im_info', 'im_id', 'im_shape'],
W
wangguanzhong 已提交
699
                 image_shape=[None, 3, None, None],
700
                 sample_transforms=[
701 702
                     DecodeImage(to_rgb=True),
                     NormalizeImage(
703 704 705
                         mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225],
                         is_scale=True,
706 707
                         is_channel_first=False),
                     Permute(to_bgr=False, channel_first=True)
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
                 ],
                 batch_transforms=[PadBatch()],
                 batch_size=1,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 num_workers=2,
                 use_process=False,
                 use_padded_im_info=True):
        sample_transforms.append(ArrangeTestRCNN())
        if isinstance(dataset, dict):
            dataset = SimpleDataSet(**dataset)
        super(MaskRCNNTestFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
            use_process=use_process,
            use_padded_im_info=use_padded_im_info)
        self.mode = 'TEST'


@register
class SSDTrainFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=VocDataSet().__dict__,
742
                 fields=['image', 'gt_box', 'gt_label'],
743
                 image_shape=[3, 300, 300],
744
                 sample_transforms=[
745 746 747 748 749 750
                     DecodeImage(to_rgb=True, with_mixup=False),
                     NormalizeBox(),
                     RandomDistort(brightness_lower=0.875,
                                   brightness_upper=1.125,
                                   is_order=True),
                     ExpandImage(max_ratio=4, prob=0.5),
751
                     CropImage(batch_sampler=[[1, 1, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],
752 753 754 755 756 757
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.1, 0.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.3, 0.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.5, 0.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.7, 0.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.9, 0.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0]],
758
                               satisfy_all=False, avoid_no_bbox=False),
759 760 761 762 763 764
                     ResizeImage(target_size=300, use_cv2=False, interp=1),
                     RandomFlipImage(is_normalized=True),
                     Permute(),
                     NormalizeImage(mean=[127.5, 127.5, 127.5],
                                    std=[127.502231, 127.502231, 127.502231],
                                    is_scale=False)
765 766 767 768 769 770 771 772
                 ],
                 batch_transforms=[],
                 batch_size=32,
                 shuffle=True,
                 samples=-1,
                 drop_last=True,
                 num_workers=8,
                 bufsize=10,
W
walloollaw 已提交
773 774
                 use_process=True,
                 memsize=None):
775 776 777 778 779 780 781 782 783 784 785 786
        sample_transforms.append(ArrangeSSD())
        super(SSDTrainFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
787
            bufsize=bufsize,
W
walloollaw 已提交
788 789
            use_process=use_process,
            memsize=None)
790 791 792 793 794 795 796 797 798 799
        self.mode = 'TRAIN'


@register
class SSDEvalFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(
            self,
            dataset=VocDataSet(VOC_VAL_ANNOTATION).__dict__,
800 801
            fields=['image', 'im_shape', 'im_id', 'gt_box',
                         'gt_label', 'is_difficult'],
802
            image_shape=[3, 300, 300],
803
            sample_transforms=[
804 805 806 807 808
                DecodeImage(to_rgb=True, with_mixup=False),
                NormalizeBox(),
                ResizeImage(target_size=300, use_cv2=False, interp=1),
                Permute(),
                NormalizeImage(
809 810 811 812 813 814 815 816 817 818 819
                    mean=[127.5, 127.5, 127.5],
                    std=[127.502231, 127.502231, 127.502231],
                    is_scale=False)
            ],
            batch_transforms=[],
            batch_size=64,
            shuffle=False,
            samples=-1,
            drop_last=True,
            num_workers=8,
            bufsize=10,
W
walloollaw 已提交
820 821
            use_process=False,
            memsize=None):
822
        sample_transforms.append(ArrangeEvalSSD(fields))
823 824 825 826 827 828 829 830 831 832 833
        super(SSDEvalFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            num_workers=num_workers,
834
            bufsize=bufsize,
W
walloollaw 已提交
835 836
            use_process=use_process,
            memsize=memsize)
837 838 839 840 841 842 843 844
        self.mode = 'VAL'


@register
class SSDTestFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
K
Kaipeng Deng 已提交
845
                 dataset=SimpleDataSet(VOC_VAL_ANNOTATION).__dict__,
846
                 fields=['image', 'im_id', 'im_shape'],
847
                 image_shape=[3, 300, 300],
848
                 sample_transforms=[
849 850 851
                     DecodeImage(to_rgb=True),
                     ResizeImage(target_size=300, use_cv2=False, interp=1),
                     Permute(),
852 853 854 855 856 857 858 859 860 861 862 863
                     NormalizeImage(
                         mean=[127.5, 127.5, 127.5],
                         std=[127.502231, 127.502231, 127.502231],
                         is_scale=False)
                 ],
                 batch_transforms=[],
                 batch_size=1,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 num_workers=8,
                 bufsize=10,
W
walloollaw 已提交
864 865
                 use_process=False,
                 memsize=None):
866 867 868 869 870 871 872 873 874 875 876 877 878
        sample_transforms.append(ArrangeTestSSD())
        if isinstance(dataset, dict):
            dataset = SimpleDataSet(**dataset)
        super(SSDTestFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
879 880
            num_workers=num_workers,
            bufsize=bufsize,
W
walloollaw 已提交
881 882
            use_process=use_process,
            memsize=memsize)
883 884 885 886 887 888 889 890 891 892
        self.mode = 'TEST'


@register
class YoloTrainFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=CocoDataSet().__dict__,
                 fields=['image', 'gt_box', 'gt_label', 'gt_score'],
893
                 image_shape=[3, 608, 608],
894
                 sample_transforms=[
895 896
                     DecodeImage(to_rgb=True, with_mixup=True),
                     MixupImage(alpha=1.5, beta=1.5),
897 898
                     NormalizeBox(),
                     RandomDistort(),
899 900
                     ExpandImage(max_ratio=4., prob=.5,
                                 mean=[123.675, 116.28, 103.53]),
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
                     CropImage([[1, 1, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.1, 1.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.3, 1.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.5, 1.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.7, 1.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.9, 1.0],
                                [1, 50, 0.3, 1.0, 0.5, 2.0, 0.0, 1.0]]),
                     RandomInterpImage(target_size=608),
                     RandomFlipImage(is_normalized=True),
                     NormalizeImage(
                         mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225],
                         is_scale=True,
                         is_channel_first=False),
                     Permute(to_bgr=False),
                 ],
                 batch_transforms=[
                     RandomShape(sizes=[
                         320, 352, 384, 416, 448, 480, 512, 544, 576, 608
                     ])
                 ],
                 batch_size=8,
                 shuffle=True,
                 samples=-1,
                 drop_last=True,
                 with_background=False,
                 num_workers=8,
                 bufsize=128,
                 use_process=True,
W
walloollaw 已提交
930
                 memsize=None,
931
                 num_max_boxes=50,
932 933
                 mixup_epoch=250,
                 class_aware_sampling=False):
934 935 936 937 938 939 940 941 942 943 944 945 946 947
        sample_transforms.append(ArrangeYOLO())
        super(YoloTrainFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            with_background=with_background,
            num_workers=num_workers,
            bufsize=bufsize,
W
walloollaw 已提交
948
            use_process=use_process,
949 950
            memsize=memsize,
            class_aware_sampling=class_aware_sampling)
951 952 953 954 955 956 957 958 959 960 961 962
        self.num_max_boxes = num_max_boxes
        self.mixup_epoch = mixup_epoch
        self.mode = 'TRAIN'


@register
class YoloEvalFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=CocoDataSet(COCO_VAL_ANNOTATION,
                                     COCO_VAL_IMAGE_DIR).__dict__,
963
                 fields=['image', 'im_size', 'im_id', 'gt_box',
964
                         'gt_label', 'is_difficult'],
965
                 image_shape=[3, 608, 608],
966 967
                 sample_transforms=[
                     DecodeImage(to_rgb=True),
968
                     ResizeImage(target_size=608, interp=2),
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
                     NormalizeImage(
                         mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225],
                         is_scale=True,
                         is_channel_first=False),
                     Permute(to_bgr=False),
                 ],
                 batch_transforms=[],
                 batch_size=8,
                 shuffle=False,
                 samples=-1,
                 drop_last=False,
                 with_background=False,
                 num_workers=8,
                 num_max_boxes=50,
W
walloollaw 已提交
984 985
                 use_process=False,
                 memsize=None):
986
        sample_transforms.append(ArrangeEvalYOLO())
987 988 989 990 991 992 993 994 995 996 997 998
        super(YoloEvalFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            with_background=with_background,
            num_workers=num_workers,
W
walloollaw 已提交
999 1000
            use_process=use_process,
            memsize=memsize)
1001
        self.num_max_boxes = num_max_boxes
1002 1003 1004
        self.mode = 'VAL'
        self.bufsize = 128

1005 1006 1007 1008 1009 1010 1011
        # support image shape config, resize image with image_shape
        for i, trans in enumerate(sample_transforms):
            if isinstance(trans, ResizeImage):
                sample_transforms[i] = ResizeImage(
                        target_size=self.image_shape[-1],
                        interp=trans.interp)

1012 1013 1014 1015 1016 1017 1018 1019

@register
class YoloTestFeed(DataFeed):
    __doc__ = DataFeed.__doc__

    def __init__(self,
                 dataset=SimpleDataSet(COCO_VAL_ANNOTATION,
                                       COCO_VAL_IMAGE_DIR).__dict__,
1020
                 fields=['image', 'im_size', 'im_id'],
1021
                 image_shape=[3, 608, 608],
1022 1023
                 sample_transforms=[
                     DecodeImage(to_rgb=True),
1024 1025 1026 1027 1028
                     ResizeImage(target_size=608, interp=2),
                     NormalizeImage(mean=[0.485, 0.456, 0.406],
                                    std=[0.229, 0.224, 0.225],
                                    is_scale=True,
                                    is_channel_first=False),
1029 1030 1031 1032 1033
                     Permute(to_bgr=False),
                 ],
                 batch_transforms=[],
                 batch_size=1,
                 shuffle=False,
K
Kaipeng Deng 已提交
1034
                 samples=-1,
1035 1036 1037 1038
                 drop_last=False,
                 with_background=False,
                 num_workers=8,
                 num_max_boxes=50,
W
walloollaw 已提交
1039 1040
                 use_process=False,
                 memsize=None):
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
        sample_transforms.append(ArrangeTestYOLO())
        if isinstance(dataset, dict):
            dataset = SimpleDataSet(**dataset)
        super(YoloTestFeed, self).__init__(
            dataset,
            fields,
            image_shape,
            sample_transforms,
            batch_transforms,
            batch_size=batch_size,
            shuffle=shuffle,
            samples=samples,
            drop_last=drop_last,
            with_background=with_background,
            num_workers=num_workers,
W
walloollaw 已提交
1056 1057
            use_process=use_process,
            memsize=memsize)
1058 1059
        self.mode = 'TEST'
        self.bufsize = 128
1060 1061 1062 1063 1064 1065 1066

        # support image shape config, resize image with image_shape
        for i, trans in enumerate(sample_transforms):
            if isinstance(trans, ResizeImage):
                sample_transforms[i] = ResizeImage(
                        target_size=self.image_shape[-1],
                        interp=trans.interp)
1067
# yapf: enable