※完全版は、有料Noteにて販売しております。
https://note.com/academicagent/n/nf046848f5c04
MNISTの画像に、数字記号の『-』を加えたモデルを作成してみました。
mk_model.py
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.optimizers import Adam
from keras.utils import np_utils
from keras import backend as Keras
from PIL import Image, ImageFilter
import numpy as np
import numpy
import os
import matplotlib.pyplot as plt
import cv2
import keras
def load_images_to_data(image_label, image_directory, features_data, label_data):
####関数の中身は有料Noteで公開いたします!!####
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Reshaping to format which CNN expects (batch, height, width, channels)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1).astype('float32')
X_train, y_train = load_images_to_data('10', '-', X_train, y_train)
X_test, y_test = load_images_to_data('10', '-', X_test, y_test)
# normalize inputs from 0-255 to 0-1
X_train/=255
X_test/=255
# one hot encode
number_of_classes = 11
y_train = np_utils.to_categorical(y_train, number_of_classes)
y_test = np_utils.to_categorical(y_test, number_of_classes)
# create model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(X_train.shape[1], X_train.shape[2], 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=7, batch_size=200)
model.save('my_model.h5')
-記号の画像は、10枚だけ追加したモデルをまず簡易的に作成し、
テストも無事、完了いたしました。
※有料Noteでは、使用した画像、pygameのテストで使用したプログラムファイルも配布しております。
是非、ご購入ください。
https://note.com/academicagent/n/nf046848f5c04