image_provider.py 3.2 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# 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.

import io
import random

import paddle.utils.image_util as image_util
from paddle.trainer.PyDataProvider2 import *


#
# {'img_size': 32,
Y
Yu Yang 已提交
24
# 'settings': a global object,
Z
zhangjinchao01 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# 'color': True,
# 'mean_img_size': 32,
# 'meta': './data/cifar-out/batches/batches.meta',
# 'num_classes': 10,
# 'file_list': ('./data/cifar-out/batches/train_batch_000',),
# 'use_jpeg': True}
def hook(settings, img_size, mean_img_size, num_classes, color, meta, use_jpeg,
         is_train, **kwargs):
    settings.mean_img_size = mean_img_size
    settings.img_size = img_size
    settings.num_classes = num_classes
    settings.color = color
    settings.is_train = is_train

    if settings.color:
        settings.img_raw_size = settings.img_size * settings.img_size * 3
    else:
        settings.img_raw_size = settings.img_size * settings.img_size

    settings.meta_path = meta
    settings.use_jpeg = use_jpeg

    settings.img_mean = image_util.load_meta(settings.meta_path,
                                             settings.mean_img_size,
49
                                             settings.img_size, settings.color)
Z
zhangjinchao01 已提交
50 51 52

    settings.logger.info('Image size: %s', settings.img_size)
    settings.logger.info('Meta path: %s', settings.meta_path)
Y
Yu Yang 已提交
53 54 55 56
    settings.input_types = {
        'image': dense_vector(settings.img_raw_size),
        'label': integer_value(settings.num_classes)
    }
Z
zhangjinchao01 已提交
57 58 59 60

    settings.logger.info('DataProvider Initialization finished')


61 62
@provider(init_hook=hook, min_pool_size=0)
def processData(settings, file_list):
Z
zhangjinchao01 已提交
63 64 65
    """
    The main function for loading data.
    Load the batch, iterate all the images and labels in this batch.
66
    file_list: the batch file list.
Z
zhangjinchao01 已提交
67
    """
68 69 70 71 72 73 74 75 76 77 78 79 80 81
    with open(file_list, 'r') as fdata:
        lines = [line.strip() for line in fdata]
        random.shuffle(lines)
        for file_name in lines:
            with io.open(file_name.strip(), 'rb') as file:
                data = cPickle.load(file)
                indexes = list(range(len(data['images'])))
                if settings.is_train:
                    random.shuffle(indexes)
                for i in indexes:
                    if settings.use_jpeg == 1:
                        img = image_util.decode_jpeg(data['images'][i])
                    else:
                        img = data['images'][i]
82 83 84
                    img_feat = image_util.preprocess_img(
                        img, settings.img_mean, settings.img_size,
                        settings.is_train, settings.color)
85
                    label = data['labels'][i]
Y
Yu Yang 已提交
86 87 88 89
                    yield {
                        'image': img_feat.astype('float32'),
                        'label': int(label)
                    }