Skip to content

Instantly share code, notes, and snippets.

@prafuld3
Created December 1, 2019 08:44
Show Gist options
  • Save prafuld3/0c14666034dfc712d0ae56a6b032a733 to your computer and use it in GitHub Desktop.
Save prafuld3/0c14666034dfc712d0ae56a6b032a733 to your computer and use it in GitHub Desktop.
Keras Lenet on MNIST
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
class LeNet:
@staticmethod
def build(width, height, depth, classes, weightsPath=None):
# initialize the model
model = Sequential()
# first set of CONV => RELU => POOL
model.add(Convolution2D(20, 5, 5, border_mode="same",
input_shape=(depth, height, width)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# second set of CONV => RELU => POOL
model.add(Convolution2D(50, 5, 5, border_mode="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# set of FC => RELU layers
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
# if weightsPath is specified load the weights
if weightsPath is not None:
model.load_weights(weightsPath)
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment