数据集
数据集可以参考我之前那篇文章,取一部分数据每个300条即可:基于卷积神经网络(CNN)的猫狗图像分类系统实现-CSDN博客
1.目的
使用VGG16的结构提取图像特征,再根据特征建立MLP模型,实现猫狗图像识别。训练/测试数据:data
1.对数据进行分离、计算测试数据准确率
2.使用VGG16提取特征,再根据特征建立MLP模型,实现猫狗图像识别。
3.从网站下载猫\狗图片,对其进行预测
mlp模型一个隐藏层,10个神经元
2.模型训练
1.数据加载
#加载数据
from keras.preprocessing.image import load_img, img_to_array
img_path = '1.jpg'
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
type(img) #查看数据类型
运行结果:
2.对图像进行预处理
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
model_vgg = VGG16(weights='imagenet', include_top=False)
x = np.expand_dims(img, axis=0) #添加一个维度
x = preprocess_input(x) #对图像进行预处理
print(x.shape)
运行结果
3.特征提取
#特征提取
features = model_vgg.predict(x) #提取特征
print(features.shape)
运行结果
4.将特征展平
#将特征展平
features = features.reshape(1, 7*7*512)
print(features.shape)
运行结果
3.可视化数据
#可视化数据
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5, 5))
img = load_img(img_path, target_size=(224, 224))
plt.imshow(img)
运行结果
4.集中处理图像并生成寻来你数据集
#用vgg16结构加载图像并进行预处理
from keras.preprocessing.image import img_to_array,load_img
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
model_vgg =VGG16(weights='imagenet',include_top=False)
#定义加载和预处理图像的方法
def modelProcess(img_path,model):
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
x= np.expand_dims(img,axis=0)
x= preprocess_input(x)
x_vgg = model.predict(x)
x_vgg =x_vgg.reshape(1,25088)
return x_vgg
#列出训练数据集的文件名
import os
folder = "./data_vgg/cats2"
dirs = os.listdir(folder)
#为图像生成路径
img_path = []
for i in dirs:
if os.path.splitext(i)[1] == ".jpg":
img_path.append(i)
img_path = [folder+"//"+i for i in img_path]
#预处理多个图像
features1 =np.zeros([len(img_path),25088])
for i in range(len(img_path)):
feature_i = modelProcess(img_path[i],model_vgg)
print('preprocessed:',img_path[i])
features1[i] = feature_i
folder ="./data_vgg/dogs2"
dirs = os.listdir(folder)
img_path = []
for i in dirs:
if os.path.splitext(i)[1] == ".jpg":
img_path.append(i)
img_path =[folder+"//"+i for i in img_path]
features2 =np.zeros([len(img_path),25088])
for i in range(len(img_path)):
featute_i = modelProcess(img_path[i],model_vgg)
print("preprocessed:",img_path[i])
features2[i] = feature_i
#标记结果
print(features1.shape,features2.shape)
y1 = np.zeros(300)
y2= np.ones(300)
#生成训练数据
X = np.concatenate((features1,features2),axis=0)
y = np.concatenate((y1,y2),axis=0)
y = y.reshape(-1,1)
print(X.shape,y.shape)
运行结果
5.拆分训练和测试数据
#拆分训练和测试数据
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=50)
print(X_train.shape,X_test.shape,X.shape)
运行结果
6.建立MLP模型
#建立MLP模型
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(units=20,activation='relu',input_dim=25088))
model.add(Dense(units=1,activation='sigmoid'))
model.summary()
运行结果
7.配置并训练模型
#配置模型
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
#训练模型
model.fit(X_train,y_train,epochs=20)
运行结果
8.导出模型
#导出模型
model.save('cat_vs_dog.keras')
3.模型评估
1.训练准确率
#评估模型
from sklearn.metrics import accuracy_score
y_train_predict = model.predict(X_train)
y_train_predict = np.argmax(y_train_predict,axis=1)
y_train_1 = np.argmax(y_train,axis=1)
accuracy_train = accuracy_score(y_train_1, y_train_predict)
print("训练集准确率:",accuracy_train)
运行结果
2.测试准确率
#测试准确率
y_test_predict = model.predict(X_test)
y_test_predict = np.argmax(y_test_predict,axis=1)
y_test_1 = np.argmax(y_test,axis=1)
accuracy_test = accuracy_score(y_test_1, y_test_predict)
print("测试集准确率:",accuracy_test)
运行结果
4.测试模型
1.测试单张图片
#测试单张图片
img_path = '1.jpg'
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
x= np.expand_dims(img,axis=0)
x= preprocess_input(x)
features = model_vgg.predict(x)
features = features.reshape(1, 7*7*512)
result = model.predict(features)
# result = np.argmax(result, axis=1)
result = (result > 0.5).astype(int) #给一个阈值进行分类
print(result)
运行结果
2.多张进行图片分类
#进行图片分类
import matplotlib as mlp
font2 = {'family' : 'SimHei',
'weight' : 'normal',
'size' : 20,
}
mlp.rcParams['font.family'] = 'SimHei'
mlp.rcParams['axes.unicode_minus'] = False
from matplotlib import pyplot as plt
from matplotlib.image import imread
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
a = [i for i in range(1,10)]
fig = plt.figure(figsize=(10,10))
for i in a:
img_name = str(i)+'.jpg'
img_path = img_name
img = load_img(img_path, target_size=(224, 224))
img = img_to_array(img)
x = np.expand_dims(img,axis=0)
x = preprocess_input(x)
x_vgg = model_vgg.predict(x)
x_vgg = np.argmax(x_vgg,axis=0)
x_vgg = x_vgg.reshape(1,7*7*512)
result = model.predict(x_vgg)
result = np.argmax(result,axis=1)
print(result)
img_ori = load_img(img_name, target_size=(250, 250))
plt.subplot(3,3,i)
plt.imshow(img_ori)
plt.title('预测为:狗狗' if result[0] == 1 else '预测为:猫咪')
plt.show()
运行结果
5.总结
使用VGG16和多层感知机(MLP)结合的方法来实现猫狗图像识别,是一种结合了迁移学习和传统机器学习分类器的有效策略。具体实现中,首先利用预训练的VGG16模型作为特征提取器,该模型已经在大型图像数据集(如ImageNet)上进行了训练,因此能够捕捉到图像中的高级特征。然后,从VGG16模型的某一层(通常是全连接层之前的全局平均池化层或卷积层的输出)提取特征,并将这些特征作为输入传递给一个较简单的MLP模型进行分类。
标签:图像识别,img,VGG16,vgg,MLP,import,np,path,model From: https://blog.csdn.net/ge20040119/article/details/140276221