3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dataaugmentation手法のmixupをkerasでオリジナルデータセットからやってみた

Posted at

##準備
まずこちらからgitcloneします
https://github.com/yu4u/mixup-generator
##データセットの準備
オリジナルデータセットの場合は、一度データを読み込み行列形式へ変換します

###読み込み

read_image.py
img = img_to_array(load_img("{0}/{1}".format(input_dir1, filename1),target_size=(224,224)))

###np.asarray(X)で行列化

###画像情報とクラス情報を持つリストをそれぞれ作成しそのリストにappendしていく

read_image.py
X.append(img)
Y.append(0)#0番目のクラスの場合0(4クラスだとしたら0,1,2,3とかクラスごとにappendする)

###yu4uさんのmixup generatorを使う

read_image.py
generator1 = MixupGenerator(X_train, y_train, batch_size=64, alpha=0.5 )()

細かい設定部分や使い方は
https://github.com/yu4u/mixup-generator
を見てください

###画像の保存
できた画像を適当な形式で保存します

read_image.py
x, y = next(generator1)
for i in range(64):
	print(x[i].shape)
	img = x[i].astype(np.uint8)
interpolation="nearest")
	plt.imsave('image'+str(i)+'.jpg', img) 

###できた画像
figure_1-3.png

##全体のコードはこちら

mixup.py
# coding:utf-8

# coding:utf-8

import keras
from keras.utils import np_utils
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.preprocessing.image import array_to_img, img_to_array, list_pictures, load_img
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import os
from keras.preprocessing.image import ImageDataGenerator
from mixup_generator import MixupGenerator
import matplotlib.pyplot as plt
import numpy as np
import keras
from keras.datasets import cifar10

# フォルダの中にある画像を順次読み込む
# カテゴリーは0から始める
HOME_DIR1 = "Apple___Apple_scab"
HOME_DIR2 = "Apple___Black_rot"
HOME_DIR3 = "Apple___Cedar_apple_rust"
HOME_DIR4 = "Apple___healthy"
X = []
Y = []

if __name__ == '__main__':

    # 入力画像ファイルの格納ディレクトリを指定
    input_dir1 = '{0}'.format(HOME_DIR1)
    input_dir2 = '{0}'.format(HOME_DIR2)
    input_dir3 = '{0}'.format(HOME_DIR3)
    input_dir4 = '{0}'.format(HOME_DIR4)


    # 入力画像ディレクトリ内のファイル一覧を取得
    filenames1 = os.listdir(input_dir1)
    filenames2 = os.listdir(input_dir2)
    filenames3 = os.listdir(input_dir3)
    filenames4 = os.listdir(input_dir4)

for filename1 in filenames1:
        print(filename1)

        # ファイルを読み込み
        img = img_to_array(load_img("{0}/{1}".format(input_dir1, filename1),target_size=(224,224)))
        X.append(img)
        Y.append(0)

for filename2 in filenames2:
        print(filename2)

        # ファイルを読み込み
        img = img_to_array(load_img("{0}/{1}".format(input_dir2, filename2),target_size=(224,224)))
        X.append(img)
        Y.append(1)

for filename3 in filenames3:
        print(filename3)

        # ファイルを読み込み
        img = img_to_array(load_img("{0}/{1}".format(input_dir3, filename3),target_size=(224,224)))
        X.append(img)
        Y.append(2)

for filename4 in filenames4:
        print(filename4)

        # ファイルを読み込み
        img = img_to_array(load_img("{0}/{1}".format(input_dir4, filename4),target_size=(224,224)))
        X.append(img)
        Y.append(3)



# arrayに変換
X = np.asarray(X)
Y = np.asarray(Y)

# クラスの形式を変換
Y = np_utils.to_categorical(Y, 4)

print("x:",X.shape)
print("y:",Y.shape)

# 学習用データとテストデータ
X_train= X
y_train= Y
#X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0, random_state=None)

generator1 = MixupGenerator(X_train, y_train, batch_size=64, alpha=0.5 )()

x, y = next(generator1)

print("x",x.dtype)
print("y",y.dtype)


for i in range(64):
	print(x[i].shape)
	img = x[i].astype(np.uint8)

	plt.imsave('image-HEL'+str(i)+'.jpg', img) 


plt.show()


##原論文
https://arxiv.org/pdf/1710.09412.pdf

##参考にさせてもらったもの
https://github.com/yu4u/mixup-generator

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?