09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Since we saved the model and the weights, we do not need to train each time:

import numpy as np

import scipy.misc

from tf.keras.models import model_from_json

from tf.keras.optimizers import SGD

# load model

model_architecture = 'cifar10_architecture.json'

model_weights = 'cifar10_weights.h5'

model = model_from_json(open(model_architecture).read())

model.load_weights(model_weights)

Chapter 4

# load images

img_names = ['cat-standing.jpg', 'dog.jpg']

imgs = [np.transpose(scipy.misc.imresize(scipy.misc.imread(img_name),

(32, 32)), (2, 0, 1)).astype('float32')

for img_name in img_names]

imgs = np.array(imgs) / 255

# train

optim = SGD()

model.compile(loss='categorical_crossentropy', optimizer=optim,

metrics=['accuracy'])

# predict

predictions = model.predict_classes(imgs)

print(predictions)

Now let us get the prediction for a cat and for a dog:

We get categories 3 (cat) and 5 (dog) as output as expected. We successfully created a

CNN to classify CIFAR-10 images. Next, we will look at VGG-16: a breakthrough in

deep learning.

[ 131 ]

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

Saved successfully!

Ooh no, something went wrong!