Let's quickly create something useful!
In this example we are going to load in a pre-trained image classifier to classify an image of our choosing. For now we are going to think of a neural network as a black box. The workflow here is that we are going to feed a photo into a pre-trained neural network and let the neural network classify the image.
We will import a pre-trained image classifier that won the Imagenet competition in 2015. Imagenet is a competition held annually to classify images. Next, we will import two modules for image preprocessing which will read in our image of our choosing and format in a way that ResNet expects. We will import numpy to expand dimensions. Finally, because we are running this code in a jupyter notebook, we will need to import a version of pillow that is compatible with Jupyter notebook. Keras will run its operations on top of tensorflow, which is default.
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
#This is special to jupyter notebooks. If you are using a test editor other than jupyter notebook you do not need this.
from IPython.display import Image
Now we are going to call the ResNet50 image classifier and assign the 'Imagenet' weights to it that won the Imagenet competition. These weights were trained by feeding the ResNet50 model the 14 million plus labelled images in the Imagenet database. No need doing that again. That's why it is common to use pretrained weights for models.
Next, we are going to load our image in, resize it to 224x224 and create an array of the image pixels. We will then use numpy to expand the dimensions of the array along the 0 axis which means we are going to wrap the entire image array with a bracket. The Resnet classifier uses BGR instead of RGB to read in the images so we will need to flip those dimensions in the array so it works with the Resnet50 classifier. Now that our image is in the format that the classifier likes to see. Let's feed it in to the model and let the model make a prediction.
model = ResNet50(weights='imagenet')
img_path = 'koala.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])
And boom! We have our predictions!
Next:
Finetuning