新手技术求助:如何在Google Colab上训练YOLOv3-tiny/YOLOv4-tiny用于自主无人机项目(附训练报错日志)
Hey there! I see you're a new programmer working on a drone project and hitting snags training YOLOv4-tiny on Google Colab. Let's walk through fixing this and get your model up and running smoothly.
First, Let's Break Down Your Log Output
Looking at the logs you shared, the good news is that the model successfully loaded the pre-trained weights (yolov4-tiny.conv.29) and initialized the training environment. The actual crash might be happening after these setup steps—you might have missed pasting the full error trace. But let's cover the most common pitfalls for your setup right now.
Step-by-Step Training Troubleshooting & Guidance
1. Double-Check Your Dataset Format
YOLO relies on a strict structure for training—get this right and you'll avoid 80% of issues:
- Each image (e.g.,
drone_001.jpg) must have a matching annotation file (drone_001.txt) in the same folder. - Each line in the
.txtannotation must follow:class_id x_center y_center width height(all values normalized to 0-1, not raw pixel coordinates). - Verify your
obj.datafile:- Ensure
classesmatches the number of drone-related classes you're detecting. From your log, each yolo layer outputs 18 filters—since each layer uses(num_classes + 5) * 3filters, this means you've setnum_classes=1; confirm this is correct for your project. - Make sure
train,valid, andnamespaths point to the correct files in your Google Drive/Colab workspace.
- Ensure
2. Tweak Colab & Model Config for Your GPU
Your log shows you're using a Tesla K80, which has limited VRAM—adjust these settings to avoid crashes:
- Confirm GPU Acceleration: Go to
Runtime > Change runtime type > Hardware accelerator > GPUand save. Sometimes Colab resets this, so double-check it's enabled. - Adjust Batch & Resolution:
- Open your
yolov4-tiny-custom.cfgfile and setbatch=32,subdivisions=16(subdivisions splits the batch into smaller chunks to fit VRAM). - If you hit an Out-of-Memory (OOM) error later, reduce input resolution to
width=320,height=320(must be multiples of 32).
- Open your
- Recompile Darknet with GPU Support: If you haven't already, ensure Darknet is built for GPU:
cd darknet sed -i 's/GPU=0/GPU=1/' Makefile sed -i 's/CUDNN=0/CUDNN=1/' Makefile sed -i 's/OPENCV=0/OPENCV=1/' Makefile make
3. Fix Your Training Command
Make sure your command points to the right files. A standard working command for your setup looks like this:
./darknet detector train data/obj.data cfg/yolov4-tiny-custom.cfg yolov4-tiny.conv.29 -dont_show -map
-dont_showhides the GUI (Colab doesn't support it),-mapcalculates mAP during training to track how well your model is performing.
Simplified "Video-Style" Walkthrough (No External Links)
If you prefer a step-by-step, video-like flow, follow these exact steps in Colab:
- Mount Google Drive:
from google.colab import drive drive.mount('/content/drive') - Clone the Darknet Repository:
!git clone https://github.com/AlexeyAB/darknet.git - Compile Darknet with GPU Support: Run the compile commands from section 2 above.
- Prepare Your Dataset:
- Upload your 850 images and 1700 annotation files to a folder named
objin your Drive. - Create
train.txtandvalid.txt(split your dataset 80-20 for train/validation) with full paths to each image (e.g.,/content/drive/MyDrive/dataset/obj/drone_001.jpg).
- Upload your 850 images and 1700 annotation files to a folder named
- Configure Model Files:
- Copy
cfg/yolov4-tiny.cfgtocfg/yolov4-tiny-custom.cfg. - In the custom cfg:
- Set
classes=1(or your actual number of classes) in both[yolo]layers. - Set
filters=(classes + 5)*3in the[conv]layers right before each[yolo]layer (for 1 class, this is 18—matches your log, so that's correct). - Set
batch=32,subdivisions=16under[net].
- Set
- Create
data/obj.data:classes=1 train=/content/drive/MyDrive/dataset/train.txt valid=/content/drive/MyDrive/dataset/valid.txt names=data/obj.names backup=/content/drive/MyDrive/dataset/backup/ - Create
data/obj.nameswith your class name (e.g.,drone).
- Copy
- Download Pre-trained Weights:
!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29 - Start Training: Run the training command from section 3.
Final Quick Tips
- Monitor the training loss: It should decrease over time—aim for a loss below 0.5 for solid performance.
- Check the
backupfolder in your Drive: Darknet saves weights every few epochs; use the latest_last.weightsor_best.weightsfor inference on your drone.
内容的提问来源于stack exchange,提问作者HalilYLMZ

