ResNet是一個(gè)預(yù)訓(xùn)練模型。它使用 ImageNet 進(jìn)行訓(xùn)練。在 ImageNet 上預(yù)訓(xùn)練的 ResNet 模型權(quán)重。它具有以下語法:
keras.applications.resnet.ResNet50 (
include_top = True,
weights = 'imagenet',
input_tensor = None,
input_shape = None,
pooling = None,
classes = 1000
)
include_top
指的是網(wǎng)絡(luò)頂部的全連接層。weights
指的是 ImageNet
上的預(yù)訓(xùn)練。input_tensor
指用作模型的圖像輸入的可選的 Keras
張量。input_shape
指可選的形狀元組。此模型的默認(rèn)輸入大小為 224x224
。clasees
指用于對(duì)圖像進(jìn)行分類的可選數(shù)量的類。讓我們通過寫一個(gè)簡單的例子來理解模型:
加載如下指定的必要模塊:
>>> 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
選擇一個(gè)輸入圖像,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()
在這里,我們加載了一個(gè)圖像(banana.jpg
)并顯示了它。
將輸入的 Banana 轉(zhuǎn)換為 NumPy 數(shù)組,以便將其傳遞到模型中以進(jìn)行預(yù)測。
>>> #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)
>>>
將輸入輸入模型以獲得預(yù)測
>>> 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)
]
]
模型就可以正確地將圖像預(yù)測為 banana。
更多建議: