def inference(self, batch): ''' Method to perform inference on a batch. ''' inferenceList = list() start = time.time() prev_time = time.time() boxes, scores = self.sess.run(self.output_tensors, feed_dict={self.input_tensor: batch}) for i in range(0, len(scores)): t_boxes = boxes[i] t_boxes = np.expand_dims(t_boxes, axis=0) t_scores = scores[i] t_scores = np.expand_dims(t_scores, axis=0) t_boxes, t_scores, t_labels = utils.cpu_nms(t_boxes, t_scores, self.num_classes, score_thresh=0.5, iou_thresh=0.4) inferenceList.append([t_labels, t_scores, t_boxes]) print(time.time() - start) curr_time = time.time() exec_time = curr_time - prev_time info = "time: %.2f ms" % (1000 * exec_time) return inferenceList
def predict_image(self, record): """ Predict boxes, scores and labels for a single image record :param record: :type record: :return: :rtype: """ # Pre-process record record = self._process_image(record) if self.use_gpu: boxes, _, labels, probs = self.sess.run( self.output_tensors, feed_dict={self.input_tensor: np.expand_dims(record, axis=0)}) else: boxes, scores, probs = self.sess.run( self.output_tensors, feed_dict={self.input_tensor: np.expand_dims(record, axis=0)}) boxes, _, labels, probs = utils.cpu_nms( boxes, scores, probs, self.num_classes, score_thresh=self.score_thresh, iou_thresh=self.iou_thresh) return {'boxes': boxes, 'probs': probs, 'labels': labels}
def inference(self, batch): ''' inference : run detection on yolov3 Parameters ---------- batch : numpy.ndarray numpy.ndarray of shape (batch, img_size, img_size, 3) Returns ------- inferenceList : list list of object bounding boxes with scores and labels ''' inferenceList = list() print(batch.shape) boxes, scores = self.sess.run(self.output_tensors, feed_dict={self.input_tensor: batch}) for i in range(0, len(scores)): t_boxes = boxes[i] t_boxes = np.expand_dims(t_boxes, axis=0) t_scores = scores[i] t_scores = np.expand_dims(t_scores, axis=0) t_boxes, t_scores, t_labels = utils.cpu_nms(t_boxes, t_scores, self.num_classes, score_thresh=0.3, iou_thresh=0.4) inferenceList.append([t_labels, t_scores, t_boxes]) return {'Detections': inferenceList}
def run(self, img): # Processing frame img_resized = self._preprocessFrame(img) boxes, scores = self.sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels = utils.cpu_nms(boxes, scores, num_classes, score_thresh=0.4, iou_thresh=0.5) # Keeping only box labelled "person" if boxes is not None: boxes = self._getOnlyDetectedPeople(boxes, labels) return boxes
def post_proc(self, results, ids, expected=None, result_dict=None): # results come as: # detection_scores, detection_boxes # batch size bs = len(results[0]) for idx in range(0, bs): scores_ = results[0][idx] boxes_ = results[1][idx] detection_boxes, scores, detection_classes = utils.cpu_nms( boxes_, scores_, self.num_class, self.max_boxes, self.score_thresh, self.nms_thresh) print("lcm debug nms out detection_boxes:{}", detection_boxes) print("lcm debug nms out scores:{}", scores) print("lcm debug nms out detection_classes:{}", detection_classes) self.append_result_info(idx, ids, expected, detection_boxes, scores, detection_classes)
def post_proc(self, results, ids, expected=None, result_dict=None): # results come as: # batch size bs = len(results[0]) for idx in range(0, bs): res = [] print("lcm debug shape", results[0][idx].shape, results[1][idx].shape, results[2][idx].shape, self.datasets.image_sizes[ids[idx]]) res.append(results[0][idx].reshape(-1, 13, 13, 3, 85)) res.append(results[1][idx].reshape(-1, 26, 26, 3, 85)) res.append(results[2][idx].reshape(-1, 52, 52, 3, 85)) boxes_, scores_ = self.decode_feature_map((416, 416), res) detection_boxes, scores, detection_classes = utils.cpu_nms( boxes_, scores_, self.num_class, self.max_boxes, self.score_thresh, self.nms_thresh) print("lcm debug nms out detection_boxes:{}", detection_boxes) print("lcm debug nms out scores:{}", scores) print("lcm debug nms out detection_classes:{}", detection_classes) self.append_result_info(idx, ids, expected, detection_boxes, scores, detection_classes)
config = tf.ConfigProto() config.gpu_options.allow_growth = True IMAGE_H, IMAGE_W = 416, 416 classes = utils.read_coco_names('./data/raccoon.names') num_classes = len(classes) image_path = "data/hide_specific_target_dataset/000000336584.jpg" # 181, img = Image.open(image_path) img_resized = np.array(img.resize(size=(IMAGE_W, IMAGE_H)), dtype=np.float32) img_resized = img_resized / 255. cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors( cpu_nms_graph, "./checkpoint/yolov3_cpu_nms.pb", ["Placeholder:0", "concat_9:0", "mul_6:0"]) with tf.Session(graph=cpu_nms_graph) as sess: boxes, scores = sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels = utils.cpu_nms(boxes, scores, num_classes, score_thresh=0.3, iou_thresh=0.5) image = utils.draw_bdeoxes(img, boxes, scores, labels, classes, [IMAGE_H, IMAGE_W], show=True)
import tensorflow as tf from core import utils, yolov3 import cv2 from PIL import Image import numpy as np input_image = "../data/raccoon_data/images/raccoon-4.jpg" image = Image.open(input_image) # image = cv2.imread(input_image) # image = Image.fromarray(image) image_resize = cv2.resize(np.array(image) / 255., (416, 416)) image_place = tf.placeholder(dtype=tf.float32, shape=(None, 416, 416, 3)) CLASSES = utils.read_coco_names('../data/objects.names') ANCHORE = utils.get_anchors("../data/objects.txt", 416, 416) model = yolov3.yolov3(len(CLASSES), ANCHORE) with tf.variable_scope('yolov3'): pred_feature_map = model.forward(image_place, is_training=False) pred = model.predict(pred_feature_map) sess = tf.Session() saver = tf.train.Saver() model_dir = tf.train.latest_checkpoint("../data/raccoon_data/model/") saver.restore(sess, model_dir) boxes, confs, prods = sess.run(pred, feed_dict={image_place: np.expand_dims(image_resize, 0)}) boxes, confs, prods = utils.cpu_nms(boxes, confs * prods, len(CLASSES)) utils.draw_boxes(image, boxes, confs, prods, CLASSES, (416, 416), "../data/font/HuaWenXinWei-1.ttf") print(boxes, confs, prods)
true_labels_list, true_boxes_list = [], [] for i in range(3): true_probs_temp = y_true[i][..., 5:] true_boxes_temp = y_true[i][..., 0:4] object_mask = true_probs_temp.sum(axis=-1) > 0 true_probs_temp = true_probs_temp[object_mask] true_boxes_temp = true_boxes_temp[object_mask] true_labels_list += np.argmax(true_probs_temp, axis=-1).tolist() true_boxes_list += true_boxes_temp.tolist() pred_boxes, pred_scores, pred_labels = utils.cpu_nms( pred_boxes, pred_confs * pred_probs, NUM_CLASSES, score_thresh=SCORE_THRESH, iou_thresh=IOU_THRESH) # image = utils.draw_boxes(image, pred_boxes, pred_scores, pred_labels, CLASSES, [IMAGE_H, IMAGE_W], show=True) true_boxes = np.array(true_boxes_list) box_centers, box_sizes = true_boxes[:, 0:2], true_boxes[:, 2:4] true_boxes[:, 0:2] = box_centers - box_sizes / 2. true_boxes[:, 2:4] = true_boxes[:, 0:2] + box_sizes pred_labels_list = [] if pred_labels is None else pred_labels.tolist() all_detections.append([pred_boxes, pred_scores, pred_labels_list]) all_annotations.append([true_boxes, true_labels_list]) image_idx += 1 if image_idx % 100 == 0: sys.stdout.write(".")
if return_value: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) else: raise ValueError("No image!") img_resized = np.array(image.resize(size=(IMAGE_H, IMAGE_W)), dtype=np.float32) img_resized = img_resized / 255. prev_time = time.time() boxes, scores, probs = sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img_resized, axis=0)}) boxes, scores, labels, _ = utils.cpu_nms(boxes, scores, probs, num_classes, score_thresh=0.4, iou_thresh=0.5) image = utils.draw_boxes(image, boxes, scores, labels, classes, (IMAGE_H, IMAGE_W), show=False) curr_time = time.time() exec_time = curr_time - prev_time result = np.asarray(image) info = "time: %.2f ms" % (1000 * exec_time) cv2.putText(result, text=info,
saver = tf.train.Saver() saver.restore(sess, "data/SVHN/checkpoint5/yolov3.ckpt-4000") acc = 0 STEPS = 13068 for step in range(STEPS): run_items = sess.run([y_pred, y_true], feed_dict={is_training: False}) if step == 5: acc = utils.compute_accuracy(run_items[0], run_items[1]) y_pred_data = run_items[0] pred_boxes = y_pred_data[0][0] pred_confs = y_pred_data[1][0] pred_probs = y_pred_data[2][0] pred_boxes, pred_scores, pred_labels = utils.cpu_nms(pred_boxes, pred_confs * pred_probs, NUM_CLASSES, score_thresh=0.3, iou_thresh=0.5) img = Image.open("data/SVHN/PaddingTest/" + str(step + 1) + ".png") image = utils.draw_boxes(img, pred_boxes, pred_scores, pred_labels, CLASSES, [IMAGE_H, IMAGE_W], show=False) # if acc == 1: # image.save("data/SVHN/RightRecognition/" + str(step + 1) + ".png") # else: # image.save("data/SVHN/WrongRecognition/" + str(step + 1) + ".png") print("=> STEP %10d [VALID]:\tacc:%7.4f" % (step+1, acc)) acc += acc * BATCH_SIZE acc /= 13068 print("精度为%7.4f" % acc)
def evaluate(sess, y_pred, y_true): NUM_CLASSES = Config.NUM_CLASSES CLASSES = Config.CLASSES all_detections = [] all_annotations = [] all_aver_precs = {CLASSES[i]:0. for i in range(NUM_CLASSES)} for _ in range(868): y_pred_o, y_true_o = sess.run([y_pred, y_true],feed_dict={is_training:False}) pred_boxes = y_pred_o[0] pred_confs = y_pred_o[1] pred_probs = y_pred_o[2] true_labels_list, true_boxes_list = [], [] true_probs_temp = y_true_o[..., 5: ] true_boxes_temp = y_true_o[..., 0:4] object_mask = true_probs_temp.sum(axis=-1) > 0 true_probs_temp = true_probs_temp[object_mask] true_boxes_temp = true_boxes_temp[object_mask] true_labels_list += np.argmax(true_probs_temp, axis=-1).tolist() true_boxes_list += true_boxes_temp.tolist() pred_boxes, pred_scores, pred_labels = utils.cpu_nms(pred_boxes, pred_confs*pred_probs, NUM_CLASSES, score_thresh=0.3, iou_thresh=0.5) true_boxes = np.array(true_boxes_list) box_centers, box_sizes = true_boxes[:,0:2], true_boxes[:,2:4] true_boxes[:,0:2] = box_centers - box_sizes / 2. true_boxes[:,2:4] = true_boxes[:,0:2] + box_sizes pred_labels_list = [] if pred_labels is None else pred_labels.tolist() all_detections.append( [pred_boxes, pred_scores, pred_labels_list]) all_annotations.append([true_boxes, true_labels_list]) for idx in range(NUM_CLASSES): true_positives = [] scores = [] num_annotations = 0 for i in range(len(all_annotations)): pred_boxes, pred_scores, pred_labels_list = all_detections[i] true_boxes, true_labels_list = all_annotations[i] detected = [] num_annotations += true_labels_list.count(idx) for k in range(len(pred_labels_list)): if pred_labels_list[k] != idx: continue scores.append(pred_scores[k]) ious = utils.bbox_iou(pred_boxes[k:k+1], true_boxes) m = np.argmax(ious) if ious[m] > 0.5 and pred_labels_list[k] == true_labels_list[m] and m not in detected: detected.append(m) true_positives.append(1) else: true_positives.append(0) num_predictions = len(true_positives) true_positives = np.array(true_positives) false_positives = np.ones_like(true_positives) - true_positives # sorted by score indices = np.argsort(-np.array(scores)) false_positives = false_positives[indices] true_positives = true_positives[indices] # compute false positives and true positives false_positives = np.cumsum(false_positives) true_positives = np.cumsum(true_positives) # compute recall and precision recall = true_positives / np.maximum(num_annotations, np.finfo(np.float64).eps) precision = true_positives / np.maximum(num_predictions, np.finfo(np.float64).eps) # compute average precision average_precision = utils.compute_ap(recall, precision) all_aver_precs[CLASSES[idx]] = average_precision MAP = sum(all_aver_precs.values()) / NUM_CLASSES return MAP
def test(self, source_dir=None, image_path=None, show=False): if source_dir: image_name = choice(os.listdir(source_dir)) image_path = os.path.join(source_dir, image_name) img = cv2.imread(image_path, 0) img = cv2.resize(src=img, dsize=(0, 0), fx=1 / 2, fy=1 / 2) if img.shape[0] < self.img_h and img.shape[1] < self.img_w: img = cv2.copyMakeBorder(src=img, top=(self.img_h - img.shape[0]) // 2, bottom=(self.img_h - img.shape[0]) // 2, left=(self.img_w - img.shape[1]) // 2, right=(self.img_w - img.shape[1]) // 2, borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255]) img = cv2.resize(img, (self.img_w, self.img_h)) img = expand_dims(img, axis=2) classes = os.listdir(self.orig_letters) cpu_nms_graph = tf.Graph() input_tensor, output_tensors = utils.read_pb_return_tensors( cpu_nms_graph, os.path.join(self.checkpoint_dir, "yolov3_cpu_nms.pb"), ["Placeholder:0", "concat_5:0", "mul_2:0"]) with tf.Session(graph=cpu_nms_graph) as sess: boxes, scores = sess.run( output_tensors, feed_dict={input_tensor: np.expand_dims(img, axis=0)}) boxes = boxes.reshape(-1, 4) scores = scores.reshape(-1, self.num_classes) min_wh, max_wh = -10000, 10000 min_ratio = 1 / 4 # 0 -- 1 mask = np.logical_and(boxes[:, 0] >= min_wh, boxes[:, 0] <= max_wh) mask = np.logical_and(mask, boxes[:, 1] >= min_wh) mask = np.logical_and(mask, boxes[:, 2] >= min_wh) mask = np.logical_and(mask, boxes[:, 3] >= min_wh) mask = np.logical_and(mask, boxes[:, 1] <= max_wh) mask = np.logical_and(mask, boxes[:, 2] <= max_wh) mask = np.logical_and(mask, boxes[:, 3] <= max_wh) mask = np.logical_and(mask, boxes[:, 0] < boxes[:, 2]) mask = np.logical_and(mask, boxes[:, 1] < boxes[:, 3]) boxes = boxes[mask] scores = scores[mask] h = abs(boxes[:, 2] - boxes[:, 0]) w = abs(boxes[:, 3] - boxes[:, 1]) mask = np.logical_and(w / h > min_ratio, h / w > min_ratio) boxes = boxes[mask] scores = scores[mask] if self.filters: # Harder filters print(f"Test: Boxes before filtering:\t{boxes.shape[0]}") mask = np.logical_and(boxes[:, 0] >= 0, boxes[:, 0] <= img.shape[1]) mask = np.logical_and(mask, boxes[:, 1] >= 0) mask = np.logical_and(mask, boxes[:, 2] >= 0) mask = np.logical_and(mask, boxes[:, 3] >= 0) mask = np.logical_and(mask, boxes[:, 1] <= img.shape[0]) mask = np.logical_and(mask, boxes[:, 2] <= img.shape[1]) mask = np.logical_and(mask, boxes[:, 3] <= img.shape[0]) mask = np.logical_and(mask, boxes[:, 0] < boxes[:, 2]) mask = np.logical_and(mask, boxes[:, 1] < boxes[:, 3]) mask = np.logical_and( mask, abs(boxes[:, 2] - boxes[:, 0]) >= self.size_threshold[0]) mask = np.logical_and( mask, abs(boxes[:, 3] - boxes[:, 1]) >= self.size_threshold[1]) boxes = boxes[mask] scores = scores[mask] print(f"Test: Boxes after filtering:\t{boxes.shape[0]}") if boxes.shape[0] == 0: print( f"Try changing the filters/thresholds in the parameters." ) boxes, scores, labels = utils.cpu_nms(boxes=boxes, scores=scores, num_classes=self.num_classes, max_boxes=self.max_boxes) (image, results) = utils.draw_boxes(img, boxes, scores, labels, classes, [self.img_h, self.img_w], show=show, size_threshold=self.size_threshold) return results
BATCH_SIZE, shuffle=SHUFFLE_SIZE, multi_image_size=False) testset = dataset(parser, TEST_TFRECORD, BATCH_SIZE, shuffle=None) example = trainset.get_next() images, *y_true = example model = yolov3.yolov3(NUM_CLASSES, ANCHORS, basic_net=MobilenetV2) with tf.variable_scope('yolov3'): model.set_anchor(images) pred_feature_map = model.forward(images, is_training=False) y_pred = model.predict(pred_feature_map) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, "./checkpoint/yolov3.ckpt-25000") run_items = sess.run([images, y_pred]) for i in range(8): image = run_items[0][i] pred_boxes = run_items[1][0][i:i + 1] pred_confs = run_items[1][1][i:i + 1] pred_probs = run_items[1][2][i:i + 1] pred_boxes, pred_scores, pred_labels = utils.cpu_nms( pred_boxes, pred_confs * pred_probs, len(CLASSES)) im = utils.draw_boxes(image * 255, pred_boxes, pred_scores, pred_labels, CLASSES, (416, 416), show=True)
import cv2 from PIL import Image import numpy as np input_image = "../data/train_dome_data/images/raccoon-4.jpg" image = Image.open(input_image) # image = cv2.imread(input_image) # image = Image.fromarray(image) image_resize = cv2.resize(np.array(image) / 255., (416, 416)) image_place = tf.placeholder(dtype=tf.float32, shape=(None, 416, 416, 3)) CLASSES = utils.read_coco_names('../data/raccoon.names') ANCHORE = utils.get_anchors("../data/raccoon_anchors.txt", 416, 416) model = yolov3.yolov3(len(CLASSES), ANCHORE) with tf.variable_scope('yolov3'): pred_feature_map = model.forward( image_place, is_training=False) #执行前向传播得到3个尺度的feature map pred = model.predict(pred_feature_map) #根据feature map进行预测 sess = tf.Session() saver = tf.train.Saver() model_dir = tf.train.latest_checkpoint("../data/train_dome_data/model/") saver.restore(sess, model_dir) boxes, confs, prods = sess.run( pred, feed_dict={image_place: np.expand_dims(image_resize, 0)}) #根据预测结果和获取目标预测框,目标类别 以及是否有object boxes, confs, prods = utils.cpu_nms(boxes, confs * prods, len(CLASSES)) #根据非最大值抑制算法的原则 筛选出最优的候选框 utils.draw_boxes(image, boxes, confs, prods, CLASSES, (416, 416), "../data/font/HuaWenXinWei-1.ttf") #在输出图片上绘制目标框,并输出标签识别率结果 print(boxes, confs, prods)
with tf.variable_scope("yoloAt"): p_feature_map = model.forward(inputs_image, is_training=False) sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(var_list=tf.global_variables()) saver.restore(sess, Config.CKPT) print(Config.F_Yellow + "starting predicting" + Config.F_Init) for pic in os.listdir('./data/test_pic'): img = Image.open('./data/test_pic/' + pic) img_resized = np.array(img.resize(size=(Config.IMAGE_H, Config.IMAGE_W)), dtype=np.float32) img_resized = img_resized / 255 boxes, confs, probs = sess.run( model.predict(p_feature_map), feed_dict={inputs_image: np.expand_dims(img_resized, axis=0)}) scores = confs * probs boxes, scores, labels = utils.cpu_nms(boxes, scores, Config.NUM_CLASSES) image = utils.draw_boxes(img, boxes, scores, labels, Config.CLASSES, [Config.IMAGE_H, Config.IMAGE_W], show=False) image.save('./jpg/pre_{}'.format(pic)) print("finished predicting {}".format(Config.F_Red + pic + Config.F_Init))