Skip to content

Instantly share code, notes, and snippets.

@csorgod
Created September 24, 2023 22:13
Show Gist options
  • Save csorgod/955580d463e069b8f74441b4a3f6e18a to your computer and use it in GitHub Desktop.
Save csorgod/955580d463e069b8f74441b4a3f6e18a to your computer and use it in GitHub Desktop.
Simple CNN for image classification
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense, Conv2D, MaxPooling2D
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size = (3, 3), input_shape = image_shape, activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(filters = 32, kernel_size = (3, 3), input_shape = image_shape, activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(filters = 32, kernel_size = (3, 3), input_shape = image_shape, activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss = 'binary_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
early_stopping = EarlyStopping(monitor = 'val_loss', patience = 2)
results = model.fit_generator(train_image_aug, epochs = 20, validation_data = test_image_aug, callbacks = [early_stopping])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment