交通路标识别(毕业设计)
概述:
代碼獲取:點(diǎn)我獲取
在TensorFlow中實(shí)現(xiàn)單鏡頭多盒檢測(cè)器(SSD),用于檢測(cè)和分類交通標(biāo)志。該實(shí)現(xiàn)能夠在具有Intel Core i7-6700K的GTX 1080上實(shí)現(xiàn)40-45 fps。
請(qǐng)注意,此項(xiàng)目仍在進(jìn)行中。現(xiàn)在的主要問(wèn)題是模型過(guò)度擬合。
我目前正在先進(jìn)行VOC2012的預(yù)培訓(xùn),然后進(jìn)行交通標(biāo)志檢測(cè)的轉(zhuǎn)移學(xué)習(xí)。目前只檢測(cè)到停車標(biāo)志和人行橫道標(biāo)志。檢測(cè)圖像示例如下。
依賴庫(kù)與代碼
Skip to content Product Solutions Open Source Pricing Search Sign in Sign up georgesung / ssd_tensorflow_traffic_sign_detection Public Code Issues 32 Pull requests Actions Projects Security Insights ssd_tensorflow_traffic_sign_detection/inference.py / @georgesung georgesung Removed unused function run_inference_old() Latest commit 88f1781 on Feb 15, 2017History1 contributor 189 lines (155 sloc) 6.08 KB''' Run inference using trained model ''' import tensorflow as tf from settings import * from model import SSDModel from model import ModelHelper from model import nms import numpy as np from sklearn.model_selection import train_test_split import cv2 import math import os import time import pickle from PIL import Image import matplotlib.pyplot as plt from moviepy.editor import VideoFileClip from optparse import OptionParser import globdef run_inference(image, model, sess, mode, sign_map):"""Run inference on a given imageArguments:* image: Numpy array representing a single RGB image* model: Dict of tensor references returned by SSDModel()* sess: TensorFlow session reference* mode: String of either "image", "video", or "demo"Returns:* Numpy array representing annotated image"""# Save original image in memoryimage = np.array(image)image_orig = np.copy(image)# Get relevant tensorsx = model['x']is_training = model['is_training']preds_conf = model['preds_conf']preds_loc = model['preds_loc']probs = model['probs']# Convert image to PIL Image, resize it, convert to grayscale (if necessary), convert back to numpy arrayimage = Image.fromarray(image)orig_w, orig_h = image.sizeif NUM_CHANNELS == 1:image = image.convert('L') # 8-bit grayscaleimage = image.resize((IMG_W, IMG_H), Image.LANCZOS) # high-quality downsampling filterimage = np.asarray(image)images = np.array([image]) # create a "batch" of 1 imageif NUM_CHANNELS == 1:images = np.expand_dims(images, axis=-1) # need extra dimension of size 1 for grayscale# Perform object detectiont0 = time.time() # keep track of duration of object detection + NMSpreds_conf_val, preds_loc_val, probs_val = sess.run([preds_conf, preds_loc, probs], feed_dict={x: images, is_training: False})if mode != 'video':print('Inference took %.1f ms (%.2f fps)' % ((time.time() - t0)*1000, 1/(time.time() - t0)))# Gather class predictions and confidence valuesy_pred_conf = preds_conf_val[0] # batch size of 1, so just take [0]y_pred_conf = y_pred_conf.astype('float32')prob = probs_val[0]# Gather localization predictionsy_pred_loc = preds_loc_val[0]# Perform NMSboxes = nms(y_pred_conf, y_pred_loc, prob)if mode != 'video':print('Inference + NMS took %.1f ms (%.2f fps)' % ((time.time() - t0)*1000, 1/(time.time() - t0)))# Rescale boxes' coordinates back to original image's dimensions# Recall boxes = [[x1, y1, x2, y2, cls, cls_prob], [...], ...]scale = np.array([orig_w/IMG_W, orig_h/IMG_H, orig_w/IMG_W, orig_h/IMG_H])if len(boxes) > 0:boxes[:, :4] = boxes[:, :4] * scale# Draw and annotate boxes over original image, and return annotated imageimage = image_origfor box in boxes:# Get box parametersbox_coords = [int(round(x)) for x in box[:4]]cls = int(box[4])cls_prob = box[5]# Annotate imageimage = cv2.rectangle(image, tuple(box_coords[:2]), tuple(box_coords[2:]), (0,255,0))label_str = '%s %.2f' % (sign_map[cls], cls_prob)image = cv2.putText(image, label_str, (box_coords[0], box_coords[1]), 0, 0.5, (0,255,0), 1, cv2.LINE_AA)return imagedef generate_output(input_files, mode):"""Generate annotated images, videos, or sample images, based on mode"""# First, load mapping from integer class ID to sign name stringsign_map = {}with open('signnames.csv', 'r') as f:for line in f:line = line[:-1] # strip newline at the endsign_id, sign_name = line.split(',')sign_map[int(sign_id)] = sign_namesign_map[0] = 'background' # class ID 0 reserved for background class# Create output directory 'inference_out/' if neededif mode == 'image' or mode == 'video':if not os.path.isdir('./inference_out'):try:os.mkdir('./inference_out')except FileExistsError:print('Error: Cannot mkdir ./inference_out')return# Launch the graphwith tf.Graph().as_default(), tf.Session() as sess:# "Instantiate" neural network, get relevant tensorsmodel = SSDModel()# Load trained modelsaver = tf.train.Saver()print('Restoring previously trained model at %s' % MODEL_SAVE_PATH)saver.restore(sess, MODEL_SAVE_PATH)if mode == 'image':for image_file in input_files:print('Running inference on %s' % image_file)image_orig = np.asarray(Image.open(image_file))image = run_inference(image_orig, model, sess, mode, sign_map)head, tail = os.path.split(image_file)plt.imsave('./inference_out/%s' % tail, image)print('Output saved in inference_out/')elif mode == 'video':for video_file in input_files:print('Running inference on %s' % video_file)video = VideoFileClip(video_file)video = video.fl_image(lambda x: run_inference(x, model, sess, mode, sign_map))head, tail = os.path.split(video_file)video.write_videofile('./inference_out/%s' % tail, audio=False)print('Output saved in inference_out/')elif mode == 'demo':print('Demo mode: Running inference on images in sample_images/')image_files = os.listdir('sample_images/')for image_file in image_files:print('Running inference on sample_images/%s' % image_file)image_orig = np.asarray(Image.open('sample_images/' + image_file))image = run_inference(image_orig, model, sess, mode, sign_map)plt.imshow(image)plt.show()else:raise ValueError('Invalid mode: %s' % mode)if __name__ == '__main__':# Configure command line optionsparser = OptionParser()parser.add_option('-i', '--input_dir', dest='input_dir',help='Directory of input videos/images (ignored for "demo" mode). Will run inference on all videos/images in that dir')parser.add_option('-m', '--mode', dest='mode', default='image',help='Operating mode, could be "image", "video", or "demo"; "demo" mode displays annotated images from sample_images/')# Get and parse command line optionsoptions, args = parser.parse_args()input_dir = options.input_dirmode = options.modeif mode != 'video' and mode != 'image' and mode != 'demo':assert ValueError('Invalid mode: %s' % mode)if mode != 'demo':input_files = glob.glob(input_dir + '/*.*')else:input_files = []generate_output(input_files, mode)Python 3.5+
TensorFlow v0.12.0
Pickle
OpenCV Python
Matplotlib(可選)
運(yùn)用
將此存儲(chǔ)庫(kù)克隆到某處,讓我們將其稱為$ROOT
從頭開始訓(xùn)練模型:
代碼流程
※Download the LISA Traffic Sign Dataset, and store it in a directory $LISA_DATA ※cd $LISA_DATA ※Follow instructions in the LISA Traffic Sign Dataset to create 'mergedAnnotations.csv' such that only stop signs and pedestrian ※crossing signs are shown ※cp $ROOT/data_gathering/create_pickle.py $LISA_DATA ※python create_pickle.py ※cd $ROOT ※ln -s $LISA_DATA/resized_images_* . ※ln -s $LISA_DATA/data_raw_*.p . ※python data_prep.py ※This performs box matching between ground-truth boxes and default ※boxes, and packages the data into a format used later in the ※pipeline ※python train.py ※This trains the SSD model ※python inference.py -m demo效果
如上所述,該SSD實(shí)現(xiàn)能夠在具有Intel Core i7 6700K的GTX 1080上實(shí)現(xiàn)40-45 fps。
推理時(shí)間是神經(jīng)網(wǎng)絡(luò)推理時(shí)間和非最大抑制(NMS)時(shí)間的總和。總的來(lái)說(shuō),神經(jīng)網(wǎng)絡(luò)推斷時(shí)間明顯小于NMS時(shí)間,神經(jīng)網(wǎng)絡(luò)推理時(shí)間通常在7-8ms之間,而NMS時(shí)間在15-16ms之間。這里實(shí)現(xiàn)的NMS算法尚未優(yōu)化,僅在CPU上運(yùn)行,因此可以在那里進(jìn)一步努力提高性能。
數(shù)據(jù)集
整個(gè)LISA交通標(biāo)志數(shù)據(jù)集由47個(gè)不同的交通標(biāo)志類別組成。因?yàn)槲覀冎魂P(guān)注這些類的子集,所以我們只使用LISA數(shù)據(jù)集的子集。此外,我們忽略了沒(méi)有找到匹配的默認(rèn)框的所有訓(xùn)練樣本,從而進(jìn)一步減小了數(shù)據(jù)集的大小。由于這個(gè)過(guò)程,我們最終只能處理很少的數(shù)據(jù)。
為了改進(jìn)這一問(wèn)題,我們可以執(zhí)行圖像數(shù)據(jù)增強(qiáng),和/或在更大的數(shù)據(jù)集上預(yù)訓(xùn)練模型(例如VOC2012、ILSVRC)
下載鏈接:`點(diǎn)擊下載
代碼可私信
代碼可私信
代碼可私信
總結(jié)
以上是生活随笔為你收集整理的交通路标识别(毕业设计)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 天堂2地点坐标(SQL语句,可直接导入数
- 下一篇: V270文件存储服务器,天堂2V270