Image Data augmentation

park paul·2021년 8월 27일
0

Intro

Image data augmentation는 Data set안에서 변형된 버전으로 생성하여 학습 데이터 셋의 사이즈를 인위적인 확장을 통해 쓰여 질 수 있다.

Keras's ImageDataGenerator class

Keras에서 Image DataGenerator 클래스를 통해 Image data augmentation을 사용할 수 있다.

  • Image data augmentation is used to expand the training dataset in order to improve the performance and ability of the model to generalize.
  • Image data augmentation is supported in the Keras deep learning library via the ImageDataGenerator class.
  • How to use shift, flip, brightness, and zoom image data augmentation.

사용법 파트

  1. Image Data Augmentation
  2. Sample Image
  3. Image Augmentation With ImageDataGenerator
  4. Horizontal and Vertical Shift Augmentation
  5. Horizontal and Vertical Flip Augmentation
  6. Random Rotation Augmentation
  7. Random Brightness Augmentation
  8. Random Zoom Augmentation

Code

	# create data generator
    datagen = ImageDataGenerator()
  
  • flow()를 써서 메모리에 로드된 이미지 데이터 셋을 만들 수 있다.

      # load image dataset
      X, y = ...
      # Create iterator
      iter = datagen.flow(X, y)
  • 아니면 directory에서 이미지 데이터를 가져와 iterator를 만둘 수 있다.

        # create iterator
        iter = datagen.flow_from_diretory(X, y, ....)

    iterator가 생성되면 fit_generator()를 호출하여 신경망 모델을 훈련하는데 쓸 수 있다.

  • steps_per_epoch의 인수는 예를 들어, 원본 데이터 셋에 10,000개의 이미지가 있고 배치 크기가 32인 경우 증강 데이터에 모델을 맞출 때 합리적인 값은 ceil(10,000/32) 또는 313개의 배치가 될 수 있다.

    	# define model
        model = ...
        # fit model on the augmented dataset
        model.fit_generator(it, steps_per_epoch=313,..)

    데이터 셋의 이미지는 직접 사용되지 않고, 대신 모델에 증강 이미지만 제공된다. 증강이 무작위로 수행되기 때문에 수정된 이미지와 원본 이미지의 가까운 facsimile를 생성하여 훈련 중에 사용할 수 있다.

What for

  • Horizontal and Vertical Shift Augmentation
    example-

    	img = load_img('something')
        data = img_to_array(img) # convert to numpy array
        samples = expand_dims(data, 0) #expand dimension to one sample
        datagen =  ImageDataGenerator(width_shift_range=[-200,200])
        #prepare iterator
        it = datagen.flow(samples, batch_size=1)
        #generage samples and plot
        for i in range(9):
        	# define subplot
            pyplot.subplot(330 + 1 + i)'
            # generate batch of images
            batch = it.next()
            #convert to ussigned itegers for viewing
            image = batch[0].astype('unit8')
            #plot raw pixel data
            pyplot.imshow(image)
        # show the figure
        pyplot.show()

    Like this (horizontal shift)

  • Random Rotation Augmentation
    0도에서 360도 사이의 값으로 랜덤하게 회전된 이미지를 받을 수 있다.

    	datagen = ImageDataGenerator(rotation_range=90)

  • Random Brightness Augmentation
    랜덤하게 밝기와 어둠을 이미지에 적용하여 준다.

    		datagen = ImageDataGenerator(brightness_range = [0.2, 1.0]

  • Random Zoom Augmentation
    랜덤하게 새로운 픽셀을 이미지 주변에 값을 더하거나 픽셀 값에 저마다 보충하여 이미지에 적용한다.

    	datagen = ImageDataGenerator(zoom_range =[0.5, 1.0])

Ref)https://machinelearningmastery.com/how-to-configure-image-data-augmentation-when-training-deep-learning-neural-networks/

profile
Innovation is mine

0개의 댓글