200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > keras_retinanet 目标检测——自定义图片数据集的模型训练步骤

keras_retinanet 目标检测——自定义图片数据集的模型训练步骤

时间:2022-07-12 15:43:38

相关推荐

keras_retinanet 目标检测——自定义图片数据集的模型训练步骤

最近在学习 keras_retinanet ,下面就记录下用自己的数据集进行的模型训练。

大致分为以下几步:

自定义训练数据图片目标标注生成用于训练的图片名称、目标标注位置及目标类别的.csv文件开始训练模型(注意参数调整)转换训练好的模型用转换后的模型进行目标检测

下面就一步一步介绍吧:

目录

1.下载包,安装环境。

2.准备数据集

3.训练模型

4.目标检测

由于我之前已经安装了vs、Anaconda和Pycharm,就不在此赘述了。

本机配置:

GTX 1060 3G Win10 64vs Anaconda3 5.1.0Pycharm

1.下载包,安装环境。

从Github上下载Github ——> keras-retinanet这个仓库确保你的环境里有tensorflow、numpy、keras切换到当前目录下运行

pip install . --user

或者直接从克隆的仓库中运行代码,但是需要运行python setup.py build_ext --inplace来首先编译Cython代码。

如果还不行就把keras_retinanet这整个目录拷贝到你自己环境的D:\Anaconda3-5.0.1\envs\tf-gpu\Lib\site-packages下

测试准备:点击测试模型下载,将用于测试的模型resnet50_coco_best_v2.1.0.h5放在snapshots目录下

下面就可以在jupyter notebook运行examples里的ResNet50RetinaNet.ipynb进行测试,当然也可以在jupyter notebook中将文件保存成.py格式的在pycharm里运行。

2.准备数据集

retinanet模型训练的数据是按照VOC格式处理的,所以你也需要将自己的数据准备成VOC的格式 准备如图三个文件夹,JPEGImages放你自己准备训练模型的图片,图片名称最好是按1.jpg,2.jpg这种类型;Annotations放图片目标的位置和类型标注的.xml文件,这个可以用Github——>labellmg里的生成目标标注的工具自动生成.xml的文件(注意使用时将保存路径改到Annotations文件夹);ImageSets里的子文件夹Main里放按比例随机抽样切分的训练集、验证集、测试集样本下标值的txt文件,这个可以用如下gen_main_txt.py自动生成

import osimport randomtrainval_percent = 0.8 # 自定义用于训练模型的数据(训练数据和交叉验证数据之和)占全部数据的比例train_percent = 0.8 # 自定义训练数据占训练数据交叉验证数据之和的比例xmlfilepath = 'Annotations'txtsavepath = 'ImageSets\Main'total_xml = os.listdir(xmlfilepath)num = len(total_xml)list = range(num)tv = int(num * trainval_percent)tr = int(tv * train_percent)trainval = random.sample(list, tv)train = random.sample(trainval, tr)ftrainval = open('ImageSets/Main/trainval.txt', 'w')ftest = open('ImageSets/Main/test.txt', 'w')ftrain = open('ImageSets/Main/train.txt', 'w')fval = open('ImageSets/Main/val.txt', 'w')for i in list:name = total_xml[i][:-4]+'\n'if i in trainval:ftrainval.write(name)if i in train:ftrain.write(name)else:fval.write(name)else:ftest.write(name)ftrainval.close()ftrain.close()fval.close()ftest .close()

生成包含文件名、目标位置、目标类型的annotations.csv文件和类别标签的classes.csv文件,这个可以用gen_csv.py自动生成

import csvimport osimport globimport sysclass PascalVOC2CSV(object):def __init__(self, xml=[], ann_path='./annotations.csv', classes_path='./classes.csv'):''':param xml: 所有Pascal VOC的xml文件路径组成的列表:param ann_path: ann_path:param classes_path: classes_path'''self.xml = xmlself.ann_path = ann_pathself.classes_path = classes_pathself.label = []self.annotations = []self.data_transfer()self.write_file()def data_transfer(self):for num, xml_file in enumerate(self.xml):try:# print(xml_file)# 进度输出sys.stdout.write('\r>> Converting image %d/%d' % (num + 1, len(self.xml)))sys.stdout.flush()with open(xml_file, 'r') as fp:for p in fp:if '<filename>' in p:self.filen_ame = p.split('>')[1].split('<')[0]if '<object>' in p:# 类别d = [next(fp).split('>')[1].split('<')[0] for _ in range(9)]self.supercategory = d[0]if self.supercategory not in self.label:self.label.append(self.supercategory)# 边界框x1 = int(d[-4]);y1 = int(d[-3]);x2 = int(d[-2]);y2 = int(d[-1])self.annotations.append([os.path.join('JPEGImages', self.filen_ame), x1, y1, x2, y2, self.supercategory])except:continuesys.stdout.write('\n')sys.stdout.flush()def write_file(self, ):with open(self.ann_path, 'w', newline='') as fp:csv_writer = csv.writer(fp, dialect='excel')csv_writer.writerows(self.annotations)class_name = sorted(self.label)class_ = []for num, name in enumerate(class_name):class_.append([name, num])with open(self.classes_path, 'w', newline='') as fp:csv_writer = csv.writer(fp, dialect='excel')csv_writer.writerows(class_)if __name__ == "__main__":xml_file = glob.glob('./Annotations/*.xml')PascalVOC2CSV(xml_file)

用keras-retinanet-master\keras_retinanet\bin目录下的debug.py测试数据集是否生成成功,这个程序需要在命令行执行

python D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/keras_retinanet/bin/debug.py csv D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/examples/annotations.csv D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/examples/classes.csv

弹出你标注的图片说明数据准备成功。

3.训练模型

用keras-retinanet-master\keras_retinanet\bin目录下的train.py来训练模型,需要修改文件里import的相对路径

from keras_retinanet import layers # noqa: F401from keras_retinanet import lossesfrom keras_retinanet import modelsfrom keras_retinanet.callbacks import RedirectModelfrom keras_retinanet.callbacks.eval import Evaluatefrom keras_retinanet.models.retinanet import retinanet_bboxfrom keras_retinanet.preprocessing.csv_generator import CSVGeneratorfrom keras_retinanet.preprocessing.kitti import KittiGeneratorfrom keras_retinanet.preprocessing.open_images import OpenImagesGeneratorfrom keras_retinanet.preprocessing.pascal_voc import PascalVocGeneratorfrom keras_retinanet.utils.anchors import make_shapes_callbackfrom keras_retinanet.utils.config import read_config_file, parse_anchor_parametersfrom keras_retinanet.utils.keras_version import check_keras_versionfrom keras_retinanet.utils.model import freeze as freeze_modelfrom keras_retinanet.utils.transform import random_transform_generator

根据你自己GPU的性能微调参数,如果报tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[1,256,100,100]错误说明你的GPU内存不够用,可以通过降低batch-size、将image-min-side,image-max-side改小、改小网络结构等方式解决

tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[1,256,100,100] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc[[{{node training/Adam/gradients/classification_submodel/pyramid_classification_3/convolution_grad/Conv2DBackpropInput}} = Conv2DBackpropInput[T=DT_FLOAT, _class=["loc:@training/Adam/cond_85/Switch_2"], data_format="NCHW", dilations=[1, 1, 1, 1], padding="SAME", strides=[1, 1, 1, 1], use_cudnn_on_gpu=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](training/Adam/gradients/classification_submodel/pyramid_classification_3/convolution_grad/ShapeN, pyramid_classification_3/kernel/read, training/Adam/gradients/classification_submodel/pyramid_classification_3/Relu_grad/ReluGrad)]]Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.

参数调整:

修改workers参数(不知道为什么多线程会出错,就暂且不开启吧) 在命令行运行train.py文件

python D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/keras_retinanet/bin/train.py csv D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/examples/annotations.csv D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/examples/classes.csv

创建模型,网络结构如下

Creating model, this may take a second...__________________________________________________________________________________________________Layer (type)Output Shape Param #Connected to==================================================================================================input_1 (InputLayer) (None, None, None, 3 0__________________________________________________________________________________________________padding_conv1 (ZeroPadding2D) (None, None, None, 3 0 input_1[0][0]__________________________________________________________________________________________________conv1 (Conv2D) (None, None, None, 6 9408 padding_conv1[0][0]__________________________________________________________________________________________________bn_conv1 (BatchNormalization) (None, None, None, 6 256 conv1[0][0]__________________________________________________________________________________________________conv1_relu (Activation) (None, None, None, 6 0 bn_conv1[0][0]__________________________________________________________________________________________________pool1 (MaxPooling2D) (None, None, None, 6 0 conv1_relu[0][0]__________________________________________________________________________________________________res2a_branch2a (Conv2D) (None, None, None, 6 4096 pool1[0][0]__________________________________________________________________________________________________bn2a_branch2a (BatchNormalizati (None, None, None, 6 256 res2a_branch2a[0][0]__________________________________________________________________________________________________res2a_branch2a_relu (Activation (None, None, None, 6 0 bn2a_branch2a[0][0]__________________________________________________________________________________________________padding2a_branch2b (ZeroPadding (None, None, None, 6 0 res2a_branch2a_relu[0][0]__________________________________________________________________________________________________res2a_branch2b (Conv2D) (None, None, None, 6 36864 padding2a_branch2b[0][0]__________________________________________________________________________________________________bn2a_branch2b (BatchNormalizati (None, None, None, 6 256 res2a_branch2b[0][0]__________________________________________________________________________________________________res2a_branch2b_relu (Activation (None, None, None, 6 0 bn2a_branch2b[0][0]__________________________________________________________________________________________________res2a_branch2c (Conv2D) (None, None, None, 2 16384 res2a_branch2b_relu[0][0]__________________________________________________________________________________________________res2a_branch1 (Conv2D)(None, None, None, 2 16384 pool1[0][0]__________________________________________________________________________________________________bn2a_branch2c (BatchNormalizati (None, None, None, 2 1024 res2a_branch2c[0][0]__________________________________________________________________________________________________bn2a_branch1 (BatchNormalizatio (None, None, None, 2 1024 res2a_branch1[0][0]__________________________________________________________________________________________________res2a (Add) (None, None, None, 2 0 bn2a_branch2c[0][0]bn2a_branch1[0][0]__________________________________________________________________________________________________res2a_relu (Activation) (None, None, None, 2 0 res2a[0][0]__________________________________________________________________________________________________res2b_branch2a (Conv2D) (None, None, None, 6 16384 res2a_relu[0][0]__________________________________________________________________________________________________bn2b_branch2a (BatchNormalizati (None, None, None, 6 256 res2b_branch2a[0][0]__________________________________________________________________________________________________res2b_branch2a_relu (Activation (None, None, None, 6 0 bn2b_branch2a[0][0]__________________________________________________________________________________________________padding2b_branch2b (ZeroPadding (None, None, None, 6 0 res2b_branch2a_relu[0][0]__________________________________________________________________________________________________res2b_branch2b (Conv2D) (None, None, None, 6 36864 padding2b_branch2b[0][0]__________________________________________________________________________________________________bn2b_branch2b (BatchNormalizati (None, None, None, 6 256 res2b_branch2b[0][0]__________________________________________________________________________________________________res2b_branch2b_relu (Activation (None, None, None, 6 0 bn2b_branch2b[0][0]__________________________________________________________________________________________________res2b_branch2c (Conv2D) (None, None, None, 2 16384 res2b_branch2b_relu[0][0]__________________________________________________________________________________________________bn2b_branch2c (BatchNormalizati (None, None, None, 2 1024 res2b_branch2c[0][0]__________________________________________________________________________________________________res2b (Add) (None, None, None, 2 0 bn2b_branch2c[0][0]res2a_relu[0][0]__________________________________________________________________________________________________res2b_relu (Activation) (None, None, None, 2 0 res2b[0][0]__________________________________________________________________________________________________res2c_branch2a (Conv2D) (None, None, None, 6 16384 res2b_relu[0][0]__________________________________________________________________________________________________bn2c_branch2a (BatchNormalizati (None, None, None, 6 256 res2c_branch2a[0][0]__________________________________________________________________________________________________res2c_branch2a_relu (Activation (None, None, None, 6 0 bn2c_branch2a[0][0]__________________________________________________________________________________________________padding2c_branch2b (ZeroPadding (None, None, None, 6 0 res2c_branch2a_relu[0][0]__________________________________________________________________________________________________res2c_branch2b (Conv2D) (None, None, None, 6 36864 padding2c_branch2b[0][0]__________________________________________________________________________________________________bn2c_branch2b (BatchNormalizati (None, None, None, 6 256 res2c_branch2b[0][0]__________________________________________________________________________________________________res2c_branch2b_relu (Activation (None, None, None, 6 0 bn2c_branch2b[0][0]__________________________________________________________________________________________________res2c_branch2c (Conv2D) (None, None, None, 2 16384 res2c_branch2b_relu[0][0]__________________________________________________________________________________________________bn2c_branch2c (BatchNormalizati (None, None, None, 2 1024 res2c_branch2c[0][0]__________________________________________________________________________________________________res2c (Add) (None, None, None, 2 0 bn2c_branch2c[0][0]res2b_relu[0][0]__________________________________________________________________________________________________res2c_relu (Activation) (None, None, None, 2 0 res2c[0][0]__________________________________________________________________________________________________res3a_branch2a (Conv2D) (None, None, None, 1 32768 res2c_relu[0][0]__________________________________________________________________________________________________bn3a_branch2a (BatchNormalizati (None, None, None, 1 512 res3a_branch2a[0][0]__________________________________________________________________________________________________res3a_branch2a_relu (Activation (None, None, None, 1 0 bn3a_branch2a[0][0]__________________________________________________________________________________________________padding3a_branch2b (ZeroPadding (None, None, None, 1 0 res3a_branch2a_relu[0][0]__________________________________________________________________________________________________res3a_branch2b (Conv2D) (None, None, None, 1 147456padding3a_branch2b[0][0]__________________________________________________________________________________________________bn3a_branch2b (BatchNormalizati (None, None, None, 1 512 res3a_branch2b[0][0]__________________________________________________________________________________________________res3a_branch2b_relu (Activation (None, None, None, 1 0 bn3a_branch2b[0][0]__________________________________________________________________________________________________res3a_branch2c (Conv2D) (None, None, None, 5 65536 res3a_branch2b_relu[0][0]__________________________________________________________________________________________________res3a_branch1 (Conv2D)(None, None, None, 5 131072res2c_relu[0][0]__________________________________________________________________________________________________bn3a_branch2c (BatchNormalizati (None, None, None, 5 2048 res3a_branch2c[0][0]__________________________________________________________________________________________________bn3a_branch1 (BatchNormalizatio (None, None, None, 5 2048 res3a_branch1[0][0]__________________________________________________________________________________________________res3a (Add) (None, None, None, 5 0 bn3a_branch2c[0][0]bn3a_branch1[0][0]__________________________________________________________________________________________________res3a_relu (Activation) (None, None, None, 5 0 res3a[0][0]__________________________________________________________________________________________________res3b_branch2a (Conv2D) (None, None, None, 1 65536 res3a_relu[0][0]__________________________________________________________________________________________________bn3b_branch2a (BatchNormalizati (None, None, None, 1 512 res3b_branch2a[0][0]__________________________________________________________________________________________________res3b_branch2a_relu (Activation (None, None, None, 1 0 bn3b_branch2a[0][0]__________________________________________________________________________________________________padding3b_branch2b (ZeroPadding (None, None, None, 1 0 res3b_branch2a_relu[0][0]__________________________________________________________________________________________________res3b_branch2b (Conv2D) (None, None, None, 1 147456padding3b_branch2b[0][0]__________________________________________________________________________________________________bn3b_branch2b (BatchNormalizati (None, None, None, 1 512 res3b_branch2b[0][0]__________________________________________________________________________________________________res3b_branch2b_relu (Activation (None, None, None, 1 0 bn3b_branch2b[0][0]__________________________________________________________________________________________________res3b_branch2c (Conv2D) (None, None, None, 5 65536 res3b_branch2b_relu[0][0]__________________________________________________________________________________________________bn3b_branch2c (BatchNormalizati (None, None, None, 5 2048 res3b_branch2c[0][0]__________________________________________________________________________________________________res3b (Add) (None, None, None, 5 0 bn3b_branch2c[0][0]res3a_relu[0][0]__________________________________________________________________________________________________res3b_relu (Activation) (None, None, None, 5 0 res3b[0][0]__________________________________________________________________________________________________res3c_branch2a (Conv2D) (None, None, None, 1 65536 res3b_relu[0][0]__________________________________________________________________________________________________bn3c_branch2a (BatchNormalizati (None, None, None, 1 512 res3c_branch2a[0][0]__________________________________________________________________________________________________res3c_branch2a_relu (Activation (None, None, None, 1 0 bn3c_branch2a[0][0]__________________________________________________________________________________________________padding3c_branch2b (ZeroPadding (None, None, None, 1 0 res3c_branch2a_relu[0][0]__________________________________________________________________________________________________res3c_branch2b (Conv2D) (None, None, None, 1 147456padding3c_branch2b[0][0]__________________________________________________________________________________________________bn3c_branch2b (BatchNormalizati (None, None, None, 1 512 res3c_branch2b[0][0]__________________________________________________________________________________________________res3c_branch2b_relu (Activation (None, None, None, 1 0 bn3c_branch2b[0][0]__________________________________________________________________________________________________res3c_branch2c (Conv2D) (None, None, None, 5 65536 res3c_branch2b_relu[0][0]__________________________________________________________________________________________________bn3c_branch2c (BatchNormalizati (None, None, None, 5 2048 res3c_branch2c[0][0]__________________________________________________________________________________________________res3c (Add) (None, None, None, 5 0 bn3c_branch2c[0][0]res3b_relu[0][0]__________________________________________________________________________________________________res3c_relu (Activation) (None, None, None, 5 0 res3c[0][0]__________________________________________________________________________________________________res3d_branch2a (Conv2D) (None, None, None, 1 65536 res3c_relu[0][0]__________________________________________________________________________________________________bn3d_branch2a (BatchNormalizati (None, None, None, 1 512 res3d_branch2a[0][0]__________________________________________________________________________________________________res3d_branch2a_relu (Activation (None, None, None, 1 0 bn3d_branch2a[0][0]__________________________________________________________________________________________________padding3d_branch2b (ZeroPadding (None, None, None, 1 0 res3d_branch2a_relu[0][0]__________________________________________________________________________________________________res3d_branch2b (Conv2D) (None, None, None, 1 147456padding3d_branch2b[0][0]__________________________________________________________________________________________________bn3d_branch2b (BatchNormalizati (None, None, None, 1 512 res3d_branch2b[0][0]__________________________________________________________________________________________________res3d_branch2b_relu (Activation (None, None, None, 1 0 bn3d_branch2b[0][0]__________________________________________________________________________________________________res3d_branch2c (Conv2D) (None, None, None, 5 65536 res3d_branch2b_relu[0][0]__________________________________________________________________________________________________bn3d_branch2c (BatchNormalizati (None, None, None, 5 2048 res3d_branch2c[0][0]__________________________________________________________________________________________________res3d (Add) (None, None, None, 5 0 bn3d_branch2c[0][0]res3c_relu[0][0]__________________________________________________________________________________________________res3d_relu (Activation) (None, None, None, 5 0 res3d[0][0]__________________________________________________________________________________________________res4a_branch2a (Conv2D) (None, None, None, 2 131072res3d_relu[0][0]__________________________________________________________________________________________________bn4a_branch2a (BatchNormalizati (None, None, None, 2 1024 res4a_branch2a[0][0]__________________________________________________________________________________________________res4a_branch2a_relu (Activation (None, None, None, 2 0 bn4a_branch2a[0][0]__________________________________________________________________________________________________padding4a_branch2b (ZeroPadding (None, None, None, 2 0 res4a_branch2a_relu[0][0]__________________________________________________________________________________________________res4a_branch2b (Conv2D) (None, None, None, 2 589824padding4a_branch2b[0][0]__________________________________________________________________________________________________bn4a_branch2b (BatchNormalizati (None, None, None, 2 1024 res4a_branch2b[0][0]__________________________________________________________________________________________________res4a_branch2b_relu (Activation (None, None, None, 2 0 bn4a_branch2b[0][0]__________________________________________________________________________________________________res4a_branch2c (Conv2D) (None, None, None, 1 262144res4a_branch2b_relu[0][0]__________________________________________________________________________________________________res4a_branch1 (Conv2D)(None, None, None, 1 524288res3d_relu[0][0]__________________________________________________________________________________________________bn4a_branch2c (BatchNormalizati (None, None, None, 1 4096 res4a_branch2c[0][0]__________________________________________________________________________________________________bn4a_branch1 (BatchNormalizatio (None, None, None, 1 4096 res4a_branch1[0][0]__________________________________________________________________________________________________res4a (Add) (None, None, None, 1 0 bn4a_branch2c[0][0]bn4a_branch1[0][0]__________________________________________________________________________________________________res4a_relu (Activation) (None, None, None, 1 0 res4a[0][0]__________________________________________________________________________________________________res4b_branch2a (Conv2D) (None, None, None, 2 262144res4a_relu[0][0]__________________________________________________________________________________________________bn4b_branch2a (BatchNormalizati (None, None, None, 2 1024 res4b_branch2a[0][0]__________________________________________________________________________________________________res4b_branch2a_relu (Activation (None, None, None, 2 0 bn4b_branch2a[0][0]__________________________________________________________________________________________________padding4b_branch2b (ZeroPadding (None, None, None, 2 0 res4b_branch2a_relu[0][0]__________________________________________________________________________________________________res4b_branch2b (Conv2D) (None, None, None, 2 589824padding4b_branch2b[0][0]__________________________________________________________________________________________________bn4b_branch2b (BatchNormalizati (None, None, None, 2 1024 res4b_branch2b[0][0]__________________________________________________________________________________________________res4b_branch2b_relu (Activation (None, None, None, 2 0 bn4b_branch2b[0][0]__________________________________________________________________________________________________res4b_branch2c (Conv2D) (None, None, None, 1 262144res4b_branch2b_relu[0][0]__________________________________________________________________________________________________bn4b_branch2c (BatchNormalizati (None, None, None, 1 4096 res4b_branch2c[0][0]__________________________________________________________________________________________________res4b (Add) (None, None, None, 1 0 bn4b_branch2c[0][0]res4a_relu[0][0]__________________________________________________________________________________________________res4b_relu (Activation) (None, None, None, 1 0 res4b[0][0]__________________________________________________________________________________________________res4c_branch2a (Conv2D) (None, None, None, 2 262144res4b_relu[0][0]__________________________________________________________________________________________________bn4c_branch2a (BatchNormalizati (None, None, None, 2 1024 res4c_branch2a[0][0]__________________________________________________________________________________________________res4c_branch2a_relu (Activation (None, None, None, 2 0 bn4c_branch2a[0][0]__________________________________________________________________________________________________padding4c_branch2b (ZeroPadding (None, None, None, 2 0 res4c_branch2a_relu[0][0]__________________________________________________________________________________________________res4c_branch2b (Conv2D) (None, None, None, 2 589824padding4c_branch2b[0][0]__________________________________________________________________________________________________bn4c_branch2b (BatchNormalizati (None, None, None, 2 1024 res4c_branch2b[0][0]__________________________________________________________________________________________________res4c_branch2b_relu (Activation (None, None, None, 2 0 bn4c_branch2b[0][0]__________________________________________________________________________________________________res4c_branch2c (Conv2D) (None, None, None, 1 262144res4c_branch2b_relu[0][0]__________________________________________________________________________________________________bn4c_branch2c (BatchNormalizati (None, None, None, 1 4096 res4c_branch2c[0][0]__________________________________________________________________________________________________res4c (Add) (None, None, None, 1 0 bn4c_branch2c[0][0]res4b_relu[0][0]__________________________________________________________________________________________________res4c_relu (Activation) (None, None, None, 1 0 res4c[0][0]__________________________________________________________________________________________________res4d_branch2a (Conv2D) (None, None, None, 2 262144res4c_relu[0][0]__________________________________________________________________________________________________bn4d_branch2a (BatchNormalizati (None, None, None, 2 1024 res4d_branch2a[0][0]__________________________________________________________________________________________________res4d_branch2a_relu (Activation (None, None, None, 2 0 bn4d_branch2a[0][0]__________________________________________________________________________________________________padding4d_branch2b (ZeroPadding (None, None, None, 2 0 res4d_branch2a_relu[0][0]__________________________________________________________________________________________________res4d_branch2b (Conv2D) (None, None, None, 2 589824padding4d_branch2b[0][0]__________________________________________________________________________________________________bn4d_branch2b (BatchNormalizati (None, None, None, 2 1024 res4d_branch2b[0][0]__________________________________________________________________________________________________res4d_branch2b_relu (Activation (None, None, None, 2 0 bn4d_branch2b[0][0]__________________________________________________________________________________________________res4d_branch2c (Conv2D) (None, None, None, 1 262144res4d_branch2b_relu[0][0]__________________________________________________________________________________________________bn4d_branch2c (BatchNormalizati (None, None, None, 1 4096 res4d_branch2c[0][0]__________________________________________________________________________________________________res4d (Add) (None, None, None, 1 0 bn4d_branch2c[0][0]res4c_relu[0][0]__________________________________________________________________________________________________res4d_relu (Activation) (None, None, None, 1 0 res4d[0][0]__________________________________________________________________________________________________res4e_branch2a (Conv2D) (None, None, None, 2 262144res4d_relu[0][0]__________________________________________________________________________________________________bn4e_branch2a (BatchNormalizati (None, None, None, 2 1024 res4e_branch2a[0][0]__________________________________________________________________________________________________res4e_branch2a_relu (Activation (None, None, None, 2 0 bn4e_branch2a[0][0]__________________________________________________________________________________________________padding4e_branch2b (ZeroPadding (None, None, None, 2 0 res4e_branch2a_relu[0][0]__________________________________________________________________________________________________res4e_branch2b (Conv2D) (None, None, None, 2 589824padding4e_branch2b[0][0]__________________________________________________________________________________________________bn4e_branch2b (BatchNormalizati (None, None, None, 2 1024 res4e_branch2b[0][0]__________________________________________________________________________________________________res4e_branch2b_relu (Activation (None, None, None, 2 0 bn4e_branch2b[0][0]__________________________________________________________________________________________________res4e_branch2c (Conv2D) (None, None, None, 1 262144res4e_branch2b_relu[0][0]__________________________________________________________________________________________________bn4e_branch2c (BatchNormalizati (None, None, None, 1 4096 res4e_branch2c[0][0]__________________________________________________________________________________________________res4e (Add) (None, None, None, 1 0 bn4e_branch2c[0][0]res4d_relu[0][0]__________________________________________________________________________________________________res4e_relu (Activation) (None, None, None, 1 0 res4e[0][0]__________________________________________________________________________________________________res4f_branch2a (Conv2D) (None, None, None, 2 262144res4e_relu[0][0]__________________________________________________________________________________________________bn4f_branch2a (BatchNormalizati (None, None, None, 2 1024 res4f_branch2a[0][0]__________________________________________________________________________________________________res4f_branch2a_relu (Activation (None, None, None, 2 0 bn4f_branch2a[0][0]__________________________________________________________________________________________________padding4f_branch2b (ZeroPadding (None, None, None, 2 0 res4f_branch2a_relu[0][0]__________________________________________________________________________________________________res4f_branch2b (Conv2D) (None, None, None, 2 589824padding4f_branch2b[0][0]__________________________________________________________________________________________________bn4f_branch2b (BatchNormalizati (None, None, None, 2 1024 res4f_branch2b[0][0]__________________________________________________________________________________________________res4f_branch2b_relu (Activation (None, None, None, 2 0 bn4f_branch2b[0][0]__________________________________________________________________________________________________res4f_branch2c (Conv2D) (None, None, None, 1 262144res4f_branch2b_relu[0][0]__________________________________________________________________________________________________bn4f_branch2c (BatchNormalizati (None, None, None, 1 4096 res4f_branch2c[0][0]__________________________________________________________________________________________________res4f (Add) (None, None, None, 1 0 bn4f_branch2c[0][0]res4e_relu[0][0]__________________________________________________________________________________________________res4f_relu (Activation) (None, None, None, 1 0 res4f[0][0]__________________________________________________________________________________________________res5a_branch2a (Conv2D) (None, None, None, 5 524288res4f_relu[0][0]__________________________________________________________________________________________________bn5a_branch2a (BatchNormalizati (None, None, None, 5 2048 res5a_branch2a[0][0]__________________________________________________________________________________________________res5a_branch2a_relu (Activation (None, None, None, 5 0 bn5a_branch2a[0][0]__________________________________________________________________________________________________padding5a_branch2b (ZeroPadding (None, None, None, 5 0 res5a_branch2a_relu[0][0]__________________________________________________________________________________________________res5a_branch2b (Conv2D) (None, None, None, 5 2359296padding5a_branch2b[0][0]__________________________________________________________________________________________________bn5a_branch2b (BatchNormalizati (None, None, None, 5 2048 res5a_branch2b[0][0]__________________________________________________________________________________________________res5a_branch2b_relu (Activation (None, None, None, 5 0 bn5a_branch2b[0][0]__________________________________________________________________________________________________res5a_branch2c (Conv2D) (None, None, None, 2 1048576res5a_branch2b_relu[0][0]__________________________________________________________________________________________________res5a_branch1 (Conv2D)(None, None, None, 2 2097152res4f_relu[0][0]__________________________________________________________________________________________________bn5a_branch2c (BatchNormalizati (None, None, None, 2 8192 res5a_branch2c[0][0]__________________________________________________________________________________________________bn5a_branch1 (BatchNormalizatio (None, None, None, 2 8192 res5a_branch1[0][0]__________________________________________________________________________________________________res5a (Add) (None, None, None, 2 0 bn5a_branch2c[0][0]bn5a_branch1[0][0]__________________________________________________________________________________________________res5a_relu (Activation) (None, None, None, 2 0 res5a[0][0]__________________________________________________________________________________________________res5b_branch2a (Conv2D) (None, None, None, 5 1048576res5a_relu[0][0]__________________________________________________________________________________________________bn5b_branch2a (BatchNormalizati (None, None, None, 5 2048 res5b_branch2a[0][0]__________________________________________________________________________________________________res5b_branch2a_relu (Activation (None, None, None, 5 0 bn5b_branch2a[0][0]__________________________________________________________________________________________________padding5b_branch2b (ZeroPadding (None, None, None, 5 0 res5b_branch2a_relu[0][0]__________________________________________________________________________________________________res5b_branch2b (Conv2D) (None, None, None, 5 2359296padding5b_branch2b[0][0]__________________________________________________________________________________________________bn5b_branch2b (BatchNormalizati (None, None, None, 5 2048 res5b_branch2b[0][0]__________________________________________________________________________________________________res5b_branch2b_relu (Activation (None, None, None, 5 0 bn5b_branch2b[0][0]__________________________________________________________________________________________________res5b_branch2c (Conv2D) (None, None, None, 2 1048576res5b_branch2b_relu[0][0]__________________________________________________________________________________________________bn5b_branch2c (BatchNormalizati (None, None, None, 2 8192 res5b_branch2c[0][0]__________________________________________________________________________________________________res5b (Add) (None, None, None, 2 0 bn5b_branch2c[0][0]res5a_relu[0][0]__________________________________________________________________________________________________res5b_relu (Activation) (None, None, None, 2 0 res5b[0][0]__________________________________________________________________________________________________res5c_branch2a (Conv2D) (None, None, None, 5 1048576res5b_relu[0][0]__________________________________________________________________________________________________bn5c_branch2a (BatchNormalizati (None, None, None, 5 2048 res5c_branch2a[0][0]__________________________________________________________________________________________________res5c_branch2a_relu (Activation (None, None, None, 5 0 bn5c_branch2a[0][0]__________________________________________________________________________________________________padding5c_branch2b (ZeroPadding (None, None, None, 5 0 res5c_branch2a_relu[0][0]__________________________________________________________________________________________________res5c_branch2b (Conv2D) (None, None, None, 5 2359296padding5c_branch2b[0][0]__________________________________________________________________________________________________bn5c_branch2b (BatchNormalizati (None, None, None, 5 2048 res5c_branch2b[0][0]__________________________________________________________________________________________________res5c_branch2b_relu (Activation (None, None, None, 5 0 bn5c_branch2b[0][0]__________________________________________________________________________________________________res5c_branch2c (Conv2D) (None, None, None, 2 1048576res5c_branch2b_relu[0][0]__________________________________________________________________________________________________bn5c_branch2c (BatchNormalizati (None, None, None, 2 8192 res5c_branch2c[0][0]__________________________________________________________________________________________________res5c (Add) (None, None, None, 2 0 bn5c_branch2c[0][0]res5b_relu[0][0]__________________________________________________________________________________________________res5c_relu (Activation) (None, None, None, 2 0 res5c[0][0]__________________________________________________________________________________________________C5_reduced (Conv2D) (None, None, None, 2 524544res5c_relu[0][0]__________________________________________________________________________________________________P5_upsampled (UpsampleLike)(None, None, None, 2 0 C5_reduced[0][0]res4f_relu[0][0]__________________________________________________________________________________________________C4_reduced (Conv2D) (None, None, None, 2 262400res4f_relu[0][0]__________________________________________________________________________________________________P4_merged (Add) (None, None, None, 2 0 P5_upsampled[0][0]C4_reduced[0][0]__________________________________________________________________________________________________P4_upsampled (UpsampleLike)(None, None, None, 2 0 P4_merged[0][0]res3d_relu[0][0]__________________________________________________________________________________________________C3_reduced (Conv2D) (None, None, None, 2 131328res3d_relu[0][0]__________________________________________________________________________________________________P6 (Conv2D) (None, None, None, 2 4718848res5c_relu[0][0]__________________________________________________________________________________________________P3_merged (Add) (None, None, None, 2 0 P4_upsampled[0][0]C3_reduced[0][0]__________________________________________________________________________________________________C6_relu (Activation) (None, None, None, 2 0 P6[0][0]__________________________________________________________________________________________________P3 (Conv2D) (None, None, None, 2 590080P3_merged[0][0]__________________________________________________________________________________________________P4 (Conv2D) (None, None, None, 2 590080P4_merged[0][0]__________________________________________________________________________________________________P5 (Conv2D) (None, None, None, 2 590080C5_reduced[0][0]__________________________________________________________________________________________________P7 (Conv2D) (None, None, None, 2 590080C6_relu[0][0]__________________________________________________________________________________________________regression_submodel (Model)(None, None, 4)2443300P3[0][0]P4[0][0]P5[0][0]P6[0][0]P7[0][0]__________________________________________________________________________________________________classification_submodel (Model) (None, None, 1)2381065P3[0][0]P4[0][0]P5[0][0]P6[0][0]P7[0][0]__________________________________________________________________________________________________regression (Concatenate) (None, None, 4)0 regression_submodel[1][0]regression_submodel[2][0]regression_submodel[3][0]regression_submodel[4][0]regression_submodel[5][0]__________________________________________________________________________________________________classification (Concatenate) (None, None, 1)0 classification_submodel[1][0]classification_submodel[2][0]classification_submodel[3][0]classification_submodel[4][0]classification_submodel[5][0]==================================================================================================Total params: 36,382,957Trainable params: 36,276,717Non-trainable params: 106,240______________________________________________________________________________________________

开始训练

____NoneEpoch 1/30-03-13 13:03:36.209789: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.07GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.266466: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.07GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.275037: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.25GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.288299: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.26GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.315962: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.13GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.651783: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.06GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.660588: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.06GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.680880: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.13GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.689715: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 1.13GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.-03-13 13:03:36.698370: W tensorflow/core/common_runtime/:215] Allocator (GPU_0_bfc) ran out of memory trying to allocate 2.26GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory were available.100/100 [==============================] - 20s 204ms/step - loss: 0.7675 - regression_loss: 0.4491 - classification_loss: 0.3185Epoch 00001: saving model to ./snapshots\resnet50_csv_01.h5Epoch 2/30100/100 [==============================] - 18s 177ms/step - loss: 1.1027 - regression_loss: 0.4177 - classification_loss: 0.6850Epoch 00002: saving model to ./snapshots\resnet50_csv_02.h5Epoch 3/30100/100 [==============================] - 16s 160ms/step - loss: 0.9961 - regression_loss: 0.3917 - classification_loss: 0.6044Epoch 00003: saving model to ./snapshots\resnet50_csv_03.h5Epoch 00003: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-07.Epoch 4/30100/100 [==============================] - 16s 161ms/step - loss: 1.0841 - regression_loss: 0.3990 - classification_loss: 0.6850Epoch 00004: saving model to ./snapshots\resnet50_csv_04.h5Epoch 5/30100/100 [==============================] - 16s 160ms/step - loss: 0.9457 - regression_loss: 0.3413 - classification_loss: 0.6044Epoch 00005: saving model to ./snapshots\resnet50_csv_05.h5Epoch 00005: ReduceLROnPlateau reducing learning rate to 9.999999974752428e-08.Epoch 6/30100/100 [==============================] - 16s 160ms/step - loss: 1.0741 - regression_loss: 0.3891 - classification_loss: 0.6850Epoch 00006: saving model to ./snapshots\resnet50_csv_06.h5Epoch 7/30100/100 [==============================] - 16s 160ms/step - loss: 0.9355 - regression_loss: 0.3311 - classification_loss: 0.6044Epoch 00007: saving model to ./snapshots\resnet50_csv_07.h5Epoch 00007: ReduceLROnPlateau reducing learning rate to 1.0000000116860975e-08.Epoch 8/30100/100 [==============================] - 16s 160ms/step - loss: 1.0721 - regression_loss: 0.3871 - classification_loss: 0.6850Epoch 00008: saving model to ./snapshots\resnet50_csv_08.h5Epoch 9/30100/100 [==============================] - 16s 160ms/step - loss: 0.9348 - regression_loss: 0.3304 - classification_loss: 0.6044Epoch 00009: saving model to ./snapshots\resnet50_csv_09.h5Epoch 00009: ReduceLROnPlateau reducing learning rate to 9.999999939225292e-10.Epoch 10/30100/100 [==============================] - 16s 160ms/step - loss: 1.0737 - regression_loss: 0.3887 - classification_loss: 0.6850Epoch 00010: saving model to ./snapshots\resnet50_csv_10.h5Epoch 11/30100/100 [==============================] - 16s 160ms/step - loss: 0.9367 - regression_loss: 0.3322 - classification_loss: 0.6044Epoch 00011: saving model to ./snapshots\resnet50_csv_11.h5Epoch 00011: ReduceLROnPlateau reducing learning rate to 9.999999717180686e-11.Epoch 12/30100/100 [==============================] - 16s 160ms/step - loss: 1.0711 - regression_loss: 0.3860 - classification_loss: 0.6850Epoch 00012: saving model to ./snapshots\resnet50_csv_12.h5Epoch 13/30100/100 [==============================] - 16s 160ms/step - loss: 0.9347 - regression_loss: 0.3303 - classification_loss: 0.6044Epoch 00013: saving model to ./snapshots\resnet50_csv_13.h5Epoch 00013: ReduceLROnPlateau reducing learning rate to 9.99999943962493e-12.Epoch 14/30100/100 [==============================] - 16s 160ms/step - loss: 1.0722 - regression_loss: 0.3872 - classification_loss: 0.6850Epoch 00014: saving model to ./snapshots\resnet50_csv_14.h5Epoch 15/30100/100 [==============================] - 16s 160ms/step - loss: 0.9349 - regression_loss: 0.3305 - classification_loss: 0.6044Epoch 00015: saving model to ./snapshots\resnet50_csv_15.h5Epoch 00015: ReduceLROnPlateau reducing learning rate to 9.999999092680235e-13.Epoch 16/30100/100 [==============================] - 16s 160ms/step - loss: 1.0713 - regression_loss: 0.3863 - classification_loss: 0.6850Epoch 00016: saving model to ./snapshots\resnet50_csv_16.h5Epoch 17/30100/100 [==============================] - 16s 160ms/step - loss: 0.9337 - regression_loss: 0.3293 - classification_loss: 0.6044Epoch 00017: saving model to ./snapshots\resnet50_csv_17.h5Epoch 00017: ReduceLROnPlateau reducing learning rate to 9.9999988758398e-14.Epoch 18/30100/100 [==============================] - 16s 160ms/step - loss: 1.0704 - regression_loss: 0.3853 - classification_loss: 0.6850Epoch 00018: saving model to ./snapshots\resnet50_csv_18.h5Epoch 19/30100/100 [==============================] - 16s 160ms/step - loss: 0.9337 - regression_loss: 0.3293 - classification_loss: 0.6044Epoch 00019: saving model to ./snapshots\resnet50_csv_19.h5Epoch 00019: ReduceLROnPlateau reducing learning rate to 9.999999146890344e-15.Epoch 20/30100/100 [==============================] - 16s 160ms/step - loss: 1.0695 - regression_loss: 0.3845 - classification_loss: 0.6850Epoch 00020: saving model to ./snapshots\resnet50_csv_20.h5Epoch 21/30100/100 [==============================] - 16s 160ms/step - loss: 0.9350 - regression_loss: 0.3306 - classification_loss: 0.6044Epoch 00021: saving model to ./snapshots\resnet50_csv_21.h5Epoch 00021: ReduceLROnPlateau reducing learning rate to 9.999998977483753e-16.Epoch 22/30100/100 [==============================] - 16s 160ms/step - loss: 1.0708 - regression_loss: 0.3858 - classification_loss: 0.6850Epoch 00022: saving model to ./snapshots\resnet50_csv_22.h5Epoch 23/30100/100 [==============================] - 16s 165ms/step - loss: 0.9333 - regression_loss: 0.3288 - classification_loss: 0.6044Epoch 00023: saving model to ./snapshots\resnet50_csv_23.h5Epoch 00023: ReduceLROnPlateau reducing learning rate to 9.999998977483754e-17.Epoch 24/30100/100 [==============================] - 17s 167ms/step - loss: 1.0715 - regression_loss: 0.3865 - classification_loss: 0.6850Epoch 00024: saving model to ./snapshots\resnet50_csv_24.h5Epoch 25/30100/100 [==============================] - 17s 165ms/step - loss: 0.9352 - regression_loss: 0.3308 - classification_loss: 0.6044Epoch 00025: saving model to ./snapshots\resnet50_csv_25.h5Epoch 00025: ReduceLROnPlateau reducing learning rate to 9.999998845134856e-18.Epoch 26/30100/100 [==============================] - 17s 167ms/step - loss: 1.0729 - regression_loss: 0.3878 - classification_loss: 0.6850Epoch 00026: saving model to ./snapshots\resnet50_csv_26.h5Epoch 27/30100/100 [==============================] - 17s 167ms/step - loss: 0.9355 - regression_loss: 0.3311 - classification_loss: 0.6044Epoch 00027: saving model to ./snapshots\resnet50_csv_27.h5Epoch 00027: ReduceLROnPlateau reducing learning rate to 9.999999010570977e-19.Epoch 28/30100/100 [==============================] - 17s 167ms/step - loss: 1.0724 - regression_loss: 0.3874 - classification_loss: 0.6850Epoch 00028: saving model to ./snapshots\resnet50_csv_28.h5Epoch 29/30100/100 [==============================] - 17s 166ms/step - loss: 0.9339 - regression_loss: 0.3294 - classification_loss: 0.6044Epoch 00029: saving model to ./snapshots\resnet50_csv_29.h5Epoch 00029: ReduceLROnPlateau reducing learning rate to 9.999999424161285e-20.Epoch 30/30100/100 [==============================] - 17s 166ms/step - loss: 1.0727 - regression_loss: 0.3877 - classification_loss: 0.6850Epoch 00030: saving model to ./snapshots\resnet50_csv_30.h5

4.目标检测

将模型进行转换后才能进行目标检测,在命令行执行convert_model.py

python D:/PyCharm/PycharmProjects/tf-gpu-env/project/keras-retinanet-master/keras_retinanet/bin/convert_model.py D:\PyCharm\PycharmProjects\tf-gpu-env\project\keras-retinanet-master\keras_retinanet\bin\snapshots\resnet50_csv_34.h5 D:\PyCharm\PycharmProjects\tf-gpu-env\project\keras-retinanet-master\cov_resnet50_csv_34.h5

如果没有转换的话,会报错误: boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0)) 这一句出错了,错误是:ValueError: not enough values to unpack (expected 3, got 2)

将转换后的模型拷贝到snapshots 目录下在example里新建一个目标检测的.py文件,可以将修改以下几处

改成转换后的模型名称

​​​​​​​改成你自己模型的类别对应关系

待检测的图片地址

修改阈值

特别感谢:

/fizyr/keras-retinanet

/tzutalin/labelImg

/gaohuazhao/article/details/60871886

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。