Yolo_tensorflow-master Code Download:
Https://github.com/gliese581gg/YOLO_tensorflow
1. Introduction to the Model
Yolo_tensorflow
(Version 0.2, last updated:2016.02.16) 1.Introduction
This is TensorFlow implementation of the Yolo:real-time Object Detection
It can only do predictions using pretrained Yolo_small & Yolo_tiny Network for now.
I ' m gonna support training later.
I extracted weight values from darknet ' s (. weight) files.
Original Code (C Implementation) & paper:http://pjreddie.com/darknet/yolo/ 2.Install
(1) Download code
(2) Download YOLO weight file from
Yolo_small:https://drive.google.com/file/d/0b2jbajsrwlpza08ys2fsunv2dle/view?usp=sharing
Yolo_tiny:https://drive.google.com/file/d/0b2jbajsrwlpza0ftqlc3ejhmtta/view?usp=sharing
(3) Put the ' Yolo_ (version). Ckpt ' in the ' Weight ' folder of downloaded code 3.Usage
(1) Direct usage with default settings (display on console, show output image, no output file writing)
Python yolo_ (small or tiny) _tf.py-fromfile (input image filename)
(2) Direct usage with custom settings
Python yolo_ (small or tiny) _tf.py Argvs
where Argvs is
-fromfile (input image filename): Input image file-
D Isp_console (0 or 1): Whether display results on terminal or not
-imshow (0 or 1): Whether display result image or N OT
-tofile_img (output image filename): Output image file
-tofile_txt (output txt filename): Output text file (c Ontains class, X, Y, W, h, probability)
(3) Import on other scripts
Import yolo_ (small or tiny) _tf
YOLO = yolo_ (small or tiny) _tf. YOLO_TF ()
Yolo.disp_console = (true or false, default = True)
Yolo.imshow = (true or false, default = True)
yo lo.tofile_img = (output image filename)
yolo.tofile_txt = (output txt filename)
yolo.filewrite_img = (True or Fals E, default = False)
Yolo.filewrite_txt = (True of false, default = False)
yolo.detect_from_file (filename)
Yolo.detect_from_cvmat (Cvmat)
4.RequirementsTensorFlow Opencv2
5.Copyright
According to the LICENSE file of the original code, Me and original author hold no liability for any damages do not use th is on commercial! 6.Changelog
2016/02/15:first upload!
2016/02/16:added yolo_tiny, fixed bug that ignores one of the boxes in grid when both boxes detected valid objects
2016/08/26:uploaded Weight File converter! (darknet weight, tensorflow ckpt)
2. Model use
When I use YOLO
Appear
Cv2.imread ('./test/person.jpg ')
The picture read to is none.
The workaround is to precede the code with
Import Cv2
If the import Cv2 is added to the import YOLO_TINY_TF, none is reported. Get Picture
Here is all the code I've researched YOLO.
#encoding: utf-8
import cv2
import yolo_tiny_tf
YOLO = yolo_tiny_tf. YOLO_TF ()
Yolo.disp_console = True
Yolo.imshow = true
yolo.tofile_img = './test/ttt.jpg '
yolo.tofile _txt = './test/ttt.txt '
yolo.filewrite_img = True
Yolo.filewrite_txt = true
filename = './test/person.jpg '
# Read in image
#im = Cv2.imread ('./test/person.jpg ')
yolo.detect_from_file (filename)
#yolo. detect_ From_cvmat (IM)
Run results
3. Model Preservation and Operation
(1). Add the input in the yolo_ting_tf.py to the name, and the code is as follows:
def build_networks (self):
if Self.disp_console:print "Building yolo_tiny graph ..."
self.x = Tf.placeholder ( ' Float32 ', [none,448,448,3],name= ' input ')
(2). Run the Save_graph model with the weights saved together
Import OS import cv2 import tensorflow as TF import NumPy as NP from tensorflow.python.framework import test_util Import Freeze_graph from YOLO_TINY_TF import yolo_tf def save_graph (sess,output_path,checkpoint,checkpoint_state_name,input _graph_name,output_graph_name): Checkpoint_prefix = Os.path.join (output_path,checkpoint) saver = tf.train.Saver (t F.all_variables ()) Saver.save (Sess, Checkpoint_prefix, Global_step=0,latest_filename=checkpoint_state_name) Tf.tra In.write_graph (Sess.graph.as_graph_def (), Output_path, Input_graph_name) # We save out the
Graph to disk, and then call the const conversion # routine.
Input_graph_path = Os.path.join (Output_path, input_graph_name) Input_saver_def_path = "" Input_binary = False
Input_checkpoint_path = checkpoint_prefix + "-0" output_node_names = "19_fc" restore_op_name = "Save/restore_all" Filename_tensor_name = "save/const:0" Output_graph_path = Os.path.join (Output_path, output_graph_name) clear_devices = False freeze_graph.freeze_graph (Input_graph_path, Input_saver_def _path, Input_binary, Input_checkpoint_path, Output_node_names, Restore_op_name, Filename_tensor_name, Output_graph_path,clear_devices, "") YOLO=YOLO_TF ( ) #with open ("WEIGHTS/SMALL_MODEL.PB", "WB") as F: # F.write (Yolo.sess.graph.as_graph_def (). Serializetostring ()) Save_graph (yolo.sess, "/home/acer/pbmake/yolo", "Saved_checkpoint", "Checkpoint_state", "
YOLOTING_INPUT_GRAPH.PB "," YOLOTING_OUTPUT_GRAPH.PB ")
(3). Read the YOLOTING_OUTPUT_GRAPH.PB you just kept, test and detect
From __future__ import Absolute_import to __future__ Import division from __future__ import print_function import Cv2 Import TensorFlow as TF import NumPy as NP def IOU (Box1, box2): TB = min (box1[0] + 0.5 * box1[2], box2[0] + 0.5 * b OX2[2]-MAX (box1[0]-0.5 * box1[2], box2[0]-0.5 * box2[2]) lr = min (box1[1] + 0.5 * box1[3], box2[1] + 0.5 * box2 [3])-MAX (Box1[1]-0.5 * box1[3], box2[1]-0.5 * box2[3]) If TB < 0 or LR < 0:intersection = 0 E Lse:intersection = TB * LR return intersection/(box1[2] * box1[3] + box2[2] * box2[3]-intersection) def
Interpret_output (output): Alpha = 0.1 threshold = 0.2 Iou_threshold = 0.5 Num_class = Num_box = 2 Grid_size = 7 classes = ["Aeroplane", "Bicycle", "Bird", "Boat", "Bottle", "Bus", "car", "cat", "chair", "cow", "D
Iningtable "," dog "," horse "," motorbike "," person "," pottedplant "," sheep "," sofa "," Train "," Tvmonitor "] W_IMG = 640 H_imG = 480 probs = Np.zeros ((7, 7, 2,)) Class_probs = Np.reshape (output[0:980], (7, 7,)) scales = Np.resha PE (output[980:1078], (7, 7, 2)) boxes = Np.reshape (output[1078:], (7, 7, 2, 4)) offset = Np.transpose (Np.reshape (n P.array ([Np.arange (7)] *), (2, 7, 7)), (1, 2, 0)) boxes[:,:,:, 0] + = offset boxes[:,:,:, 1] + = Np.transpos E (offset, (1, 0, 2)) boxes[:,:,:, 0:2] = boxes[:,:,:, 0:2]/7.0 boxes[:,:,:, 2] = np.multiply (boxes[:,:,: , 2], boxes[:,:,:, 2]) boxes[:,:,:, 3] = np.multiply (boxes[:,:,:, 3], boxes[:,:,:, 3]) boxes[:,:,:, 0] *= w_img boxes[:,:,:, 1] *= h_img boxes[:,:,:, 2] *= w_img boxes[:,:,:, 3] *= h_img for I in range ( 2): For J in range: probs[:,:, I, j] = Np.multiply (class_probs[:,:, J], scales[:,:, I]) fi Lter_mat_probs = Np.array (probs >= threshold, dtype= ' bool ') filter_mat_boxes = Np.nonzero (filter_mat_probs) box es_filtered = Boxes[filtEr_mat_boxes[0], filter_mat_boxes[1], filter_mat_boxes[2]] probs_filtered = probs[filter_mat_probs] Classes_num_fi
ltered = Np.argmax (Filter_mat_probs, axis=3) [Filter_mat_boxes[0], filter_mat_boxes[1], filter_mat_boxes[2]] Argsort = Np.array (Np.argsort (probs_filtered)) [:: -1] boxes_filtered = boxes_filtered[argsort] probs_filtered = Pro
Bs_filtered[argsort] classes_num_filtered = Classes_num_filtered[argsort] for i in range (len (boxes_filtered)): If probs_filtered[i] = = 0:continue for j in range (i + 1, len (boxes_filtered)): If IOU (boxes_fil Tered[i], boxes_filtered[j]) > Iou_threshold:probs_filtered[j] = 0.0 Filter_iou = Np.array (probs_ Filtered > 0.0, dtype= ' bool ') boxes_filtered = Boxes_filtered[filter_iou] probs_filtered = Probs_filtered[filte R_iou] classes_num_filtered = Classes_num_filtered[filter_iou] result = [] for i in range (len (boxes_filtered) ): Result.append([Classes[classes_num_filtered[i]], boxes_filtered[i][0], boxes_filtered[i][1], boxes_filtered[i][2], BOXES_FILTERED[I][3], Probs_filtered[i]]) return result def show_results (IMG, results): filewrite_img = F
Alse Filewrite_txt = True IMG_CP = img.copy () If filewrite_txt:ftxt = Open ('./test/xsss.txt ', ' W ') For I in range (len (results)): x = Int (results[i][1]) y = Int (results[i][2]) w = Int (results[i][ 3])//2 h = Int (results[i][4])//2 Cv2.rectangle (IMG_CP, (X-w, Y-h), (x + W, y + h), (0, 255, 0), 2 ) Cv2.rectangle (IMG_CP, (X-w, Y-h-), (x + W, y-h), (1) cv2.puttext (IMG_CP, ResU Lts[i][0] + ':%.2f '% results[i][5], (x-w + 5, y-h-7), Cv2. Font_hershey_simplex, 0.5, (0, 0, 0), 1) ftxt.write (results[i][0] + ', ' + str (x) + ', ' + str (y) + ', ' + str (w) +
', ' + str (h) + ', ' + str (results[i][5]) + ' \ n ') Cv2.imwrite ('./test/xlsld.jpg ', IMG_CP) # produces the expected result. With TF. Graph (). As_default (): Output_graph_def = tf. Graphdef () Output_graph_path = '/HOME/ACER/PBMAKE/YOLO/YOLOTING_OUTPUT_GRAPH.PB ' x = Tf.placeholder (' float32 ', [N One, 448, 448, 3]) with open (Output_graph_path, "RB") as F:output_graph_def. Parsefromstring (F.read ()) _ = Tf.import_graph_def (Output_graph_def, Name= "") with TF.
Session () as Sess:tf.initialize_all_variables (). Run () input_x = Sess.graph.get_tensor_by_name ("input:0") Print (input_x) output = Sess.graph.get_tensor_by_name ("19_fc:0") print (output) filename = './test/person.jpg ' img = cv2.imread (filename) h_img, w_img, _ = Img.shape img_resized = cv2.res Ize (IMG, (448, 448)) Img_rgb = Cv2.cvtcolor (img_resized, Cv2. COLOR_BGR2RGB) img_resized_np = Np.asarray (IMG_RGB) inputs = Np.zeros ((1, 448, 448, 3), dtype= ' float32 ')
Inputs[0] = (img_resized_np/255.0) * 2.0-1.0 #input_node = sess.graph.get_operation_by_name ("input") ) In_dict = {Input_x:inputs} net_output = Sess.run (output, {input_x:inputs}) print ("Net_output", Net_output) #net_output = Sess.run (Output_node, feed_dict=in_dict) result = Interpret_output (net_output [0]) Show_results (IMG, result)
The result is the same as the result of running the model directly.