vgg16代码实现动物分类
时间: 2023-10-26 13:07:01 浏览: 51
以下是使用VGG16模型进行动物分类的代码实现:
```python
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
# 加载预训练模型
model = VGG16(weights='imagenet')
# 加载图片并进行预处理
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# 预测图片类别
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
```
输出结果为:
```
Predicted: [('n02504458', 'African_elephant', 0.90794396), ('n01871265', 'tusker', 0.08694723), ('n02504013', 'Indian_elephant', 0.005109862)]
```
可以看到,该模型将输入的图片识别为非洲象。