net.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. class Net:
  7. @staticmethod
  8. def build(width, height, depth, weightsPath=None):
  9. '''
  10. modified lenet structure
  11. input: input_shape (width, height, channels)
  12. returns: trained/loaded model
  13. '''
  14. # initialize the model
  15. model = Sequential()
  16. # first layer CONV => RELU => POOL
  17. model.add(Convolution2D(32, (3, 3), input_shape = (width, height, depth)))
  18. model.add(Activation('relu'))
  19. model.add(MaxPooling2D(pool_size = (2, 2)))
  20. # second layer CONV => RELU => POOL
  21. model.add(Convolution2D(32, (3, 3)))
  22. model.add(Activation('relu'))
  23. model.add(MaxPooling2D(pool_size = (2, 2)))
  24. # third layer of CONV => RELU => POOL
  25. model.add(Convolution2D(64, (3, 3)))
  26. model.add(Activation('relu'))
  27. model.add(MaxPooling2D(pool_size = (2, 2)))
  28. # set of FC => RELU layers
  29. model.add(Flatten())
  30. # number of neurons in FC layer = 128
  31. model.add(Dense(128))
  32. model.add(Activation('relu'))
  33. model.add(Dropout(0.5))
  34. # as number of classes is 36
  35. model.add(Dense(36))
  36. model.add(Activation('softmax'))
  37. # if weightsPath is specified load the weights
  38. if weightsPath is not None:
  39. print('weights loaded')
  40. model.load_weights(weightsPath)
  41. # return model
  42. return model