首页 > 其他分享 >使用VGG16和MLP实现猫狗图像识别

使用VGG16和MLP实现猫狗图像识别

时间:2024-07-12 16:58:18浏览次数:12  
标签:图像识别 img VGG16 vgg MLP import np path model

数据集

数据集可以参考我之前那篇文章,取一部分数据每个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

相关文章

  • 【深度学习】探讨最新的深度学习算法、模型创新以及在图像识别、自然语言处理等领域的
    深度学习作为人工智能领域的重要分支,近年来在算法、模型以及应用领域都取得了显著的进展。以下将探讨最新的深度学习算法与模型创新,以及它们在图像识别、自然语言处理(NLP)等领域的应用进展。一、深度学习算法与模型创新新型神经网络结构Transformer及其变种:近年来,Transformer......
  • mlp
    #导入必要的库importtorchimporttorch.nnasnnimporttorch.optimasoptimfromtorchvisionimportdatasets,transforms#定义一个多层感知器(MLP)类,继承自nn.ModuleclassMLP(nn.Module):#构造函数初始化数据,创建类的实例的时候需要输入这三个参数def__i......
  • 调用智谱清言的图像识别接口
    github地址:https://github.com/LLM-Red-Team/glm-free-api1.docker来取镜像并部署 拉取镜像dockerpullvinlic/glm-free-api:latest docker部署dockerrun-it-d--init--nameglm-free-api-p8000:8000-eTZ=Asia/Shanghaivinlic/glm-free-api:latest查看服......
  • 【坚果识别】果实识别+图像识别系统+Python+计算机课设+人工智能课设+卷积算法
    一、介绍坚果识别系统,使用Python语言进行开发,通过TensorFlow搭建卷积神经网络算法模型,对10种坚果果实('杏仁','巴西坚果','腰果','椰子','榛子','夏威夷果','山核桃','松子','开心果','核桃')等图片数据集进行训练,得到一个识别精度较高的模型文件,让后......
  • 阿里巴巴中国站拍立淘API返回值分析:图像识别技术助力电商用户体验升级
    阿里巴巴中国站拍立淘API的返回值分析,以及图像识别技术如何助力电商用户体验升级,可以从以下几个方面进行详细阐述:一、拍立淘API返回值分析拍立淘API是阿里巴巴中国站提供的一项基于图片搜索的商品搜索服务,它允许用户通过上传商品图片,系统自动识别图片中的商品信息,并返回与之......
  • 课前准备---HD数据结合图像识别获取真实的空间单细胞级数据
    作者,EvilGeniusHD数据不同于Xenium,目前还是横屏竖直的一刀切数据分析模式,但是真实的细胞绝对不是如此分布的,那么实际分析中,2um的精度配合图像的信息,获取真实的细胞分布数据,就成了分析的必须。多说一句,分析的准确性和超前化也是公司对核心分析人员的核心要求。如下如,我们最......
  • ONNX Runtime入门示例:在C#中使用ResNet50v2进行图像识别
    ONNXRuntime简介ONNXRuntime是一个跨平台的推理和训练机器学习加速器。ONNX运行时推理可以实现更快的客户体验和更低的成本,支持来自深度学习框架(如PyTorch和TensorFlow/Keras)以及经典机器学习库(如scikit-learn、LightGBM、XGBoost等)的模型。ONNX运行时与不同的硬件、......
  • 极验图标点选图像识别
    一、简介极验的图标点选验证码有很多种,今天我们就来讲其中一种的识别方法。如上图所示,这种图标的是特点是,中间是数字、字母。外圈有一种装饰作为干扰。由于外圈的装饰占了很大一部分,所以对识别的干扰比较大。我根据实际情况分别做了两种识别方式,一种是原图识别、另一种是截......
  • mlp
    importtorchfromd2limporttorchasd2lfromtorchimportnnbatch_size=100train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size=batch_size)input_size=784hidden_size=300output_size=10W1=nn.Parameter(torch.randn(input_siz......
  • 海洋生物识别系统+图像识别+Python+人工智能课设+深度学习+卷积神经网络算法+TensorFl
    一、介绍海洋生物识别系统。以Python作为主要编程语言,通过TensorFlow搭建ResNet50卷积神经网络算法,通过对22种常见的海洋生物('蛤蜊','珊瑚','螃蟹','海豚','鳗鱼','水母','龙虾','海蛞蝓','章鱼','水獭','企鹅',�......