为了账号安全,请及时绑定邮箱和手机立即绑定

在机器学习中混洗如何与 ImageDataGenerator 一起工作?

在机器学习中混洗如何与 ImageDataGenerator 一起工作?

三国纷争 2021-05-30 16:09:48
我正在使用Inception V3创建图像分类模型,并且有两个类。我将数据集和标签分为两个numpy数组,数据分别以trainX和testY作为图像,trainY和testY作为对应的标签。data = np.array(data, dtype="float")/255.0labels = np.array(labels,dtype ="uint8")(trainX, testX, trainY, testY) = train_test_split(                                data,labels,                                 test_size=0.2,                                 random_state=42) train_datagen = keras.preprocessing.image.ImageDataGenerator(          zoom_range = 0.1,          width_shift_range = 0.2,           height_shift_range = 0.2,          horizontal_flip = True,          fill_mode ='nearest') val_datagen = keras.preprocessing.image.ImageDataGenerator()train_generator = train_datagen.flow(        trainX,         trainY,        batch_size=batch_size,        shuffle=True)validation_generator = val_datagen.flow(                testX,                testY,                batch_size=batch_size) 当我使用 ImageDataGenerator shuffle train_generator 时,图像是否仍然匹配相应的标签?验证数据集也应该改组吗?
查看完整描述

1 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

是的,图像仍会匹配相应的标签,因此您可以安全地设置shuffle为True。在引擎盖下,其工作方式如下。调用.flow()的ImageDataGenerator将返回一个NumpyArrayIterator对象,它实现了洗牌的指标以下逻辑:


def _set_index_array(self):

    self.index_array = np.arange(self.n)

    if self.shuffle: # if shuffle==True, shuffle the indices

        self.index_array = np.random.permutation(self.n) 

self.index_array然后用于生成图像(x)和标签(y)(为了可读性而截断了代码):


def _get_batches_of_transformed_samples(self, index_array):

    batch_x = np.zeros(tuple([len(index_array)] + list(self.x.shape)[1:]),

                       dtype=self.dtype)

    # use index_array to get the x's

    for i, j in enumerate(index_array):

        x = self.x[j]

        ... # data augmentation is done here

        batch_x[i] = x

     ...

     # use the same index_array to fetch the labels

     output += (self.y[index_array],)


    return output

自己检查源代码,它可能比您想象的要容易理解。


改组验证数据应该没什么大不了的。改组的主要目的是在训练过程中引入一些额外的随机性。


查看完整回答
反对 回复 2021-06-01
  • 1 回答
  • 0 关注
  • 173 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号