PaddleHub可以便捷地獲取PaddlePaddle生態(tài)下的預(yù)訓(xùn)練模型翔横,完成模型的管理和一鍵預(yù)測。配合使用Fine-tune API委造,可以基于大規(guī)模預(yù)訓(xùn)練模型快速完成遷移學(xué)習(xí)职祷,讓預(yù)訓(xùn)練模型能更好地服務(wù)于用戶特定場景的應(yīng)用。
本次介紹如何使用paddlehub調(diào)用vgg模型實現(xiàn)圖像分類扭屁。
模型概述
VGG是牛津大學(xué)計算機視覺組和DeepMind在2014年提出的一種圖像分類模型。該系列模型探索了卷積神經(jīng)網(wǎng)絡(luò)的深度與其性能之間的關(guān)系涩禀,通過實驗證明了增加網(wǎng)絡(luò)的深度能夠在一定程度上影響網(wǎng)絡(luò)最終的性能,到目前為止然眼,VGG仍然被許多其他圖像任務(wù)用作特征提取的BackBone網(wǎng)絡(luò)艾船。該PaddleHub Module結(jié)構(gòu)為VGG16,基于ImageNet-2012數(shù)據(jù)集訓(xùn)練高每,接受輸入圖片大小為224 x 224 x 3屿岂,支持直接通過命令行或者Python接口進行預(yù)測。
module = hub.Module(name="vgg16_imagenet")
test_img_path = "./cat1.jpg"
# 預(yù)測結(jié)果展示
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"
# 預(yù)測結(jié)果展示
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}]
整體效果相當不錯鲸匿。