provider.py 1.1 KB
Newer Older
1
import io, os
D
dangqingqing 已提交
2 3 4 5
import random
import numpy as np
from paddle.trainer.PyDataProvider2 import *

6

D
dangqingqing 已提交
7
def initHook(settings, height, width, color, num_class, **kwargs):
8 9 10 11
    settings.height = height
    settings.width = width
    settings.color = color
    settings.num_class = num_class
D
dangqingqing 已提交
12 13 14 15
    if settings.color:
        settings.data_size = settings.height * settings.width * 3
    else:
        settings.data_size = settings.height * settings.width
T
tensor-tang 已提交
16 17 18 19 20
    settings.is_infer = kwargs.get('is_infer', False)
    if settings.is_infer:
        settings.slots = [dense_vector(settings.data_size)]
    else:
        settings.slots = [dense_vector(settings.data_size), integer_value(1)]
D
dangqingqing 已提交
21

22 23 24

@provider(
    init_hook=initHook, min_pool_size=-1, cache=CacheType.CACHE_PASS_IN_MEM)
D
dangqingqing 已提交
25
def process(settings, file_list):
T
tensor-tang 已提交
26
    for i in xrange(2560 if settings.is_infer else 1024):
27
        img = np.random.rand(1, settings.data_size).reshape(-1, 1).flatten()
T
tensor-tang 已提交
28 29 30 31 32
        if settings.is_infer:
            yield img.astype('float32')
        else:
            lab = random.randint(0, settings.num_class - 1)
            yield img.astype('float32'), int(lab)