Skip to content

Instantly share code, notes, and snippets.

@collinhundley
Created August 20, 2019 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collinhundley/ce44603ebfc8d807e2030e0d890c41de to your computer and use it in GitHub Desktop.
Save collinhundley/ce44603ebfc8d807e2030e0d890c41de to your computer and use it in GitHub Desktop.
Keras CNN model for character-based OCR
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, Activation
from keras import losses, optimizers, activations
# Create model
model = Sequential()
# Convolution 1 (28x28 input)
model.add(Conv2D(
64,
kernel_size=5,
input_shape=(28, 28, 1),
activation='relu'
))
# Max pooling 1
model.add(MaxPooling2D(
pool_size=2
))
# Convolution 2
model.add(Conv2D(
96,
kernel_size=4,
activation='relu'
))
# Max pooling 2
model.add(MaxPooling2D(
pool_size=2
))
# Convolution 3
model.add(Conv2D(
128,
kernel_size=3,
activation='relu'
))
# Max pooling 3
model.add(MaxPooling2D(
pool_size=2
))
# Flatten
model.add(Flatten())
# Fully connected 1
model.add(Dense(
512,
activation='relu'
))
# Dropout
model.add(Dropout(
0.2
))
# Fully connected 2
# (79 classifications)
model.add(Dense(
79,
activation='softmax'
))
# Compile model
model.compile(
loss=losses.categorical_crossentropy,
optimizer=optimizers.Adadelta(),
metrics=['accuracy']
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment