# -*- coding: utf-8 -*-
import datetime
from keras.models import Sequential
from keras.layers import *
from keras.preprocessing.image import ImageDataGenerator
def main():
width = 227
height = 227
batch_size = 16
generator = ImageDataGenerator(horizontal_flip=True,
vertical_flip=True,
validation_split=0.2)
train_generator = generator.flow_from_directory(directory="datasets/train",
target_size=(width, height),
batch_size=batch_size,
class_mode="binary",
subset="training")
val_generator = generator.flow_from_directory(directory="datasets/train",
target_size=(width, height),
batch_size=batch_size,
class_mode="binary",
subset="validation")
# AlexNet
model = Sequential()
model.add(Conv2D(filters=96, kernel_size=(11, 11), strides=(4, 4), padding='valid', input_shape=(width, height, 3),
activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
model.add(Conv2D(filters=256, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
model.add(Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))
model.add(Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu'))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
# Output Layer
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.summary()
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit_generator(train_generator, validation_data=val_generator, epochs=10, verbose=1)
if __name__ == '__main__':
tic = datetime.datetime.now()
main()
toc = datetime.datetime.now()
print("\nThis model took ", (toc - tic))