新闻详情

新闻详情

首页 / 资讯中心 / 详情

T8-猫狗识别

发布时间:2026/9/26 16:15:38来源:尧图网络
T8-猫狗识别
● 本文为365天深度学习训练营中的学习记录博客● 原作者K同学啊一、前期准备1.设置GPUimport tensorflow as tf gpus tf.config.list_physical_devices(GPU) if gpus: tf.config.experimental.set_memory_growth(gpus[0], True) #设置GPU显存用量按需使用 tf.config.set_visible_devices([gpus[0]],GPU)2.导入数据import matplotlib.pyplot as plt # 支持中文 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号 import os,PIL,pathlib #隐藏警告 import warnings warnings.filterwarnings(ignore) data_dir D:/新建文件夹/365-7-data data_dir pathlib.Path(data_dir) image_count len(list(data_dir.glob(*/*))) print(图片总数为,image_count)图片总数为 34003.加载数据batch_size 8 img_height 224 img_width 224 train_ds tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split0.2, subsettraining, seed12, image_size(img_height, img_width), batch_sizebatch_size) val_ds tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split0.2, subsetvalidation, seed12, image_size(img_height, img_width), batch_sizebatch_size)Found 3400 files belonging to 2 classes. Using 2720 files for training. Found 3400 files belonging to 2 classes. Using 680 files for validation.class_names train_ds.class_names print(class_names)[cat, dog]4.再次检查数据for image_batch, labels_batch in train_ds: print(image_batch.shape) print(labels_batch.shape) break(8, 224, 224, 3) (8,)5.配置数据集AUTOTUNE tf.data.AUTOTUNE def preprocess_image(image,label): return (image/255.0,label) # 归一化处理 train_ds train_ds.map(preprocess_image, num_parallel_callsAUTOTUNE) val_ds val_ds.map(preprocess_image, num_parallel_callsAUTOTUNE) train_ds train_ds.cache().shuffle(1000).prefetch(buffer_sizeAUTOTUNE) val_ds val_ds.cache().prefetch(buffer_sizeAUTOTUNE)6.数据可视化plt.figure(figsize(15, 10)) for images, labels in train_ds.take(1): for i in range(8): ax plt.subplot(5, 8, i 1) plt.imshow(images[i]) plt.title(class_names[labels[i]]) plt.axis(off)二、建立VGG-16模型from tensorflow.keras import layers, models, Input from tensorflow.keras.models import Model from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout def VGG16(nb_classes, input_shape): input_tensor Input(shapeinput_shape) # 1st block x Conv2D(64, (3,3), activationrelu, paddingsame,nameblock1_conv1)(input_tensor) x Conv2D(64, (3,3), activationrelu, paddingsame,nameblock1_conv2)(x) x MaxPooling2D((2,2), strides(2,2), name block1_pool)(x) # 2nd block x Conv2D(128, (3,3), activationrelu, paddingsame,nameblock2_conv1)(x) x Conv2D(128, (3,3), activationrelu, paddingsame,nameblock2_conv2)(x) x MaxPooling2D((2,2), strides(2,2), name block2_pool)(x) # 3rd block x Conv2D(256, (3,3), activationrelu, paddingsame,nameblock3_conv1)(x) x Conv2D(256, (3,3), activationrelu, paddingsame,nameblock3_conv2)(x) x Conv2D(256, (3,3), activationrelu, paddingsame,nameblock3_conv3)(x) x MaxPooling2D((2,2), strides(2,2), name block3_pool)(x) # 4th block x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock4_conv1)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock4_conv2)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock4_conv3)(x) x MaxPooling2D((2,2), strides(2,2), name block4_pool)(x) # 5th block x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock5_conv1)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock5_conv2)(x) x Conv2D(512, (3,3), activationrelu, paddingsame,nameblock5_conv3)(x) x MaxPooling2D((2,2), strides(2,2), name block5_pool)(x) # full connection x Flatten()(x) x Dense(4096, activationrelu, namefc1)(x) x Dense(4096, activationrelu, namefc2)(x) output_tensor Dense(nb_classes, activationsoftmax, namepredictions)(x) model Model(input_tensor, output_tensor) return model modelVGG16(1000, (img_width, img_height, 3)) model.summary()三、编译model.compile(optimizeradam, loss sparse_categorical_crossentropy, metrics [accuracy])四、模型训练from tqdm import tqdm import tensorflow.keras.backend as K epochs 10 lr 1e-4 # 记录训练数据方便后面的分析 history_train_loss [] history_train_accuracy [] history_val_loss [] history_val_accuracy [] for epoch in range(epochs): train_total len(train_ds) val_total len(val_ds) with tqdm(totaltrain_total, descfEpoch {epoch 1}/{epochs},mininterval1,ncols100) as pbar: lr lr*0.92 K.set_value(model.optimizer.lr, lr) for image,label in train_ds: history model.train_on_batch(image,label) train_loss history[0] train_accuracy history[1] pbar.set_postfix({loss: %.4f%train_loss, accuracy:%.4f%train_accuracy, lr: K.get_value(model.optimizer.lr)}) pbar.update(1) history_train_loss.append(train_loss) history_train_accuracy.append(train_accuracy) print(开始验证) with tqdm(totalval_total, descfEpoch {epoch 1}/{epochs},mininterval0.3,ncols100) as pbar: for image,label in val_ds: history model.test_on_batch(image,label) val_loss history[0] val_accuracy history[1] pbar.set_postfix({loss: %.4f%val_loss, accuracy:%.4f%val_accuracy}) pbar.update(1) history_val_loss.append(val_loss) history_val_accuracy.append(val_accuracy) print(结束验证) print(验证loss为%.4f%val_loss) print(验证准确率为%.4f%val_accuracy)结果可视化from datetime import datetime current_time datetime.now() # 获取当前时间 epochs_range range(epochs) plt.figure(figsize(12, 4)) plt.subplot(1, 2, 1) plt.plot(epochs_range, history_train_accuracy, labelTraining Accuracy) plt.plot(epochs_range, history_val_accuracy, labelValidation Accuracy) plt.legend(loclower right) plt.title(Training and Validation Accuracy) plt.xlabel(current_time) plt.subplot(1, 2, 2) plt.plot(epochs_range, history_train_loss, labelTraining Loss) plt.plot(epochs_range, history_val_loss, labelValidation Loss) plt.legend(locupper right) plt.title(Training and Validation Loss) plt.show()可以发现从第二轮起训练集和验证集的准确率就达到100%且一直保持这个训练结果明显是不正常的回头检查代码可以发现VGG-16的网络结构中缺少了dropout层以及分类数写成了1000而不是2。对模型后面部分做了更改后x Flatten()(x) x Dense(4096, activationrelu, namefc1)(x) x Dropout(0.5)(x) x Dense(4096, activationrelu, namefc2)(x) x Dropout(0.5)(x) output_tensor Dense(nb_classes, activationsoftmax, namepredictions)(x) model Model(input_tensor, output_tensor) return model modelVGG16(2, (img_width, img_height, 3)) model.summary()此时训练集准确率只有50%乱猜级别个人总结本周使用VGG-16模型进行猫狗识别有问题后做了以下尝试使用VGG-16的迁移学习测试集准确率50%验证集37.5%去掉一个Dropout层结果同上把batch_size改成16验证集25%使用另一个简易模型loss没有任何变化。本周没有把代码改好下周参照文章再继续修改。
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

video-use:用ffmpeg和Claude Code搭建自动化视频处理流水线 2026/9/26 19:42:49

video-use:用ffmpeg和Claude Code搭建自动化视频处理流水线

1. 从“video-use”这个标题说起:它到底想解决什么问题第一次看到“video-use”这个标题,我脑子里蹦出来的不是某个具体工具,而是一类需求:用代码和命令行把视频处理这件事自动化起来。结合热搜词里高频出现的 Claude Code、ffmpe…

阅读更多 →
Substrate区块链开发框架入门:从核心概念到本地链实操 2026/9/26 19:42:43

Substrate区块链开发框架入门:从核心概念到本地链实操

1. 从零认识 Substrate:它到底是什么,能解决什么问题第一次听到 Substrate 这个词,很多人会以为是某个前端框架或者构建工具。其实不是。Substrate 是一个用于构建区块链的开发框架,由 Parity Technologies 团队打造,最…

阅读更多 →
DeepOpen × Banking77 复现指南:Laya 决策引擎的 77 类银行意图分类实战 2026/9/26 19:42:30

DeepOpen × Banking77 复现指南:Laya 决策引擎的 77 类银行意图分类实战

【免费下载链接】deepopen 非自回归System 1决策引擎,专为结构化类型决策场景设计 DeepOpen Multilingual, non-autoregressive System 1 decision engine. 项目地址: https://gitcode.com/gh_mirrors/de/deepopen 点击查看 免费下载 本指南完整讲解在…

阅读更多 →
Arthas 已接入 MCP:用 JSON-RPC 打通 JVM 线上问题定位链路 2026/9/26 19:42:17

Arthas 已接入 MCP:用 JSON-RPC 打通 JVM 线上问题定位链路

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
AI 说得很流畅,不代表它说得对-CSDN博客 2026/9/26 19:42:11

AI 说得很流畅,不代表它说得对-CSDN博客

首屏导读 本教程配套付费专栏: 大模型工程师修炼手记 19.9 元(AI 编程 / Agent 实战 | 本文同主题系统课程) AI时代程序员的自我提升 49.9 元(AI 时代成长方法论)。 单篇不过瘾?订阅解锁全量源…

阅读更多 →
CRM系统选型与落地:从通信集成到客户管理实战 2026/9/26 19:41:58

CRM系统选型与落地:从通信集成到客户管理实战

前因我在一次销售运营复盘会上第一次注意到 DeskcommCRM。当时团队的数据是这样的:外呼量上去了,商机数却没涨,翻客户跟进记录时,电话内容在手机通话记录里,邮件往来散落在个人邮箱,报价单和合同在另一个文…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉