最近在學習 keras_retinanet ,下面就記錄下用自己的數據集進行的模型訓練。
大致分爲以下幾步:
- 自定義訓練數據
- 圖片目標標註
- 生成用於訓練的圖片名稱、目標標註位置及目標類別的.csv文件
- 開始訓練模型(注意參數調整)
- 轉換訓練好的模型
- 用轉換後的模型進行目標檢測
下面就一步一步介紹吧:
目錄
由於我之前已經安裝了vs2015、Anaconda和Pycharm,就不在此贅述了。
本機配置:
- GTX 1060 3G
- Win10 64
- vs2015
- Anaconda3 5.1.0
- Pycharm
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模型訓練的數據是按照VOC2007格式處理的,所以你也需要將自己的數據準備成VOC2007的格式
- 準備如圖三個文件夾,JPEGImages放你自己準備訓練模型的圖片,圖片名稱最好是按1.jpg,2.jpg這種類型;
- Annotations放圖片目標的位置和類型標註的.xml文件,這個可以用Github——>labellmg裏的生成目標標註的工具自動生成.xml的文件(注意使用時將保存路徑改到Annotations文件夾);
- ImageSets裏的子文件夾Main裏放按比例隨機抽樣切分的訓練集、驗證集、測試集樣本下標值的txt文件,這個可以用如下gen_main_txt.py自動生成
import os import random trainval_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 csv import os import glob import sys class 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 = xml self.ann_path = ann_path self.classes_path = classes_path self.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: continue sys.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: F401 from keras_retinanet import losses from keras_retinanet import models from keras_retinanet.callbacks import RedirectModel from keras_retinanet.callbacks.eval import Evaluate from keras_retinanet.models.retinanet import retinanet_bbox from keras_retinanet.preprocessing.csv_generator import CSVGenerator from keras_retinanet.preprocessing.kitti import KittiGenerator from keras_retinanet.preprocessing.open_images import OpenImagesGenerator from keras_retinanet.preprocessing.pascal_voc import PascalVocGenerator from keras_retinanet.utils.anchors import make_shapes_callback from keras_retinanet.utils.config import read_config_file, parse_anchor_parameters from keras_retinanet.utils.keras_version import check_keras_version from keras_retinanet.utils.model import freeze as freeze_model from 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 147456 padding3a_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 131072 res2c_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 147456 padding3b_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 147456 padding3c_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 147456 padding3d_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 131072 res3d_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 589824 padding4a_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 262144 res4a_branch2b_relu[0][0]
__________________________________________________________________________________________________
res4a_branch1 (Conv2D) (None, None, None, 1 524288 res3d_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 262144 res4a_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 589824 padding4b_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 262144 res4b_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 262144 res4b_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 589824 padding4c_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 262144 res4c_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 262144 res4c_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 589824 padding4d_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 262144 res4d_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 262144 res4d_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 589824 padding4e_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 262144 res4e_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 262144 res4e_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 589824 padding4f_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 262144 res4f_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 524288 res4f_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 2359296 padding5a_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 1048576 res5a_branch2b_relu[0][0]
__________________________________________________________________________________________________
res5a_branch1 (Conv2D) (None, None, None, 2 2097152 res4f_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 1048576 res5a_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 2359296 padding5b_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 1048576 res5b_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 1048576 res5b_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 2359296 padding5c_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 1048576 res5c_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 524544 res5c_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 262400 res4f_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 131328 res3d_relu[0][0]
__________________________________________________________________________________________________
P6 (Conv2D) (None, None, None, 2 4718848 res5c_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 590080 P3_merged[0][0]
__________________________________________________________________________________________________
P4 (Conv2D) (None, None, None, 2 590080 P4_merged[0][0]
__________________________________________________________________________________________________
P5 (Conv2D) (None, None, None, 2 590080 C5_reduced[0][0]
__________________________________________________________________________________________________
P7 (Conv2D) (None, None, None, 2 590080 C6_relu[0][0]
__________________________________________________________________________________________________
regression_submodel (Model) (None, None, 4) 2443300 P3[0][0]
P4[0][0]
P5[0][0]
P6[0][0]
P7[0][0]
__________________________________________________________________________________________________
classification_submodel (Model) (None, None, 1) 2381065 P3[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,957
Trainable params: 36,276,717
Non-trainable params: 106,240
______________________________________________________________________________________________
開始訓練
____
None
Epoch 1/30
2019-03-13 13:03:36.209789: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.266466: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.275037: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.288299: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.315962: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.651783: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.660588: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.680880: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.689715: W tensorflow/core/common_runtime/bfc_allocator.cc: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.
2019-03-13 13:03:36.698370: W tensorflow/core/common_runtime/bfc_allocator.cc: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.3185
Epoch 00001: saving model to ./snapshots\resnet50_csv_01.h5
Epoch 2/30
100/100 [==============================] - 18s 177ms/step - loss: 1.1027 - regression_loss: 0.4177 - classification_loss: 0.6850
Epoch 00002: saving model to ./snapshots\resnet50_csv_02.h5
Epoch 3/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9961 - regression_loss: 0.3917 - classification_loss: 0.6044
Epoch 00003: saving model to ./snapshots\resnet50_csv_03.h5
Epoch 00003: ReduceLROnPlateau reducing learning rate to 9.999999747378752e-07.
Epoch 4/30
100/100 [==============================] - 16s 161ms/step - loss: 1.0841 - regression_loss: 0.3990 - classification_loss: 0.6850
Epoch 00004: saving model to ./snapshots\resnet50_csv_04.h5
Epoch 5/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9457 - regression_loss: 0.3413 - classification_loss: 0.6044
Epoch 00005: saving model to ./snapshots\resnet50_csv_05.h5
Epoch 00005: ReduceLROnPlateau reducing learning rate to 9.999999974752428e-08.
Epoch 6/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0741 - regression_loss: 0.3891 - classification_loss: 0.6850
Epoch 00006: saving model to ./snapshots\resnet50_csv_06.h5
Epoch 7/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9355 - regression_loss: 0.3311 - classification_loss: 0.6044
Epoch 00007: saving model to ./snapshots\resnet50_csv_07.h5
Epoch 00007: ReduceLROnPlateau reducing learning rate to 1.0000000116860975e-08.
Epoch 8/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0721 - regression_loss: 0.3871 - classification_loss: 0.6850
Epoch 00008: saving model to ./snapshots\resnet50_csv_08.h5
Epoch 9/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9348 - regression_loss: 0.3304 - classification_loss: 0.6044
Epoch 00009: saving model to ./snapshots\resnet50_csv_09.h5
Epoch 00009: ReduceLROnPlateau reducing learning rate to 9.999999939225292e-10.
Epoch 10/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0737 - regression_loss: 0.3887 - classification_loss: 0.6850
Epoch 00010: saving model to ./snapshots\resnet50_csv_10.h5
Epoch 11/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9367 - regression_loss: 0.3322 - classification_loss: 0.6044
Epoch 00011: saving model to ./snapshots\resnet50_csv_11.h5
Epoch 00011: ReduceLROnPlateau reducing learning rate to 9.999999717180686e-11.
Epoch 12/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0711 - regression_loss: 0.3860 - classification_loss: 0.6850
Epoch 00012: saving model to ./snapshots\resnet50_csv_12.h5
Epoch 13/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9347 - regression_loss: 0.3303 - classification_loss: 0.6044
Epoch 00013: saving model to ./snapshots\resnet50_csv_13.h5
Epoch 00013: ReduceLROnPlateau reducing learning rate to 9.99999943962493e-12.
Epoch 14/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0722 - regression_loss: 0.3872 - classification_loss: 0.6850
Epoch 00014: saving model to ./snapshots\resnet50_csv_14.h5
Epoch 15/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9349 - regression_loss: 0.3305 - classification_loss: 0.6044
Epoch 00015: saving model to ./snapshots\resnet50_csv_15.h5
Epoch 00015: ReduceLROnPlateau reducing learning rate to 9.999999092680235e-13.
Epoch 16/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0713 - regression_loss: 0.3863 - classification_loss: 0.6850
Epoch 00016: saving model to ./snapshots\resnet50_csv_16.h5
Epoch 17/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9337 - regression_loss: 0.3293 - classification_loss: 0.6044
Epoch 00017: saving model to ./snapshots\resnet50_csv_17.h5
Epoch 00017: ReduceLROnPlateau reducing learning rate to 9.9999988758398e-14.
Epoch 18/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0704 - regression_loss: 0.3853 - classification_loss: 0.6850
Epoch 00018: saving model to ./snapshots\resnet50_csv_18.h5
Epoch 19/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9337 - regression_loss: 0.3293 - classification_loss: 0.6044
Epoch 00019: saving model to ./snapshots\resnet50_csv_19.h5
Epoch 00019: ReduceLROnPlateau reducing learning rate to 9.999999146890344e-15.
Epoch 20/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0695 - regression_loss: 0.3845 - classification_loss: 0.6850
Epoch 00020: saving model to ./snapshots\resnet50_csv_20.h5
Epoch 21/30
100/100 [==============================] - 16s 160ms/step - loss: 0.9350 - regression_loss: 0.3306 - classification_loss: 0.6044
Epoch 00021: saving model to ./snapshots\resnet50_csv_21.h5
Epoch 00021: ReduceLROnPlateau reducing learning rate to 9.999998977483753e-16.
Epoch 22/30
100/100 [==============================] - 16s 160ms/step - loss: 1.0708 - regression_loss: 0.3858 - classification_loss: 0.6850
Epoch 00022: saving model to ./snapshots\resnet50_csv_22.h5
Epoch 23/30
100/100 [==============================] - 16s 165ms/step - loss: 0.9333 - regression_loss: 0.3288 - classification_loss: 0.6044
Epoch 00023: saving model to ./snapshots\resnet50_csv_23.h5
Epoch 00023: ReduceLROnPlateau reducing learning rate to 9.999998977483754e-17.
Epoch 24/30
100/100 [==============================] - 17s 167ms/step - loss: 1.0715 - regression_loss: 0.3865 - classification_loss: 0.6850
Epoch 00024: saving model to ./snapshots\resnet50_csv_24.h5
Epoch 25/30
100/100 [==============================] - 17s 165ms/step - loss: 0.9352 - regression_loss: 0.3308 - classification_loss: 0.6044
Epoch 00025: saving model to ./snapshots\resnet50_csv_25.h5
Epoch 00025: ReduceLROnPlateau reducing learning rate to 9.999998845134856e-18.
Epoch 26/30
100/100 [==============================] - 17s 167ms/step - loss: 1.0729 - regression_loss: 0.3878 - classification_loss: 0.6850
Epoch 00026: saving model to ./snapshots\resnet50_csv_26.h5
Epoch 27/30
100/100 [==============================] - 17s 167ms/step - loss: 0.9355 - regression_loss: 0.3311 - classification_loss: 0.6044
Epoch 00027: saving model to ./snapshots\resnet50_csv_27.h5
Epoch 00027: ReduceLROnPlateau reducing learning rate to 9.999999010570977e-19.
Epoch 28/30
100/100 [==============================] - 17s 167ms/step - loss: 1.0724 - regression_loss: 0.3874 - classification_loss: 0.6850
Epoch 00028: saving model to ./snapshots\resnet50_csv_28.h5
Epoch 29/30
100/100 [==============================] - 17s 166ms/step - loss: 0.9339 - regression_loss: 0.3294 - classification_loss: 0.6044
Epoch 00029: saving model to ./snapshots\resnet50_csv_29.h5
Epoch 00029: ReduceLROnPlateau reducing learning rate to 9.999999424161285e-20.
Epoch 30/30
100/100 [==============================] - 17s 166ms/step - loss: 1.0727 - regression_loss: 0.3877 - classification_loss: 0.6850
Epoch 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文件,可以將修改以下幾處
改成轉換後的模型名稱
改成你自己模型的類別對應關係
待檢測的圖片地址
修改閾值
特別感謝:
https://github.com/fizyr/keras-retinanet