Skip to content
RANJITH ROSAN edited this page Sep 2, 2023 · 3 revisions

YOLOv3 Model Training Guide! This GitHub Wiki provides detailed instructions on training the YOLOv3 object detection model using the Darknet framework in a Google Colab environment. Below is a step-by-step guide that explains the purpose of each code block provided in the instructions.

Table of Contents Check GPU Mount Google Drive Clone, Configure & Compile Darknet Configure yolov3.cfg File Create .names and .data Files Save Configuration Files to Google Drive Create a Folder and Unzip Image Dataset Create train.txt File Download Pre-trained Weights Start Training

  1. Check GPU

!nvidia-smi

This command checks the available GPU resources in your Google Colab environment. It helps ensure that your Colab session is using a GPU for training, which is significantly faster than CPU training for deep learning tasks.

  1. Mount Google Drive

from google.colab import drive drive.mount('/content/gdrive')

This code mounts your Google Drive to your Colab environment. You'll be able to access and save files from and to your Google Drive during the training process. It's especially useful for saving model checkpoints and configuration files.

  1. Clone, Configure & Compile Darknet

!git clone https://github.com/AlexeyAB/darknet

%cd darknet !sed -i 's/OPENCV=0/OPENCV=1/' Makefile !sed -i 's/GPU=0/GPU=1/' Makefile !sed -i 's/CUDNN=0/CUDNN=1/' Makefile

!make

These commands clone the Darknet repository, modify the Makefile to enable GPU and CUDNN support, and then compile the Darknet framework. Darknet is a popular framework for YOLOv3 training.

  1. Configure yolov3.cfg File

!cp cfg/yolov3.cfg cfg/yolov3_training.cfg

!sed -i 's/batch=1/batch=64/' cfg/yolov3_training.cfg !sed -i 's/subdivisions=1/subdivisions=16/' cfg/yolov3_training.cfg !sed -i 's/max_batches = 500200/max_batches = 4000/' cfg/yolov3_training.cfg !sed -i '610 s@classes=80@classes=2@' cfg/yolov3_training.cfg !sed -i '696 s@classes=80@classes=2@' cfg/yolov3_training.cfg !sed -i '783 s@classes=80@classes=2@' cfg/yolov3_training.cfg !sed -i '603 s@filters=255@filters=21@' cfg/yolov3_training.cfg !sed -i '689 s@filters=255@filters=21@' cfg/yolov3_training.cfg !sed -i '776 s@filters=255@filters=21@' cfg/yolov3_training.cfg

These commands make a copy of the yolov3.cfg file, modify it to suit your specific training requirements (e.g., batch size, number of classes, and filters), and save it as yolov3_training.cfg.

  1. Create .names and .data Files

!echo -e 'job\nbeam_number' > data/obj.names !echo -e 'classes= 2\ntrain = data/train.txt\nvalid = data/test.txt\nnames = data/obj.names\nbackup = /content/weight' > data/obj.data

These commands create the .names and .data files required for YOLOv3 training. The .names file lists the class names, and the .data file contains configuration information such as the number of classes and paths to training and validation data.

  1. Save Configuration Files to Google Drive

!mkdir /content/gdrive/MyDrive/Yolo_v3/yolov3_testing.cfg !mkdir /content/gdrive/MyDrive/Yolo_v3/classes.txt

!cp cfg/yolov3_training.cfg /content/gdrive/MyDrive/Yolo_v3/yolov3_testing.cfg !cp data/obj.names /content/gdrive/MyDrive/Yolo_v3/classes.txt

These commands create directories in your Google Drive and save the modified configuration files (yolov3_training.cfg and obj.names) to your Google Drive for safekeeping.

  1. Create a Folder and Unzip Image Dataset

!mkdir data/obj !unzip /content/gdrive/MyDrive/ocr_ds.zip -d data/obj

These commands create a directory named obj in the data folder and unzip your image dataset into it. The dataset should contain your labeled images for training.

  1. Create train.txt File

import glob images_list = glob.glob("data/obj/ocr_ds/*.jpg") with open("data/train.txt", "w") as f: f.write("\n".join(images_list))

This Python code generates a train.txt file that lists the file paths of the training images. It scans the images in the data/obj/ocr_ds directory and writes their paths to the train.txt file.

  1. Download Pre-trained Weights

!wget https://pjreddie.com/media/files/darknet53.conv.74

This command downloads pre-trained weights for the Darknet53 model, which will be used as the initial weights for your YOLOv3 model. Pre-trained weights can significantly speed up the training process.

  1. Start Training

!./darknet detector train data/obj.data cfg/yolov3_training.cfg darknet53.conv.74 -dont_show

Uncomment below and comment above to re-start your training from last saved weights

#!./darknet detector train data/obj.data cfg/yolov3_training.cfg /mydrive/yolov3/yolov3_training_last.weights -dont_show

These commands start the YOLOv3 model training process using the configuration and data files you've prepared. The -dont_show flag prevents displaying the training progress on the Colab notebook, which is useful for headless training.

You can also uncomment the second line if you want to continue training from previously saved weights.

Please make sure to customize the paths and parameters in these instructions to match your specific setup and dataset. Additionally, ensure you have the necessary permissions and storage space in your Google Drive to save the model weights and configuration files.

Clone this wiki locally