首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在keras中使用albumentations记录数据

在keras中使用albumentations记录数据
EN

Stack Overflow用户
提问于 2021-05-03 12:38:38
回答 3查看 351关注 0票数 0

我正在尝试使用tutorial训练一个用于图像分类模型的keras ResNet50模型。我不想使用内置的数据生成器,而是使用albumentations库进行增强。

代码语言:javascript
复制
from albumentations import Compose
transforms = Compose([HorizontalFlip()])

我已经读了一些文章,但我不知道如何实现albumentations。

我应该修改哪一行代码来实现albumentations。

我在删除不必要的行后重现下面的代码。

代码语言:javascript
复制
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]
)
EN

回答 3

Stack Overflow用户

发布于 2021-05-03 13:40:51

我认为您可以使用ImageDataGenerator preprocessing_function来完成此操作。该函数应将单个图像作为输入,并返回图像。所以在你的情况下。

代码语言:javascript
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2021-12-06 10:07:30

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

代码语言:javascript
复制
def preprocessing_function(x):
    preprocessed_x = preprocess_input(x)
    transformed_image = transforms(image=preprocessed_x)['image']
    return transformed_image

ImageDataGenerator(preprocessing_function = preprocessing_function)
票数 1
EN

Stack Overflow用户

发布于 2021-05-03 14:39:17

这就是(IMO)使用内置数据生成器(ImageDataGenerator)时可能遇到的限制或losing the flexibility。您应该实现自己的自定义数据生成器。

查看这个内核:[TF.Keras]: SOTA Augmentation in Sequence Generator,我们已经展示了如何在自定义生成器中使用albumentationcutmixmixupfmix类型的高级增强。下面是如何在自定义数据生成器中使用albumentaiton的基本方法。

代码语言:javascript
复制
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.)
代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67363539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档