使用 TensorFlow-Keras API 进行数据增强

以下代码允许训练集的图像在每个时期结束时旋转 90°。


from skimage.io import imread

from skimage.transform import resize, rotate

import numpy as np


import tensorflow as tf

from tensorflow import keras

from tensorflow.keras import layers

from keras.utils import Sequence 

from keras.models import Sequential

from keras.layers import Conv2D, Activation, Flatten, Dense


# Model architecture  (dummy)

model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=(15, 15, 4)))

model.add(Activation('relu'))

model.add(Flatten())

modl.add(Dense(1))

model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',

              optimizer='rmsprop',

              metrics=['accuracy'])


# Data iterator 

class CIFAR10Sequence(Sequence):

    def __init__(self, filenames, labels, batch_size):

        self.filenames, self.labels = filenames, labels

        self.batch_size = batch_size

        self.angles = [0,90,180,270]

        self.current_angle_idx = 0


    # Method to loop throught the available angles

    def change_angle(self):

      self.current_angle_idx += 1

      if self.current_angle_idx >= len(self.angles):

        self.current_angle_idx = 0

  

    def __len__(self):

        return int(np.ceil(len(self.filenames) / float(self.batch_size)))


    # read, resize and rotate the image and return a batch of images

    def __getitem__(self, idx):

        angle = self.angles[self.current_angle_idx]

        print (f"Rotating Angle: {angle}")


        batch_x = self.filenames[idx * self.batch_size:(idx + 1) * self.batch_size]

        batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]

        return np.array([

            rotate(resize(imread(filename), (15, 15)), angle)

               for filename in batch_x]), np.array(batch_y)

如何修改代码以便在每个纪元期间发生图像的旋转?

换句话说,我如何编写一个在纪元“结束”时运行的回调,该回调会更改角度值并继续在同一纪元上进行训练(而不更改到下一个纪元)?


跃然一笑
浏览 1540回答 2
2回答

慕尼黑8549860

由于您有一个自定义序列生成器,您可以创建一个在纪元开始或结束时运行的函数。您可以在其中放置代码来修改图像。文档位于[此处。][1]Epoch-level methods (training only)on_epoch_begin(self, epoch, logs=None)Called at the beginning of an epoch during training.on_epoch_end(self, epoch, logs=None)Called at the end of an epoch during training.  [1]: https://keras.io/guides/writing_your_own_callbacks/

UYOU

没有必要CustomCallback为此目的创建一个;最后,您希望在训练期间进行增强。解决方案是应用旋转操作的概率# read, resize and rotate the image and return a batch of imagesdef __getitem__(self, idx):    angle = self.angles[self.current_angle_idx]    print(f"Rotating Angle: {angle}")    batch_x = self.filenames[idx * self.batch_size:(idx + 1) * self.batch_size]    batch_y = self.labels[idx * self.batch_size:(idx + 1) * self.batch_size]    #These new lines (say we augment with probability > 0.5)    #Number between 0 and 1    images = []    for filename in batch_x:        probability = random.random()        apply_rotate = probability > 0.5        if apply_rotate:            images.append(rotate(resize(imread(filename), (15, 15)), angle))        else:            images.append(resize(imread(filename), (15, 15)))    return np.array(images), np.array(batch_y)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python