问题描述
我正在尝试使用 tuorial 为图像分类模型训练 keras resnet50
模型。安装 inbult 数据生成器后,我想使用 albumentations 进行扩充。
from albumentations import Compose
transforms = Compose([HorizontalFlip()])
我读了几篇文章,但我不知道如何实现相册。
我应该修改哪一行代码来实现相册。
删除不必要的行后,我正在复制下面的代码。
NUM_CLASSES = 2
CHANNELS = 3
IMAGE_RESIZE = 224
resnet50_POOLING_AVERAGE = 'avg'
DENSE_LAYER_ACTIVATION = 'softmax'
OBJECTIVE_FUNCTION = 'categorical_crossentropy'
LOSS_METRICS = ['accuracy']
NUM_EPOCHS = 300
EARLY_STOP_PATIENCE = 20
STEPS_PER_EPOCH_TRAINING = 20
STEPS_PER_EPOCH_VALIDATION = 20
BATCH_SIZE_TRAINING = 10
BATCH_SIZE_VALIDATION = 10
# %% ---------------------------------------------------------------------
Trainingdata_directory = 'C:/datafolder/Train'
Validationdata_directory = 'C:/datafolder/Validation'
ModelCheckpointPath = 'C:/datafolder/resnet50_Weights.hdf5'
# %% ---------------------------------------------------------------------
from albumentations import Compose
import tensorflow as tf
from tensorflow.keras.applications import resnet50
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# %% ---------------------------------------------------------------------
model = Sequential()
model.add(resnet50(include_top = False,pooling = resnet50_POOLING_AVERAGE,weights = 'imagenet'))
model.add(Dense(NUM_CLASSES,activation = DENSE_LAYER_ACTIVATION))
model.layers[0].trainable = False
from tensorflow.keras import optimizers
sgd = optimizers.SGD(lr = 0.001,decay = 1e-6,momentum = 0.9,nesterov = True)
model.compile(optimizer = sgd,loss = OBJECTIVE_FUNCTION,metrics = LOSS_METRICS)
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
image_size = IMAGE_RESIZE
data_generator = ImageDataGenerator(preprocessing_function = preprocess_input)
train_generator = data_generator.flow_from_directory(Trainingdata_directory,target_size = (image_size,image_size),batch_size = BATCH_SIZE_TRAINING,class_mode = 'categorical')
validation_generator = data_generator.flow_from_directory(Validationdata_directory,batch_size = BATCH_SIZE_VALIDATION,class_mode = 'categorical')
from tensorflow.python.keras.callbacks import EarlyStopping,ModelCheckpoint
cb_early_stopper = EarlyStopping(monitor = 'val_loss',patience = EARLY_STOP_PATIENCE)
cb_checkpointer = ModelCheckpoint(filepath = ModelCheckpointPath,monitor = 'val_loss',save_best_only = True,mode = 'auto')
fit_history = model.fit_generator(
train_generator,steps_per_epoch=STEPS_PER_EPOCH_TRAINING,epochs = NUM_EPOCHS,validation_data=validation_generator,validation_steps=STEPS_PER_EPOCH_VALIDATION,callbacks=[cb_checkpointer,cb_early_stopper]
)
解决方法
我认为您可以使用 ImageDataGenerator preprocessing_function 来完成。该函数应将单个图像作为输入并返回图像。所以在你的情况下。
def augmentor (img)
# place you code here do to the albumentations transforms
# your code should result in a single transformed image I called aug_img
return aug_img/127.5-1 #scales the pixels between -1 and +1 which it what preprocees_input does
data_generator = ImageDataGenerator(preprocessing_function = augmentor)
,
这是 (IMO) 使用内置数据生成器 (ImageDataGenerator
) 可能遇到的限制或 losing the flexibility。您应该实现自己的自定义数据生成器。
检查这个内核:[TF.Keras]: SOTA Augmentation in Sequence Generator,我们已经展示了如何使用 albumentation
、cutmix
、mixup
和 fmix
将高级增强输入到自定义生成器。以下是如何在自定义数据生成器中使用 albumentaiton
的基本方法。
import albumentations as A
# For Training
def albu_transforms_train(data_resize):
return A.Compose([
A.ToFloat(),A.Resize(data_resize,data_resize),A. [.....what ever......]
],p=1.)
class Generator(tf.keras.utils.Sequence):
def __getitem__(self,index):
...........
Data = np.empty((self.batch_size,*self.dim))
Target = np.empty((self.batch_size,5),dtype = np.float32)
for i,k in enumerate(idx):
# load the image file using cv2
image = cv2.imread(self.img_path + self.data['image_id'][k])
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
# call augmentor / albumentation
res = self.augment(image=image)
image = res['image']
# assign
Data[i,:,:] = image
Target[i,:] = self.label.loc[k,:].values
return Data,Target
# call the generator
check_gens = Generator(....,transform = albu_transforms_train(128))