import cv2
import pickle
import os.path
import numpy as np
from imutils import paths
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.core import Flatten, Dense
import tensorflow as tf
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
tf.config.set_logical_device_configuration(
gpus[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=5120)])
except RuntimeError as e:
print(e)
LETTER_IMAGES_FOLDER = "images"
MODEL_FILENAME = "model.hdf5"
MODEL_LABELS_FILENAME = "labels.dat"
WIGHT_SHAPE = 134
HEIGH_SHAPE = 40
data = []
labels = []
def resize_to_fit(image, width, height):
padW = int((width - image.shape[1]) / 2.0)
padH = int((height - image.shape[0]) / 2.0)
image = cv2.copyMakeBorder(image, padH, padH, padW, padW,
cv2.BORDER_REPLICATE)
image = cv2.resize(image, (width, height))
return image
for image_file in paths.list_images(LETTER_IMAGES_FOLDER):
image = cv2.imread(image_file)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = resize_to_fit(image, WIGHT_SHAPE, HEIGH_SHAPE)
image = np.expand_dims(image, axis=2)
label = image_file.split(os.path.sep)[-2]
data.append(image)
labels.append(label)
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
(X_train, X_test, Y_train, Y_test) = train_test_split(data, labels, test_size=0.25, random_state=0)
lb = LabelBinarizer().fit(Y_train)
Y_train = lb.transform(Y_train)
Y_test = lb.transform(Y_test)
with open(MODEL_LABELS_FILENAME, "wb") as f:
pickle.dump(lb, f)
early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', patience=5, verbose=1, restore_best_weights = True)
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(MODEL_FILENAME, monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)
model = Sequential()
model.add(Conv2D(20, (5, 5), padding="same", input_shape=(HEIGH_SHAPE, WIGHT_SHAPE, 1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(50, (5, 5), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(500, activation="relu"))
model.add(Dense(313, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
model.fit(X_train, Y_train, validation_data=(X_test, Y_test), batch_size=32, epochs=20, verbose=1, callbacks=[early_stopping, model_checkpoint])