- Keras 教程
- Keras - 主頁
- Keras - 介紹
- Keras - 安裝
- Keras - 後端配置
- Keras - 深度學習概述
- Keras - 深度學習
- Keras - 模組
- Keras - 層
- Keras - 自定義層
- Keras - 模型
- Keras - 模型編譯
- Keras - 模型評估和預測
- Keras - 卷積神經網路
- Keras - 利用 MPL 進行迴歸預測
- Keras - 利用 LSTM RNN 進行時間序列預測
- Keras - 應用
- Keras - ResNet 模型實現即時預測
- Keras - 預訓練模型
- Keras 的有用資源
- Keras - 簡要指南
- Keras - 有用資源
- Keras - 討論
ResNet 模型實現即時預測
ResNet 是一個預訓練的模型。它是使用 ImageNet 進行訓練的。ResNet 模型的權值預訓練於 ImageNet。它的語法如下 -
keras.applications.resnet.ResNet50 ( include_top = True, weights = 'imagenet', input_tensor = None, input_shape = None, pooling = None, classes = 1000 )
此處,
include_top 表示網路頂部的全連線層。
weights 表示在 ImageNet 上的預訓練。
input_tensor 表示可選的 Keras 張量,用作模型的影像輸入。
input_shape 表示可選的形狀元組。此模型的預設輸入大小為 224x224。
classes 表示對影像進行分類的可選類別數。
讓我們透過編寫一個簡單示例來了解該模型 -
步驟 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)
<matplotlib.image.AxesImage object at 0x1304756d8>
>>> 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))
<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)
>>>
步驟 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)
]
]
在此,模型正確地將影像預測為香蕉。
廣告