Keras 简明教程
Real Time Prediction using ResNet Model
ResNet 是一个预训练模型,它使用 ImageNet 进行了训练。在 ImageNet 上预训练了 ResNet 模型权重。它的语法如下所示:
ResNet is a pre-trained model. It is trained using ImageNet. ResNet model weights pre-trained on ImageNet. It has the following syntax −
keras.applications.resnet.ResNet50 (
include_top = True,
weights = 'imagenet',
input_tensor = None,
input_shape = None,
pooling = None,
classes = 1000
)
在此,
Here,
-
include_top refers the fully-connected layer at the top of the network.
-
weights refer pre-training on ImageNet.
-
input_tensor refers optional Keras tensor to use as image input for the model.
-
input_shape refers optional shape tuple. The default input size for this model is 224x224.
-
classes refer optional number of classes to classify images.
让我们通过写一个简单的示例来了解该模型:
Let us understand the model by writing a simple example −
Step 1: import the modules
让我们加载必要的模块,如下所示:
Let us load the necessary modules as specified below −
>>> import PIL
>>> from keras.preprocessing.image import load_img
>>> from keras.preprocessing.image import img_to_array
>>> from keras.applications.imagenet_utils import decode_predictions
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from keras.applications.resnet50 import ResNet50
>>> from keras.applications import resnet50
Step 2: Select an input
让我们选择一个输入图像, Lotus 如下所示:
Let us choose an input image, Lotus as specified below −
>>> filename = 'banana.jpg'
>>> ## load an image in PIL format
>>> original = load_img(filename, target_size = (224, 224))
>>> print('PIL image size',original.size)
PIL image size (224, 224)
>>> plt.imshow(original)
<matplotlib.image.AxesImage object at 0x1304756d8>
>>> plt.show()
在这里,我们加载了一张图像 (banana.jpg) 并显示了它。
Here, we have loaded an image (banana.jpg) and displayed it.
Step 3: Convert images into NumPy array
让我们将我们的输入 Banana 转换成 NumPy 数组,以便将其传递到模型中以进行预测。
Let us convert our input, Banana into NumPy array, so that it can be passed into the model for the purpose of prediction.
>>> #convert the PIL image to a numpy array
>>> numpy_image = img_to_array(original)
>>> plt.imshow(np.uint8(numpy_image))
<matplotlib.image.AxesImage object at 0x130475ac8>
>>> print('numpy array size',numpy_image.shape)
numpy array size (224, 224, 3)
>>> # Convert the image / images into batch format
>>> image_batch = np.expand_dims(numpy_image, axis = 0)
>>> print('image batch size', image_batch.shape)
image batch size (1, 224, 224, 3)
>>>
Step 4: Model prediction
让我们将我们的输入输入模型以获取预测
Let us feed our input into the model to get the predictions
>>> prepare the image for the resnet50 model >>>
>>> processed_image = resnet50.preprocess_input(image_batch.copy())
>>> # create resnet model
>>>resnet_model = resnet50.ResNet50(weights = 'imagenet')
>>> Downloavding data from https://github.com/fchollet/deep-learning-models/releas
es/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
102858752/102853048 [==============================] - 33s 0us/step
>>> # get the predicted probabilities for each class
>>> predictions = resnet_model.predict(processed_image)
>>> # convert the probabilities to class labels
>>> label = decode_predictions(predictions)
Downloading data from https://storage.googleapis.com/download.tensorflow.org/
data/imagenet_class_index.json
40960/35363 [==================================] - 0s 0us/step
>>> print(label)