YOLOv4-tiny Darknet 目标检测训练实战:轻量模型配置、训练与推理
YOLOv4-tiny Darknet 目标检测训练实战轻量模型配置、训练与推理这篇教程根据我复现 YOLOv4-tiny Darknet 训练流程时整理重点演示 cuDNN 环境、Darknet 编译、YOLO 数据整理、配置生成和测试集推理。本文整理自我的学习和项目复现过程尽量按实操顺序保留 notebook 的关键步骤同时把数据集获取方式调整为适合中文教程发布的写法。本文会重点跑通以下流程配置 Colab CUDA/cuDNN 环境安装并编译 Darknet从数据集后台获取 YOLO Darknet 格式数据动态生成 YOLOv4-tiny 自定义配置训练模型并随机选择测试图推理如果你正在系统学习目标检测、实例分割、OCR、多目标跟踪或视觉大模型建议收藏本文配套 notebook、示例图片和运行环境说明后续会继续整理。如果环境配置卡住可以在评论区说明具体报错。 文章目录YOLOv4-tiny Darknet 目标检测训练实战轻量模型配置、训练与推理⚙️ 配置 CUDA/cuDNN 安装 Darknet 从数据集后台获取 Darknet 数据 整理训练文件 生成训练配置️ 训练 YOLOv4-tiny 推理工具函数 小结 同系列教程汇总⚙️ 配置 CUDA/cuDNNYOLOv4-tiny Darknet 对 CUDA/cuDNN 配置敏感先检查运行环境。# 检查 CUDA 是否已安装以及当前版本。!/usr/local/cuda/bin/nvcc--version# We need to install the correct cuDNN according to this output# 查看当前 GPU 型号!nvidia-smi# This cell ensures you have the correct architecture for your respective GPU# If you command is not found, look through these GPUs, find the respective# GPU and add them to the archTypes dictionary# Tesla V100# ARCH -gencode archcompute_70,code[sm_70,compute_70]# Tesla K80# ARCH -gencode archcompute_37,codesm_37# GeForce RTX 2080 Ti, RTX 2080, RTX 2070, Quadro RTX 8000, Quadro RTX 6000, Quadro RTX 5000, Tesla T4, XNOR Tensor Cores# ARCH -gencode archcompute_75,code[sm_75,compute_75]# Jetson XAVIER# ARCH -gencode archcompute_72,code[sm_72,compute_72]# GTX 1080, GTX 1070, GTX 1060, GTX 1050, GTX 1030, Titan Xp, Tesla P40, Tesla P4# ARCH -gencode archcompute_61,codesm_61# GP100/Tesla P100 - DGX-1# ARCH -gencode archcompute_60,codesm_60# For Jetson TX1, Tegra X1, DRIVE CX, DRIVE PX - uncomment:# ARCH -gencode archcompute_53,code[sm_53,compute_53]# For Jetson Tx2 or Drive-PX2 uncomment:# ARCH -gencode archcompute_62,code[sm_62,compute_62]importos os.environ[GPU_TYPE]str(os.popen(nvidia-smi --query-gpuname --formatcsv,noheader).read())defgetGPUArch(argument):try:argumentargument.strip()# All Colab GPUsarchTypes{Tesla V100-SXM2-16GB:-gencode archcompute_70,code[sm_70,compute_70],Tesla K80:-gencode archcompute_37,codesm_37,Tesla T4:-gencode archcompute_75,code[sm_75,compute_75],Tesla P40:-gencode archcompute_61,codesm_61,Tesla P4:-gencode archcompute_61,codesm_61,Tesla P100-PCIE-16GB:-gencode archcompute_60,codesm_60}returnarchTypes[argument]exceptKeyError:returnGPU must be added to GPU Commandsos.environ[ARCH_VALUE]getGPUArch(os.environ[GPU_TYPE])print(GPU Type: os.environ[GPU_TYPE])print(ARCH Value: os.environ[ARCH_VALUE]) 安装 Darknet克隆并编译 Darknet开启 GPU、cuDNN 和 OpenCV 支持。%cd/content/%rm-rf darknet# 克隆 Darknet 仓库用于 YOLOv4-tiny 训练!git clone https://github.com/AlexeyAB/darknet.git#install environment from the Makefile%cd/content/darknet/# compute_37, sm_37 for Tesla K80# compute_75, sm_75 for Tesla T4# !sed -i s/ARCH -gencode archcompute_60,codesm_60/ARCH -gencode archcompute_75,codesm_75/g Makefile#install environment from the Makefile#note if you are on Colab Pro this works on a P100 GPU#if you are on Colab free, you may need to change the Makefile for the K80 GPU#this goes for any GPU, you need to change the Makefile to inform darknet which GPU you are running on.!sed-is/OPENCV0/OPENCV1/gMakefile !sed-is/GPU0/GPU1/gMakefile !sed-is/CUDNN0/CUDNN1/gMakefile !sed-is/ARCH -gencode archcompute_60,codesm_60/ARCH ${ARCH_VALUE}/gMakefile !make#download the newly released yolov4-tiny weights%cd/content/darknet !wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights !wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29fromtypesimportSimpleNamespace# 从数据集后台下载 YOLO Darknet 格式数据集后修改 DATASET_DIR 指向解压目录。DATASET_DIR/content/dataset# 修改为数据集后台导出的数据集目录datasetSimpleNamespace(locationDATASET_DIR,version1,namecustom-dataset) 从数据集后台获取 Darknet 数据从数据集后台导出 YOLO Darknet 格式数据并接入训练目录。fromtypesimportSimpleNamespace# 从数据集后台下载 YOLO Darknet 检测 格式数据集后修改 DATASET_DIR 指向解压目录。DATASET_DIR/content/dataset# 修改为数据集后台导出的数据集目录datasetSimpleNamespace(locationDATASET_DIR,version1,namecustom-dataset)# 数据集已在上一单元配置如需更换数据请修改 DATASET_DIR。print(dataset.location) 整理训练文件复制图片、标签和类别文件生成 Darknet 训练所需的 obj.data。# 根据类别数量动态生成配置#we build iteratively from base config files. This is the same file shape as cfg/yolo-obj.cfgdeffile_len(fname):withopen(fname)asf:fori,linenumerate(f):passreturni1num_classesfile_len(dataset.location/train/_darknet.labels)max_batchesnum_classes*2000steps1.8*max_batches steps2.9*max_batches steps_strstr(steps1),str(steps2)num_filters(num_classes5)*3print(writing config for a custom YOLOv4 detector detecting number of classes: str(num_classes))#Instructions from the darknet repo#change line max_batches to (classes*2000 but not less than number of training images, and not less than 6000), f.e. max_batches6000 if you train for 3 classes#change line steps to 80% and 90% of max_batches, f.e. steps4800,5400ifos.path.exists(./cfg/custom-yolov4-tiny-detector.cfg):os.remove(./cfg/custom-yolov4-tiny-detector.cfg)#customize iPython writefile so we can write variablesfromIPython.core.magicimportregister_line_cell_magicregister_line_cell_magicdefwritetemplate(line,cell):withopen(line,w)asf:f.write(cell.format(**globals())) 生成训练配置根据类别数动态修改 filters、classes、max_batches 等配置。%%writetemplate./cfg/custom-yolov4-tiny-detector.cfg[net]# Testing#batch1#subdivisions1# Trainingbatch64subdivisions24width416height416channels3momentum0.9decay0.0005angle0saturation1.5exposure1.5hue.1learning_rate0.00261burn_in1000max_batches{max_batches}policysteps steps{steps_str}scales.1,.1[convolutional]batch_normalize1filters32size3stride2pad1activationleaky[convolutional]batch_normalize1filters64size3stride2pad1activationleaky[convolutional]batch_normalize1filters64size3stride1pad1activationleaky[route]layers-1groups2group_id1[convolutional]batch_normalize1filters32size3stride1pad1activationleaky[convolutional]batch_normalize1filters32size3stride1pad1activationleaky[route]layers-1,-2[convolutional]batch_normalize1filters64size1stride1pad1activationleaky[route]layers-6,-1[maxpool]size2stride2[convolutional]batch_normalize1filters128size3stride1pad1activationleaky[route]layers-1groups2group_id1[convolutional]batch_normalize1filters64size3stride1pad1activationleaky[convolutional]batch_normalize1filters64size3stride1pad1activationleaky[route]layers-1,-2[convolutional]batch_normalize1filters128size1stride1pad1activationleaky[route]layers-6,-1[maxpool]size2stride2[convolutional]batch_normalize1filters256size3stride1pad1activationleaky[route]layers-1groups2group_id1[convolutional]batch_normalize1filters128size3stride1pad1activationleaky[convolutional]batch_normalize1filters128size3stride1pad1activationleaky[route]layers-1,-2[convolutional]batch_normalize1filters256size1stride1pad1activationleaky[route]layers-6,-1[maxpool]size2stride2[convolutional]batch_normalize1filters512size3stride1pad1activationleaky##################################[convolutional]batch_normalize1filters256size1stride1pad1activationleaky[convolutional]batch_normalize1filters512size3stride1pad1activationleaky[convolutional]size1stride1pad1filters{num_filters}activationlinear[yolo]mask3,4,5anchors10,14,23,27,37,58,81,82,135,169,344,319classes{num_classes}num6jitter.3scale_x_y1.05cls_normalizer1.0iou_normalizer0.07iou_lossciou ignore_thresh.7truth_thresh1random0nms_kindgreedynms beta_nms0.6[route]layers-4[convolutional]batch_normalize1filters128size1stride1pad1activationleaky[upsample]stride2[route]layers-1,23[convolutional]batch_normalize1filters256size3stride1pad1activationleaky[convolutional]size1stride1pad1filters{num_filters}activationlinear[yolo]mask1,2,3anchors10,14,23,27,37,58,81,82,135,169,344,319classes{num_classes}num6jitter.3scale_x_y1.05cls_normalizer1.0iou_normalizer0.07iou_lossciou ignore_thresh.7truth_thresh1random0nms_kindgreedynms beta_nms0.6# 查看刚写入的配置文件。#you may consider adjusting certain things#like the number of subdivisions 64 runs faster but Colab GPU may not be big enough#if Colab GPU memory is too small, you will need to adjust subdivisions to 16%cat cfg/custom-yolov4-tiny-detector.cfg!./darknet detector train data/obj.data cfg/custom-yolov4-tiny-detector.cfg yolov4-tiny.conv.29-dont_show-map#If you get CUDA out of memory adjust subdivisions above!#adjust max batches down for shorter training above# 定义图片显示工具函数defimShow(path):importcv2importmatplotlib.pyplotasplt%matplotlib inline imagecv2.imread(path)height,widthimage.shape[:2]resized_imagecv2.resize(image,(3*width,3*height),interpolationcv2.INTER_CUBIC)figplt.gcf()fig.set_size_inches(18,10)plt.axis(off)#plt.rcParams[figure.figsize] [10, 5]plt.imshow(cv2.cvtColor(resized_image,cv2.COLOR_BGR2RGB))plt.show()️ 训练 YOLOv4-tiny启动 Darknet 训练。如果显存不足优先调整 subdivisions。# 检查权重是否已保存#backup houses the last weights for our detector#(file yolo-obj_last.weights will be saved to the build\darknet\x64\backup\ for each 100 iterations)#(file yolo-obj_xxxx.weights will be saved to the build\darknet\x64\backup\ for each 1000 iterations)#After training is complete - get result yolo-obj_final.weights from path build\darknet\x64\bac!ls backup#if it is empty you havent trained for long enough yet, you need to train for at least 100 iterations 推理工具函数定义图片显示工具方便查看 Darknet 推理结果。#coco.names is hardcoded somewhere in the detector%cp data/obj.names data/coco.names# test 目录中包含可用于测试的图片test_images[fforfinos.listdir(test)iff.endswith(.jpg)]importrandom img_pathtest/random.choice(test_images);#test out our detector!!./darknet detect cfg/custom-yolov4-tiny-detector.cfg backup/custom-yolov4-tiny-detector_best.weights{img_path}-dont-show imShow(/content/darknet/predictions.jpg) 小结这篇教程完整整理了YOLOv4-tiny Darknet 目标检测训练的核心复现流程。实际操作时建议先确认 GPU、依赖版本、数据集路径和模型权重路径再逐段运行 notebook。后续我会继续按源项目顺序整理同系列中的目标检测、实例分割、OCR、多目标跟踪和视觉大模型教程。 同系列教程汇总Google Gemini 3.5 Flash 零样本目标检测教程从提示词到可视化结果GLM-OCR 文档识别实战教程从验证码、公式到车牌 OCRRF-DETR ByteTrack 多目标跟踪实战教程从命令行到 Python 视频轨迹可视化SAM 3 图像分割实战教程文本、框和点提示的多种分割方式SAM 3 视频分割实战教程用文本提示分割并跟踪视频中的目标YOLOv4-tiny Darknet 目标检测训练实战轻量模型配置、训练与推理-本文

相关新闻

音频视觉融合新突破:DLA课程Wav2Lip与SadTalker实现会说话的人脸

音频视觉融合新突破:DLA课程Wav2Lip与SadTalker实现会说话的人脸

音频视觉融合新突破:DLA课程Wav2Lip与SadTalker实现会说话的人脸 【免费下载链接】dla Deep learning for audio processing 项目地址: https://gitcode.com/gh_mirrors/dla/dla DLA(Deep Learning for Audio)课程是一个专注于音频深度…

2026/7/22 21:37:50阅读更多 →
CLIP 零样本图像分类实战:无需训练的多类别识别流程

CLIP 零样本图像分类实战:无需训练的多类别识别流程

CLIP 零样本图像分类实战:无需训练的多类别识别流程 这篇教程根据我复现 OpenAI CLIP 分类流程时整理,重点演示依赖安装、分类数据准备、文本 prompt 配置和零样本推理。 本文整理自我的学习和项目复现过程,尽量按实操顺序保留 notebook 的关…

2026/7/22 21:37:50阅读更多 →
2026绥化黄金回收白银回收铂金回收中检持证鉴定师铂金银饰高价回收门店联系方式推荐

2026绥化黄金回收白银回收铂金回收中检持证鉴定师铂金银饰高价回收门店联系方式推荐

2026绥化黄金白银铂金回收实测榜单|公安工商双备案中检持证鉴定师门店推荐 绥化黄金回收哪家靠谱|工商公安双备案中检认证实体门店 依托绥化本地落笔,这座北疆城市近年贵金属回收店铺遍地丛生,行业套路层出不穷,不少市…

2026/7/22 21:35:50阅读更多 →
【JVM原理详解】08-类加载器实战-Tomcat类加载架构

【JVM原理详解】08-类加载器实战-Tomcat类加载架构

类加载器实战:Tomcat类加载架构 前两篇我们学习了双亲委派模型及其被打破的场景(SPI/TCCL),这些更多是"框架层面"的机制。本篇我们将目光投向一个工业级的产品——Apache Tomcat。Tomcat是最流行的Java Web容器之一&…

2026/7/22 22:34:01阅读更多 →
前后端分离架构

前后端分离架构

GoViewBoard 系统中前端和后端的联系: 🏗️ 架构概述 这个系统采用 前后端分离架构,包含两个独立的部分: 后端 (Backend) 技术栈: Go Gin 框架端口: 8080功能: 提供 RESTful API 接口,处理业务逻辑,数据库…

2026/7/22 22:34:01阅读更多 →
如何将iPad备份到外置硬盘(4种经过验证的方法)

如何将iPad备份到外置硬盘(4种经过验证的方法)

现代 iPad,尤其是搭载 M2/M4 芯片的 iPad Pro 和 iPad Air 机型,通常会存储数百 GB 的 4K ProRes 视频、Procreate 绘图、RAW 照片和系统文件。然而,当您在Mac或Windows PC 上备份 iPad时,苹果的原生软件(Finder、iTun…

2026/7/22 22:34:01阅读更多 →
Windows 下 Nginx + Flask 应用迁移阿里云完整部署指南(附安全加固)

Windows 下 Nginx + Flask 应用迁移阿里云完整部署指南(附安全加固)

阿里云 Nginx Flask 应用部署配置文档 适用场景:将现有 Flask 应用(监听 5000 端口)迁移到阿里云服务器,使用 Nginx 提供 HTTPS 反向代理,并确保后端服务仅限本机访问。 一、环境准备 1. 服务器信息(示例…

2026/7/22 22:34:01阅读更多 →
OwlCarousel2 响应式轮播插件:从入门到精通的完整实践指南

OwlCarousel2 响应式轮播插件:从入门到精通的完整实践指南

OwlCarousel2 响应式轮播插件:从入门到精通的完整实践指南 【免费下载链接】OwlCarousel2 DEPRECATED jQuery Responsive Carousel. 项目地址: https://gitcode.com/gh_mirrors/ow/OwlCarousel2 OwlCarousel2 是一款基于 jQuery 的现代化响应式轮播插件&…

2026/7/22 22:34:01阅读更多 →
WAIC 2026 | 艾络迅一站式AI玩具解决方案,加速AI产品商用落地

WAIC 2026 | 艾络迅一站式AI玩具解决方案,加速AI产品商用落地

7月17日至20日,以”智能伙伴共创未来”为主题的2026世界人工智能大会(WAIC 2026)在上海举办。移远通信旗下物联网智能品牌艾络迅携”AI玩具整体解决方案”亮相核心展区,集中展示了覆盖AI玩具终端、App与飞鸢物联网平台的一体化能力…

2026/7/22 22:32:00阅读更多 →
Go语言静态资源打包方案对比与实践指南

Go语言静态资源打包方案对比与实践指南

1. 项目背景与核心需求在Go语言开发中,我们经常需要处理静态资源文件的打包问题。无论是Web应用的模板文件、前端资源,还是配置文件、证书等,都需要随程序一起分发。传统做法是将这些文件与编译后的二进制文件放在同一目录下,但这…

2026/7/22 0:53:59阅读更多 →
Go语言实现高性能LDAP认证服务的架构与实践

Go语言实现高性能LDAP认证服务的架构与实践

1. 项目背景与核心价值LDAP(轻量级目录访问协议)作为企业级身份认证的黄金标准,已经服务了超过80%的财富500强公司。我在金融科技领域实施统一认证体系时,发现传统Java方案存在启动慢、内存占用高等痛点。而Go语言凭借其协程并发模…

2026/7/22 0:53:59阅读更多 →
【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

【AI面试官实战指南】:用ChatGPT模拟10类高频技术岗面试,3天提升应答精准度92%

更多请点击: https://intelliparadigm.com 第一章:AI面试官实战指南的核心价值与适用场景 AI面试官并非替代人类HR的“黑箱工具”,而是以可解释、可审计、可迭代的方式,赋能招聘全链路的关键基础设施。其核心价值在于将主观经验沉…

2026/7/22 0:53:59阅读更多 →
中小企业小程序开发公司怎么选:预算、上手和售后避坑指南

中小企业小程序开发公司怎么选:预算、上手和售后避坑指南

中小企业做小程序,最常见的矛盾是预算有限,但又不希望功能太单薄;没有技术团队,但又希望后续能自己运营;想快速上线,又担心隐性收费和售后失联。选型时如果只看“低价套餐”或“案例数量”,很容…

2026/7/22 0:01:17阅读更多 →
GEO优化如何沉淀长期内容资产?广拓时代谈AI搜索时代的内容ROI

GEO优化如何沉淀长期内容资产?广拓时代谈AI搜索时代的内容ROI

企业做营销,最怕钱花完了,资产没有留下。 效果广告能带来一段时间的曝光,但预算停止后,流量往往也随之停止。短视频内容可能在几天内冲高,也可能很快沉下去。AI搜索时代,企业需要重新思考一个问题&#xff…

2026/7/22 0:01:17阅读更多 →
Agent 终态判定:何时该停止思考、给出最终回复

Agent 终态判定:何时该停止思考、给出最终回复

Agent 终态判定:何时该停止思考、给出最终回复 一、你的 Agent 在"再想想"的循环里绕了 12 轮,用户已经关窗口了 Agent 与人最大的区别是:人知道什么时候该停下来给答案,Agent 会一直"想"下去。你给 Agent 接…

2026/7/22 0:01:17阅读更多 →
YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

YOLOv8推理性能优化:从1.2FPS到35FPS的全链路加速实践

如果你在部署 YOLOv8 时,发现推理速度只有可怜的 1-2 FPS,而别人的演示视频却能跑到 30 FPS 以上,那么问题很可能不在模型本身,而在于你的整个处理链路。很多开发者拿到一个训练好的 YOLOv8 模型后,会直接使用官方示例…

2026/7/21 22:53:50阅读更多 →
Coze与Dify对比指南:低代码AI应用开发从入门到实战

Coze与Dify对比指南:低代码AI应用开发从入门到实战

1. 从零到一:为什么你需要了解 Coze 和 Dify?如果你对 AI 应用开发感兴趣,但一看到“大模型”、“智能体”、“工作流”这些词就头疼,觉得门槛太高,那这篇文章就是为你准备的。很多开发者,包括我自己&#…

2026/7/22 18:55:50阅读更多 →
AI生图工具怎么选?2026年6月版实测对比

AI生图工具怎么选?2026年6月版实测对比

做自媒体的朋友应该都有体会:配图一直是个让人头疼的问题。2026年,AI生图工具已经非常成熟了,但工具太多反而不知道怎么选。以下是截至2026年6月我对主流AI生图工具的实测对比。Midjourney V8.1:速度之王2026年6月11日&#xff0c…

2026/7/22 18:55:50阅读更多 →