def build_dictionary(self): # load dataset path csv_path = os.path.join(config.params['data_path'], config.params['train_labels']) # build dictionary and key self.dictionary, self.classes = build_label_dictionary(csv_path) self.n_classes = len(self.classes) self.keys = np.array(list(self.dictionary.keys())) return csv_path = os.path.join(config.params['data_path'], config.params['test_labels']) self.test_dictionary, _ = build_label_dictionary(csv_path) self.test_keys = np.array(list(self.test_dictionary.keys()))
def build_dictionary(self): """Read input image filenames and obj detection labels from a csv file and store in a dictionary. """ # train dataset path path = os.path.join(self.args.data_path, self.args.train_labels) # build dictionary: # key=image filaname, value=box coords + class label # self.classes is a list of class labels self.dictionary, self.classes = build_label_dictionary(path) self.n_classes = len(self.classes) self.keys = np.array(list(self.dictionary.keys()))
def evaluate_test(self): # test labels csv path path = os.path.join(self.args.data_path, self.args.test_labels) # test dictionary dictionary, _ = build_label_dictionary(path) keys = np.array(list(dictionary.keys())) # sum of precision s_precision = 0 # sum of recall s_recall = 0 # sum of IoUs s_iou = 0 # evaluate per image for key in keys: # grounnd truth labels labels = np.array(dictionary[key]) # 4 boxes coords are 1st four items of labels gt_boxes = labels[:, 0:-1] # last one is class gt_class_ids = labels[:, -1] # load image id by key image_file = os.path.join(self.args.data_path, key) image = skimage.img_as_float(imread(image_file)) image, classes, offsets = self.detect_objects(image) # perform nms _, _, class_ids, boxes = show_boxes(args, image, classes, offsets, self.feature_shapes, show=False) boxes = np.reshape(np.array(boxes), (-1,4)) # compute IoUs iou = layer_utils.iou(gt_boxes, boxes) # skip empty IoUs if iou.size ==0: continue # the class of predicted box w/ max iou maxiou_class = np.argmax(iou, axis=1) # true positive tp = 0 # false positiove fp = 0 # sum of objects iou per image s_image_iou = [] for n in range(iou.shape[0]): # ground truth bbox has a label if iou[n, maxiou_class[n]] > 0: s_image_iou.append(iou[n, maxiou_class[n]]) # true positive has the same class and gt if gt_class_ids[n] == class_ids[maxiou_class[n]]: tp += 1 else: fp += 1 # objects that we missed (false negative) fn = abs(len(gt_class_ids) - tp) s_iou += (np.sum(s_image_iou) / iou.shape[0]) s_precision += (tp/(tp + fp)) s_recall += (tp/(tp + fn)) n_test = len(keys) print_log("mIoU: %f" % (s_iou/n_test), self.args.verbose) print_log("Precision: %f" % (s_precision/n_test), self.args.verbose) print_log("Recall : %f" % (s_recall/n_test), self.args.verbose)
def evaluate_test(self): csv_path = os.path.join(config.params['data_path'], config.params['test_labels']) print("CSV", csv_path) dictionary, _ = build_label_dictionary(csv_path) keys = np.array(list(dictionary.keys())) n_iou = 0 s_iou = 0 i = 0 tp = 0 fp = 0 for key in keys: labels = dictionary[key] labels = np.array(labels) # 4 boxes coords are 1st four items of labels gt_boxes = labels[:, 0:-1] gt_class_ids = labels[:, -1] image_file = os.path.join(config.params['data_path'], key) print("Image: ", image_file) image = skimage.img_as_float(imread(image_file)) image = np.expand_dims(image, axis=0) classes, offsets = self.ssd.predict(image) image = np.squeeze(image, axis=0) classes = np.squeeze(classes) offsets = np.squeeze(offsets) _, _, class_ids, boxes = show_boxes(image, classes, offsets, self.feature_shapes, show=False, normalize=self.normalize) boxes = np.reshape(np.array(boxes), (-1, 4)) iou = layer_utils.iou(gt_boxes, boxes) if iou.size == 0: continue print("--------------") print("gt:", gt_class_ids, gt_boxes) print("iou w/ gt:", iou) print("iou shape:", iou.shape) maxiou_class = np.argmax(iou, axis=1) print("classes: ", maxiou_class) n = iou.shape[0] n_iou += n s = [] for j in range(n): s.append(iou[j, maxiou_class[j]]) if gt_class_ids[j] == class_ids[maxiou_class[j]]: tp += 1 else: fp += 1 fp += abs(len(class_ids) - len(gt_class_ids)) print("max ious: ", s) s = np.sum(s) s_iou += s print("pred:", class_ids, boxes) print("--------------") # i += 1 #if i==10: # break print("sum:", s_iou) print("num:", n_iou) print("mIoU:", s_iou / n_iou) print("tp:", tp) print("fp:", fp) print("precision:", tp / (tp + fp))