# -*- coding: utf-8 -*-
import datetime
from keras.models import Model
from keras.layers import *
from keras.preprocessing.image import ImageDataGenerator
def dense_block(x, blocks, name):
for i in range(blocks):
x = conv_block(x, 32, name=name + '_block' + str(i + 1))
return x
def conv_block(x, growth_rate, name):
bn_axis = 3
x1 = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_0_bn')(x)
x1 = Activation('relu', name=name + '_0_relu')(x1)
x1 = Conv2D(4 * growth_rate, 1, use_bias=False, name=name + '_1_conv')(x1)
x1 = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_1_bn')(x1)
x1 = Activation('relu', name=name + '_1_relu')(x1)
x1 = Conv2D(growth_rate, 3, padding='same', use_bias=False, name=name + '_2_conv')(x1)
x = Concatenate(axis=bn_axis, name=name + '_concat')([x, x1])
return x
def transition_block(x, reduction, name):
bn_axis = 3
x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name=name + '_bn')(x)
x = Activation('relu', name=name + '_relu')(x)
x = Conv2D(int(K.int_shape(x)[bn_axis] * reduction), 1, use_bias=False, name=name + '_conv')(x)
x = AveragePooling2D(2, strides=2, name=name + '_pool')(x)
return x
def DenseNet121(input_shape=(224, 224, 3), classes=1000):
blocks = [6, 12, 24, 16]
img_input = Input(shape=input_shape)
bn_axis = 3
x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
x = Activation('relu', name='conv1/relu')(x)
x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
x = MaxPooling2D(3, strides=2, name='pool1')(x)
x = dense_block(x, blocks[0], name='conv2')
x = transition_block(x, 0.5, name='pool2')
x = dense_block(x, blocks[1], name='conv3')
x = transition_block(x, 0.5, name='pool3')
x = dense_block(x, blocks[2], name='conv4')
x = transition_block(x, 0.5, name='pool4')
x = dense_block(x, blocks[3], name='conv5')
x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
x = GlobalAveragePooling2D(name='avg_pool')(x)
if classes == 2:
x = Dense(1, activation='sigmoid', name='pred')(x)
else:
x = Dense(classes, activation='softmax', name='pred')(x)
model = Model(img_input, x, name='densenet121')
return model
def main():
width = 224
height = 224
batch_size = 8
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")
model = DenseNet121(classes=2)
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))