日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > 目标检测 >内容正文

目标检测

训练目标检测模型

發(fā)布時間:2024/3/26 目标检测 101 豆豆
生活随笔 收集整理的這篇文章主要介紹了 训练目标检测模型 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

目錄

?

前言:

一、VOC數(shù)據(jù)集的制作

1、數(shù)據(jù)的標注工具:labelIImg ----我也是在github上下載的,這里我提供我的鏈接

2、數(shù)據(jù)集的文件夾:由于我的只涉及目標檢測,故只需以下幾個文件目錄:

二、實現(xiàn)

1、Pascal_label_map.pbtxt文件格式:

2、將數(shù)據(jù)集轉(zhuǎn)換為tfrecord格式,書中提供了create_pascal_tf_record.py,在這里,需要對書中的代碼修改部分。如下:

3、voc.config文件:這個按照書中的方式添加參數(shù)即可,下面是我的:

4、最后配置完后的文件目錄:

5、開始訓(xùn)練:train.py

6、導(dǎo)出模型:export_inference_graph.py


前言:

? ? ?由于畢業(yè)設(shè)計涉及到圖像識別的目標檢測,故需要訓(xùn)練一個識別家具的目標檢測模型。而訓(xùn)練模型的代碼是參考《21個項目玩轉(zhuǎn)深度學(xué)習(xí)》中的第五章的目標檢測代碼。但是書中代碼的環(huán)境是python2.x版本的,而我的是python3.6。故需要在他的代碼基礎(chǔ)上做些修改,才可以正確訓(xùn)練,書中已經(jīng)大致介紹了模型訓(xùn)練的整個過程,而我會在他的基礎(chǔ)簡單補充一下我的步驟以及部分代碼的修改。

一、VOC數(shù)據(jù)集的制作

? ??書中的代碼是在VOC數(shù)據(jù)集的基礎(chǔ)上訓(xùn)練模型的,但是由于我的畢業(yè)設(shè)計要識別是室內(nèi)家具等,故我要制作自己的VOC格式的數(shù)據(jù)集。而制作voc數(shù)據(jù)集的方法步驟網(wǎng)上很多,我也是按照他們的步驟制作的。

附上書的鏈接以及書中的代碼:

? ? 鏈接:https://pan.baidu.com/s/1RGlJkxfqE9ba71kUGmwfWA?
? ? 提取碼:3mfx

可以參考一下:https://blog.csdn.net/weixin_38385446/article/details/81081172?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3

1、數(shù)據(jù)的標注工具:labelIImg ----我也是在github上下載的,這里我提供我的鏈接

? ? 鏈接:https://pan.baidu.com/s/17oAJU2_tfp5X5yjrFI4v1g

? ? 提取碼:kqdw

2、數(shù)據(jù)集的文件夾:由于我的只涉及目標檢測,故只需以下幾個文件目錄:

? ??

  • ? ? 首先將訓(xùn)練圖片放到JPEGImages,然后將圖片重命名為VOC2007的“000001.jpg”形式
  • ? ? 圖片批量重名代碼參考:http://blog.csdn.net/u011574296/article/details/72956446
  • ? ? Annotations文件夾中存放的是每張圖片對應(yīng)的標注文件,注意:其中標注的標簽名最好是小寫英文字母,大寫的可能會報錯。
  • ? ? 最后是ImageSets文件夾,該文件夾下存放的是4個txt,分別是:

? ? test.txt是測試集

? ?train.txt是訓(xùn)練集

? ?val.txt是驗證集

? ?trainval.txt是訓(xùn)練和驗證集

? 如圖:

?

?

而這四個txt可以用代碼生成:

import os import randomtrainval_percent = 0.8 train_percent = 0.8 xmlfilepath = 'D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/Annotations' txtsavepath = 'D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/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('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/trainval.txt', 'w') ftest = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/test.txt', 'w') ftrain = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/ImageSets/Main/train.txt', 'w') fval = open('D:/PycharmProjects/chapter_5/research/object_detection/voc/VOCdevkit/VOC2012/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()

至此數(shù)據(jù)集已經(jīng)制作完成

二、實現(xiàn)

1、Pascal_label_map.pbtxt文件格式:

2、將數(shù)據(jù)集轉(zhuǎn)換為tfrecord格式,書中提供了create_pascal_tf_record.py,在這里,需要對書中的代碼修改部分。如下:

第84、85行需要修改,這是書的代碼:

?修改為:

?

第162、163行代碼也需要修改,書中代碼:

修改為:

?

然后在create_pascal_tf_record.py中還需添加以下文件路徑:

在生成pascal_train.record時:

在生成pascal_val.record時:

隨后便會生成相應(yīng)的兩個tfrecord文件:

?

3、voc.config文件:這個按照書中的方式添加參數(shù)即可,下面是我的:

?

?

?

?

?

4、最后配置完后的文件目錄:

?

5、開始訓(xùn)練:train.py

添加兩行參數(shù):

?

注:當(dāng)你在訓(xùn)練時,出現(xiàn)內(nèi)存不足的錯誤時,可以調(diào)整voc.config中的:

分別減小一半。

6、導(dǎo)出模型:export_inference_graph.py

添加一些參數(shù):

注意:第83行中模型的步數(shù)是你實際訓(xùn)練的步數(shù)。


參考代碼:

import numpy as np import os import six.moves.urllib as urllib import sys import tarfile import tensorflow as tf import pandas as pd import zipfile import cv2 from collections import defaultdict from io import StringIO from matplotlib import pyplot as plt from PIL import Image# # This is needed to display the images. # %matplotlib inline# This is needed since the notebook is stored in the object_detection folder. sys.path.append("..")# from utils import label_map_util # from utils import visualization_utils as vis_util from research.object_detection.utils import label_map_util from research.object_detection.utils import visualization_utils as vis_utilMODEL_NAME = 'frozen_inference_graph.pb'# # Path to frozen detection graph. This is the actual model that is used for the object detection. PATH_TO_CKPT = r'voc\export\frozen_inference_graph.pb' # # # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = r'voc\pascal_label_map.pbtxt' NUM_CLASSES = 12# download model # 這部分也是下載模型的,注釋掉即可 #opener = urllib.request.URLopener() # 下載模型,如果已經(jīng)下載好了下面這句代碼可以注釋掉 # opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE) # tar_file = tarfile.open(MODEL_FILE) # for file in tar_file.getmembers(): # file_name = os.path.basename(file.name) # if 'frozen_inference_graph.pb' in file_name: # tar_file.extract(file, os.getcwd())# Load a (frozen) Tensorflow model into memory. detection_graph = tf.Graph() with detection_graph.as_default():od_graph_def = tf.GraphDef()with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:serialized_graph = fid.read()od_graph_def.ParseFromString(serialized_graph)tf.import_graph_def(od_graph_def, name='') # Loading label map label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,use_display_name=True) category_index = label_map_util.create_category_index(categories)# Helper code def load_image_into_numpy_array(image):(im_width, im_height) = image.sizereturn np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)# For the sake of simplicity we will use only 2 images: # 這里說明測試圖片的命名規(guī)則為imagen.jpg, 遵守規(guī)則即可 # image1.jpg # image.jpg# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS. PATH_TO_TEST_IMAGES_DIR = r'test_images' # 存放測試圖片的路徑 imagename = 'test.jpg' TEST_IMAGE_PATHS = [os.path.join(PATH_TO_TEST_IMAGES_DIR, imagename)] # 修改測試圖片的張數(shù)range(1, n + 1), 為測試圖片的張數(shù)# Size, in inches, of the output images. IMAGE_SIZE = (12, 8)with detection_graph.as_default():with tf.Session(graph=detection_graph) as sess:image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')# Each box represents a part of the image where a particular object was detected.boxes = detection_graph.get_tensor_by_name('detection_boxes:0')# Each score represent how level of confidence for each of the objects.# Score is shown on the result image, together with the class label.scores = detection_graph.get_tensor_by_name('detection_scores:0')classes = detection_graph.get_tensor_by_name('detection_classes:0')num_detections = detection_graph.get_tensor_by_name('num_detections:0')for image_path in TEST_IMAGE_PATHS:image = Image.open(image_path)width,height = image.size# the array based representation of the image will be used later in order to prepare the# result image with boxes and labels on it.image_np = load_image_into_numpy_array(image)# Expand dimensions since the model expects images to have shape: [1, None, None, 3]image_np_expanded = np.expand_dims(image_np, axis=0)# 開始檢測(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections],feed_dict={image_tensor: image_np_expanded})# 可視化vis_util.visualize_boxes_and_labels_on_image_array(image_np,np.squeeze(boxes),np.squeeze(classes).astype(np.int32),np.squeeze(scores),category_index,use_normalized_coordinates=True,line_thickness=8)plt.figure(figsize=IMAGE_SIZE)plt.imshow(image_np)plt.show()print(num_detections)plt.imsave('save_image/' + imagename,image_np)

我的檢測效果:

?

總結(jié)

以上是生活随笔為你收集整理的训练目标检测模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。