当前位置: 首页 > news >正文

深度学习-卷积神经网络-基于VGG16模型, 实现猫狗二分类(文末附带数据集下载链接, 长期有效)

简介: 

        1.基于VGG16模型进行特征提取, 结合mlp实现猫狗二分类

        2.训练数据--"dog_cat_class\training_set"

        3.模型训练流程

                        1.对图像数据进行导入和预处理

                        2.搭建模型, 导入VGG16模型, 去除mlp层,  将经过VGG16训练后的数据作为输入, 输入到自建的mlp层中进行训练,

                                要求:

                                           hidden layers=1, units=10, activation=relu

                                           out layer:units=1, activation=sigmoid

                        3.对模型进行评估和预测

                        4.随机下载百度的12张猫/狗的图片, 对模型进行实战测试

        4.代码实现(推荐直接看第二个, 比较规范)

4.1,   小垃圾写的(我写的)

# ============================================================
# 1.数据集导入, 单张图片导入, 可以通过load_image导入
from keras.preprocessing.image import load_img, img_to_array
img_path=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dogs\dog.1.jpg"
img_data=load_img(img_path, target_size=(224,224))
img_data=img_to_array(img_data)
# print(img_data.shape)
# type(img_data)# 2.模型搭建和模型训练
# 对图像增加一个维度, 然后通过图像预处理, 成为合格的图像输入格式, 
from keras.applications.vgg16 import preprocess_input
import numpy as np
x_train=np.expand_dims(img_data, axis=0)  # 这里最好换一个名字, 如果接受变量还是img_data的话, 当再一次执行这个代码单元, 会增加数组的维度
x_train=preprocess_input(x_train)
# print(x_train.shape)
#  将合格的图片数据输入到VGG16模型中
from keras.applications.vgg16 import VGG16
extract_model=VGG16(include_top=False, weights='imagenet')
img_features=extract_model.predict(x_train)
print(img_features.shape)# 实现对图片的批量读入
import numpy as npfrom keras.applications.vgg16 import VGG16
vgg=VGG16(include_top=False, weights='imagenet')from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input 
def model_prepro(model, img_path):img_data=load_img(img_path, target_size=(224,224))img_array=img_to_array(img_data)x_train=np.expand_dims(img_array, axis=0)x_train=preprocess_input(x_train)x_vgg=model.predict(x_train)x_vgg=x_vgg.reshape(1, 25088)return x_vggimport os
# file_path=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\"+sub_file
file_path1=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\cat"
img_name_list = os.listdir(file_path1)img_list=[]
for i in img_name_list:if os.path.splitext(i)[1]=='.jpg':  img_list.append(i)
img_path_list=[os.path.join(file_path1, i) for i in img_list]  
img_feature_array1=np.zeros([len(img_path_list), 25088])        
for i in range(len(img_path_list)):img_feature=model_prepro(vgg, img_path_list[i])img_feature_array1[i]=img_feature# 显示正在处理的图片print("preprecessing is"+img_list[i])#####################################################
file_path2=r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dog"
img_name_list = os.listdir(file_path2)img_list=[]
for i in img_name_list:if os.path.splitext(i)[1]=='.jpg':  img_list.append(i)
img_path_list=[os.path.join(file_path2, i) for i in img_list]  
img_feature_array2=np.zeros([len(img_path_list), 25088])        
for i in range(len(img_path_list)):img_feature=model_prepro(vgg, img_path_list[i])img_feature_array2[i]=img_feature# 显示正在处理的图片print("preprecessing is"+img_list[i])print(img_feature_array1.shape, img_feature_array2.shape)
y1=np.zeros(600)
y2=np.ones(602)
x_all=np.concatenate((img_feature_array1, img_feature_array2), axis=0)
y_all=np.concatenate((y1, y2), axis=0)
y_all=y_all.reshape(-1,1)
print(x_all.shape, y_all.shape)
# 分割数据集
from sklearn.model_selection import train_test_split
x_train, x_test,y_train, y_test = train_test_split(x_all, y_all, test_size=0.3, random_state=10)
print(x_train.shape, x_test.shape, y_train.shape, y_test.shape)# MLP模型搭建和训练
from keras.models import Sequential
vgg_model=Sequential()
from keras.layers import Dense
vgg_model.add(Dense(units=10, input_dim=25088, activation='relu'))
vgg_model.add(Dense(units=1, activation='sigmoid'))
vgg_model.compile(optimizer='adam', metrics=['accuracy'], loss='binary_crossentropy')
vgg_model.fit(x_train, y_train, epochs=50)
vgg_model.summary()# ==========================================================================
# 训练集预测
y_train_predict=vgg_model.predict(x_train)
y_train_predict=np.argmax(y_train_predict, axis=1)
print(y_train_predict.shape)
# 计算train准确率
from sklearn.metrics import accuracy_score
accuracy_score=accuracy_score(y_train, y_train_predict)
print("accuracy is ", accuracy_score)# 测试集预测
y_test_predict=vgg_model.predict(x_test)
y_test_predict=np.argmax(y_test_predict, axis=1)
print(y_test_predict.shape)
# 计算test准确率
from sklearn.metrics import accuracy_score
accuracy_score=accuracy_score(y_test, y_test_predict)
print("accuracy is ", accuracy_score)# ====================================================================
#  在网上下载图片, 进行随机测试
from keras.preprocessing.image import load_img, img_to_array
pic_animal=r"C:\Users\鹰\Desktop\Dog+Cat\11.jpg"
pic_animal=load_img(pic_animal, target_size=(224,224))
pic_animal=img_to_array(pic_animal)
x_train=np.expand_dims(pic_animal, axis=0)
x_train=preprocess_input(x_train)
# 特征提取
features=vgg.predict(x_train)
x=features.reshape(1, -1)
print(x.shape)
print(features.shape)
y_predict=vgg_model.predict(x)
import numpy as np
y_predict=np.argmax(y_predict, axis=1)
print("result is :", y_predict)
# 结果为0--猫, 结果为1--狗
结果是...

 

 4.2: 千问大模型修改后的

import numpy as np
from keras.preprocessing.image import load_img, img_to_array
from keras.applications.vgg16 import preprocess_input, VGG16
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from sklearn.metrics import accuracy_score
import os# 1. 数据集导入, 单张图片导入, 可以通过load_image导入
img_path = r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dogs\dog.1.jpg"
img_data = load_img(img_path, target_size=(224, 224))
img_data = img_to_array(img_data)
print("Single image shape:", img_data.shape)# 2. 模型搭建和模型训练
def model_prepro(model, img_path):img_data = load_img(img_path, target_size=(224, 224))img_array = img_to_array(img_data)x_train = np.expand_dims(img_array, axis=0)x_train = preprocess_input(x_train)x_vgg = model.predict(x_train)x_vgg = x_vgg.reshape(1, -1)return x_vgg# 加载 VGG16 模型
vgg = VGG16(include_top=False, weights='imagenet')# 处理 cat 文件夹
file_path1 = r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\cat"
img_name_list = os.listdir(file_path1)
img_list = [i for i in img_name_list if os.path.splitext(i)[1].lower() == '.jpg']
img_path_list = [os.path.join(file_path1, i) for i in img_list]
img_feature_array1 = np.zeros([len(img_path_list), 25088])
for i in range(len(img_path_list)):img_feature = model_prepro(vgg, img_path_list[i])img_feature_array1[i] = img_featureprint(f"Processing: {img_list[i]} (Cat)")# 处理 dog 文件夹
file_path2 = r"C:\Users\鹰\Desktop\ML_Set\dog_cat_class\training_set\dog"
img_name_list = os.listdir(file_path2)
img_list = [i for i in img_name_list if os.path.splitext(i)[1].lower() == '.jpg']
img_path_list = [os.path.join(file_path2, i) for i in img_list]
img_feature_array2 = np.zeros([len(img_path_list), 25088])
for i in range(len(img_path_list)):img_feature = model_prepro(vgg, img_path_list[i])img_feature_array2[i] = img_featureprint(f"Processing: {img_list[i]} (Dog)")print("Feature array shapes:", img_feature_array1.shape, img_feature_array2.shape)# 创建标签
y1 = np.zeros(len(img_feature_array1))
y2 = np.ones(len(img_feature_array2))# 合并特征和标签
x_all = np.concatenate((img_feature_array1, img_feature_array2), axis=0)
y_all = np.concatenate((y1, y2), axis=0)
y_all = y_all.reshape(-1, 1)
print("Combined data shapes:", x_all.shape, y_all.shape)# 分割数据集
x_train, x_test, y_train, y_test = train_test_split(x_all, y_all, test_size=0.3, random_state=10)
print("Data split shapes:", x_train.shape, x_test.shape, y_train.shape, y_test.shape)# MLP模型搭建和训练
vgg_model = Sequential()
vgg_model.add(Dense(units=128, input_dim=25088, activation='relu'))
vgg_model.add(Dense(units=64, activation='relu'))
vgg_model.add(Dense(units=1, activation='sigmoid'))
vgg_model.compile(optimizer='adam', metrics=['accuracy'], loss='binary_crossentropy')
vgg_model.fit(x_train, y_train, epochs=100, batch_size=32, validation_data=(x_test, y_test))
vgg_model.summary()# 训练集预测
y_train_predict = vgg_model.predict(x_train)
y_train_predict = (y_train_predict > 0.5).astype(int)  # 使用阈值 0.5 进行二分类
print("Train prediction shape:", y_train_predict.shape)# 计算train准确率
train_accuracy = accuracy_score(y_train, y_train_predict)
print("Train accuracy is:", train_accuracy)# 测试集预测
y_test_predict = vgg_model.predict(x_test)
y_test_predict = (y_test_predict > 0.5).astype(int)  # 使用阈值 0.5 进行二分类
print("Test prediction shape:", y_test_predict.shape)# 计算test准确率
test_accuracy = accuracy_score(y_test, y_test_predict)
print("Test accuracy is:", test_accuracy)# 在网上下载图片, 进行随机测试
pic_animal = r"C:\Users\鹰\Desktop\Dog+Cat\11.jpg"
pic_animal = load_img(pic_animal, target_size=(224, 224))
pic_animal = img_to_array(pic_animal)
x_train = np.expand_dims(pic_animal, axis=0)
x_train = preprocess_input(x_train)# 特征提取
features = vgg.predict(x_train)
x = features.reshape(1, -1)
print("Feature shape:", x.shape)
print("Feature shape before reshape:", features.shape)# 预测
y_predict = vgg_model.predict(x)
y_predict = (y_predict > 0.5).astype(int)  # 使用阈值 0.5 进行二分类
print("Prediction result is:", "猫" if y_predict[0][0] == 0 else "狗")
 结果是...

这对我的心灵的伤害是百分百的暴击, 我的是反面教材........

想要看正版规范代码, 就看第二个,     当然, 

如果觉得50%的成功率还行的话,   那我的勉强也能看

兄弟们不嫌弃的话, 也可以看看,     吸取一下经验教训, 看个乐子

5.扩展

        扩展1:

                keras.models 模块中的主要组成部分:

                        1.Sequential 模型是一种线性堆叠的层结构,适用于大多数简单的神经网络

                        2.Functional API 是一种更灵活的模型构建方式,允许创建复杂的非线性拓扑结构

        扩展2:

                keras.applications 导入 VGG16 时,你可以得到以下主要部分:

                       VGG16 Model: 这是整个 VGG16 网络模型,可以直接用来进行预测或者作为迁移学习的基础。

                       Preprocess Input: 一个函数,用于对输入图像数据进行预处理,以便与 VGG16 模型兼容。实现:from keras.applications.vgg16 import preprocess_input。

                      Decode Predictions: 一个函数,用于将 VGG16 模型的输出转换为人类可读的标签。实现:from keras.applications.vgg16 import decode_predictions。

                        Weights: 预训练的权重文件。这些权重是在 ImageNet 数据集上训练得到的,可以帮助你在自己的任务上快速获得较好的性能。

 6.数据集链接:

官网:

Cat and Dog | KaggleCats and Dogs dataset to train a DL modelicon-default.png?t=O83Ahttps://www.kaggle.com/datasets/tongpython/cat-and-dog?resource=download

百度网盘分享:

链接:https://pan.baidu.com/s/1T1mymwIqOOF3MKfWxRtnpQ 
提取码:6axn

相关文章:

深度学习-卷积神经网络-基于VGG16模型, 实现猫狗二分类(文末附带数据集下载链接, 长期有效)

简介: 1.基于VGG16模型进行特征提取, 结合mlp实现猫狗二分类 2.训练数据--"dog_cat_class\training_set" 3.模型训练流程 1.对图像数据进行导入和预处理 2.搭建模型, 导入VGG16模型, 去除mlp层, 将经过VGG16训练后的数据作为输入, 输入到自建的mlp层中进行训练, 要…...

计算Java集合占用的空间【详解】

以ArrayList为例,假设集合元素类型是Person类型,假设集合容量为10,目前有两个person对象{name:“Jack”,age12} {name:“Tom”,age14} public class Person{private String name;private int age; }估算Person对象占用的大小: 对…...

仕考网:关于中级经济师考试的介绍

中级经济师考试是一种职称考试,每年举办一次,报名时间在7-8月,考试时间在10-11月 报名入口:中guo人事考试网 报名条件: 1.高中毕业并取得初级经济专业技术资格,从事相关专业工作满10年; 2.具备大学专科…...

SYN590RL 300MHz至450MHz ASK接收机芯片IC

一般描述 SYN590RL是赛诺克全新开发设计的一款宽电压范围,低功耗,高性能,无需外置AGC电容,灵敏度达到典型-110dBm,300MHz”450MHz 频率范围应用的单芯片ASK或OOK射频接收器。 SYN59ORL是一款典型的即插即用型单片高集成度无线接收器&…...

15分钟学 Go 第 20 天:Go的错误处理

第20天:Go的错误处理 目标 学习如何处理错误,以确保Go程序的健壮性和可维护性。 1. 错误处理的重要性 在开发中,错误处理至关重要。程序在运行时可能会出现各种问题,例如文件未找到、网络连接失败等。正确的错误处理能帮助我们…...

C++——string的模拟实现(上)

目录 引言 成员变量 1.基本框架 成员函数 1.构造函数和析构函数 2.拷贝构造函数 3.容量操作函数 3.1 有效长度和容量大小 3.2 容量操作 3.3 访问操作 (1)operator[]函数 (2)iterator迭代器 3.4 修改操作 (1)push_back()和append() (2)operator函数 引言 在 C—…...

JavaCV 之均值滤波:图像降噪与模糊的权衡之道

🧑 博主简介:CSDN博客专家,历代文学网(PC端可以访问:https://literature.sinhy.com/#/literature?__c1000,移动端可微信小程序搜索“历代文学”)总架构师,15年工作经验,…...

桥接模式,外界与主机通,与虚拟机不通

一 二 在此选择Windows与外界连接的网卡,通过有线连就选有线网卡,通过无线连就选无线网卡。 三 如果需要设置固定IP,则选择"Manual"进行设置。我这边根据实际需要,走无线的时候用DHCP,走有线的时候设固定IP…...

用HTML构建酷炫的文件上传下载界面

1. 基础HTML结构 首先&#xff0c;我们构建一个基本的HTML结构&#xff0c;包括一个表单用于文件上传&#xff0c;以及一个列表用于展示已上传文件&#xff1a; HTML <!DOCTYPE html> <html> <head><title>酷炫文件上传下载</title><link …...

Gateway 统一网关

一、初识 Gateway 1. 为什么需要网关 我们所有的服务可以让任何请求访问&#xff0c;但有些业务不是对外公开的&#xff0c;这就需要用网关来统一替我们筛选请求&#xff0c;它就像是房间的一道门&#xff0c;想进入房间就必须经过门。而请求想要访问微服务&#xff0c;就必须…...

7 种常见的前端攻击

大家都知道&#xff0c;保证网站的安全是十分重要的&#xff0c;一旦网站被攻陷&#xff0c;就有可能造成用户的经济损失&#xff0c;隐私泄露&#xff0c;网站功能被破坏&#xff0c;或者是传播恶意病毒等重大危害。所以下面我们就来讲讲7 种常见的前端攻击。 1. 跨站脚本 (X…...

element plus实现点击上传于链接上传并且回显到upload组件中

摘要&#xff1a; 今天遇到一个问题&#xff1a;vue3使用elemnt plus的上传图片时&#xff0c;数据是从别人的系统导出来的商品&#xff0c;图片是http的形式的&#xff0c;并且商品很多的&#xff0c;一个一个下载下来再上传很麻烦的&#xff0c;所以本系统插件商品时图片使用…...

ELK日志分析系统部署

ELK日志分析系统 ELK指的是ElasticsearchLogstashKibana这种架构的缩写。 ELK是一种日志分析平台&#xff0c;在很早之前我们经常使用Shell三剑客&#xff08;一般泛指grep、sed、awk&#xff09;来进行日志分析&#xff0c;这种方式虽然也可以应对多种场景&#xff0c;但是当…...

驾校小程序:一站式学车解决方案的设计与实践

​​一、引言 随着移动互联网技术的飞速发展&#xff0c;人们的生活方式和消费习惯正在发生深刻变化。驾校作为传统的服务行业&#xff0c;也面临着数字化转型的迫切需求。驾校小程序作为一种轻量级的应用&#xff0c;能够为用户提供便捷、丰富的学车服务&#xff0c;成…...

【自然语言处理】BERT模型

BERT&#xff1a;Bidirectional Encoder Representations from Transformers BERT 是 Google 于 2018 年提出的 自然语言处理&#xff08;NLP&#xff09;模型&#xff0c;它基于 Transformer 架构的 Encoder 部分。BERT 的出现极大提升了 NLP 任务的性能&#xff0c;如问答系…...

Android 添加如下飞行模式(飞行模式开和关、飞行模式开关菜单显示隐藏)接口

请添加如下飞行模式(飞行模式开关、飞行模式开关显示隐藏)接口: 飞行模式飞行模式开关com.action.airplankey: enable value:boolean true open the airplan false close the airplan关闭Intent intent = new Intent(); intent.setAction("com.action.airplan");…...

【Vue3】基于 Vue3 + ECharts 实现北京市区域地图可视化

文章目录 基于 Vue3 ECharts 实现北京市区域地图可视化1、引言2、项目初始化2.1、环境搭建2.2 、安装依赖2.3、项目结构 3、地图数据准备3.1、地图 JSON 文件获取&#xff08;具体的json数据&#xff09; 4、 组件开发4.1、Map 组件的设计思路4.2、基础结构实现4.3、核心数据结…...

【IC】什么是min period check

在 Synopsys Primetime 工具中可以检查.lib 文件中时钟输入的最小周期。想象这样一个场景&#xff0c;有一个设计 A&#xff0c;它有一个名为 clk 的时钟&#xff0c;并且该设计的 clk 周期被设定为一个值&#xff0c;比如 2 纳秒&#xff0c;即 500MHz。假设我们在进行静态时序…...

MyBatis入门之一对多关联关系(示例)

【图书介绍】《SpringSpring MVCMyBatis从零开始学&#xff08;视频教学版&#xff09;&#xff08;第3版&#xff09;》-CSDN博客 《SpringSpring MVCMyBatis从零开始学(视频教学版)&#xff08;第3版&#xff09;》(杨章伟&#xff0c;刘祥淼)【摘要 书评 试读】- 京东图书 …...

【Git 】Windows 系统下 Git 文件名大小写不敏感

背景 在 Windows 系统上&#xff0c;Git 对文件名大小写的不敏感性问题确实存在。由于 Windows 文件系统&#xff08;如 NTFS &#xff09;在默认情况下不区分文件名大小写所导致的。 原因分析 文件系统差异 Windows文件系统&#xff08;如 NTFS&#xff09;默认不区分文件名…...

下载**Qwen3.5-35B-A3B**的GGUF格式文件

要下载Qwen3.5-35B-A3B的GGUF格式文件&#xff0c;可通过Hugging Face&#xff08;国际主流&#xff09;或ModelScope&#xff08;国内镜像&#xff09;平台获取&#xff0c;以下是具体步骤和注意事项&#xff1a; 一、核心下载地址 Qwen3.5-35B-A3B的GGUF文件主要由Unsloth团队…...

ArdaTask:面向MCU的轻量级时间驱动任务调度框架

1. 项目概述ArdaTask 是一个面向嵌入式系统的轻量级、时间驱动型多任务调度框架&#xff0c;其设计目标明确&#xff1a;在资源受限的MCU&#xff08;如Cortex-M0/M3/M4、RISC-V内核&#xff09;上实现确定性、低开销、无动态内存分配的周期性任务管理。它不替代RTOS&#xff0…...

雪女-斗罗大陆-造相Z-Turbo实战:卷积神经网络(CNN)特征与生成图像的风格融合

雪女-斗罗大陆-造相Z-Turbo实战&#xff1a;卷积神经网络&#xff08;CNN&#xff09;特征与生成图像的风格融合 最近在玩一个挺有意思的东西&#xff0c;就是把现实世界照片里的“感觉”提取出来&#xff0c;然后让AI照着这个“感觉”去画一张斗罗大陆风格的画。听起来有点玄…...

导师推荐!盘点2026年好评如潮的AI论文平台

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文平台正在席卷学术圈&#xff0c;覆盖选题构思、文献综述、内容生成、降重润色与格式排版全流程&#xff0c;真正帮你高效搞定论文写作。 一、全流程王者&#xff1a;一站式搞定论文全链路&…...

如何用智能工具提升暗黑破坏神3战斗效率:D3KeyHelper全功能指南

如何用智能工具提升暗黑破坏神3战斗效率&#xff1a;D3KeyHelper全功能指南 【免费下载链接】D3keyHelper D3KeyHelper是一个有图形界面&#xff0c;可自定义配置的暗黑3鼠标宏工具。 项目地址: https://gitcode.com/gh_mirrors/d3/D3keyHelper 暗黑破坏神3的战斗节奏快…...

企业级跨平台字体架构:PingFangSC现代化部署方案

企业级跨平台字体架构&#xff1a;PingFangSC现代化部署方案 【免费下载链接】PingFangSC PingFangSC字体包文件、苹果平方字体文件&#xff0c;包含ttf和woff2格式 项目地址: https://gitcode.com/gh_mirrors/pi/PingFangSC 在数字产品设计日益精细化的今天&#xff0c…...

新手必须掌握的6个Python爬虫库,非常实用!

Python中有非常多用于网络数据采集的库&#xff0c;功能非常强大&#xff0c;有的用于抓取网页&#xff0c;有的用于解析网页&#xff0c;这里介绍6个最常用的库。 1. BeautifulSoup BeautifulSoup是最常用的Python网页解析库之一&#xff0c;可将 HTML 和 XML 文档解析为树形…...

如何用免费AI助手提升3倍编码效率?DeepSeek-Coder-V2全解析

如何用免费AI助手提升3倍编码效率&#xff1f;DeepSeek-Coder-V2全解析 【免费下载链接】DeepSeek-Coder-V2 项目地址: https://gitcode.com/GitHub_Trending/de/DeepSeek-Coder-V2 在AI编程工具层出不穷的今天&#xff0c;开发者面临着一个关键选择&#xff1a;是为商…...

ESLint-Plugin-React 终极配置指南:如何创建适合不同团队的个性化规则组合

ESLint-Plugin-React 终极配置指南&#xff1a;如何创建适合不同团队的个性化规则组合 【免费下载链接】eslint-plugin-react React-specific linting rules for ESLint 项目地址: https://gitcode.com/gh_mirrors/es/eslint-plugin-react ESLint-Plugin-React 是一个专…...

【NSudo】功能定位:开源权限管理工具的系统运维解决方案

【NSudo】功能定位&#xff1a;开源权限管理工具的系统运维解决方案 【免费下载链接】NSudo [Deprecated, work in progress alternative: https://github.com/M2Team/NanaRun] Series of System Administration Tools 项目地址: https://gitcode.com/gh_mirrors/ns/NSudo …...