1

我正在尝试使用教程ResNet50为图像分类模型训练 keras 模型。而不是内置的数据生成器,我想使用albumentations库进行扩充。

from albumentations import Compose
transforms = Compose([HorizontalFlip()])

我已经阅读了几篇文章,但我无法弄清楚如何实现allementations。

我应该修改哪一行代码来实现标注。
删除不必要的行后,我正在复制下面的代码。

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,
        target_size = (image_size, image_size),
        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]
)
4

3 回答 3

1

我认为您可以使用 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)
于 2021-05-03T05:40:51.313 回答
1

您可以将其包含在传递给 ImageDataGenerator 的预处理函数中:

def preprocessing_function(x):
    preprocessed_x = preprocess_input(x)
    transformed_image = transforms(image=preprocessed_x)['image']
    return transformed_image

ImageDataGenerator(preprocessing_function = preprocessing_function)
于 2021-12-06T10:07:29.967 回答
0

这是 ( IMO )使用内置数据生成器 () 可能遇到的限制或失去灵活性ImageDataGenerator。您应该实现自己的自定义数据生成器。

检查这个内核:[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))
于 2021-05-03T06:39:17.367 回答