-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlenet.py
70 lines (59 loc) · 2.69 KB
/
lenet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from multiprocessing import pool
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten
from keras.layers.core import Dense
from keras import backend as K
# numChannels = depth of our input images
# imgCols = width of input image
# imgRows = height of input images
class LeNet:
@staticmethod
def build(numChannels, imgRows, imgCols, numClasses,
activation="relu", weightsPath=None):
# initialize the model
model = Sequential()
inputShape = (imgRows, imgCols, numChannels)
# Channels First = [channels][rows][cols]
# Channels Last = [rows][cols][channels]
# if we are using "channels first", update the input shape
if K.image_data_format() == "channels_first":
inputShape = (numChannels, imgRows, imgCols)
# First sert of Conv => ACTIVATION => POOL Layers
# Conv2D(filters, kernels, padding, input_shape)
model.add(Conv2D(20, 5, padding="same",
input_shape = inputShape))
model.add(Activation(activation))
# 2x2 convolution matrix that takes a step of 2 pixels
# horizontally and vertically.
model.add(MaxPooling2D(pool_size=(2, 2), striders = (2, 2)))
# Second set of CONV => ACTIVATION => POOL Layers
# Num of convolutional filters increase as layers deepen in
# network
model.add(Conv2D(50, 5, padding="same"))
model.add(Activation(activation))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Fully connected layers (Dense layers)
# Define the first FC => ACTIVATION layers
model.add(Flatten()) # Flatten last output of MaxPooling2D layer
# Represents 500 units in Dense layer
model.add(Dense(500))
model.add(Activation(activation))
# define the second FC layer
# numClasses = number of images model will learn
# Ex: Banana dataset has 3 categories to learn
model.add(Dense(numClasses))
# lastly, define the soft-max classifier\
# Returns a list of probabilities for each of the
# numClasses labels
# Class label with the largest probability will be chosen
# as final classification from network
model.add(Activation("softmax"))
# if a weights path is supplied (inicating that the model was
# pre-trained), then load the weights
if weightsPath is not None:
model.load_weights(weightsPath)
# return the constructed network architecture
return model