16.03.2021 Views

Advanced Deep Learning with Keras

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Deep Neural Networks

# reshape and normalize input images

image_size = x_train.shape[1]

x_train = np.reshape(x_train,[-1, image_size, image_size, 1])

x_test = np.reshape(x_test,[-1, image_size, image_size, 1])

x_train = x_train.astype('float32') / 255

x_test = x_test.astype('float32') / 255

# network parameters

# image is processed as is (square grayscale)

input_shape = (image_size, image_size, 1)

batch_size = 128

kernel_size = 3

filters = 64

dropout = 0.3

# use functional API to build cnn layers

inputs = Input(shape=input_shape)

y = Conv2D(filters=filters,

kernel_size=kernel_size,

activation='relu')(inputs)

y = MaxPooling2D()(y)

y = Conv2D(filters=filters,

kernel_size=kernel_size,

activation='relu')(y)

y = MaxPooling2D()(y)

y = Conv2D(filters=filters,

kernel_size=kernel_size,

activation='relu')(y)

# image to vector before connecting to dense layer

y = Flatten()(y)

# dropout regularization

y = Dropout(dropout)(y)

outputs = Dense(num_labels, activation='softmax')(y)

# build the model by supplying inputs/outputs

model = Model(inputs=inputs, outputs=outputs)

# network model in text

model.summary()

# classifier loss, Adam optimizer, classifier accuracy

model.compile(loss='categorical_crossentropy',

[ 42 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!