目录
- 数据获取
- TFrecord文件
-
- 什么是TFrecord文件
- 将数据转换成TFrecord文件
- 读取TFrecord文件
- 数据处理
- 模型构建
- 模型训练
-
- 模型预测
数据获取
![目标检测——yoloV3案例_第1张图片](http://img.e-com-net.com/image/info8/bfc85370f00947c9bf0c10ebe6deeab5.jpg)
labellmage使用方法
![目标检测——yoloV3案例_第2张图片](http://img.e-com-net.com/image/info8/3d4ba1f1295149288197eebc7a9fc396.jpg)
![目标检测——yoloV3案例_第3张图片](http://img.e-com-net.com/image/info8/325c142cfd064522b774a3a474889ef2.jpg)
TFrecord文件
![目标检测——yoloV3案例_第4张图片](http://img.e-com-net.com/image/info8/35370d94de644e2aa288f22c47d9c7a1.jpg)
什么是TFrecord文件
![目标检测——yoloV3案例_第5张图片](http://img.e-com-net.com/image/info8/d6ec3475dae54ef7a336dbb9787c6288.jpg)
![目标检测——yoloV3案例_第6张图片](http://img.e-com-net.com/image/info8/443cd09f3ae74931ae0a5060cda22d9d.jpg)
将数据转换成TFrecord文件
![目标检测——yoloV3案例_第7张图片](http://img.e-com-net.com/image/info8/328b6db5fc1d4cad8a2684935a6ff04a.jpg)
from dataset.vocdata_tfrecord import load_labels,write_to_tfrecord
datapath='./VOCdevkit/VOC2007/'
all_xml=load_labels(datapath,'train')
tfrecord_path='./yolov3/dataset/voc_train.tfrecords'
img_path=os.path.join(datapath,'JPEGImages')
write_to_tfrecord(all_xml,tfrecord_path,img_path)
读取TFrecord文件
from dataset.get_tfdata import getdata
dataset=getdata('./dataset/voc_val.tfrecords')
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from utils.config_utils import read_class_names
classes=read_class_names('config/classname')
plt.figure(15,10)
i=0
for image,width,height,boxes,boxes_category in datasets.take(3):
plt.subplot(1,3,i+1)
plt.imshow(image)
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)
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()
![目标检测——yoloV3案例_第8张图片](http://img.e-com-net.com/image/info8/a5b04ae616b14036a74a2973fe662f0e.jpg)
数据处理
![目标检测——yoloV3案例_第9张图片](http://img.e-com-net.com/image/info8/fc832bdad69f4eb0af1290d77edf81f6.jpg)
from dataset.preprocess import preprocess
plt.figure(15,10)
i=0
for image,width,height,boxes,boxes_category in datasets.take(3):
image,boxes=preprocess(image,boxes)
plt.subplot(1,3,i+1)
plt.imshow(image)
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)
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()
![目标检测——yoloV3案例_第10张图片](http://img.e-com-net.com/image/info8/bb0b81ec523f49f7934d3b08e23ad0f1.jpg)
模型构建
![目标检测——yoloV3案例_第11张图片](http://img.e-com-net.com/image/info8/d17dc655b6a0413aa2a87b1f30007e98.jpg)
模型训练
from model.yolov3 import YOLOv3
yolov3=YOLOv3((416,416,3),80)
yolov3.summary()
损失函数的计算
![目标检测——yoloV3案例_第12张图片](http://img.e-com-net.com/image/info8/83b0cea6353947a8843701744ba19e04.jpg)
from core.loss import Loss
yolov3_loss=Loss((416,416,3),80)
正负样本的设定
![目标检测——yoloV3案例_第13张图片](http://img.e-com-net.com/image/info8/645bea290a6541f38ed27cb9965cae75.jpg)
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:]
模型训练
![目标检测——yoloV3案例_第14张图片](http://img.e-com-net.com/image/info8/b6ddad829ce8466485cb11bdb3574b2d.jpg)
获取数据集
from dataset.preprocess import dataset
batch_size=1
trainset=dataset('dataset/voc_train.tfrecords',batch_size)
加载模型
from model.yoloV3 import YOLOv3
yolov3=YOLOv3((416,416,3),20)
from core.loss import Loss
yoloV3_loss=Loss((416,416,3),20)
模型训练
![目标检测——yoloV3案例_第15张图片](http://img.e-com-net.com/image/info8/23bfeb8281d14fdb9f59465a7af59778.jpg)
![目标检测——yoloV3案例_第16张图片](http://img.e-com-net.com/image/info8/9c59b5fd82a244a884117ac75bd72dfb.jpg)
optimizer=tf.keras.optimizers.SGD(learning_rate=0.1,momentum=0.9)
for epoch in range(2):
for (batch,inputs) in enumerate(trainset):
images,labels=inputs
with tf.GradientTape() as Tape:
outputs=yoloV3(image)
loss=yoloV3_loss([*outputs,*labels])
grads=Tape.gradient(loss,yolov3.trainable_variables)
optimizer.apply_gradients(zip(grads,yolov3.trainable_variables))
print(loss)
yolov3.save('yolov3.h5')
模型预测
![目标检测——yoloV3案例_第17张图片](http://img.e-com-net.com/image/info8/3024f74d1bd14a35a44977f0475e8684.jpg)
img=cv2.imread('image.jpg')
predicter=Predict(class_num=80,yolov3='weights/yolov3.h5')
boundings=predicter(img)
plt.imshow(img[:,:,::-1])
![目标检测——yoloV3案例_第18张图片](http://img.e-com-net.com/image/info8/684841921674457ab5462e6e48096515.jpg)
![目标检测——yoloV3案例_第19张图片](http://img.e-com-net.com/image/info8/11ebb0f0693b4b5c881dd1d19df24dd6.jpg)