训练:
打开train_yolo3.py
修改预训练模型部分
pretrained_path = 'logs/fackoff823/trained_weights_final.h5' # 预训练模型
然后点击“运行”,开始训练
最后运行结果如下:
检测:使用yolo.py中的“dect_image”方法
编写yolo_image.py小程序(在此特别感谢博主:chaser_ming7 我是借鉴的他的)
为了方便调试,我们修改原来yolo中的generate方法,引出一个model接口
最后的检测结果:初步测试
最后附上我的程序代码:
import sys
import argparse
from yolo import YOLO, detect_video
from PIL import Image
import os
import numpy as np
import tensorflow as tf
import keras.backend as K
from keras.backend import mean
from keras.layers import Input, Lambda
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
from keras.utils import plot_model
from yolo3.model import preprocess_true_boxes, yolo_body_mobilenet3s_5_15, yolo_body, tiny_yolo_body, yolo_loss, yolo_body_mobilenetv2, yolo_body_mobilenetv2_5_9, yolo_body_5_14, yolo_body_mobilenet3_5_14
from yolo3.utils import get_classes, get_anchors, data_generator_wrapper
import matplotlib.pyplot as plt
from yolo3.mobilenetv3 import yolo_body_mobilenet3s_5_15 as my_yolo_body
def detect_img(yolo):
while True:
img = input('Input image filename:')
try:
image = Image.open(img)
except:
print('Open Error! Try again!')
continue
else:
r_image = yolo.detect_image(image)
r_image.show()
yolo.close_session()
FLAGS = None
if __name__ == '__main__':
"为了生成我们的body模型"
input_shape = (288, 384) # 32的倍数,输入图像
h, w = input_shape # 尺寸
image_input = Input(shape=(h, w, 3)) # 图片输入格式
num_classes = 3
model_body = yolo_body(image_input, 3, num_classes)
yolo=YOLO(model_body,'/home/tz/iarc2019_keras_yolo3-master/logs/384/trained_weights_final.h5', 'model_data/yolo_anchors.txt')
path = '/home/tz/iarc2019_keras_yolo3-master/test5.jpg'
try:
image = Image.open(path)
except:
print('Open Error! Try again!')
else:
r_image = yolo.detect_image(image)
v=r_image[0]
v.show()
yolo.close_session()
由此可以看到,功能全部跑通,但是检测结果还很垃圾,下一步进行参数调节,以及结构改变的学习。 |