Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 29.74 KB | None | 0 0
  1. import tensorflow as tf
  2. import tensorflow.contrib.slim as slim
  3. import numpy as np
  4. import cv2
  5. import random
  6. import math
  7. from utils import *
  8. import os
  9. from sklearn.utils import shuffle
  10. from roi_pooling import ROIPoolingLayer
  11.  
  12. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
  13.  
  14. def _base_inference(inputs):
  15.     with slim.arg_scope([slim.conv2d], activation_fn = tf.nn.relu , trainable = False, \
  16.             weights_initializer = tf.truncated_normal_initializer(mean = 0.0, stddev = 0.01), \
  17.             weights_regularizer = slim.l2_regularizer(0.0005)):
  18.         net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], padding = 'SAME', scope='conv1', trainable = False)
  19.         net = slim.max_pool2d(net, [2, 2], padding='VALID', scope = 'pool1')
  20.         net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], padding = 'SAME', scope='conv2', trainable = False)
  21.         net = slim.max_pool2d(net, [2, 2], padding='VALID', scope = 'pool2')
  22.         net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], padding = 'SAME', scope='conv3')
  23.         net = slim.max_pool2d(net, [2, 2], padding='VALID', scope = 'pool3')
  24.         net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], padding = 'SAME', scope='conv4')
  25.         net = slim.max_pool2d(net, [2, 2], padding='VALID', scope = 'pool4')
  26.         net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], padding = 'SAME', scope='conv5')
  27.         #net = slim.max_pool2d(net, [2, 2], padding='VALID', scope = 'pool5')
  28.     return net
  29.  
  30. def _inference(x, k_anchors, name=None, dim_reduce = 16, aspect_ratios = [1, 2, 3], scales = [32, 64, 128], cls_thresh = 0.7):
  31.  
  32.     with slim.arg_scope([slim.conv2d], trainable = False, activation_fn = tf.nn.relu, \
  33.             weights_initializer = tf.truncated_normal_initializer(mean = 0.0, stddev = 0.01), \
  34.             weights_regularizer = slim.l2_regularizer(0.0005)):
  35.  
  36.         # Itermediate layer
  37.         itermediate_layer = slim.conv2d(x, 512, [3, 3], padding = 'SAME', scope = 'itermediate_layer')
  38.  
  39.         # Class regression
  40.         RPN_cls = slim.conv2d(itermediate_layer, 2*k_anchors, [1, 1], padding = 'VALID', scope = 'classification', activation_fn = None)
  41.  
  42.         # Bounding box regression
  43.         RPN_reg = slim.conv2d(itermediate_layer, 4*k_anchors, [1, 1], padding = 'VALID', scope = 'regression', activation_fn = None)
  44.  
  45.     # Get feature map shape
  46.     ftmap_shape = tf.shape(itermediate_layer)
  47.  
  48.     confidence_list = tf.reshape(RPN_cls, [-1, 2])
  49.     regression_list = tf.reshape(RPN_reg, [-1, 4])
  50.     print('Itermediate Layer shape: ')
  51.     print(itermediate_layer.shape)
  52.     print('Confidence Layer shape: ')
  53.     print(RPN_cls.shape)
  54.     print('Bounding boxes Regression Layer shape: ')
  55.     print(RPN_reg.shape)
  56.     print('Confidence list shape: ')
  57.     print(confidence_list.shape)
  58.     print('Bounding boxes Regression list shape: ')
  59.     print(regression_list.shape)
  60.  
  61.     probabilities = tf.nn.softmax(confidence_list, axis = 1)
  62.     probabilities = probabilities[:, 1]
  63.  
  64.     return confidence_list, regression_list, probabilities
  65.  
  66. def _predict_boxes(probabilities, regression_list, placeholder_anchors, placeholder_anchors_cleaned, cls_thresh = 0.7, top_k = 128, nms_threshold = 0.7):
  67.  
  68.     # Remove unclean boxes
  69.     cleaned_probabilites = tf.gather(probabilities, placeholder_anchors_cleaned)
  70.     cleaned_regression = tf.gather(regression_list, placeholder_anchors_cleaned)
  71.     cleaned_anchors = tf.gather(placeholder_anchors, placeholder_anchors_cleaned)
  72.    
  73.     # Remove boxes by threshold
  74.     indices = tf.where(cleaned_probabilites > cls_thresh)
  75.     cleaned_probabilites = tf.gather(cleaned_probabilites, indices)
  76.     cleaned_probabilites = tf.reshape(cleaned_probabilites, [-1])
  77.     cleaned_regression = tf.gather(cleaned_regression, indices)
  78.     cleaned_regression = tf.reshape(cleaned_regression, [-1, 4])
  79.     cleaned_anchors = tf.gather(cleaned_anchors, indices)
  80.     cleaned_anchors = tf.reshape(cleaned_anchors, [-1, 4])
  81.  
  82.     # Get predict box
  83.     predict_boxes_x = tf.add(tf.multiply(cleaned_anchors[:, 2], cleaned_regression[:, 0]), cleaned_anchors[:, 0])
  84.     predict_boxes_x = tf.reshape(predict_boxes_x, (-1, 1))
  85.     predict_boxes_y = tf.add(tf.multiply(cleaned_anchors[:, 3], cleaned_regression[:, 1]), cleaned_anchors[:, 1])
  86.     predict_boxes_y = tf.reshape(predict_boxes_y, (-1, 1))
  87.     predict_boxes_w = tf.multiply(cleaned_anchors[:, 2], tf.exp(cleaned_regression[:, 2]))
  88.     predict_boxes_w = tf.reshape(predict_boxes_w, (-1, 1))
  89.     predict_boxes_h = tf.multiply(cleaned_anchors[:, 3], tf.exp(cleaned_regression[:, 3]))
  90.     predict_boxes_h = tf.reshape(predict_boxes_h, (-1, 1))
  91.     predict_boxes = tf.concat([predict_boxes_x - predict_boxes_w/2, predict_boxes_y - predict_boxes_h/2, predict_boxes_x + predict_boxes_w/2, predict_boxes_y + predict_boxes_h/2], axis = -1)
  92.  
  93.     # Reduce boxes by NMS
  94.     # Using tf.image.non_max_suppression
  95.     cleaned_indices = tf.image.non_max_suppression(predict_boxes, cleaned_probabilites, max_output_size = top_k, iou_threshold = nms_threshold)
  96.     nms_cleaned_probabilites = tf.gather(cleaned_probabilites, cleaned_indices)
  97.     nms_cleaned_probabilites = tf.reshape(nms_cleaned_probabilites, [-1])
  98.     nms_predict_boxes = tf.gather(predict_boxes, cleaned_indices)
  99.     nms_predict_boxes = tf.reshape(nms_predict_boxes, [-1, 4])
  100.  
  101.     #return predict_boxes, cleaned_probabilites
  102.     return nms_predict_boxes, nms_cleaned_probabilites
  103.  
  104. def _fastRCNN(feature_map, boxes_proposal, pooled_width = 7, pooled_height = 7, total_class = 1):
  105.     roi_layer = ROIPoolingLayer(pooled_height, pooled_width)
  106.     pooled_features = roi_layer([feature_map, boxes_proposal])[0]
  107.     pooled_features = tf.layers.flatten(pooled_features)
  108.     with slim.arg_scope([slim.fully_connected], activation_fn = tf.nn.relu, \
  109.             weights_initializer = tf.truncated_normal_initializer(mean = 0.0, stddev = 0.01), \
  110.             weights_regularizer = slim.l2_regularizer(0.0005)):
  111.         fc1 = slim.fully_connected(pooled_features, 2048, scope='rcnn/fc_1')
  112.         fc2 = slim.fully_connected(fc1, 2048, scope='rcnn/fc_2')
  113.  
  114.         cls = slim.fully_connected(fc2, total_class+1, scope='rcnn/classification')
  115.         reg = slim.fully_connected(fc2, 4, scope='rcnn/regression')
  116.     return cls, reg
  117.  
  118.  
  119. def name_in_checkpoint(v):
  120.     return 'vgg_16/' + v.op.name
  121.  
  122. def fine_turning_vgg16():
  123.     variables_to_restore = slim.get_model_variables()
  124.     variables_to_restore = {name_in_checkpoint(v):v for v in variables_to_restore if v.name.startswith('conv')}
  125.     restorer = tf.train.Saver(variables_to_restore)
  126.     return restorer
  127.  
  128.  
  129. def rpn_generate_batch_label(gt_boxes, dim_reduce, imageSize, aspect_ratios, scales, batch_size, visualize = False, minibatch_size = 256, repeat_positives = True, \
  130.     positives_threshold = 0.7, negatives_threshold = 0.3):
  131.  
  132.     # Prepare
  133.     feature_map_width = int(imageSize[0]/dim_reduce)
  134.     feature_map_height = int(imageSize[1]/dim_reduce)
  135.     anchors = rpn_generate_anchor_boxes((feature_map_width, feature_map_height), imageSize, aspect_ratios, scales)
  136.     #print('Anchors shape {}'.format(anchors.shape))
  137.     anchors_cleaned = remove_cross_boundaries_anchors(anchors, imageSize[0], imageSize[1])
  138.  
  139.     # Anchors're in center, width, height format
  140.     # Convert anchors from center width, height -> topleft bottom right
  141.     A = format_anchors(anchors)
  142.     n_anchors = len(A)
  143.     batch_anchors = np.reshape(anchors, (-1, 4))
  144.     batch_anchors = np.tile(batch_anchors, (batch_size, 1))
  145.  
  146.     #print('Anchor shape: ' + str(batch_anchors.shape))
  147.     #print('GT: ' + str(gt_boxes))
  148.     #print('Batch anchors: {} {}'.format(batch_anchors[6163], batch_anchors[6463]) )
  149.     # Calculate iou between each groundtruth box & anchor box
  150.     gt_each_img = np.zeros((batch_size), dtype = np.int32)
  151.     for i in range(batch_size):
  152.         gt_each_img[i] = len(gt_boxes[i])
  153.     #print('Number of groundtruth: ')
  154.     #print(gt_each_img)
  155.     gt_boxes_flatten = [item for sublist in gt_boxes for item in sublist]
  156.     gt_boxes_flatten = np.reshape(gt_boxes_flatten, (-1, 4))
  157.     # Convert Groundtruth box from c, w, h -> tl br
  158.     gt_boxes_flatten_formated = format_anchors(gt_boxes_flatten)
  159.  
  160.     #print('Groundtruth box: ')
  161.     #print(gt_boxes_flatten)
  162.  
  163.     total_gt = len(gt_boxes_flatten_formated)
  164.     IoU = iou_tensorial(gt_boxes_flatten_formated, A)
  165.     idx_of_GT = np.argmax(IoU, axis = 0)
  166.     A_scores_list = np.max(IoU, axis = 0)
  167.  
  168.     # Get safety object
  169.     safety_object = np.argmax(IoU, axis = 1)
  170.     A_scores_list[A_scores_list>positives_threshold] = 1
  171.     A_scores_list[A_scores_list<negatives_threshold] = 0
  172.     A_scores_list[np.logical_and(A_scores_list>0, A_scores_list<1)] = 0.5
  173.     #print(A_scores_list)
  174.     A_scores_list_cleaned = A_scores_list[anchors_cleaned]
  175.  
  176.     object_indices = anchors_cleaned[A_scores_list_cleaned > 0.9]
  177.     # print('Object indices: {}'.format(object_indices))
  178.     non_object_indices = anchors_cleaned[A_scores_list_cleaned < 0.1]
  179.     # print('non_object indices: {}'.format(non_object_indices))
  180.     # include at least one object
  181.     if (len(object_indices) == 0):
  182.         object_indices = np.array(safety_object)
  183.         #print('Safety obj: {}'.format(safety_object))
  184.         A_scores_list[safety_object] = 1
  185.         idx = np.argwhere(non_object_indices == safety_object)
  186.         non_object_indices = np.delete(non_object_indices, idx)
  187.  
  188.     assert(len(object_indices) > 0)
  189.  
  190.     if repeat_positives:
  191.         chosen_idx_obj = np.random.choice(object_indices, int(minibatch_size/2),replace = True)
  192.         assert(len(chosen_idx_obj) + len(non_object_indices) >= minibatch_size)
  193.     else:
  194.         assert(len(object_indices) + len(non_object_indices) >= minibatch_size)
  195.         chosen_idx_obj = np.random.choice(object_indices, min(len(object_indices), int(minibatch_size/2)),replace = False)
  196.     chosen_idx_non_obj = np.random.choice(non_object_indices, minibatch_size - len(chosen_idx_obj), replace = False)
  197.     #print('Chosen Object indices: {}'.format(chosen_idx_obj))
  198.     #print('Chosen NonObject indices: {}'.format(chosen_idx_non_obj))
  199.     minibatch_indices = np.concatenate((chosen_idx_obj, chosen_idx_non_obj))
  200.  
  201.     # minibatch only
  202.     ground_truth_anchors_list = np.zeros((len(minibatch_indices), 4))
  203.     ground_truth_anchors_list[:,0] = (gt_boxes_flatten[idx_of_GT[minibatch_indices],0] - batch_anchors[minibatch_indices,0]) / batch_anchors[minibatch_indices,2]
  204.     ground_truth_anchors_list[:,1] = (gt_boxes_flatten[idx_of_GT[minibatch_indices],1] - batch_anchors[minibatch_indices,1]) / batch_anchors[minibatch_indices,3]
  205.     ground_truth_anchors_list[:,2] = np.log(gt_boxes_flatten[idx_of_GT[minibatch_indices],2]/batch_anchors[minibatch_indices,2])
  206.     ground_truth_anchors_list[:,3] = np.log(gt_boxes_flatten[idx_of_GT[minibatch_indices],3]/batch_anchors[minibatch_indices,3])
  207.     #print(ground_truth_anchors_list)
  208.     a = A_scores_list[minibatch_indices]
  209.     A_scores_one_hot = np.zeros((a.shape[0], 2))
  210.     A_scores_one_hot[np.arange(a.shape[0]), a.astype(int)] = 1
  211.     #print(A_scores_one_hot)
  212.  
  213.     return minibatch_indices, A_scores_one_hot, ground_truth_anchors_list
  214.  
  215. def fastRCNN_generate_batch_labels(boxes_proposal, groundtruth, labels, cls_thresh = 0.5, number_samples = 128, positive_percent = 0.25, n_classes = 1):
  216.     # boxes_proposal - array [?, 4] in topleft bottomright format
  217.     # groundtruth - array [?, 4] in center, width, height format
  218.     # labels - array [?, n] - one hot embedding
  219.     # n = number of classes
  220.  
  221.     # Compute IOU
  222.     fgroundtruth = format_anchors(groundtruth)
  223.     iou = iou_tensorial(fgroundtruth, boxes_proposal)
  224.     idx_of_GT = np.argmax(iou, axis = 0)
  225.     bp_labels = labels[idx_of_GT]
  226.     probabilities = np.max(iou, axis = 0)
  227.  
  228.     # Generate positive & negative
  229.     positive = np.arange(len(idx_of_GT))[probabilities > cls_thresh]
  230.     negative = np.arange(len(idx_of_GT))[probabilities <= cls_thresh]
  231.  
  232.     # Get idx
  233.     choosen_positive = np.random.choice(positive, int(number_samples*positive_percent), replace = True)
  234.     choosen_negative = np.random.choice(negative, number_samples - int(number_samples*positive_percent), replace = True)
  235.     choosen_idx = np.concatenate((choosen_positive, choosen_negative))
  236.  
  237.     # Get classes label
  238.     positive_labels = list(bp_labels[choosen_positive])
  239.     sample_labels = positive_labels + list((number_samples - int(number_samples*positive_percent))*[0])
  240.     sample_labels_onehot = np.zeros((len(sample_labels), n_classes+1), dtype = np.uint8)
  241.     sample_labels_onehot[np.arange(len(sample_labels)), sample_labels] = 1
  242.     sample_labels_onehot = np.array(sample_labels_onehot, dtype = np.float32)
  243.  
  244.     # Get regression labels - called 'offsets'
  245.     fboxes_proposal = format_anchors_inv(boxes_proposal)
  246.     offsets = np.zeros((len(choosen_positive), 4))
  247.     offsets[:, 0] = (fboxes_proposal[choosen_positive, 0] - groundtruth[idx_of_GT[choosen_positive], 0]) / fboxes_proposal[choosen_positive, 2]
  248.     offsets[:, 1] = (fboxes_proposal[choosen_positive, 1] - groundtruth[idx_of_GT[choosen_positive], 1]) / fboxes_proposal[choosen_positive, 3]
  249.     offsets[:, 2] = np.log(fboxes_proposal[choosen_positive, 2] / groundtruth[idx_of_GT[choosen_positive], 2])
  250.     offsets[:, 3] = np.log(fboxes_proposal[choosen_positive, 3] / groundtruth[idx_of_GT[choosen_positive], 3])
  251.    
  252.     return choosen_positive, choosen_idx, sample_labels_onehot, offsets
  253.  
  254.  
  255. def rpn_calculate_loss(confidence_list, regression_list, minibatchIndices, confidence_gt, regression_gt, name=None):
  256.  
  257.     with tf.name_scope(name, 'RPN_Loss', [confidence_list, regression_list, minibatchIndices, confidence_gt, regression_gt]):
  258.         minibatchIndices = tf.convert_to_tensor(minibatchIndices)
  259.         confidence_gt = tf.convert_to_tensor(confidence_gt)
  260.         regression_gt = tf.convert_to_tensor(regression_gt)
  261.  
  262.         # Confidence loss: Cross Entropy
  263.         cls_minibatch = tf.gather(confidence_list, minibatchIndices)
  264.         confidence_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = confidence_gt, logits = cls_minibatch), name = 'confidence_loss')
  265.         #confidence_loss = tf.divide(confidence_loss, n_classes)
  266.  
  267.         # Regression loss: Smooth L1
  268.         reg_minibatch = tf.gather(regression_list, minibatchIndices)
  269.         abs_deltaT = tf.abs(reg_minibatch - regression_gt)
  270.         L1smooth = tf.where(tf.less(abs_deltaT, 1.0), 0.5 * abs_deltaT * abs_deltaT, abs_deltaT - 0.5)
  271.         reg_loss = tf.reduce_mean(confidence_gt[:,1] * tf.reduce_sum(L1smooth, axis=-1))
  272.         reg_loss = 10*reg_loss
  273.  
  274.         # Total Loss
  275.         total_loss = tf.add(confidence_loss, reg_loss)
  276.  
  277.     return total_loss, confidence_loss, reg_loss
  278.  
  279. def fastRCNN_calculate_loss(cls, reg, positive_idx, choosen_idx, gt_cls_onehot, gt_reg, name = None):
  280.     with tf.name_scope(name, 'FastRCNN_Loss', [cls, reg, positive_idx, choosen_idx, gt_cls_onehot, gt_reg]):
  281.         # Convert
  282.         positive_idx = tf.convert_to_tensor(positive_idx)
  283.         choosen_idx = tf.convert_to_tensor(choosen_idx)
  284.         gt_cls_onehot = tf.convert_to_tensor(gt_cls_onehot)
  285.         gt_reg = tf.convert_to_tensor(gt_reg)
  286.  
  287.         # Calculate confidence loss
  288.         cls_minibatch = tf.gather(cls, choosen_idx)
  289.         cls_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = gt_cls_onehot, logits = cls_minibatch), name = 'RCNN_loss')
  290.  
  291.         # Calculate regression loss
  292.         reg_minibatch = tf.gather(reg, positive_idx)
  293.         abs_deltaT = tf.abs(reg_minibatch - gt_reg)
  294.         L1smooth = tf.where(tf.less(abs_deltaT, 1.0), 0.5 * abs_deltaT * abs_deltaT, abs_deltaT - 0.5)
  295.         reg_loss = tf.reduce_mean(tf.reduce_sum(L1smooth, axis=-1))
  296.  
  297.         total_loss = tf.add(cls_loss, reg_loss)
  298.     return total_loss, cls_loss, reg_loss
  299.  
  300. def get_region_of_interest(ft_shape, boxes_proposal, dim_reduce = 16):
  301.     # Normalize boxes_proposal
  302.     boxes_proposal = boxes_proposal/dim_reduce
  303.    
  304.  
  305.     # Convert from center, width, height to topleft bottom right
  306.     boxes_proposal_x1 = boxes_proposal[:, 0]
  307.     boxes_proposal_x1 = tf.reshape(boxes_proposal_x1, (-1, 1))
  308.     boxes_proposal_x1 = tf.clip_by_value(boxes_proposal_x1, 0.0, boxes_proposal_x1)
  309.    
  310.     boxes_proposal_y1 = boxes_proposal[:, 1]
  311.     boxes_proposal_y1 = tf.reshape(boxes_proposal_y1, (-1, 1))
  312.     boxes_proposal_y1 = tf.clip_by_value(boxes_proposal_y1, 0.0, boxes_proposal_y1)
  313.  
  314.     boxes_proposal_x2 = boxes_proposal[:, 2]
  315.     boxes_proposal_x2 = tf.reshape(boxes_proposal_x2, (-1, 1))
  316.     boxes_proposal_x2 = tf.clip_by_value(boxes_proposal_x2, boxes_proposal_x1, ft_shape[2])
  317.  
  318.     boxes_proposal_y2 = boxes_proposal[:, 3]
  319.     boxes_proposal_y2 = tf.reshape(boxes_proposal_y2, (-1, 1))
  320.     boxes_proposal_y2 = tf.clip_by_value(boxes_proposal_y2, boxes_proposal_y1, ft_shape[1])
  321.    
  322.  
  323.     # Format to int
  324.     boxes_proposal_cell = tf.cast(tf.concat([boxes_proposal_x1, boxes_proposal_y1, boxes_proposal_x2, boxes_proposal_y2], axis = -1), tf.int32)
  325.    
  326.     return boxes_proposal_cell
  327.  
  328. def calculate_eval(predict_cls, predict_reg, gt_boxes, cls_thresh, reg_iou, imWidth, imHeight):
  329.     total_gt_boxes = len(gt_boxes)
  330.  
  331.     total_reg_boxes = len(predict_reg)
  332.  
  333.     if total_reg_boxes == 0:
  334.         return 0, 0
  335.     reg_respone = format(predict_reg)   # Convert to tl-br
  336.  
  337.     gt_boxes = np.array(gt_boxes)
  338.     gt_boxes_formated = format_anchors(gt_boxes)
  339.  
  340.     iou_matrix = iou_tensorial(gt_boxes_formated, reg_respone)
  341.    
  342.     precision_respone = np.max(iou_matrix, axis = 0)
  343.     #print('Precision_respone')
  344.     #print(precision_respone)
  345.     recall_respone = np.max(iou_matrix, axis = 1)
  346.    
  347.     precision_wrong = len(np.argwhere(precision_respone < reg_iou).flatten())
  348.     recall_miss = len(np.argwhere(recall_respone < reg_iou).flatten())
  349.  
  350.     precision = (total_reg_boxes - precision_wrong)/total_reg_boxes
  351.     recall = (total_gt_boxes - recall_miss)/total_gt_boxes
  352.     return precision, recall
  353.  
  354. class RPN:
  355.     def __init__(self):
  356.         self.BATCH_SIZE = 1
  357.         self.lr = 0.0001
  358.         self.max_size = 1024
  359.         self.min_size = 608
  360.         self.aspect_ratios = [1, 2, 3]
  361.         self.scales = [32, 64, 128]     # In pixel
  362.         self.k_anchors = len(self.aspect_ratios)*len(self.scales)
  363.         self.n_classes = 1
  364.         self.max_epoches = 20
  365.         self.epoches_to_save = 5
  366.         self.global_step = 0
  367.         self.valid = 0.1
  368.         self.check_point = 'vgg_16.ckpt'
  369.         self.eval_iou = [0.5, 0.75]
  370.         self.eval_cls = [0.5, 0.75]
  371.         self.epoch_to_validate = 5
  372.         self.rpn_checkpoint = 'graphs/model.ckpt'
  373.         self.rpn_trained = 'rpn_trained.ckpt'
  374.  
  375.     def summary(self):
  376.         with tf.name_scope('summaries'):
  377.             tf.summary.scalar('confidence_loss', self.confidence_loss)
  378.             tf.summary.scalar('regression_loss', self.regression_loss)
  379.             tf.summary.scalar('total_loss', self.total_loss)
  380.             self.summary_op = tf.summary.merge_all()
  381.  
  382.     def build_net(self):
  383.        
  384.         # Forward
  385.         self.image_batch_placeholder = tf.placeholder(shape = [None, None, None, 3],dtype='float32')
  386.         self.placeholder_anchors = tf.placeholder(shape = [None, 4], dtype='float32', name = 'PL_anchors')
  387.         self.placeholder_anchors_cleaned = tf.placeholder(shape = [None],dtype='int32', name = 'PL_anchors_cleaned')
  388.  
  389.         # Inference
  390.         self.feature_map = _base_inference(self.image_batch_placeholder)
  391.         self.ft_shape = tf.cast(tf.shape(self.feature_map), tf.float32)
  392.         self.confidence_list, self.regression_list, self.pp_probabilities = _inference(self.feature_map, self.k_anchors)
  393.         self.prediction, self.probabilities = _predict_boxes(self.pp_probabilities, self.regression_list, self.placeholder_anchors, self.placeholder_anchors_cleaned)
  394.         self.boxes_proposal_cell = get_region_of_interest(self.ft_shape, self.prediction)
  395.         self.boxes_proposal_cell = tf.convert_to_tensor([self.boxes_proposal_cell])
  396.         self.cls, self.reg = _fastRCNN(self.feature_map, self.boxes_proposal_cell)
  397.  
  398.         # Calculate loss
  399.         self.minibatch_indices = tf.placeholder(dtype='int32', name = 'PL_RPN_indices')
  400.         self.A_scores_one_hot = tf.placeholder(dtype='float32', name = 'PL_RPN_onehot_label')
  401.         self.gt_anchors_list = tf.placeholder(dtype='float32', name = 'PL_RPN_gt')
  402.         self.total_loss, self.confidence_loss, self.regression_loss = rpn_calculate_loss(self.confidence_list, self.regression_list, \
  403.             self.minibatch_indices, \
  404.             self.A_scores_one_hot, self.gt_anchors_list)
  405.         self.saver = tf.train.Saver()
  406.  
  407.         # Calculate fastRCNN loss
  408.         self.placeholder_choosen_idx = tf.placeholder(shape = [None], dtype='int32', name = 'PL_indices')
  409.         self.placeholder_positive_idx = tf.placeholder(shape = [None], dtype='int32', name = 'PL_positive_indices')
  410.         self.placeholder_gt_reg = tf.placeholder(shape = [None, 4], dtype='float32', name = 'PL_gt_reg')
  411.         self.placeholder_gt_cls = tf.placeholder(shape = [None, self.n_classes+1], dtype='float32', name = 'PL_gt_cls')
  412.         self.rcnn_total_loss, self.rcnn_cls_loss, self.rcnn_reg_loss = fastRCNN_calculate_loss(self.cls, self.reg, self.placeholder_positive_idx, \
  413.             self.placeholder_choosen_idx, self.placeholder_gt_cls, self.placeholder_gt_reg)
  414.  
  415.         # Accuracy
  416.         self.summary()
  417.  
  418.         # Optimizer
  419.         #self.RPN_trainer = tf.train.MomentumOptimizer(learning_rate = self.lr, momentum = 0.9).minimize(self.total_loss, name = 'RPN_trainer')
  420.         self.RPN_trainer = tf.train.AdamOptimizer(learning_rate = self.lr).minimize(self.total_loss, name = 'RPN_trainer')
  421.         self.FastRCNN_trainer = tf.train.AdamOptimizer(learning_rate = self.lr).minimize(self.rcnn_total_loss, name = 'FastRCNN_trainer')
  422.         self.session = tf.Session()
  423.  
  424.     def get_data(self, visualize = True):
  425.         data_imgs, data_labels, data_coordinates, self.train_sizes = get_data('plate/images', 'plate/labels', self.min_size, self.max_size, visualize = False)
  426.         self.train_imgs, self.train_labels, self.train_coordinates, self.val_imgs, self.val_labels, self.val_coordinates = split_dataset(data_imgs, data_labels, data_coordinates, self.valid)
  427.         print('Train: {}    Val: {}'.format(len(self.train_imgs), len(self.val_imgs)))
  428.  
  429.     def shuffle_data(self):
  430.         ind = [i for i in range(len(self.train_labels))]
  431.         ind = shuffle(ind)
  432.         self.train_imgs, self.train_labels, self.train_coordinates = shuffle(self.train_imgs, self.train_labels, self.train_coordinates)
  433.  
  434.     def rpn_train(self):
  435.         max_steps = int(len(self.train_labels) / self.BATCH_SIZE)
  436.         writer = tf.summary.FileWriter('graphs', self.session.graph)
  437.         self.session.run([tf.global_variables_initializer()])
  438.  
  439.         # Restore parameter
  440.         print('Fineturning with VGG16-ImageNet ...')
  441.         self.fturning_saver = fine_turning_vgg16()
  442.         self.fturning_saver.restore(self.session, self.check_point)
  443.  
  444.         for epoch in range(self.max_epoches):
  445.  
  446.             # Shuffle data
  447.             self.shuffle_data()
  448.  
  449.             print('Epoches {} / {} :'.format(epoch, self.max_epoches))
  450.             sumary_total_loss = []
  451.             sumary_confidence_loss = []
  452.             sumary_regression_loss = []
  453.             for step in range(max_steps):
  454.                 minibatch_imgs, minibatch_labels, minibatch_gt_coors, minibatch_sizes = self.train_imgs[step], self.train_labels[step], self.train_coordinates[step], self.train_sizes[step]
  455.                 height, width,_ = minibatch_imgs.shape
  456.                 #print('Image shape: {}x{}'.format(width, height))
  457.                 minibatch_imgs = np.array([minibatch_imgs])
  458.                 minibatch_indices, A_scores_one_hot, ground_truth_anchors_list = rpn_generate_batch_label(minibatch_gt_coors, 16, (width, height), self.aspect_ratios, self.scales, self.BATCH_SIZE)
  459.                 _, total_loss, confidence_loss, regression_loss, conf_list, summaries = self.session.run([self.RPN_trainer, self.total_loss, self.confidence_loss, self.regression_loss, self.confidence_list, self.summary_op], \
  460.                         feed_dict = { self.minibatch_indices: minibatch_indices, \
  461.                                 self.A_scores_one_hot: A_scores_one_hot, \
  462.                                 self.gt_anchors_list: ground_truth_anchors_list, \
  463.                                 self.image_batch_placeholder: minibatch_imgs})
  464.                 #print('Confidence list shape {}'.format(conf_list.shape))
  465.                 writer.add_summary(summaries, global_step = epoch*max_steps + step)
  466.                 sumary_total_loss.append(total_loss)
  467.                 sumary_confidence_loss.append(confidence_loss)
  468.                 sumary_regression_loss.append(regression_loss)
  469.                 print('Step: {}/{} | Conf_loss: {} | Reg_loss: {} | Total_loss: {}'.format(step, max_steps, confidence_loss, regression_loss, total_loss))
  470.             mean_total_loss = np.mean(np.array(sumary_total_loss))
  471.             mean_confidence_loss = np.mean(np.array(sumary_confidence_loss))
  472.             mean_regression_loss = np.mean(np.array(sumary_regression_loss))
  473.             print('-----------------------------------------------------------------------------------------------------------------------------')
  474.             print('Epoch: {}      Conf_loss: {}      Reg_loss: {}       Total_loss:  {}'.format(epoch, mean_confidence_loss, mean_regression_loss, mean_total_loss))
  475.             print('-----------------------------------------------------------------------------------------------------------------------------')
  476.             # if epoch % self.epoch_to_validate == 0:
  477.             #   self.eval()
  478.                 #pass
  479.         self.saver.save(self.session, 'graphs/model.ckpt')
  480.         writer.close()
  481.  
  482.     def fastRCNN_train(self, dim_reduce = 16):
  483.         max_steps = int(len(self.train_labels) / self.BATCH_SIZE)
  484.         writer = tf.summary.FileWriter('graphs', self.session.graph)
  485.         print('Fast R-CNN training ...')
  486.         self.session.run([tf.global_variables_initializer()])
  487.         print('Restore RPN parameters ...')
  488.         self.saver.restore(self.session, self.rpn_trained)
  489.         for epoch in range(self.max_epoches):
  490.             self.shuffle_data()
  491.             print('Epoches {} / {} :'.format(epoch, self.max_epoches))
  492.             sumary_total_loss = []
  493.             sumary_confidence_loss = []
  494.             sumary_regression_loss = []
  495.             for step in range(max_steps):
  496.                 minibatch_imgs, minibatch_labels, minibatch_gt_coors, minibatch_sizes = self.train_imgs[step], self.train_labels[step], self.train_coordinates[step], self.train_sizes[step]
  497.                 height, width,_ = minibatch_imgs.shape
  498.                 minibatch_imgs = np.array([minibatch_imgs])
  499.                 minibatch_labels = np.array(minibatch_labels, dtype = np.uint8)
  500.                 minibatch_gt_coors = np.array(minibatch_gt_coors)
  501.  
  502.                 # Get proposal boxes
  503.                 anchors = rpn_generate_anchor_boxes((int(width/dim_reduce), int(height/dim_reduce)), (width, height), self.aspect_ratios, self.scales)
  504.                 anchors = anchors.reshape((-1, 4))
  505.                 anchors_cleaned = remove_cross_boundaries_anchors(anchors, width, height)
  506.                 boxes_proposal = self.session.run(self.prediction, \
  507.                         feed_dict = {self.image_batch_placeholder : minibatch_imgs, \
  508.                         self.placeholder_anchors : anchors,\
  509.                         self.placeholder_anchors_cleaned: anchors_cleaned})
  510.  
  511.                 # Training
  512.                 choosen_positive, choosen_idx, sample_labels_onehot, offsets = fastRCNN_generate_batch_labels(boxes_proposal, minibatch_gt_coors, minibatch_labels)
  513.                 _, total_loss, confidence_loss, regression_loss = self.session.run([self.FastRCNN_trainer, self.rcnn_total_loss, self.rcnn_cls_loss, self.rcnn_reg_loss], \
  514.                     feed_dict = {self.image_batch_placeholder : minibatch_imgs, \
  515.                     self.placeholder_positive_idx: choosen_positive, \
  516.                     self.placeholder_choosen_idx: choosen_idx, \
  517.                     self.placeholder_gt_cls: sample_labels_onehot, \
  518.                     self.placeholder_gt_reg: offsets, \
  519.                     self.placeholder_anchors : anchors,\
  520.                     self.placeholder_anchors_cleaned: anchors_cleaned })
  521.                 #print('Confidence list shape {}'.format(conf_list.shape))
  522.                 #writer.add_summary(summaries, global_step = epoch*max_steps + step)
  523.                 sumary_total_loss.append(total_loss)
  524.                 sumary_confidence_loss.append(confidence_loss)
  525.                 sumary_regression_loss.append(regression_loss)
  526.                 print('Step: {}/{} | Conf_loss: {} | Reg_loss: {} | Total_loss: {}'.format(step, max_steps, confidence_loss, regression_loss, total_loss))
  527.             mean_total_loss = np.mean(np.array(sumary_total_loss))
  528.             mean_confidence_loss = np.mean(np.array(sumary_confidence_loss))
  529.             mean_regression_loss = np.mean(np.array(sumary_regression_loss))
  530.             print('-----------------------------------------------------------------------------------------------------------------------------')
  531.             print('Epoch: {}      Conf_loss: {}      Reg_loss: {}       Total_loss:  {}'.format(epoch, mean_confidence_loss, mean_regression_loss, mean_total_loss))
  532.             print('-----------------------------------------------------------------------------------------------------------------------------')
  533.  
  534.  
  535.  
  536.     def eval(self):
  537.         print('Validating ...')
  538.         total_val_img = len(self.train_imgs)
  539.         list_precision = []
  540.         list_recall = []
  541.         for i in range(total_val_img):
  542.             precision, recall = [], []
  543.             img, labels, gt_coors = self.train_imgs[i], self.train_labels[i], self.train_coordinates[i]
  544.             img = img.reshape((1, self.img_height, self.img_width, 3))
  545.             conf_list, reg_list, box_predict = self.session.run([self.confidence_list, self.regression_list, self.prediction], feed_dict = {self.image_batch_placeholder: img})
  546.             cls_op = tf.nn.softmax(conf_list, axis = 1)
  547.             with tf.Session() as sess:
  548.                 proba = sess.run(cls_op)
  549.             proba = proba[:, 1]
  550.             for cs in self.eval_cls:
  551.                 for iou in self.eval_iou:
  552.                     tmp1, tmp2 = calculate_eval(proba, box_predict, gt_coors, cs, iou, self.img_width, self.img_height)
  553.                     precision.append(tmp1)
  554.                     recall.append(tmp2)
  555.             list_precision.append(precision)
  556.             list_recall.append(recall)
  557.  
  558.         # Show result
  559.         list_precision = np.array(list_precision)
  560.         list_recall = np.array(list_recall)
  561.         count = 0
  562.         for cs in self.eval_cls:
  563.             for iou in self.eval_iou:
  564.                 precision = np.mean(list_precision[count, :])
  565.                 recall = np.mean(list_recall[count, :])
  566.                 print('Cls: {}  | IoU: {}   | Precision: {}     | Recall: {}'.format(cs, iou, precision, recall))
  567.  
  568.  
  569.     def predict(self, image, cls_thresh = 0.7, dim_reduce = 16):
  570.         img = read_and_rescale(image)
  571.         height, width, _ = img.shape
  572.         print('Resized: {}x{}'.format(width, height))
  573.         self.session.run([tf.global_variables_initializer()])
  574.         print('Loading pretrained model ...')
  575.         #saver_temp = tf.train.Saver(v.op.name for v in slim.get_model_variables() if not v.startswith('rcnn'))
  576.         self.saver.restore(self.session, 'graphs/model.ckpt')
  577.         image = np.array([img])
  578.         print('Predict: ')
  579.  
  580.         # Get anchors
  581.         anchors = rpn_generate_anchor_boxes((int(width/dim_reduce), int(height/dim_reduce)), (width, height), self.aspect_ratios, self.scales)
  582.         anchors = anchors.reshape((-1, 4))
  583.         anchors_cleaned = remove_cross_boundaries_anchors(anchors, width, height)
  584.         predict_boxes, probabilities, pp_cell = self.session.run([self.prediction, self.probabilities, self.reg], \
  585.                 feed_dict = {self.image_batch_placeholder:image, \
  586.                             self.placeholder_anchors: anchors,\
  587.                             self.placeholder_anchors_cleaned: anchors_cleaned})
  588.  
  589.  
  590.         #predict_boxes = nms(predict_boxes, cleaned_probabilites)
  591.         #print(boxes_cell)
  592.         #print(reg)
  593.         #print(reg.shape)
  594.         #print(ftmap)
  595.         print(pp_cell)
  596.         for i in range(len(predict_boxes)):
  597.             x1, y1, x2, y2 = predict_boxes[i]
  598.             if x1>0 and y1>0 and x2>0 and y2>0:
  599.                 img = cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 1)
  600.         cv2.imshow('predict', img)
  601.         cv2.waitKey(0)
  602.  
  603. if __name__ == '__main__':
  604.     net = RPN()
  605.     net.get_data(visualize = False)
  606.     net.build_net()
  607.     #net.summary()
  608.     net.fastRCNN_train()
  609.  
  610.     #net.predict('plate/images/file_0.jpg')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement