Created
October 18, 2019 14:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Todo
1 - Training data path
2 - Model Evaluation