每个时期使用不同的数据扩充参数

时间:2019-08-23 16:27:13

标签: python keras

我正在使用具有简单cnn模型的keras。 我想在训练中向图像添加高斯噪声。我想基于某些功能在每个时期更改噪声参数(均值和西格玛)。例如,

in epoch 1 i want to add noise with sigma=1
in epoch 2 i want to add noise with sigma=2
in epoch 3 i want to add noise with sigma=3
# note-mean is always zero

以此类推...

解决问题的低效率方法是使用for循环,在每个时期后保存并加载模式,并调用增强功能。 更有效的方法是使用自定义回调或生成器,但我没有成功

低效方式:

total_num_of_epochs=100
def sigma_function(current_epoch):
     sigma_fun=current_epoch/total_num_of_epochs
     return sigma_fun

for i in range(total_num_of_epochs):
    x_train += np.random.normal(mean=0,sigma=sigma_fun(i),size=x_train shape) # augment x_train based on sigma_function and current epochs

    model.compile(...)
    model.fit(x_train ,y_train...initial_epoch=i,epochs=i+1) #load the model 
    # from previous loop
    save model
    load model for next loop

所需的结果(我尝试使用ImageDataGenerator,但也许可以回调):

def sigma_function(current_epoch):
     sigma_fun=current_epoch/total_num_of_epochs
     return sigma_fun

datagen=ImageDataGenerator(preprocessing_function=sigma_function)
datagen.fit(x_train)


model.fit_generator(... don't know what to put here)

编辑

根据丹尼尔·莫勒(DanielMöller)提出的解决方案,我尝试了这种方法,但仍然出现错误

sigmaParam = 1

def apply_sigma(x):
    return x + np.random.normal(mean=0,scale=sigmaParam,size=(3,32,32))

imgGen = ImageDataGenerator( preprocesing_function=apply_sigma)
generator = imgGen.flow_from_directory('data/train') # folder that contains 
# only x_train and y_train 


from keras.utils import Sequence

class SigmaGenerator(Sequence):

    def __init__(self, keras_generator):
        self.keras_generator = keras_generator

    def __len__(self):
        return len(self.keras_generator)

    def __getitem__(self,i):
        return self.keras_generator[i]

    def on_epoch_end(self):
        sigmaParam += 1
        self.keras_generator.on_epoch_end()

training_generator = SigmaGenerator(generator)

model.fit_generator(training_generator,validation_data=(x_test,y_test),
                steps_per_epoch=x_train.shape[0]//batch_size,epochs=100)

我得到的错误:

process finished with exit code -1073741819 (0xC0000005)

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

sigmaParam = 1

def applySigma(x):
    return x + np.random.normal(mean=0,scale=sigmaParam,size=x.shape)

创建原始生成器:

imgGen = ImageDataGenerator(..., preprocesing_function=apply_sigma)
generator = imgGen.flow_from_directory(....)

创建一个自定义生成器以包装原始生成器,替换其on_epoch_end方法以更新sigmaParam。

from keras.utils import Sequence

class SigmaGenerator(Sequence):

    def __init__(self, keras_generator):
        self.keras_generator = keras_generator

    def __len__(self):
        return len(self.keras_generator)

    def __getitem__(self,i):
        return self.keras_generator[i]

    def on_epoch_end(self):
        sigmaParam += 1
        self.keras_generator.on_epoch_end()

training_generator = SigmaGenerator(generator)