Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/CharClassification/net.py
3118 views
1
# import required modules
2
from keras.models import Sequential
3
from keras.layers import Convolution2D, MaxPooling2D
4
from keras.layers import Activation, Dropout, Flatten, Dense
5
import matplotlib.pyplot as plt
6
7
class Net:
8
@staticmethod
9
def build(width, height, depth, weightsPath=None):
10
'''
11
modified lenet structure
12
input: input_shape (width, height, channels)
13
returns: trained/loaded model
14
'''
15
# initialize the model
16
model = Sequential()
17
18
# first layer CONV => RELU => POOL
19
model.add(Convolution2D(32, (3, 3), input_shape = (width, height, depth)))
20
model.add(Activation('relu'))
21
model.add(MaxPooling2D(pool_size = (2, 2)))
22
23
# second layer CONV => RELU => POOL
24
model.add(Convolution2D(32, (3, 3)))
25
model.add(Activation('relu'))
26
model.add(MaxPooling2D(pool_size = (2, 2)))
27
28
# third layer of CONV => RELU => POOL
29
model.add(Convolution2D(64, (3, 3)))
30
model.add(Activation('relu'))
31
model.add(MaxPooling2D(pool_size = (2, 2)))
32
33
# set of FC => RELU layers
34
model.add(Flatten())
35
36
# number of neurons in FC layer = 128
37
model.add(Dense(128))
38
model.add(Activation('relu'))
39
model.add(Dropout(0.5))
40
41
# as number of classes is 36
42
model.add(Dense(36))
43
model.add(Activation('softmax'))
44
45
# if weightsPath is specified load the weights
46
if weightsPath is not None:
47
print('weights loaded')
48
model.load_weights(weightsPath)
49
# return model
50
51
return model
52
53