reader.py 11.3 KB
Newer Older
D
dengkaipeng 已提交
1
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved
D
dengkaipeng 已提交
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 30
#
# 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 division
from __future__ import print_function
from __future__ import unicode_literals

import numpy as np
import os
import random
import time
import copy
import cv2
import box_utils
import image_utils
from pycocotools.coco import COCO
from data_utils import GeneratorEnqueuer
T
tink2123 已提交
31
from config import cfg
D
dengkaipeng 已提交
32 33 34 35 36 37 38 39 40


class DataSetReader(object):
    """A class for parsing and read COCO dataset"""

    def __init__(self):
        self.has_parsed_categpry = False

    def _parse_dataset_dir(self, mode):
D
dengkaipeng 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
        if 'coco2014' in cfg.dataset:
            cfg.train_file_list = 'annotations/instances_train2014.json'
            cfg.train_data_dir = 'train2014'
            cfg.val_file_list = 'annotations/instances_val2014.json'
            cfg.val_data_dir = 'val2014'
        elif 'coco2017' in cfg.dataset:
            cfg.train_file_list = 'annotations/instances_train2017.json'
            cfg.train_data_dir = 'train2017'
            cfg.val_file_list = 'annotations/instances_val2017.json'
            cfg.val_data_dir = 'val2017'
        else:
            raise NotImplementedError('Dataset {} not supported'.format(
                cfg.dataset))
D
dengkaipeng 已提交
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95

        if mode == 'train':
            cfg.train_file_list = os.path.join(cfg.data_dir, cfg.train_file_list)
            cfg.train_data_dir = os.path.join(cfg.data_dir, cfg.train_data_dir)
            self.COCO = COCO(cfg.train_file_list)
            self.img_dir = cfg.train_data_dir
        elif mode == 'test' or mode == 'infer':
            cfg.val_file_list = os.path.join(cfg.data_dir, cfg.val_file_list)
            cfg.val_data_dir = os.path.join(cfg.data_dir, cfg.val_data_dir)
            self.COCO = COCO(cfg.val_file_list)
            self.img_dir = cfg.val_data_dir


    def _parse_dataset_catagory(self):
        self.categories = self.COCO.loadCats(self.COCO.getCatIds())
        self.num_category = len(self.categories)
        self.label_names = []
        self.label_ids = []
        for category in self.categories:
            self.label_names.append(category['name'])
            self.label_ids.append(int(category['id']))
        self.category_to_id_map = {
            v: i
            for i, v in enumerate(self.label_ids)
        }
        print("Load in {} categories.".format(self.num_category))
        self.has_parsed_categpry = True

    def get_label_infos(self):
        if not self.has_parsed_categpry:
            self._parse_dataset_dir("test")
            self._parse_dataset_catagory()
        return (self.label_names, self.label_ids)

    def _parse_gt_annotations(self, img):
        img_height = img['height']
        img_width = img['width']
        anno = self.COCO.loadAnns(self.COCO.getAnnIds(imgIds=img['id'], iscrowd=None))
        gt_index = 0
        for target in anno:
            if target['area'] < cfg.gt_min_area:
                continue
D
dengkaipeng 已提交
96
            if 'ignore' in target and target['ignore']:
D
dengkaipeng 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                continue

            box = box_utils.coco_anno_box_to_center_relative(target['bbox'], img_height, img_width)
            if box[2] <= 0 and box[3] <= 0:
                continue

            img['gt_id'][gt_index] = np.int32(target['id'])
            img['gt_boxes'][gt_index] = box
            img['gt_labels'][gt_index] = self.category_to_id_map[target['category_id']]
            gt_index += 1
            if gt_index >= cfg.max_box_num:
                break

    def _parse_images(self, is_train):
        image_ids = self.COCO.getImgIds()
        image_ids.sort()
        imgs = copy.deepcopy(self.COCO.loadImgs(image_ids))
        for img in imgs:
            img['image'] = os.path.join(self.img_dir, img['file_name'])
            assert os.path.exists(img['image']), \
                    "image {} not found.".format(img['image'])
            box_num = cfg.max_box_num
            img['gt_id'] = np.zeros((cfg.max_box_num), dtype=np.int32)
            img['gt_boxes'] = np.zeros((cfg.max_box_num, 4), dtype=np.float32)
            img['gt_labels'] = np.zeros((cfg.max_box_num), dtype=np.int32)
            for k in ['date_captured', 'url', 'license', 'file_name']:
D
dengkaipeng 已提交
123
                if k in img:
D
dengkaipeng 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
                    del img[k]

            if is_train:
                self._parse_gt_annotations(img)

        print("Loaded {0} images from {1}.".format(len(imgs), cfg.dataset))

        return imgs

    def _parse_images_by_mode(self, mode):
        if mode == 'infer':
            return []
        else:
            return self._parse_images(is_train=(mode=='train'))

139
    def get_reader(self, mode, size=416, batch_size=None, shuffle=False, mixup_iter=0, random_sizes=[], image=None):
D
dengkaipeng 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153
        assert mode in ['train', 'test', 'infer'], "Unknow mode type!"
        if mode != 'infer':
            assert batch_size is not None, "batch size connot be None in mode {}".format(mode)
            self._parse_dataset_dir(mode)
            self._parse_dataset_catagory()

        def img_reader(img, size, mean, std):
            im_path = img['image']
            im = cv2.imread(im_path).astype('float32')
            im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)

            h, w, _ = im.shape
            im_scale_x = size / float(w)
            im_scale_y = size / float(h)
D
dengkaipeng 已提交
154
            out_img = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=cv2.INTER_CUBIC)
D
dengkaipeng 已提交
155 156 157 158
            mean = np.array(mean).reshape((1, 1, -1))
            std = np.array(std).reshape((1, 1, -1))
            out_img = (out_img / 255.0 - mean) / std
            out_img = out_img.transpose((2, 0, 1))
D
dengkaipeng 已提交
159 160 161 162 163 164 165 166 167

            return (out_img, int(img['id']), (h, w))

        def img_reader_with_augment(img, size, mean, std, mixup_img):
            im_path = img['image']
            im = cv2.imread(im_path)
            im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
            gt_boxes = img['gt_boxes'].copy()
            gt_labels = img['gt_labels'].copy()
D
dengkaipeng 已提交
168
            gt_scores = np.ones_like(gt_labels)
D
dengkaipeng 已提交
169

170 171 172 173 174 175 176 177 178 179
            if mixup_img:
                mixup_im = cv2.imread(mixup_img['image'])
                mixup_im = cv2.cvtColor(mixup_im, cv2.COLOR_BGR2RGB)
                mixup_gt_boxes = np.array(mixup_img['gt_boxes']).copy()
                mixup_gt_labels = np.array(mixup_img['gt_labels']).copy()
                mixup_gt_scores = np.ones_like(mixup_gt_labels)
                im, gt_boxes, gt_labels, gt_scores = image_utils.image_mixup(im, gt_boxes, \
                        gt_labels, gt_scores, mixup_im, mixup_gt_boxes, mixup_gt_labels, \
                        mixup_gt_scores)

D
dengkaipeng 已提交
180
            im, gt_boxes, gt_labels, gt_scores = image_utils.image_augment(im, gt_boxes, gt_labels, gt_scores, size, mean)
D
dengkaipeng 已提交
181

D
dengkaipeng 已提交
182 183 184 185
            mean = np.array(mean).reshape((1, 1, -1))
            std = np.array(std).reshape((1, 1, -1))
            out_img = (im / 255.0 - mean) / std
            out_img = out_img.astype('float32').transpose((2, 0, 1))
D
dengkaipeng 已提交
186

D
dengkaipeng 已提交
187
            return (out_img, gt_boxes, gt_labels, gt_scores)
D
dengkaipeng 已提交
188 189 190 191 192 193

        def get_img_size(size, random_sizes=[]):
            if len(random_sizes):
                return np.random.choice(random_sizes)
            return size

194 195 196 197 198 199 200 201
        def get_mixup_img(imgs, mixup_iter, total_iter, read_cnt):
            if total_iter >= mixup_iter:
                return None

            mixup_idx = np.random.randint(1, len(imgs))
            mixup_img = imgs[(read_cnt + mixup_idx) % len(imgs)]
            return mixup_img

D
dengkaipeng 已提交
202 203 204 205 206 207
        def reader():
            if mode == 'train':
                imgs = self._parse_images_by_mode(mode)
                if shuffle:
                    np.random.shuffle(imgs)
                read_cnt = 0
D
dengkaipeng 已提交
208
                total_iter = 0
D
dengkaipeng 已提交
209 210 211 212
                batch_out = []
                img_size = get_img_size(size, random_sizes)
                while True:
                    img = imgs[read_cnt % len(imgs)]
213
                    mixup_img = get_mixup_img(imgs, mixup_iter, total_iter, read_cnt)
D
dengkaipeng 已提交
214 215 216
                    read_cnt += 1
                    if read_cnt % len(imgs) == 0 and shuffle:
                        np.random.shuffle(imgs)
D
dengkaipeng 已提交
217
                    im, gt_boxes, gt_labels, gt_scores = img_reader_with_augment(img, img_size, cfg.pixel_means, cfg.pixel_stds, mixup_img)
D
dengkaipeng 已提交
218
                    batch_out.append([im, gt_boxes, gt_labels, gt_scores])
D
dengkaipeng 已提交
219 220 221 222

                    if len(batch_out) == batch_size:
                        yield batch_out
                        batch_out = []
D
dengkaipeng 已提交
223
                        total_iter += 1
D
dengkaipeng 已提交
224
                        img_size = get_img_size(size, random_sizes)
D
dengkaipeng 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

            elif mode == 'test':
                imgs = self._parse_images_by_mode(mode)
                batch_out = []
                for img in imgs:
                    im, im_id, im_shape = img_reader(img, size, cfg.pixel_means, cfg.pixel_stds)
                    batch_out.append((im, im_id, im_shape))
                    if len(batch_out) == batch_size:
                        yield batch_out
                        batch_out = []
                if len(batch_out) != 0:
                    yield batch_out
            else:
                img = {}
                img['image'] = image
                img['id'] = 0
                im, im_id, im_shape = img_reader(img, size, cfg.pixel_means, cfg.pixel_stds)
                batch_out = [(im, im_id, im_shape)]
                yield batch_out

        return reader


dsr = DataSetReader()

def train(size=416, 
          batch_size=64, 
          shuffle=True, 
253
          total_iter=0,
254
          mixup_iter=0,
D
dengkaipeng 已提交
255
          random_sizes=[],
256 257
          num_workers=8,
          max_queue=32,
T
tink2123 已提交
258
          use_multiprocessing=True):
D
dengkaipeng 已提交
259
    generator = dsr.get_reader('train', size, batch_size, shuffle, int(mixup_iter/num_workers), random_sizes)
D
dengkaipeng 已提交
260

T
tink2123 已提交
261 262 263
    if not use_multiprocessing:
        return generator

D
dengkaipeng 已提交
264 265 266 267 268 269
    def infinite_reader():
        while True:
            for data in generator():
                yield data

    def reader():
270
        cnt = 0
D
dengkaipeng 已提交
271 272
        try:
            enqueuer = GeneratorEnqueuer(
D
dengkaipeng 已提交
273 274
                infinite_reader(), use_multiprocessing=use_multiprocessing)
            enqueuer.start(max_queue_size=max_queue, workers=num_workers)
D
dengkaipeng 已提交
275 276 277
            generator_out = None
            while True:
                while enqueuer.is_running():
D
dengkaipeng 已提交
278 279
                    if not enqueuer.queue.empty():
                        generator_out = enqueuer.queue.get()
D
dengkaipeng 已提交
280 281 282 283
                        break
                    else:
                        time.sleep(0.02)
                yield generator_out
284 285 286 287
                cnt += 1
                if cnt >= total_iter:
                    enqueuer.stop()
                    return
D
dengkaipeng 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                generator_out = None
        finally:
            if enqueuer is not None:
                enqueuer.stop()
    
    return reader

def test(size=416, batch_size=1):
    return dsr.get_reader('test', size, batch_size)

def infer(size=416, image=None):
    return dsr.get_reader('infer', size, image=image)

def get_label_infos():
    return dsr.get_label_infos()