本文主要介绍通过预训练的ImageNet模型实现图像分类,主要使用到的网络结构有:VGG16、InceptionV3、ResNet50、MobileNet。
代码:
import keras
import numpy as np
from keras.applications import vgg16, inception_v3, resnet50, mobilenet
# 加载模型
vgg_model = vgg16.VGG16(weights='imagenet')
inception_model = inception_v3.InceptionV3(weights='imagenet')
resnet_model = resnet50.ResNet50(weights='imagenet')
mobilenet_model = mobilenet.MobileNet(weights='imagenet')
# 导入所需的图像预处理模块
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
%matplotlib inline
filename= 'images/cat.jpg'
# 将图片输入到网络之前执行预处理
'''
1、加载图像,load_img
2、将图像从PIL格式转换为Numpy格式,image_to_array
3、将图像形成批次,Num