PaddleHub可以便捷地獲取PaddlePaddle生態下的預訓練模型,完成模型的管理和一鍵預測。配合使用Fine-tune API,可以基於大規模預訓練模型快速完成遷移學習,讓預訓練模型能更好地服務於用戶特定場景的應用。
本次介紹如何使用paddlehub調用vgg模型實現圖像分類。
模型概述
VGG是牛津大學計算機視覺組和DeepMind在2014年提出的一種圖像分類模型。該系列模型探索了卷積神經網絡的深度與其性能之間的關係,通過實驗證明了增加網絡的深度能夠在一定程度上影響網絡最終的性能,到目前爲止,VGG仍然被許多其他圖像任務用作特徵提取的BackBone網絡。該PaddleHub Module結構爲VGG16,基於ImageNet-2012數據集訓練,接受輸入圖片大小爲224 x 224 x 3,支持直接通過命令行或者Python接口進行預測。
module = hub.Module(name="vgg16_imagenet")
test_img_path = "./cat1.jpg"
# 預測結果展示
img = mpimg.imread(test_img_path)
plt.imshow(img)
plt.axis('off')
plt.show()
# set input dict
input_dict = {"image": [test_img_path]}
# execute predict and print the result
results = module.classification(data=input_dict)
for result in results:
print(result)
test_img_path = "./dog1.jpg"
# 預測結果展示
img = mpimg.imread(test_img_path)
plt.imshow(img)
plt.axis('off')
plt.show()
# set input dict
input_dict = {"image": [test_img_path]}
# execute predict and print the result
results = module.classification(data=input_dict)
for result in results:
print(result)
[2020-01-03 09:19:50,058] [ INFO] - Installing vgg16_imagenet module
2020-01-03 09:19:50,058-INFO: Installing vgg16_imagenet module
Downloading vgg16_imagenet
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/cache/vgg16_imagenet
[==================================================] 100.00%
[2020-01-03 09:20:10,875] [ INFO] - Successfully installed vgg16_imagenet-1.0.0
2020-01-03 09:20:10,875-INFO: Successfully installed vgg16_imagenet-1.0.0
[2020-01-03 09:20:11,640] [ INFO] - 32 pretrained paramaters loaded by PaddleHub
2020-01-03 09:20:11,640-INFO: 32 pretrained paramaters loaded by PaddleHub
[{'tiger cat': 0.600113570690155}]
[{'Labrador retriever': 0.9380330443382263}]
整體效果相當不錯。