Target detection - yoloV3 case

data collection

insert image description here
how to use labelmage
insert image description here
insert image description here

TFrecord file

insert image description here

What is a TFrecord file

insert image description here
insert image description here

Convert data into TFrecord files

insert image description here

from dataset.vocdata_tfrecord import load_labels,write_to_tfrecord
#1
datapath='./VOCdevkit/VOC2007/'
#2
all_xml=load_labels(datapath,'train')
#3
tfrecord_path='./yolov3/dataset/voc_train.tfrecords'
#4
img_path=os.path.join(datapath,'JPEGImages')
#5
write_to_tfrecord(all_xml,tfrecord_path,img_path)

Read TFrecord files

from dataset.get_tfdata import getdata
dataset=getdata('./dataset/voc_val.tfrecords')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

#数据展示
#1获取类别信息
from utils.config_utils import read_class_names
classes=read_class_names('config/classname')
#2创建画布
plt.figure(15,10)
#3获取数据遍历
i=0
for image,width,height,boxes,boxes_category in datasets.take(3):
    #4划分不同的坐标轴
    plt.subplot(1,3,i+1)
    #5显示图像:plt.imshow()
    plt.imshow(image)
    #6显示box,遍历所有的bbox,rectange进行绘制
    ax=plt.gca()
    for j in range(boxes.shape[0]):
        rect=Rectangle((boxes[j,0],boxes[j,1]),boxes[j,2] -boxes[j,0],boxes[j,3]-boxes[j,1],color='r',fill=False)
        ax.add_patch(rect)
        #7显示类别
        label_id=boxes_category[j]
        label=classes.get(label_id.numpy())
        ax.text(boxes[j,0],boxes[j,1]+8,label,color='w',size=11,backgroundcolor='none')
    i+=1
plt.show()

insert image description here

data processing

insert image description here

from dataset.preprocess import preprocess
#2创建画布
plt.figure(15,10)
#3获取数据遍历
i=0
for image,width,height,boxes,boxes_category in datasets.take(3):
    #进行数据处理
    image,boxes=preprocess(image,boxes)
    #4划分不同的坐标轴
    plt.subplot(1,3,i+1)
    #5显示图像:plt.imshow()
    plt.imshow(image)
    #6显示box,遍历所有的bbox,rectange进行绘制
    ax=plt.gca()
    for j in range(boxes.shape[0]):
        rect=Rectangle((boxes[j,0],boxes[j,1]),boxes[j,2] -boxes[j,0],boxes[j,3]-boxes[j,1],color='r',fill=False)
        ax.add_patch(rect)
        #7显示类别
        label_id=boxes_category[j]
        label=classes.get(label_id.numpy())
        ax.text(boxes[j,0],boxes[j,1]+8,label,color='w',size=11,backgroundcolor='none')
    i+=1
plt.show()

insert image description here

model building

insert image description here

model training

from model.yolov3 import YOLOv3 
yolov3=YOLOv3((416,416,3),80)
yolov3.summary()

Calculation of loss function

insert image description here

from core.loss import Loss
yolov3_loss=Loss((416,416,3),80)

Positive and negative sample settings

insert image description here
insert image description here

from core.bbox_target import bbox_to_target
#获取数据进行目标值设置
for image,width,height,boxes,labels in dataset.take[1]:
    #获取目标值
    label1,label2,label3=bbox_to_target(boxes,label,num_classes=20)
import tensorflow as tf
#获取正样本索引
tf.where(tf.equal(label[...,4],1))
#坐标值
label1[12,12,0,0:4]
label1[12,12,0,5:]

model training

insert image description here

get dataset

from dataset.preprocess import dataset
batch_size=1
trainset=dataset('dataset/voc_train.tfrecords',batch_size)

load model

from model.yoloV3 import YOLOv3
yolov3=YOLOv3((416,416,3),20)

from core.loss import Loss
yoloV3_loss=Loss((416,416,3),20)

model training

insert image description here
insert image description here

#1定义优化器
optimizer=tf.keras.optimizers.SGD(learning_rate=0.1,momentum=0.9)
#2设置epoch
for epoch in range(2):
    for (batch,inputs) in enumerate(trainset):
        images,labels=inputs
        #3计算损失函数进行参数更新
        #3.1定义上下文缓解
        with tf.GradientTape() as Tape:
            #3.2将图像送入网络中
            outputs=yoloV3(image)
            #3.3计算损失函数
            loss=yoloV3_loss([*outputs,*labels])
            #3.4计算梯度
            grads=Tape.gradient(loss,yolov3.trainable_variables)
            #3.5梯度更新
            optimizer.apply_gradients(zip(grads,yolov3.trainable_variables))
            print(loss)
yolov3.save('yolov3.h5')

model prediction

insert image description here
insert image description here

#1
img=cv2.imread('image.jpg')
#2
predicter=Predict(class_num=80,yolov3='weights/yolov3.h5')
#3
boundings=predicter(img)
#4
plt.imshow(img[:,:,::-1])

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_40527560/article/details/131781804