1, first at UIUC Image Database for Car Detection download training data set.
Download: http: //cogcomp.org/Data/Car/
After the download, unzip the file directory as shown, what we need here is TrainImages this folder and TestImages folder.
2, the input image is a gray scale 100 * 40 * 1, using the CNN network, after several convolution results obtained and pooled 1 * 1 * 1, as shown in the following parameters of each layer.
My file directory organization: -CarDetect --CarDetect.py --datas --- CarData --models
input : [None, 40, 100, 1] conv-pool1 : f : 5*5 strides : 1 nc : 6 padding : VALID maxpool : 2 conv-pool2: f : 5*5 strides : 1 nc : 16 padding : VALID maxpool : 2 conv3: f : 5*5 strides : 1 nc : 32 padding : VALID conv4: f : 3*18 strides : 1 nc : 64 padding : VALID conv5: f : 1*1 strides : 1 nc : 1 padding : VALID output : [None, 1, 1, 1]
tensorflow TF AS Import Import numpy AS NP Import matplotlib.pyplot PLT AS function, note the position of the training data storage #define load data DEF load_carDats (): Import CV2 Import OS file_path = './datas/CarData/TrainImages/' Files = the os.listdir (file_path) the Samples = [] for file_name in Files: Data = cv2.imread (file_name file_path +, 0) .reshape (-1) / 255 label file_name.split IF = 0 ( '-') [0] == 'NEG'. 1 the else samples.append ((data, label)) return the Samples # loading data DATAS = load_carDats () # disrupted random data np.random.shuffle (DATAS) # divided data, xs, ys for training network, x_test, y_test used to test the effect of network training = XS [I [0] for DATAS in I [: 1000]] '' 'calculates the convolution, x is the input layer (shape = [- 1, width , height, channel]), = np.reshape YS ([I [. 1] for I in DATAS [: 1000]], = newshape (- 1,1)) x_test = [I [0] for DATAS I in [1000:]] android.permission.FACTOR. = NP. -: reshape (1,1), newshape = ([] i [1] for i in datas [1000]) function defined network # ---------------- frequently used which was reconstituted # ------ # weight variable DEF weight_variables (Shape): weights = tf.truncated_normal (Shape, STDDEV = 0.1, DTYPE = tf.float32) tf.Variable return (weights) # offset variable DEF biase_variables (Shape): biases tf.constant = (value = 1.0, Shape = Shape) return tf.Variable (biases) # convolution DEF conv2d (X, W is): , out_layers_num], the horizontal and the vertical direction step are 1 '' ' W is shared f * f = weighting matrix Shape [F, F, in_layers_num, out_layers_num], return tf.nn.conv2d (X, W, Strides = [1,1,1,1], = padding "the VALID") # pooled maximum DEF max_pooling (X): '' 'hybrid computing the maximum, x is the input layer (typically convolution result) Shape = [-. 1, width, height, channels] ksize is mixed pooling of core size 2 * 2, and the horizontal step in the vertical direction are both 2 '' ' return tf.nn.max_pool (X, ksize = [1,2,2,1], Strides = [1,2,2,1], = padding "the VALID") # --------------------- network before propagating portion ----------------- - # DEF deepnn (X, keep_prop): '' 'deep convolutional network is defined, comprising the convolution of two - three convolution mixed layer and layers' '' # Step1: to convert the raw data into a 2-dimensional and have dimension, represents a first number of samples, the ranks second three, the last one is the channel number # tf.reshape X = (X, Shape = [-. 1, 40, 100,. 1]) # Step2: defining a first of convolution - mixed layer with tf.name_scope ( "conv-pooling1" ): W_conv1 = weight_variables([5,5,1,6]) = biase_variables b_conv1 ([. 6]) ret_conv1 = tf.nn.relu (conv2d (X, W_conv1) + b_conv1) # convolution calculation, and correction means for using the result of the convolution is further processed ret_pooling1 = max_pooling (ret_conv1) # performing mixing operation # step3: defining a second convolution - mixed layer with tf.name_scope ( "CONV-pooling2"): W_conv2 weight_variables = ([5,5,6,16]) b_conv2 biase_variables = ([16]) ret_conv2 = TF. nn.relu (conv2d (ret_pooling1, W_conv2) + b_conv2) ret_pooling2 = max_pooling (ret_conv2) # Step4: convolution defining a third layer with tf.name_scope ( "CONV-pooling3"): W_conv3 weight_variables = ([5, 5, 16,32]) b_conv3 biase_variables = ([32]) ret_conv3 = tf.nn.relu (conv2d (ret_pooling2, W_conv3) + b_conv3) # step5: fourth convolution defined layers with tf.name_scope ( "CONV4"): W_conv4 weight_variables = ([3,18,32,64]) b_conv4 biase_variables = ([64]) ret_conv4 tf.nn.relu = ( conv2d (ret_conv3, W_conv4) + b_conv4) # Step6: a fifth convolution defined layers with tf.name_scope ( "conv5"): W_conv5 weight_variables = ([1,1,64,1]) b_conv5 biase_variables = ([. 1] ) ret_conv5 = conv2d (ret_conv4, W_conv5) + b_conv5 return ret_conv5 # --------------------- ---------- ready before training network ------------- # placeholder # stated input data and labels x = tf.placeholder (dtype = tf.float32, shape = [None, None, None, 1], name = "X-INPUT") Labels = tf.placeholder (DTYPE = tf.float32, Shape = [None,. 1], name = "Y-Output") # affirmed abstained placeholder = tf.placeholder keep_prop (DTYPE = tf.float32, name = "kprob") # create a classification model RET = deepnn (X, keep_prop) # At this time the return value is -1 * 1 * 1 * 1 for convenience to obtain the result of the operation, here the RESHAPE Y = tf.reshape (RET, Shape = [- 1,1]) # loss function defined with tf.name_scope ( "loss_function"): loss = tf.nn.sigmoid_cross_entropy_with_logits (logits = Y, Labels = Labels) cost = tf.reduce_mean (Loss) # define the training model (optimization model) with tf.name_scope ( "optimizor"): train = tf.train.AdamOptimizer (0.0005) .minimize (cost) # define verify the accuracy of the model method with tf.name_scope ( "Accuracy"): y_hat = tf.nn.sigmoid (Y) accuracy_rate = tf.abs (y_hat - Labels) <0.5 accuracy_rate = tf.cast (accuracy_rate, DTYPE = tf.float32) = tf.reduce_mean Accuracy (accuracy_rate) # -------------- start training network, and save the results to a file --------------- training # Saver = tf.train.Saver () Sess = tf.Session () sess.run (tf.global_variables_initializer ()) # initialize variables for I in Range (10): Skip = 10 for K in Range (0,1000, He Skips): x_train = np.reshape (XS [K: K + Skip], = newshape (-. 1, 40, 100,. 1)) sess.run (Train, feed_dict = {X: x_train, Labels: YS [K: k + skip], keep_prop: 0.5 }) # training model # IF (I +. 1)% 10 == 0: train_accuracy = sess.run (Accuracy, feed_dict = {X: np.reshape (XS, (-1,40,100 ,. 1)), Labels: YS, keep_prop:} 1.0) Print ( '% STEP D, G Train Accuracy%'% (I, train_accuracy)) saver.save (sess, "./models/carDetect_model.ckpt", global_step = i) This is my training results: the STEP 0, Train Accuracy 0.859 the STEP 1, Train Accuracy 0.934 the STEP 2, Train Accuracy 0.965 the STEP 3, Train Accuracy 0.971 STEP. 4, Train Accuracy 0.985 STEP. 5, Train Accuracy 0.991 STEP. 6, Train Accuracy 0.995 STEP. 7, Train Accuracy 0.994 STEP. 8, Train Accuracy 0.995 STEP. 9, Train Accuracy 0.997 # ----------------------- -------------- ------------------- # starts detecting new picture import CV2 # import pictures pic = cv2.imread ( "../../datas/CarData/TestImages/test-100.pgm", 0) size = pic.shape IMG = np.reshape (PIC, (-1, size [0], size [. 1], 1)) # Using the above trained network starts to detect new pictures = sess.run Result (RET, feed_dict = {X: IMG}) # The test results show pt1 = np.array ([result.argmax () // result.shape [2], result.argmax ()% result. Shape [2]]). 4 * PT2 + np.array PT1 = ([40, 100]) PIC_2 = cv2.rectangle (PIC, (PT1 [. 1], PT1 [0]), (PT2 [. 1], PT2 [ 0]), 0, 2) plt.imshow (PIC_2, "Gray") plt.show ()
3. Results
Source acquisition mode, the total number of public attention RaoRao1994, wonderful view to the stage - all articles, we can get the download link resources
Get more resources, please pay attention to the public the total number RaoRao1994