YOLOv3, YOLOv4 Training (with.darknet)

1) PREPARE

파일트리

darknet
	ㄴmonday (학습 dir 생성)
		ㄴobj.names
		ㄴobj.data
		ㄴyolov3-tiny.cfg
		ㄴback
		ㄴtrain.txt
		ㄴval.txt
		ㄴtrain_img (train_img, train_txt dir)
			ㄴtrain_img(1).png
			ㄴtrain_img(1).txt
			ㄴtrain_img(2).png
			ㄴtrain_img(2).txt
			ㄴ...
		ㄴval_img (val_img, val_txt dir)
			ㄴval_img(1).png
			ㄴval_img(1).txt
			ㄴval_img(2).png
			ㄴval_img(2).txt
			ㄴ...

<aside> 📌 train_img, val_img 파일의 비율은 8:2 폴더 내에 이미지 파일, txt 파일 둘 다 넣어야 함.

</aside>

train.txt 파일 만드는 코드

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

def list_files(directory, output_file):
    with open(output_file, 'w') as file:
        with os.scandir(directory) as entries:
            for entry in entries:
                if entry.is_file() and entry.name.endswith('.png'): #파일중에 이미지 파일만 선택
                    absolute_path = os.path.abspath(entry)
                    file.write(absolute_path + '\\n')

directory_path = '/home/road/darknet/monday/train_img' #train_img 파일이 있는 경로
output_file = 'train.txt' #저장할 파일 이름

list_files(directory_path, output_file)

val.txt 파일 만드는 코드

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

def list_files(directory, output_file):
    with open(output_file, 'w') as file:
        with os.scandir(directory) as entries:
            for entry in entries:
                if entry.is_file() and entry.name.endswith('.png'): #파일중에 이미지 파일만 선택
                    absolute_path = os.path.abspath(entry)
                    file.write(absolute_path + '\\n')

directory_path = '/home/road/darknet/monday/val_img' #train_img 파일이 있는 경로
output_file = 'val.txt' #저장할 파일 이름

	list_files(directory_path, output_file)

obj.names 파일 만들기 ‼파일 내에 한글은 없어야 함

person
bicycle
car

#인식할 객체 이름 작성 (labeling 때 순서 꼭 지켜야 함)

obj.data 파일 만들기‼파일 내에 한글은 없어야 함

classes = 3 #학습할 클래스 수
train = monday/train.txt #train.txt 파일 경로
valid = monday/val.txt #val.txt 파일 경로
names = monday/obj.names #obj.names 파일 경로
backup = monday/back/ #가중치 파일 저장 경로

obj.cfg 파일 만들기

  1. darknet/cfg 폴더 내에 있는 cfg 파일들 중 학습하고자 하는 버전에 맞는 cfg 파일을 가져온다.

    ex) yolv3_tiny 버전으로 학습할 예정이라면 yolov3-tiny.cfg 파일

       yolov4 버전으로 학습할 계획이라면 yolov4.cfg 파일
    
  2. cfg파일 수정하기

Untitled

  1. 빨간 네모
    1. batch=64, subdivision=16 수정
  2. 주황 네모
    1. max_batches=클래수 수 * 2000
    2. 학습하고자 하는 클래스 수가 3개라면 3*2000=6000
  3. 노란 네모
    1. steps=max_batches0.8,max_batches0.9
    2. 현재 max_batches=6000이므로 steps=4800,4500

Untitled

Untitled

  1. ctrl+f 로 classes를 검색했을 때 나오는 3군데의 filters, classes 수정
    1. filters=(클래스 수 + 5) * 3

      클래스 수가 3이라면, (3 + 5) * 3 = 24

    2. classes=학습 클래스 수

<aside> 📌 Caution