provider.py 841 字节
Newer Older
1
import io, os
2 3 4 5
import random
import numpy as np
from paddle.trainer.PyDataProvider2 import *

6

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
12 13 14 15 16 17 18
    if settings.color:
        settings.data_size = settings.height * settings.width * 3
    else:
        settings.data_size = settings.height * settings.width

    settings.slots = [dense_vector(settings.data_size), integer_value(1)]

19 20 21

@provider(
    init_hook=initHook, min_pool_size=-1, cache=CacheType.CACHE_PASS_IN_MEM)
22
def process(settings, file_list):
23 24 25 26
    for i in xrange(1024):
        img = np.random.rand(1, settings.data_size).reshape(-1, 1).flatten()
        lab = random.randint(0, settings.num_class)
        yield img.astype('float32'), int(lab)