provider.py 1.7 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15
import io, os
D
dangqingqing 已提交
16 17 18 19
import random
import numpy as np
from paddle.trainer.PyDataProvider2 import *

20

D
dangqingqing 已提交
21
def initHook(settings, height, width, color, num_class, **kwargs):
22 23 24 25
    settings.height = height
    settings.width = width
    settings.color = color
    settings.num_class = num_class
D
dangqingqing 已提交
26 27 28 29
    if settings.color:
        settings.data_size = settings.height * settings.width * 3
    else:
        settings.data_size = settings.height * settings.width
T
tensor-tang 已提交
30
    settings.is_infer = kwargs.get('is_infer', False)
31
    settings.num_samples = kwargs.get('num_samples', 2560)
T
tensor-tang 已提交
32 33 34 35
    if settings.is_infer:
        settings.slots = [dense_vector(settings.data_size)]
    else:
        settings.slots = [dense_vector(settings.data_size), integer_value(1)]
D
dangqingqing 已提交
36

37 38 39

@provider(
    init_hook=initHook, min_pool_size=-1, cache=CacheType.CACHE_PASS_IN_MEM)
D
dangqingqing 已提交
40
def process(settings, file_list):
41
    for i in xrange(settings.num_samples):
42
        img = np.random.rand(1, settings.data_size).reshape(-1, 1).flatten()
T
tensor-tang 已提交
43 44 45 46 47
        if settings.is_infer:
            yield img.astype('float32')
        else:
            lab = random.randint(0, settings.num_class - 1)
            yield img.astype('float32'), int(lab)