Skip to content

Instantly share code, notes, and snippets.

@mujeebishaque
Created October 18, 2019 14:22
Show Gist options
  • Save mujeebishaque/2bc636e38cc722d13e7cccf11ef70b03 to your computer and use it in GitHub Desktop.
Save mujeebishaque/2bc636e38cc722d13e7cccf11ef70b03 to your computer and use it in GitHub Desktop.
custom model creation and saving the model. Binary Classification, keras code for CNN
# imports
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dense, Flatten, Dropout
from batchGenerator import generateData
# creating a model - function based approach
def create_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(Conv2D(32, (3, 3), dim_ordering='th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(Conv2D(62, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
return model
# function for training and saving model
def train():
model = create_model()
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
train_generator, validation_generator, batch_size = generateData()
model.fit_generator(
train_generator,
steps_per_epoch=500 // batch_size,
epochs=50,
validation_data=validation_generator,
validation_steps=500 // batch_size)
model.save_weights('cracks_detection.h5') # saving model
return model
@mujeebishaque
Copy link
Author

Todo
1 - Training data path
2 - Model Evaluation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment