转:使用VGG16模型对图片进行分类

代码:

# 使用VGG16模型
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import preprocess_input
from keras.layers import Dense, Flatten, Input
from keras import Model
import matplotlib.pyplot as plt
from keras.utils.vis_utils import plot_model
from keras.utils.image_utils import load_img,img_to_array
import numpy as np


# 获取vgg16的卷积部分,如果要获取整个vgg16网络需要设置:include_top=True
# weights='imagenet', include_top=False
model_vgg16_conv = VGG16()
model_vgg16_conv.summary()



img_path = "query/8.jpg"
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
img = img.reshape((1,img.shape[0],img.shape[1],img.shape[2]))
#img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
y = model_vgg16_conv.predict(img)
label = decode_predictions(y)
for number, name, score in label[0]:
    print(number)
    print(name)
    print(score)
    print()

 

效果:

 

 

 

 

 

参考:

https://cloud.tencent.com/developer/article/1725538

https://image-net.org/challenges/LSVRC/2014/browse-synsets   分类详细说明

 

posted @ 2023-03-10 16:57  河北大学-徐小波  阅读(87)  评论(0编辑  收藏  举报