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

Utilizing tf.keras built-in VGG16 Net module

tf.Keras applications are prebuilt and pretrained deep learning models. Weights

are downloaded automatically when instantiating a model and stored at ~/.keras/

models/. Using built-in code is very easy:

import tensorflow as tf

from tensorflow.keras.applications.vgg16 import VGG16

import matplotlib.pyplot as plt

import numpy as np

import cv2

# prebuild model with pre-trained weights on imagenet

model = VGG16(weights='imagenet', include_top=True)

model.compile(optimizer='sgd', loss='categorical_crossentropy')

# resize into VGG16 trained images' format

im = cv2.resize(cv2.imread('steam-locomotive.jpg'), (224, 224)

.astype(np.float32))

im = np.expand_dims(im, axis=0)

# predict

out = model.predict(im)

index = np.argmax(out)

print(index)

plt.plot(out.ravel())

plt.show()

# this should print 820 for steaming train

Now, let us consider a train:

[ 135 ]

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

Saved successfully!

Ooh no, something went wrong!