directory

Abstract

training

Download the code

Download weight files

Making data sets

Install the packages you need to run

Modify the category

Example Modify the configuration file cfg.py

Modified models. Py

Modify the train.py file

test


Abstract

YOLOV4 achieved 43.5%AP on the coco and 65FPS on the Tesla V100. It’s not much of a score compared to other models this year, but it doesn’t improve its score by increasing the resolution of the input images, it improves the network structure. The innovation mainly includes the following aspects:

(1) Input: The innovation here refers to the improvement of input during training, mainly including Mosaic data enhancement, cmBN, and SAT autoadroit training.

BackBone: It combines all kinds of new methods, including CSPDarknet53, Mish activation function, Dropblock

(3) Neck: Target detection network usually inserts some layers between BackBone and the last output layer, such as SPP module and FPN+PAN structure in Yolov4

(4) Prediction: The anchor frame mechanism of the output layer is the same as that of Yolov3. The main improvement is the loss function CIOU_Loss during training, and the NMS filtered by the Prediction frame will be changed to DIOU_nms

training

Local Environment:

   Ubuntu20.04

   pytorch1.5.1

   CUDA10.1

   Python3.7

Download the code

The code used for this article is: github.com/Tianxiaomo/…

Training and testing the reasoning code has been completed.

Download weight files

yolov4.conv.137.pth

Link a: pan.baidu.com/s/1ovBie4Yy… Extraction code: Kcel

Link 2:

Drive.google.com/open?id=1fc…

Place the downloaded weight files under the data folder

 

Making data sets

Address: data sets download.csdn.net/download/hh…

Copy the Labelme dataset under the PyTorch – YOLOv4-Master folder as shown:

Then create the labelme2txt.py file using Pycharm. Write the code that generates the training set and validation set.

Labelme2txt. Py code:

from os import getcwd from sklearn.model_selection import train_test_split import json import glob wd = getcwd() “Json data set labeled by Labelme is converted to pyTorch yoloV4 training set classes = [“aircraft”, “oiltank” ] image_ids = glob.glob(r”LabelmeData/*jpg” ) print(image_ids) train_list_file = open( ‘data/train.txt’ , ‘w’ ) val_list_file = open( ‘data/val.txt’ , ‘w’ ) def convert_annotation(image_id, list_file): jsonfile=open( ‘%s.json’ % (image_id)) in_file = json.load(jsonfile) for i in range(0,len(in_file[ “shapes” ])): object=in_file[ “shapes” ][i] cls=object[ “label” ] points=object[ “points” ] xmin=int(points[0][0]) ymin=int(points[0][1]) xmax=int(points[1][0]) ymax=int(points[1][1]) if cls not in classes: print( “cls not in classes” ) continue cls_id = classes.index(cls) b = (xmin, ymin, xmax, ymax) list_file.write( ” ” + “,” .join([str(a) for a in b]) + ‘,’ + str(cls_id)) jsonfile.close() def ChangeData2TXT(image_List,dataFile): for image_id in image_List: dataFile.write( ‘%s’ % (image_id.split( ‘ \\ ‘ )[-1])) convert_annotation(image_id.split( ‘.’ )[0], dataFile) dataFile.write( ‘ \n ‘ ) dataFile.close() trainval_files, test_files = train_test_split(image_ids, Test_size = 0.2, random_state = 55) ChangeData2TXT (trainval_files train_list_file) ChangeData2TXT (test_files val_list_file)

Install the packages you need to run

Refer to requirements.txt to install packages that do not exist on the machine. The version does not have to be consistent, as long as there is no error in the later period, there is no problem.

Modify the category

Change the categories in coco. Names and VOC. names to the categories in your own dataset. , in the same order as classes in Labelme2txt.py.Copy the code

Example Modify the configuration file cfg.py

Cfg.use_darknet_cfg = False

Cfg. Batch = 2(according to their graphics card modification, my graphics card is 8G can be trained up to 2 batches).

Cfg.subdivisions = 1

Modified models. Py

Change inplace=True to inplace=False in lines 51 and 53. If not, an error will be reported during training.

Modify the train.py file

Find line 526. This method takes an update to the parameters in cfg.py.

Modify the following parameters:

Parser. add_argument(‘-g’, ‘–gpu’, metavar= ‘g ‘, type= STR, default= ‘0’, help= ‘gpu’, dest= ‘gpu’)# Set the GPU used by the GPU

parser.add_argument( ‘-dir’ , ‘–data-dir’ , type=str, default= “LabelmeData” , help= ‘dataset dir’ , Dest = ‘dataset_dir’)# directory where images are located.

parser.add_argument( ‘-pretrained’ , type=str, default= “data/yolov4.conv.137.pth” , Help = ‘pretrained yolov4.conv.137’)# set the path for the pretrained weight file.

Parser. Add_argument (‘-classes’, type=int, default=80, help= ‘dataset classes’)

parser.add_argument( ‘-train_label_path’ , dest= ‘train_label’ , type=str, default= ‘data/train.txt’ , Help = “train label path”)

Comment lines 415 through 440. This code keeps reporting errors while validating and I can’t find the cause. Find the cause and then update.

After the above content is modified, you can click Run to start training.

 

test

The test focused on modifying the models.py code. Replace the following code from line 449.

 

if __name__ == "__main__": import sys import cv2 namesfile = None n_classes=2 weightfile="checkpoints/Yolov4_epoch151.pth" Imgfile ="data/aircraft_4.jpg"# imgfile width=608 height=608 model = Yolov4(yolov4conv137weight=None, n_classes=n_classes, (weightfile, map_location=torch. Device (' CPU '))  #pretrained_dict = torch.load(weightfile, map_location=torch.device('cuda'))Copy the code

    model.load_state_dict(pretrained_dict)



use_cuda = True

if use_cuda:

model.cuda()



img = cv2.imread(imgfile)



# Inference input size is 416*416 does not mean training size is the same

# Training size could be 608*608 or even other sizes

# Optional inference sizes:

#   Hight in {320, 416, 512, 608, … 320 + 96 * n}

#   Width in {320, 416, 512, 608, … 320 + 96 * m}

sized = cv2.resize(img, (width, height))

sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)



from tool.utils import load_class_names, plot_boxes_cv2

from tool.torch_utils import do_detect



for i in range(2):  # This ‘for’ loop is for speed check

# Because the first iteration is usually longer

boxes = do_detect(model, sized, 0.4, 0.6, use_cuda)



if namesfile == None:

if n_classes == 2:

namesfile = ‘data/voc.names’

elif n_classes == 80:

namesfile = ‘data/coco.names’

else:

print(“please give namefile”)



class_names = load_class_names(namesfile)

resultImg=plot_boxes_cv2(img, boxes[0], ‘predictions.jpg’, class_names)

cv2.imshow(“image”,resultImg)

cv2.waitKey(0);

Test results:

Source link: download.csdn.net/download/hh…

Reference article:

Simple Yolo series of Yolov3&Yolov4 core basic knowledge complete explanation

Blog.csdn.net/nan35565560…