09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

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

Chapter 4

Figure 15: An example of image augmentation

Now we can apply this intuition directly for training. Using the same CNN

defined before, we simply generate more augmented images and then we train.

For efficiency, the generator runs in parallel to the model. This allows image

augmentation on a CPU while training in parallel on a GPU. Here is the code:

# train

batch_size = 64

model.fit_generator(datagen.flow(x_train, y_train,

batch_size=batch_size),

epochs=EPOCHS,

verbose=1,validation_data=(x_test,y_test))

# save to disk

model_json = model.to_json()

with open('model.json', 'w') as json_file:

json_file.write(model_json)

model.save_weights('model.h5')

# test

scores = model.evaluate(x_test, y_test, batch_size=128, verbose=1)

print('\nTest result: %.3f loss: %.3f' % (scores[1]*100,scores[0]))

[ 129 ]

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

Saved successfully!

Ooh no, something went wrong!