Example #1
0
def get_map(pred, gt, f):
    T = {}
    P = {}
    fx, fy = f
    for bbox in gt:
        bbox['bbox_matched'] = False

    pred_probs = np.array(s['prob'] for s in pred)
    box_idx_sorted_by_prob = np.argsort(pred_probs)[::-1]

    for box_idx in box_idx_sorted_by_prob:
        pred_box = pred[box_idx]
        pred_class = pred_box['class']
        pred_x1 = pred_box['x1']
        pred_x2 = pred_box['x2']
        pred_y1 = pred_box['y1']
        pred_y2 = pred_box['y2']
        pred_prob = pred_box['prob']
        if pred_class not in P:
            P[pred_class] = []
            T[pred_class] = []
        P[pred_class].append(pred_prob)
        found_match = False

        for gt_box in gt:
            gt_class = gt_box['class']
            gt_x1 = gt_box['x1'] / fx
            gt_x2 = gt_box['x2'] / fx
            gt_y1 = gt_box['y1'] / fy
            gt_y2 = gt_box['y2'] / fy
            gt_seen = gt_box['bbox_matched']
            if gt_class != pred_class:
                continue
            if gt_seen:
                continue
            iou = data_generators.iou((pred_x1, pred_y1, pred_x2, pred_y2),
                                      (gt_x1, gt_y1, gt_x2, gt_y2))
            if iou >= 0.5:
                found_match = True
                gt_box['bbox_matched'] = True
                break
            else:
                continue
        T[pred_class].append(int(found_match))

    for gt_box in gt:
        if not gt_box['bbox_matched'] and not gt_box['difficult']:
            if gt_box['class'] not in P:
                P[gt_box['class']] = []
                T[gt_box['class']] = []
            T[gt_box['class']].append(1)
            P[gt_box['class']].append(0)
    return T, P
Example #2
0
def calc_iou(R, img_data, C, class_mapping):

    bboxes = img_data['bboxes']
    (width, height) = (img_data['width'], img_data['height'])
    # get image dimensions for resizing
    (resized_width, resized_height) = data_generators.get_new_img_size(
        width, height, C.im_size)

    gta = np.zeros((len(bboxes), 4))

    for bbox_num, bbox in enumerate(bboxes):
        # get the GT box coordinates, and resize to account for image resizing
        gta[bbox_num, 0] = int(
            round(bbox['x1'] * (resized_width / float(width)) / C.rpn_stride))
        gta[bbox_num, 1] = int(
            round(bbox['x2'] * (resized_width / float(width)) / C.rpn_stride))
        gta[bbox_num, 2] = int(
            round(bbox['y1'] * (resized_height / float(height)) /
                  C.rpn_stride))
        gta[bbox_num, 3] = int(
            round(bbox['y2'] * (resized_height / float(height)) /
                  C.rpn_stride))

    x_roi = []
    y_class_num = []
    y_class_regr_coords = []
    y_class_regr_label = []
    IoUs = []  # for debugging only

    for ix in range(R.shape[0]):
        (x1, y1, x2, y2) = R[ix, :]
        x1 = int(round(x1))
        y1 = int(round(y1))
        x2 = int(round(x2))
        y2 = int(round(y2))

        best_iou = 0.0
        best_bbox = -1
        for bbox_num in range(len(bboxes)):
            curr_iou = data_generators.iou([
                gta[bbox_num, 0], gta[bbox_num, 2], gta[bbox_num, 1],
                gta[bbox_num, 3]
            ], [x1, y1, x2, y2])
            if curr_iou > best_iou:
                best_iou = curr_iou
                best_bbox = bbox_num

        if best_iou < C.classifier_min_overlap:
            continue
        else:
            w = x2 - x1
            h = y2 - y1
            x_roi.append([x1, y1, w, h])
            IoUs.append(best_iou)

            if C.classifier_min_overlap <= best_iou < C.classifier_max_overlap:
                # hard negative example
                cls_name = 'bg'
            elif C.classifier_max_overlap <= best_iou:
                cls_name = bboxes[best_bbox]['class']
                cxg = (gta[best_bbox, 0] + gta[best_bbox, 1]) / 2.0
                cyg = (gta[best_bbox, 2] + gta[best_bbox, 3]) / 2.0

                cx = x1 + w / 2.0
                cy = y1 + h / 2.0

                tx = (cxg - cx) / float(w)
                ty = (cyg - cy) / float(h)
                tw = np.log((gta[best_bbox, 1] - gta[best_bbox, 0]) / float(w))
                th = np.log((gta[best_bbox, 3] - gta[best_bbox, 2]) / float(h))
            else:
                print('roi = {}'.format(best_iou))
                raise RuntimeError

        class_num = class_mapping[cls_name]
        class_label = len(class_mapping) * [0]
        class_label[class_num] = 1
        y_class_num.append(copy.deepcopy(class_label))
        coords = [0] * 4 * (len(class_mapping) - 1)
        labels = [0] * 4 * (len(class_mapping) - 1)
        if cls_name != 'bg':
            label_pos = 4 * class_num
            sx, sy, sw, sh = C.classifier_regr_std
            coords[label_pos:4 +
                   label_pos] = [sx * tx, sy * ty, sw * tw, sh * th]
            labels[label_pos:4 + label_pos] = [1, 1, 1, 1]
            y_class_regr_coords.append(copy.deepcopy(coords))
            y_class_regr_label.append(copy.deepcopy(labels))
        else:
            y_class_regr_coords.append(copy.deepcopy(coords))
            y_class_regr_label.append(copy.deepcopy(labels))

    if len(x_roi) == 0:
        return None, None, None, None

    X = np.array(x_roi)
    Y1 = np.array(y_class_num)
    Y2 = np.concatenate(
        [np.array(y_class_regr_label),
         np.array(y_class_regr_coords)], axis=1)

    return np.expand_dims(X, axis=0), np.expand_dims(
        Y1, axis=0), np.expand_dims(Y2, axis=0), IoUs
Example #3
0
def calc_iou(R, img_data, C,class_mapping):
	# R = (boxes, probs)

	bboxes = img_data['bboxes'] # all the ground truthbboxes of one image
	(width, height) = (img_data['width'], img_data['height'])
	# get image dimensions for resizing
	(resized_width, resized_height) = data_generators.get_new_img_size(width, height, C.im_size)

	gta = np.zeros((len(bboxes), 4))

	# Transform all the landmars into the resized image frame
	resize_ratio = (resized_width / float(width))/float(C.rpn_stride)
	sx, sy, sw, sh = C.classifier_regr_std

	for bbox_num, bbox in enumerate(bboxes):
		# get the GT box coordinates, and resize to account for image resizing
		gta[bbox_num, 0] = int(round(bbox['x1'] * (resized_width / float(width))/C.rpn_stride))
		gta[bbox_num, 1] = int(round(bbox['x2'] * (resized_width / float(width))/C.rpn_stride))
		gta[bbox_num, 2] = int(round(bbox['y1'] * (resized_height / float(height))/C.rpn_stride))
		gta[bbox_num, 3] = int(round(bbox['y2'] * (resized_height / float(height))/C.rpn_stride))

	x_roi = []
	y_class_num = []
	y_class_regr_coords = []
	y_class_regr_label = []
	y_gender = []
	y_pose = []
	y_viz = []
	y_landmark = []
	

	# For each predicted box find the gt box that best overlaps above a threshold iou
	for ix in range(R.shape[0]):  # R.shape[0] = numboxes?
		# bp()
		(x1, y1, x2, y2) = R[ix, :]
		x1 = int(round(x1))
		y1 = int(round(y1))
		x2 = int(round(x2))
		y2 = int(round(y2))

		best_iou = 0.0
		best_bbox = -1
		# For the predicted(rpn) box <- iterate over all the ground truth box to find the best box
		for bbox_num in range(len(bboxes)):
			curr_iou = data_generators.iou([gta[bbox_num, 0], gta[bbox_num, 2], gta[bbox_num, 1], gta[bbox_num, 3]], [x1, y1, x2, y2])
			if curr_iou > best_iou:
				best_iou = curr_iou
				best_bbox = bbox_num

		if best_iou < C.classifier_min_overlap:
				continue
		else:
			w = x2 - x1
			h = y2 - y1
			x_roi.append([x1, y1, w, h])
			pose_label = [0,0,0]
			gender_label = [0,0]
			viz_label = np.zeros(21)
			landmark_label = np.zeros(42)
			if C.classifier_min_overlap <= best_iou < C.classifier_max_overlap:
				# hard negative example
				cls_name = 'bg'
				class_label = [0,1]
			elif C.classifier_max_overlap <= best_iou:
				cls_name = 'face'
				class_label = [1,0]
				pose_label = [ bbox['roll'],bbox['pitch'],bbox['yaw'] ]
				gender_label[ int(bbox['sex']=='f') ] = 1
				

				cxg = (gta[best_bbox, 0] + gta[best_bbox, 1]) / 2.0
				cyg = (gta[best_bbox, 2] + gta[best_bbox, 3]) / 2.0

				cx = x1 + w / 2.0
				cy = y1 + h / 2.0

				tx = (cxg - cx) / float(w)
				ty = (cyg - cy) / float(h)
				tw = np.log((gta[best_bbox, 1] - gta[best_bbox, 0]) / float(w))
				th = np.log((gta[best_bbox, 3] - gta[best_bbox, 2]) / float(h))

				#************ CHECK ************#
				viz_label  = bbox['feature_visible'] # Assuming a list of 21 ints

				# bp()
				trans_x = [sx*(xi - cx)*1./w for xi in (bbox['feature_x'] * resize_ratio) ]	# Transform the landmark coordinate from the original image to the feature map
				trans_y = [sy*(yi - cy)*1./h for yi in (bbox['feature_y'] * resize_ratio) ] 
				# bp()
			else:
				print('roi = {}'.format(best_iou))
				raise RuntimeError

		coords = [0] * 4
		landmark_label = [0] * 42

		y_class_num.append(copy.deepcopy(class_label))
		y_gender.append(copy.deepcopy(gender_label))
		y_pose.append(copy.deepcopy(pose_label))
		y_viz.append(copy.deepcopy(viz_label))
		# labels = [0] * 4 * (len(class_mapping) - 1)

		if cls_name != 'bg':
			# sx, sy, sw, sh = C.classifier_regr_std
			coords = [sx*tx, sy*ty, sw*tw, sh*th]
			y_class_regr_coords.append(copy.deepcopy(coords))

			landmark_label = trans_x + trans_y     # Assuming both are lists of 21 ints each
			y_landmark.append(copy.deepcopy(landmark_label))

		else:
			y_class_regr_coords.append(copy.deepcopy(coords))
			y_landmark.append(copy.deepcopy(landmark_label))


	if len(x_roi) == 0:
		return None, None, None

	X = np.array(x_roi)
	# Y1 = np.array(y_class_num)
	# Y2 = np.concatenate([np.array(y_class_regr_label),np.array(y_class_regr_coords)],axis=1)
	Y = np.concatenate([np.array(y_class_num), np.array(y_pose), np.array(y_gender), np.array(y_viz), np.array(y_landmark), np.array(y_class_regr_coords)],axis=-1)

	return[np.expand_dims(X, axis=0), np.expand_dims(Y, axis=0)]
Example #4
0
def calc_iou(R, img_data, C, class_mapping):

	bboxes = img_data['bboxes']
	(width, height) = (img_data['width'], img_data['height'])
	# get image dimensions for resizing
	(resized_width, resized_height) = data_generators.get_new_img_size(width, height, C.im_size)

	gta = np.zeros((len(bboxes), 4))

	for bbox_num, bbox in enumerate(bboxes):
		# get the GT box coordinates, and resize to account for image resizing
		gta[bbox_num, 0] = int(round(bbox['x1'] * (resized_width / float(width))/C.rpn_stride))
		gta[bbox_num, 1] = int(round(bbox['x2'] * (resized_width / float(width))/C.rpn_stride))
		gta[bbox_num, 2] = int(round(bbox['y1'] * (resized_height / float(height))/C.rpn_stride))
		gta[bbox_num, 3] = int(round(bbox['y2'] * (resized_height / float(height))/C.rpn_stride))

	x_roi = []
	y_class_num = []
	y_class_regr_coords = []
	y_class_regr_label = []

	for ix in range(R.shape[0]):
		(x1, y1, x2, y2) = R[ix, :]
		x1 = int(round(x1))
		y1 = int(round(y1))
		x2 = int(round(x2))
		y2 = int(round(y2))

		best_iou = 0.0
		best_bbox = -1
		for bbox_num in range(len(bboxes)):
			curr_iou = data_generators.iou([gta[bbox_num, 0], gta[bbox_num, 2], gta[bbox_num, 1], gta[bbox_num, 3]], [x1, y1, x2, y2])
			if curr_iou > best_iou:
				best_iou = curr_iou
				best_bbox = bbox_num

		if best_iou < C.classifier_min_overlap:
				continue
		else:
			w = x2 - x1
			h = y2 - y1
			x_roi.append([x1, y1, w, h])

			if C.classifier_min_overlap <= best_iou < C.classifier_max_overlap:
				# hard negative example
				cls_name = 'bg'
			elif C.classifier_max_overlap <= best_iou:
				cls_name = bboxes[best_bbox]['class']
				cxg = (gta[best_bbox, 0] + gta[best_bbox, 1]) / 2.0
				cyg = (gta[best_bbox, 2] + gta[best_bbox, 3]) / 2.0

				cx = x1 + w / 2.0
				cy = y1 + h / 2.0

				tx = (cxg - cx) / float(w)
				ty = (cyg - cy) / float(h)
				tw = np.log((gta[best_bbox, 1] - gta[best_bbox, 0]) / float(w))
				th = np.log((gta[best_bbox, 3] - gta[best_bbox, 2]) / float(h))
			else:
				print('roi = {}'.format(best_iou))
				raise RuntimeError

		class_num = class_mapping[cls_name]
		class_label = len(class_mapping) * [0]
		class_label[class_num] = 1
		y_class_num.append(copy.deepcopy(class_label))
		coords = [0] * 4 * (len(class_mapping) - 1)
		labels = [0] * 4 * (len(class_mapping) - 1)
		if cls_name != 'bg':
			label_pos = 4 * class_num
			sx, sy, sw, sh = C.classifier_regr_std
			coords[label_pos:4+label_pos] = [sx*tx, sy*ty, sw*tw, sh*th]
			labels[label_pos:4+label_pos] = [1, 1, 1, 1]
			y_class_regr_coords.append(copy.deepcopy(coords))
			y_class_regr_label.append(copy.deepcopy(labels))
		else:
			y_class_regr_coords.append(copy.deepcopy(coords))
			y_class_regr_label.append(copy.deepcopy(labels))

	if len(x_roi) == 0:
		return None, None, None

	X = np.array(x_roi)
	Y1 = np.array(y_class_num)
	Y2 = np.concatenate([np.array(y_class_regr_label),np.array(y_class_regr_coords)],axis=1)

	return np.expand_dims(X, axis=0), np.expand_dims(Y1, axis=0), np.expand_dims(Y2, axis=0)
Example #5
0
def calc_iou(R, img_data, C, class_mapping):
    """Converts from (x1,y1,x2,y2) to (x,y,w,h) format

    Args:
        R: bboxes, probs
    """
    bboxes = img_data['bboxes']
    (width, height) = (img_data['width'], img_data['height'])
    # get image dimensions for resizing
    (resized_width, resized_height) = get_new_img_size(width, height,
                                                       C.im_size)

    gta = np.zeros((len(bboxes), 4))

    for bbox_num, bbox in enumerate(bboxes):
        # get the GT box coordinates, and resize to account for image resizing
        # gta[bbox_num, 0] = (40 * (600 / 800)) / 16 = int(round(1.875)) = 2 (x in feature map)
        gta[bbox_num, 0] = int(
            round(bbox['x1'] * (resized_width / float(width)) / C.rpn_stride))
        gta[bbox_num, 1] = int(
            round(bbox['x2'] * (resized_width / float(width)) / C.rpn_stride))
        gta[bbox_num, 2] = int(
            round(bbox['y1'] * (resized_height / float(height)) /
                  C.rpn_stride))
        gta[bbox_num, 3] = int(
            round(bbox['y2'] * (resized_height / float(height)) /
                  C.rpn_stride))

    x_roi = []
    y_class_num = []
    y_class_regr_coords = []
    y_class_regr_label = []
    IoUs = []  # for debugging only

    # R.shape[0]: number of bboxes (=300 from non_max_suppression)
    for ix in range(R.shape[0]):
        (x1, y1, x2, y2) = R[ix, :]
        x1 = int(round(x1))
        y1 = int(round(y1))
        x2 = int(round(x2))
        y2 = int(round(y2))

        best_iou = 0.0
        best_bbox = -1
        # Iterate through all the ground-truth bboxes to calculate the iou
        for bbox_num in range(len(bboxes)):
            curr_iou = iou([
                gta[bbox_num, 0], gta[bbox_num, 2], gta[bbox_num, 1],
                gta[bbox_num, 3]
            ], [x1, y1, x2, y2])

            # Find out the corresponding ground-truth bbox_num with larget iou
            if curr_iou > best_iou:
                best_iou = curr_iou
                best_bbox = bbox_num

        if best_iou < C.classifier_min_overlap:
            continue
        else:
            w = x2 - x1
            h = y2 - y1
            x_roi.append([x1, y1, w, h])
            IoUs.append(best_iou)

            if C.classifier_min_overlap <= best_iou < C.classifier_max_overlap:
                # hard negative example
                cls_name = 'bg'
            elif C.classifier_max_overlap <= best_iou:
                cls_name = bboxes[best_bbox]['class']
                cxg = (gta[best_bbox, 0] + gta[best_bbox, 1]) / 2.0
                cyg = (gta[best_bbox, 2] + gta[best_bbox, 3]) / 2.0

                cx = x1 + w / 2.0
                cy = y1 + h / 2.0

                tx = (cxg - cx) / float(w)
                ty = (cyg - cy) / float(h)
                tw = np.log((gta[best_bbox, 1] - gta[best_bbox, 0]) / float(w))
                th = np.log((gta[best_bbox, 3] - gta[best_bbox, 2]) / float(h))
            else:
                print('roi = {}'.format(best_iou))
                raise RuntimeError

        class_num = class_mapping[cls_name]
        class_label = len(class_mapping) * [0]
        class_label[class_num] = 1
        y_class_num.append(copy.deepcopy(class_label))
        coords = [0] * 4 * (len(class_mapping) - 1)
        labels = [0] * 4 * (len(class_mapping) - 1)
        if cls_name != 'bg':
            label_pos = 4 * class_num
            sx, sy, sw, sh = C.classifier_regr_std
            coords[label_pos:4 +
                   label_pos] = [sx * tx, sy * ty, sw * tw, sh * th]
            labels[label_pos:4 + label_pos] = [1, 1, 1, 1]
            y_class_regr_coords.append(copy.deepcopy(coords))
            y_class_regr_label.append(copy.deepcopy(labels))
        else:
            y_class_regr_coords.append(copy.deepcopy(coords))
            y_class_regr_label.append(copy.deepcopy(labels))

    if len(x_roi) == 0:
        return None, None, None, None

    # bboxes that iou > C.classifier_min_overlap for all gt bboxes in 300 non_max_suppression bboxes
    X = np.array(x_roi)
    # one hot code for bboxes from above => x_roi (X)
    Y1 = np.array(y_class_num)
    # corresponding labels and corresponding gt bboxes
    Y2 = np.concatenate(
        [np.array(y_class_regr_label),
         np.array(y_class_regr_coords)], axis=1)

    return np.expand_dims(X, axis=0), np.expand_dims(
        Y1, axis=0), np.expand_dims(Y2, axis=0), IoUs
Example #6
0
def calc_iou(R,img_data,C, class_mapping):
    bboxes = img_data['bboxes']
    (width,height) = (img_data['width'],img_data['height'])
    #get image dimensions for resizing
    (resized_width,resized_height) = data_generators.get_new_img_size(width,height,C.im_size)

    gta = np.zeros((len(bboxes),4))

    for bbox_num,bbox in enumerate(bboxes):
        # get the GT box coordinates, and resize to account for image resizing
        gta[bbox_num, 0] = int(round(bbox['x1'] * (resized_width / float(width)) / C.rpn_stride ))
        gta[bbox_num, 1] = int(round(bbox['x2'] * (resized_width / float(width)) / C.rpn_stride ))
        gta[bbox_num, 2] = int(round(bbox['y1'] * (resized_height / float(height)) / C.rpn_stride))
        gta[bbox_num, 3] = int(round(bbox['y2'] * (resized_height / float(height)) / C.rpn_stride))

    x_roi = []
    y_class_num = []
    y_class_regr_coords = []
    y_class_regr_label = []
    IoUs = [] #for debugging only

    for ix in range(R.shape[0]):
        (x1,y1,x2,y2) = R[ix,:]
        x1 = int(round(x1))
        y1 = int(round(y1))
        x2 = int(round(x2))
        y2 = int(round(y2))

        best_iou =0.0
        best_bbox = -1
        for bbox_num in range(len(bboxes)):
            curr_iou = data_generators.iou([gta[bbox_num, 0],gta[bbox_num, 2],gta[bbox_num, 1],gta[bbox_num, 3]],
                                           [x1,y1,x2,y2])
            if curr_iou>best_iou:
                best_iou=curr_iou
                best_bbox = bbox_num

        if best_iou < C.classifier_min_overlap:
            continue
        else:
            w = x2-x1
            h = y2-y1
            x_roi.append([x1,y1,w,h])
            IoUs.append(best_iou)
            if C.classifier_min_overlap <=best_iou < C.classifier_max_overlap:
                cls_name = 'bg'
            elif C.classifier_max_overlap <=best_iou:
                cls_name = bboxes[best_bbox]['class']
                cxg = (gta[best_bbox,0]+gta[best_bbox,1])/2.0
                cyg = (gta[best_bbox, 2] + gta[best_bbox, 3]) / 2.0
                cx = x1+w/2.0
                cy = y1+h/2.0

                tx = (cxg-cx)/float(w)
                ty = (cyg-cy)/float(h)
                tw = np.log((gta[best_bbox,1]-gta[best_bbox,0])/float(w))
                th = np.log((gta[best_bbox, 3] - gta[best_bbox, 2]) / float(h))
            else:
                print('roi={}'.format(best_iou))
                raise RuntimeError

        class_num = class_mapping[cls_name]
        class_label = len(class_mapping)*[0]
        class_label[class_num] = 1
        y_class_num.append(copy.deepcopy(class_label))
Example #7
0
def get_map(pred, gt, iou_threshold=0.5):
	T = {}
	P = {}

	true_positives = 0

	predicted_count = len(pred)
	gt_count = len(gt)

	for bbox in gt:
		bbox['bbox_matched'] = False

	pred_probs = np.array([s['prob'] for s in pred])
	box_idx_sorted_by_prob = np.argsort(pred_probs)[::-1]

	# For each predicted box sorted by probabilty
	for box_idx in box_idx_sorted_by_prob:

		# Get bbox predicted data
		pred_box = pred[box_idx]
		pred_class = pred_box['class']
		pred_x1 = pred_box['x1']
		pred_x2 = pred_box['x2']
		pred_y1 = pred_box['y1']
		pred_y2 = pred_box['y2']
		pred_prob = pred_box['prob']

		# If class was not predicted yet, create a new list for that class
		if pred_class not in P:
			P[pred_class] = []
			T[pred_class] = []

		# Add the predicted probability to P
		P[pred_class].append(pred_prob)

		# Set found match to False, meaning the prediction was not found yet on GT
		found_match = False

		# For each bbox in GT
		for gt_box in gt:

			# Get GT bbox data
			gt_class = gt_box['class']
			gt_x1 = gt_box['x1']
			gt_x2 = gt_box['x2']
			gt_y1 = gt_box['y1']
			gt_y2 = gt_box['y2']
			gt_seen = gt_box['bbox_matched']

			# If the class does not match the predicted class, continue
			if gt_class != pred_class:
				continue

			# If the bbox was already matched, continue
			if gt_seen:
				continue

			# Else, check if IoU is greater than a value
			iou = data_generators.iou((pred_x1, pred_y1, pred_x2, pred_y2), (gt_x1, gt_y1, gt_x2, gt_y2))
			if iou >= iou_threshold:
				# Set found_patch and bbox matched to true
				found_match = True
				gt_box['bbox_matched'] = True

				# Add one to the true positives
				true_positives += 1

				break
			else:
				continue

		T[pred_class].append(int(found_match))

	for gt_box in gt:
		#if not gt_box['bbox_matched'] and not gt_box['difficult']:
		if not gt_box['bbox_matched']:
			if gt_box['class'] not in P:
				P[gt_box['class']] = []
				T[gt_box['class']] = []

			T[gt_box['class']].append(1)
			P[gt_box['class']].append(0)

	#import pdb
	#pdb.set_trace()

	false_positives = predicted_count - true_positives
	false_negatives = gt_count - true_positives

	return T, P, true_positives, false_positives, false_negatives
Example #8
0
def calc_iou(R, img_data, C, class_mapping):

    bboxes = img_data['bboxes']
    (width, height) = (img_data['width'], img_data['height'])

    # Resizing v. Image -> Mindestgröße kleinerer Kante (px = 600)!
    (resized_width,
     resized_height) = data_augment.get_new_img_size(width, height, C.im_size)

    # Initialisierung GT Box-Matrix mit Koordinaten
    gta = np.zeros((len(bboxes), 4))

    for bbox_num, bbox in enumerate(bboxes):

        # Abbildung Eck-Koordinaten GT Box auf Feature-Map -> Resize & Stride (r = 16)
        gta[bbox_num, 0] = int(
            round(bbox['x1'] * (resized_width / float(width)) /
                  C.rpn_stride))  # x-Koordinate
        gta[bbox_num, 1] = int(
            round(bbox['x2'] * (resized_width / float(width)) /
                  C.rpn_stride))  # x-Koordinate
        gta[bbox_num, 2] = int(
            round(bbox['y1'] * (resized_height / float(height)) /
                  C.rpn_stride))  # y-Koordinate
        gta[bbox_num, 3] = int(
            round(bbox['y2'] * (resized_height / float(height)) /
                  C.rpn_stride))  # y-Koordinate

    # Initialisierung Listen
    x_roi = []
    y_class_num = []
    y_class_regr_coords = []
    y_class_regr_label = []
    IoUs = []

    # Für jede ROI (Region-Proposal) im Image ...
    for ix in range(R.shape[0]):

        # Initialisierung Eck-Koordinaten mit Inhalt ROI-Matrix
        (x1, y1, x2, y2) = R[ix, :]

        x1 = int(round(x1))
        y1 = int(round(y1))
        x2 = int(round(x2))
        y2 = int(round(y2))

        best_iou = 0.0
        best_bbox = -1

        # Für jede Ground-Truth Box im Image ...
        for bbox_num in range(len(bboxes)):

            # IOU (Ground-Truth Box & ROI im Image)
            curr_iou = data_generators.iou([
                gta[bbox_num, 0], gta[bbox_num, 2], gta[bbox_num, 1],
                gta[bbox_num, 3]
            ], [x1, y1, x2, y2])

            # Falls IOU v. GT Box & ROI größer als momentane IOU ist ...
            if curr_iou > best_iou:

                best_iou = curr_iou  # Initialisierung Variable mit Wert
                best_bbox = bbox_num

        # Falls IOU v. ROI mit Ground-Truth Box kleiner als Mindestgrenzwert ist -> nächste ROI
        if best_iou < C.classifier_min_overlap:
            continue

        else:
            w = x2 - x1  # ROI-Weite
            h = y2 - y1  # ROI-Höhe

            x_roi.append(
                [x1, y1, w,
                 h])  # Einfügen ROI-Werte (Ecke, Weite, Höhe) in ROI-Liste
            IoUs.append(
                best_iou)  # Einfügen IOU v. ROI mit GT Box in IOU-Liste

            # Falls IOU < 0.5 & IOU >= 0.1 ist ...
            if C.classifier_min_overlap <= best_iou < C.classifier_max_overlap:
                cls_name = 'bg'

            # Falls IOU >= 0.5 ist ...
            elif C.classifier_max_overlap <= best_iou:
                cls_name = bboxes[best_bbox][
                    'class']  # ROI erhält Klassen-Name v. GT Box

                cxg = (gta[best_bbox, 0] +
                       gta[best_bbox, 1]) / 2.0  # x-Koordinate Zentrum GT Box
                cyg = (gta[best_bbox, 2] +
                       gta[best_bbox, 3]) / 2.0  # y-Koordinate Zentrum GT Box

                cx = x1 + w / 2.0  # x-Koordinate Zentrum ROI
                cy = y1 + h / 2.0  # y-Koordinate Zentrum ROI

                # Regressions-Variablen für Regressions-Kostenfunktion
                tx = (cxg - cx) / float(w)
                ty = (cyg - cy) / float(h)
                tw = np.log((gta[best_bbox, 1] - gta[best_bbox, 0]) / float(w))
                th = np.log((gta[best_bbox, 3] - gta[best_bbox, 2]) / float(h))

            else:
                print('roi = {}'.format(best_iou))
                raise RuntimeError

        class_num = class_mapping[
            cls_name]  # Klassen-Nummer         -> Abbildung Klassen-Name
        class_label = len(class_mapping) * [
            0
        ]  # 0-Vektor (Klasse)      -> Länge Klassen-Anzahl
        class_label[
            class_num] = 1  # Vektor wird an Klassen-Nummer auf 1 gesetzt

        y_class_num.append(
            copy.deepcopy(class_label))  # Einfügen Vektor in Klassen-Liste

        coords = [0] * 4 * (len(class_mapping) - 1
                            )  # 0-Vektor (Regressions-Variable)
        labels = [0] * 4 * (len(class_mapping) - 1
                            )  # 0-Vektor (Labels) -> jede Klasse 4 Labels

        # Falls ROI kein Background darstellt ...
        if cls_name != 'bg':
            label_pos = 4 * class_num  # Einstellung Label-Position
            sx, sy, sw, sh = C.classifier_regr_std

            coords[label_pos:(4 + label_pos)] = [
                (sx * tx), (sy * ty), (sw * tw), (sh * th)
            ]  # Visualisierung Regressions-Variable in Vektor -> Label-Position
            labels[label_pos:(4 + label_pos)] = [
                1, 1, 1, 1
            ]  # Visualisierung Label in Vektor		 -> Label-Position

            y_class_regr_coords.append(
                copy.deepcopy(coords))  # Einfügen Regressions-Vektor in Liste
            y_class_regr_label.append(
                copy.deepcopy(labels))  # Einfügen Label-Vektor in Liste

        # Falls ROI Background darstellt ...
        else:
            y_class_regr_coords.append(copy.deepcopy(coords))
            y_class_regr_label.append(copy.deepcopy(labels))

    # Falls ROI-Liste keine Elemente enthält ...
    if len(x_roi) == 0:
        return None, None, None, None

    # Erstellung Arrays aus ROI-Liste & Klassen-Liste
    X = np.array(x_roi)
    Y1 = np.array(y_class_num)

    Y2 = np.concatenate(
        [np.array(y_class_regr_label),
         np.array(y_class_regr_coords)], axis=1)

    return np.expand_dims(X, axis=0), np.expand_dims(
        Y1, axis=0), np.expand_dims(Y2, axis=0), IoUs