Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
import keras
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Dense, Dropout, Activation, Flatten
from sklearn.model_selection import train_test_split
from PIL import Image
import glob
from keras.preprocessing.image import load_img, img_to_array
from keras.initializers import TruncatedNormal, Constant
from keras.layers import Input, Dropout, Flatten, Conv2D, MaxPooling2D, Dense, Activation, BatchNormalization
from keras.optimizers import SGD
# -------------------------------------------------------------------------------------
# 初期設定部
# -------------------------------------------------------------------------------------
# GrayScaleのときに1、COLORのときに3にする
COLOR_CHANNEL = 3
# 入力画像サイズ(画像サイズは正方形とする)
INPUT_IMAGE_SIZE = 224
# 訓練時のバッチサイズとエポック数
BATCH_SIZE = 32
EPOCH_NUM = 100
# 使用する訓練画像の入ったフォルダ(ルート)
TRAIN_PATH = "..\\training_dataset\\training_anime1"
# 使用する訓練画像の各クラスのフォルダ名
folder = ["000_hatsune_miku", "001_kinomoto_sakura", "002_suzumiya_haruhi",
"003_fate_testarossa", "004_takamachi_nanoha", "005_lelouch_lamperouge",
"006_akiyama_mio", "007_nagato_yuki", "008_shana", "009_hakurei_reimu"]
# CLASS数を取得する
CLASS_NUM = len(folder)
print("クラス数 : " + str(CLASS_NUM))
# -------------------------------------------------------------------------------------
# 訓練画像入力部
# -------------------------------------------------------------------------------------
# 各フォルダの画像を読み込む
v_image = []
v_label = []
for index, name in enumerate(folder):
dir = TRAIN_PATH + "\\" + name
files = glob.glob(dir + "\\*.png")
print(dir)
for i, file in enumerate(files):
if COLOR_CHANNEL == 1:
img = load_img(file, color_mode = "grayscale", target_size=(INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE))
elif COLOR_CHANNEL == 3:
img = load_img(file, color_mode = "rgb", target_size=(INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE))
array = img_to_array(img)
v_image.append(array)
v_label.append(index)
v_image = np.array(v_image)
v_label = np.array(v_label)
# imageの画素値をint型からfloat型にする
v_image = v_image.astype('float32')
# 画素値を[0~255]⇒[0~1]とする
v_image = v_image / 255.0
# 正解ラベルの形式を変換
v_label = np_utils.to_categorical(v_label, CLASS_NUM)
# 学習用データと検証用データに分割する
train_images, valid_images, train_labels, valid_labels = train_test_split(v_image, v_label, test_size=0.20)
# -------------------------------------------------------------------------------------
# モデルアーキテクチャ定義部
# -------------------------------------------------------------------------------------
def conv2d(filters, kernel_size, strides=1, bias_init=1, **kwargs):
trunc = TruncatedNormal(mean=0.0, stddev=0.01)
cnst = Constant(value=bias_init)
return Conv2D(
filters,
kernel_size,
strides=strides,
padding='same',
activation='relu',
kernel_initializer=trunc,
bias_initializer=cnst,
**kwargs
)
def dense(units, **kwargs):
trunc = TruncatedNormal(mean=0.0, stddev=0.01)
cnst = Constant(value=1)
return Dense(
units,
activation='tanh',
kernel_initializer=trunc,
bias_initializer=cnst,
**kwargs
)
def AlexNet():
model = Sequential()
# 第1畳み込み層
model.add(conv2d(96, 11, strides=(4,4), bias_init=0, input_shape=(INPUT_IMAGE_SIZE, INPUT_IMAGE_SIZE, COLOR_CHANNEL)))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(BatchNormalization())
# 第2畳み込み層
model.add(conv2d(256, 5, bias_init=1))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(BatchNormalization())
# 第3~5畳み込み層
model.add(conv2d(384, 3, bias_init=0))
model.add(conv2d(384, 3, bias_init=1))
model.add(conv2d(256, 3, bias_init=1))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(BatchNormalization())
# 全結合層
model.add(Flatten())
model.add(dense(4096))
model.add(Dropout(0.5))
model.add(dense(4096))
model.add(Dropout(0.5))
# 出力層
model.add(Dense(CLASS_NUM, activation='softmax'))
model.compile(optimizer=SGD(lr=0.01), loss='categorical_crossentropy', metrics=['accuracy'])
return model
# コンパイル
model = AlexNet()
# 訓練
history = model.fit(train_images, train_labels, batch_size=BATCH_SIZE, epochs=EPOCH_NUM)
# -------------------------------------------------------------------------------------
# 訓練実行&結果確認部
# -------------------------------------------------------------------------------------
# モデル構成の確認
model.summary()
score = model.evaluate(valid_images, valid_labels, verbose=0)
print(len(valid_images))
print('Loss:', score[0])
print('Accuracy:', score[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment