📜  使用ResNet模型进行实时预测

📅  最后修改于: 2020-12-11 05:01:15             🧑  作者: Mango


ResNet是一个预先训练的模型。它使用ImageNet进行培训。在ImageNet上预训练的ResNet模型权重。它具有以下语法-

keras.applications.resnet.ResNet50 (
   include_top = True, 
   weights = 'imagenet', 
   input_tensor = None, 
   input_shape = None, 
   pooling = None, 
   classes = 1000
)

这里,

  • include_top指网络顶部的完全连接层。

  • 权重参考ImageNet上的预训练。

  • input_tensor引用可选的Keras张量,以用作模型的图像输入。

  • input_shape表示可选的形状元组。该模型的默认输入大小为224×224。

  • 是指可选的若干类,以对图像进行分类。

让我们通过写一个简单的例子来理解模型-

步骤1:导入模块

让我们加载以下指定的必要模块-

>>> 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

步骤2:选择输入

让我们选择一个输入图像, Lotus如下所示-

>>> 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) 
 
>>> plt.show()

在这里,我们加载了图像(banana.jpg)并显示了它。

步骤3:将图片转换成NumPy数组

让我们将输入Banana转换为NumPy数组,以便可以将其传递到模型中以进行预测。

>>> #convert the PIL image to a numpy array 
>>> numpy_image = img_to_array(original) 

>>> plt.imshow(np.uint8(numpy_image)) 
 

>>> 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)
>>> 

步骤4:模型预测

让我们将输入信息输入模型以获取预测

>>> 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)

输出

[
   [
      ('n07753592', 'banana', 0.99229723), 
      ('n03532672', 'hook', 0.0014551596), 
      ('n03970156', 'plunger', 0.0010738898), 
      ('n07753113', 'fig', 0.0009359837) , 
      ('n03109150', 'corkscrew', 0.00028538404)
   ]
]

在此,模型正确地将图像预测为香蕉。