reader.py 14.3 KB
Newer Older
R
ruri 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#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.

W
wangmeng28 已提交
15
import os
W
wangmeng28 已提交
16
import math
W
wangmeng28 已提交
17 18 19
import random
import functools
import numpy as np
R
ruri 已提交
20
import cv2
21 22
import logging
import imghdr
W
wangmeng28 已提交
23

R
ruri 已提交
24
import paddle
25
from paddle import fluid
26 27 28 29
from utils.autoaugment import ImageNetPolicy
from PIL import Image

policy = None
R
ruri 已提交
30

31 32 33
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

W
wangmeng28 已提交
34
random.seed(0)
35
np.random.seed(0)
W
wangmeng28 已提交
36 37


R
ruri 已提交
38 39 40 41 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 82 83 84 85 86 87 88 89 90 91
def rotate_image(img):
    """rotate image

    Args:
        img: image data

    Returns:
        rotated image data
    """
    (h, w) = img.shape[:2]
    center = (w / 2, h / 2)
    angle = np.random.randint(-10, 11)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    rotated = cv2.warpAffine(img, M, (w, h))
    return rotated


def random_crop(img, size, settings, scale=None, ratio=None,
                interpolation=None):
    """random crop image
        
    Args:
        img: image data
        size: crop size
        settings: arguments
        scale: scale parameter
        ratio: ratio parameter

    Returns:
        random cropped image data
    """
    lower_scale = settings.lower_scale
    lower_ratio = settings.lower_ratio
    upper_ratio = settings.upper_ratio
    scale = [lower_scale, 1.0] if scale is None else scale
    ratio = [lower_ratio, upper_ratio] if ratio is None else ratio

    aspect_ratio = math.sqrt(np.random.uniform(*ratio))
    w = 1. * aspect_ratio
    h = 1. / aspect_ratio

    bound = min((float(img.shape[0]) / img.shape[1]) / (h**2),
                (float(img.shape[1]) / img.shape[0]) / (w**2))

    scale_max = min(scale[1], bound)
    scale_min = min(scale[0], bound)

    target_area = img.shape[0] * img.shape[1] * np.random.uniform(scale_min,
                                                                  scale_max)
    target_size = math.sqrt(target_area)
    w = int(target_size * w)
    h = int(target_size * h)
    i = np.random.randint(0, img.shape[0] - h + 1)
    j = np.random.randint(0, img.shape[1] - w + 1)
W
wangmeng28 已提交
92

R
ruri 已提交
93
    img = img[i:i + h, j:j + w, :]
W
wangmeng28 已提交
94

R
ruri 已提交
95 96 97 98 99
    if interpolation:
        resized = cv2.resize(img, (size, size), interpolation=interpolation)
    else:
        resized = cv2.resize(img, (size, size))
    return resized
W
wangmeng28 已提交
100 101


R
ruri 已提交
102 103 104 105 106 107 108 109 110 111
#NOTE:(2019/08/08) distort color func is not implemented
def distort_color(img):
    """distort image color

    Args:
        img: image data

    Returns:
        distorted color image data
    """
W
wangmeng28 已提交
112 113 114
    return img


R
ruri 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
def resize_short(img, target_size, interpolation=None):
    """resize image
    
    Args:
        img: image data
        target_size: resize short target size
        interpolation: interpolation mode

    Returns:
        resized image data
    """
    percent = float(target_size) / min(img.shape[0], img.shape[1])
    resized_width = int(round(img.shape[1] * percent))
    resized_height = int(round(img.shape[0] * percent))
    if interpolation:
        resized = cv2.resize(
            img, (resized_width, resized_height), interpolation=interpolation)
    else:
        resized = cv2.resize(img, (resized_width, resized_height))
    return resized


W
wangmeng28 已提交
137
def crop_image(img, target_size, center):
R
ruri 已提交
138 139 140 141 142 143 144 145 146 147 148
    """crop image 
    
    Args:
        img: images data
        target_size: crop target size
        center: crop mode
    
    Returns:
        img: cropped image data
    """
    height, width = img.shape[:2]
W
wangmeng28 已提交
149 150
    size = target_size
    if center == True:
R
ruri 已提交
151 152
        w_start = (width - size) // 2
        h_start = (height - size) // 2
W
wangmeng28 已提交
153
    else:
M
minqiyang 已提交
154 155
        w_start = np.random.randint(0, width - size + 1)
        h_start = np.random.randint(0, height - size + 1)
W
wangmeng28 已提交
156 157
    w_end = w_start + size
    h_end = h_start + size
R
ruri 已提交
158
    img = img[h_start:h_end, w_start:w_end, :]
W
wangmeng28 已提交
159 160 161
    return img


R
ruri 已提交
162 163 164
def create_mixup_reader(settings, rd):
    """
    """
W
wangmeng28 已提交
165

R
ruri 已提交
166 167 168 169 170
    class context:
        tmp_mix = []
        tmp_l1 = []
        tmp_l2 = []
        tmp_lam = []
W
wangmeng28 已提交
171

R
ruri 已提交
172
    alpha = settings.mixup_alpha
W
wangmeng28 已提交
173

R
ruri 已提交
174
    def fetch_data():
175 176
        for item in rd():
            yield item
W
wangmeng28 已提交
177

R
ruri 已提交
178 179 180 181 182 183 184 185 186 187 188 189
    def mixup_data():
        for data_list in fetch_data():
            if alpha > 0.:
                lam = np.random.beta(alpha, alpha)
            else:
                lam = 1.
            l1 = np.array(data_list)
            l2 = np.random.permutation(l1)
            mixed_l = [
                l1[i][0] * lam + (1 - lam) * l2[i][0] for i in range(len(l1))
            ]
            yield (mixed_l, l1, l2, lam)
W
wangmeng28 已提交
190

R
ruri 已提交
191 192 193 194 195 196 197 198 199
    def mixup_reader():
        for context.tmp_mix, context.tmp_l1, context.tmp_l2, context.tmp_lam in mixup_data(
        ):
            for i in range(len(context.tmp_mix)):
                mixed_l = context.tmp_mix[i]
                l1 = context.tmp_l1[i]
                l2 = context.tmp_l2[i]
                lam = context.tmp_lam
                yield (mixed_l, int(l1[1]), int(l2[1]), float(lam))
W
wangmeng28 已提交
200

R
ruri 已提交
201
    return mixup_reader
W
wangmeng28 已提交
202

R
ruri 已提交
203

R
ruri 已提交
204 205
def process_image(sample, settings, mode, color_jitter, rotate):
    """ process_image """
W
wangmeng28 已提交
206

R
ruri 已提交
207 208
    mean = settings.image_mean
    std = settings.image_std
R
ruri 已提交
209
    crop_size = settings.image_shape[1]
W
wangmeng28 已提交
210 211

    img_path = sample[0]
R
ruri 已提交
212
    img = cv2.imread(img_path)
W
wangmeng28 已提交
213

214 215 216 217
    if img is None:
        logger.warning("img({0}) is None, pass it.".format(img_path))
        return None

W
wangmeng28 已提交
218
    if mode == 'train':
R
ruri 已提交
219 220 221
        if rotate:
            img = rotate_image(img)
        if crop_size > 0:
R
ruri 已提交
222 223
            img = random_crop(
                img, crop_size, settings, interpolation=settings.interpolation)
W
wangmeng28 已提交
224 225
        if color_jitter:
            img = distort_color(img)
M
minqiyang 已提交
226
        if np.random.randint(0, 2) == 1:
R
ruri 已提交
227 228 229 230
            img = img[:, ::-1, :]
    else:
        if crop_size > 0:
            target_size = settings.resize_short_size
R
ruri 已提交
231 232
            img = resize_short(
                img, target_size, interpolation=settings.interpolation)
R
ruri 已提交
233 234
            img = crop_image(img, target_size=crop_size, center=True)

235 236 237 238 239 240 241 242 243
    img = img[:, :, ::-1]

    if 'use_aa' in settings and settings.use_aa and mode == 'train':
        img = np.ascontiguousarray(img)
        img = Image.fromarray(img)
        img = policy(img)
        img = np.asarray(img)

    img = img.astype('float32').transpose((2, 0, 1)) / 255
R
ruri 已提交
244 245
    img_mean = np.array(mean).reshape((3, 1, 1))
    img_std = np.array(std).reshape((3, 1, 1))
W
wangmeng28 已提交
246
    img -= img_mean
W
wangmeng28 已提交
247
    img /= img_std
248 249 250
    # doing training (train.py)
    if mode == 'train' or (mode == 'val' and
                           not hasattr(settings, 'save_json_path')):
R
ruri 已提交
251
        return (img, sample[1])
252 253 254 255
    #doing testing (eval.py)
    elif mode == 'val' and hasattr(settings, 'save_json_path'):
        return (img, sample[1], sample[0])
    #doing predict (infer.py)
256
    elif mode == 'test':
257
        return (img, sample[0])
258 259
    else:
        raise Exception("mode not implemented")
W
wangmeng28 已提交
260

R
ruri 已提交
261

262 263 264 265
def process_batch_data(input_data, settings, mode, color_jitter, rotate):
    batch_data = []
    for sample in input_data:
        if os.path.isfile(sample[0]):
266 267 268 269 270
            tmp_data = process_image(sample, settings, mode, color_jitter,
                                     rotate)
            if tmp_data is None:
                continue
            batch_data.append(tmp_data)
271
        else:
272
            logger.info("File not exist : {0}".format(sample[0]))
273 274
    return batch_data

R
ruri 已提交
275

276 277 278 279 280 281 282 283
class ImageNetReader:
    def __init__(self, seed=None):
        self.shuffle_seed = seed

    def set_shuffle_seed(self, seed):
        assert isinstance(seed, int), "shuffle seed must be int"
        self.shuffle_seed = seed

284 285
    def _get_single_card_bs(self, settings, mode):
        if settings.use_gpu:
286
            if mode == "val" and hasattr(settings, "test_batch_size"):
287 288 289
                single_card_bs = int(
                    settings.test_batch_size
                ) // paddle.fluid.core.get_cuda_device_count()
290
            else:
291 292 293
                single_card_bs = int(
                    settings.
                    batch_size) // paddle.fluid.core.get_cuda_device_count()
294
        else:
295
            if mode == "val" and hasattr(settings, "test_batch_size"):
296
                single_card_bs = int(settings.test_batch_size) // int(
297 298
                    os.environ.get('CPU_NUM', 1))
            else:
299
                single_card_bs = int(settings.batch_size) // int(
300 301 302
                    os.environ.get('CPU_NUM', 1))
        return single_card_bs

R
ruri 已提交
303 304
    def _reader_creator(self,
                        settings,
305 306 307 308 309 310 311
                        file_list,
                        mode,
                        shuffle=False,
                        color_jitter=False,
                        rotate=False,
                        data_dir=None):
        num_trainers = int(os.environ.get('PADDLE_TRAINERS_NUM', 1))
312

313
        batch_size = self._get_single_card_bs(settings, mode)
R
ruri 已提交
314

315 316 317 318 319
        def reader():
            def read_file_list():
                with open(file_list) as flist:
                    full_lines = [line.strip() for line in flist]
                    if mode != "test" and len(full_lines) < settings.batch_size:
320
                        logger.error(
321
                            "Error: The number of the whole data ({}) is smaller than the batch_size ({}), and drop_last is turnning on, so nothing  will feed in program, Terminated now. Please reset batch_size to a smaller number or feed more data!".
R
ruri 已提交
322
                            format(len(full_lines), settings.batch_size))
323 324 325
                        os._exit(1)
                    if num_trainers > 1 and mode == "train":
                        assert self.shuffle_seed is not None, "multiprocess train, shuffle seed must be set!"
R
ruri 已提交
326 327
                        np.random.RandomState(self.shuffle_seed).shuffle(
                            full_lines)
328
                    elif shuffle:
329
                        if not settings.enable_ce or not settings.same_feed:
R
ruri 已提交
330
                            np.random.shuffle(full_lines)
331 332

                batch_data = []
333
                if (mode == "train" or mode == "val") and settings.same_feed:
R
ruri 已提交
334
                    temp_file = full_lines[0]
335 336
                    logger.info("Same images({},nums:{}) will feed in the net".
                                format(str(temp_file), settings.same_feed))
R
ruri 已提交
337 338 339
                    full_lines = []
                    for i in range(settings.same_feed):
                        full_lines.append(temp_file)
340

341 342 343 344 345 346 347 348 349 350 351 352
                for line in full_lines:
                    img_path, label = line.split()
                    img_path = os.path.join(data_dir, img_path)
                    batch_data.append([img_path, int(label)])
                    if len(batch_data) == batch_size:
                        if mode == 'train' or mode == 'val' or mode == 'test':
                            yield batch_data
                        batch_data = []

            return read_file_list

        data_reader = reader()
353

354 355 356 357 358
        if mode == 'train' and num_trainers > 1:
            assert self.shuffle_seed is not None, \
                "If num_trainers > 1, the shuffle_seed must be set, because " \
                "the order of batch data generated by reader " \
                "must be the same in the respective processes."
R
ruri 已提交
359 360
            data_reader = paddle.fluid.contrib.reader.distributed_batch_reader(
                data_reader)
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

        mapper = functools.partial(
            process_batch_data,
            settings=settings,
            mode=mode,
            color_jitter=color_jitter,
            rotate=rotate)

        return fluid.io.xmap_readers(
            mapper,
            data_reader,
            settings.reader_thread,
            settings.reader_buf_size,
            order=False)

    def train(self, settings):
        """Create a reader for trainning

        Args:
            settings: arguments

        Returns:
            train reader
        """
        file_list = os.path.join(settings.data_dir, 'train_list.txt')
        assert os.path.isfile(
            file_list), "{} doesn't exist, please check data list path".format(
                file_list)

        if 'use_aa' in settings and settings.use_aa:
            global policy
            policy = ImageNetPolicy()

        reader = self._reader_creator(
            settings,
            file_list,
            'train',
            shuffle=True,
            color_jitter=False,
            rotate=False,
            data_dir=settings.data_dir)

        if settings.use_mixup == True:
            reader = create_mixup_reader(settings, reader)
            reader = fluid.io.batch(
                reader,
R
ruri 已提交
407 408
                batch_size=int(settings.batch_size /
                               paddle.fluid.core.get_cuda_device_count()),
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
                drop_last=True)
        return reader

    def val(self, settings):
        """Create a reader for eval

        Args:
            settings: arguments

        Returns:
            eval reader
        """

        file_list = os.path.join(settings.data_dir, 'val_list.txt')
        assert os.path.isfile(
            file_list), "{} doesn't exist, please check data list path".format(
                file_list)
        return self._reader_creator(
R
ruri 已提交
427 428 429 430 431
            settings,
            file_list,
            'val',
            shuffle=False,
            data_dir=settings.data_dir)
432 433 434 435 436 437 438 439 440 441

    def test(self, settings):
        """Create a reader for testing

        Args:
            settings: arguments

        Returns:
            test reader
        """
442 443 444 445 446 447 448 449 450 451 452 453 454 455
        file_list = ".tmp.txt"
        imgType_list = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff'}
        with open(file_list, "w") as fout:
            if settings.image_path:
                fout.write(settings.image_path + " 0" + "\n")
                settings.batch_size = 1
                settings.data_dir = ""
            else:
                tmp_file_list = os.listdir(settings.data_dir)
                for file_name in tmp_file_list:
                    file_path = os.path.join(settings.data_dir, file_name)
                    if imghdr.what(file_path) not in imgType_list:
                        continue
                    fout.write(file_name + " 0" + "\n")
456
        return self._reader_creator(
R
ruri 已提交
457 458 459 460 461
            settings,
            file_list,
            'test',
            shuffle=False,
            data_dir=settings.data_dir)