我正在为模型使用数据增强,并希望在训练中包含原始未增强图像以及增强图像。
到目前为止,我已经使用了以下代码:
main_dir = "____" (file directory)
train_dir = os.path.join(main_dir, 'training_set')
validation_dir = os.path.join(main_dir, 'validation_set')
test_dir = os.path.join(main_dir, 'test_set')
conv_base = VGG16(weights='imagenet',include_top=False,input_shape=(150, 150, 3))
conv_base.summary()
model = models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.summary()
conv_base.trainable = True
model.summary()
train_datagen = ImageDataGenerator(rescale=1./255,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,
shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir,target_size=(150, 150),batch_size=20,class_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_dir,target_size=(150, 150),batch_size=20,class_mode='binary')
test_generator = test_datagen.flow_from_directory(test_dir,target_size=(150, 150),batch_size=20,class_mode='binary')
model.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(lr=2e-5),metrics=['acc'])
start = time.time()
history=model.fit_generator(train_generator,steps_per_epoch=300,epochs=15,validation_data=validation_generator,validation_steps=50,verbose=2)
print("Time taken to train the MLP %.1f seconds."%(time.time()-start))
请让我知道是否有人能够提供帮助!谢谢 :)