该生成器函数有什么作用?

时间:2019-06-17 15:40:16

标签: python function while-loop generator

我不明白这部分代码是什么意思? 我认为它可以改组某些图片,而不是生成矩阵。

我不了解此代码从#populate当前批次中产生的作用。周期中的“ i”和“ t”是什么意思?

def generator(batch_size, datapath):
    from random import shuffle  

    target = glob.glob(datapath + '*.png')  

    n_samples = len(target) 
    n_batches = n_samples // batch_size
    b = n_batches

    while True:

        if b == n_batches: 
            shuffle(target)
            b = 0
            print("epoch finished - " + datapath)

        # initialize current batch
        batch_features = np.zeros((batch_size, LEFT, RIGHT, 3))  
        batch_labels = np.zeros((batch_size, 2)) 
        target_b = target[b * batch_size:(b + 1) * batch_size]  

        # populate current batch
        for i, t in enumerate(target_b):
            batch_features[i, :, :, :] = imageio.imread(t)[:, :, :3]  

            batch_labels[i, :] = np.array([1, 0]) if "_avalanche_" in t else np.array([0, 1])

        b += 1

        yield batch_features, batch_labels

0 个答案:

没有答案