Esempio n. 1
0
def getProcessedBoxes(self, net_out):
	"""
	Takes net output, draw net_out, save to disk
	"""
	# meta
	meta = self.meta
	H, W, _ = meta['out_size']
	C, B = meta['classes'], meta['num']
	anchors = meta['anchors']
	threshold = self.FLAGS.threshold
	max_iou = self.FLAGS.slice_max_iou
	net_out = net_out.reshape([H, W, B, -1])

	boxes = list()
	for row in range(H):
		for col in range(W):
			for b in range(B):
				bx = BoundBox(C)
				bx.x, bx.y, bx.w, bx.h, bx.c = net_out[row, col, b, :5]

				#print(bx.x, bx.y, bx.w, bx.h, bx.c)

				bx.c = expit(bx.c)
				bx.x = (col + expit(bx.x)) / W
				bx.y = (row + expit(bx.y)) / H
				bx.w = math.exp(bx.w) * anchors[2 * b + 0] / W
				bx.h = math.exp(bx.h) * anchors[2 * b + 1] / H
				classes = net_out[row, col, b, 5:]
				bx.probs = _softmax(classes) * bx.c
				bx.probs *= bx.probs > threshold
				boxes.append(bx)

	for c in range(C):
		for i in range(len(boxes)):
			max_indx = np.argmax(boxes[i].probs)
			boxes[i].class_num = max_indx
	#	boxes = sorted(boxes, key = prob_compare)
	# 	for i in range(len(boxes)):
	# 		boxi = boxes[i]
	# 		if boxi.probs[c] == 0: continue
	# 		for j in range(i + 1, len(boxes)):
	# 			boxj = boxes[j]
	# 			if box_iou(boxi, boxj) >= max_iou:
	# 				boxes[j].probs[c] = 0.

	return boxes
Esempio n. 2
0
def findboxes(self, net_out):
    meta, FLAGS = self.meta, self.FLAGS
    threshold, sqrt = FLAGS.threshold, meta['sqrt'] + 1
    C, B, S = meta['classes'], meta['num'], meta['side']

    boxes = []
    SS = S * S  # number of grid cells
    prob_size = SS * C  # class probabilities
    conf_size = SS * B  # confidences for each grid cell

    # net_out = net_out[0]
    probs = net_out[0:prob_size]
    confs = net_out[prob_size:(prob_size + conf_size)]
    cords = net_out[(prob_size + conf_size):]
    probs = probs.reshape([SS, C])
    confs = confs.reshape([SS, B])
    cords = cords.reshape([SS, B, 4])

    for grid in range(SS):
        for b in range(B):
            bx = BoundBox(C)
            bx.c = confs[grid, b]
            bx.x = (cords[grid, b, 0] + grid % S) / S
            bx.y = (cords[grid, b, 1] + grid // S) / S
            bx.w = cords[grid, b, 2]**sqrt
            bx.h = cords[grid, b, 3]**sqrt
            p = probs[grid, :] * bx.c
            p *= (p > threshold)
            bx.probs = p
            boxes.append(bx)

    # non max suppress boxes
    for c in range(C):
        for i in range(len(boxes)):
            boxes[i].class_num = c
        boxes = sorted(boxes, key=prob_compare, reverse=True)
        for i in range(len(boxes)):
            boxi = boxes[i]
            if boxi.probs[c] == 0: continue
            for j in range(i + 1, len(boxes)):
                boxj = boxes[j]
                if box_iou(boxi, boxj) >= .4:
                    boxes[j].probs[c] = 0.
    return boxes
Esempio n. 3
0
def findboxes(self, net_out):
    # meta
    meta = self.meta
    H, W, _ = meta['out_size']
    threshold = meta['thresh']
    C, B = meta['classes'], meta['num']
    anchors = meta['anchors']
    net_out = net_out.reshape([H, W, B, -1])

    boxes = list()
    for row in range(H):
        for col in range(W):
            for b in range(B):
                bx = BoundBox(C)
                bx.x, bx.y, bx.w, bx.h, bx.c = net_out[row, col, b, :5]
                bx.c = expit(bx.c)
                bx.x = (col + expit(bx.x)) / W
                bx.y = (row + expit(bx.y)) / H
                bx.w = math.exp(bx.w) * anchors[2 * b + 0] / W
                bx.h = math.exp(bx.h) * anchors[2 * b + 1] / H
                classes = net_out[row, col, b, 5:]
                bx.probs = _softmax(classes) * bx.c
                bx.probs *= bx.probs > threshold
                boxes.append(bx)

    # non max suppress boxes
    for c in range(C):
        for i in range(len(boxes)):
            boxes[i].class_num = c
        boxes = sorted(boxes, key=prob_compare, reverse=True)
        for i in range(len(boxes)):
            boxi = boxes[i]
            if boxi.probs[c] == 0: continue
            for j in range(i + 1, len(boxes)):
                boxj = boxes[j]
                if box_iou(boxi, boxj) >= .4:
                    boxes[j].probs[c] = 0.
    return boxes
Esempio n. 4
0
def postprocess(self, net_out, im, save = True):
	"""
	Takes net output, draw net_out, save to disk
	"""
	# meta
	meta = self.meta
	H, W, _ = meta['out_size']
	threshold = meta['thresh']
	C, B = meta['classes'], meta['num']
	anchors = meta['anchors']
	net_out = net_out.reshape([H, W, B, -1])

	boxes = list()
	for row in range(H):
		for col in range(W):
			for b in range(B):
				bx = BoundBox(C)
				bx.x, bx.y, bx.w, bx.h, bx.c = net_out[row, col, b, :5]
				bx.c = expit(bx.c)
				bx.x = (col + expit(bx.x)) / W
				bx.y = (row + expit(bx.y)) / H
				bx.w = math.exp(bx.w) * anchors[2 * b + 0] / W
				bx.h = math.exp(bx.h) * anchors[2 * b + 1] / H
				classes = net_out[row, col, b, 5:]
				bx.probs = _softmax(classes) * bx.c
				bx.probs *= bx.probs > threshold
				boxes.append(bx)

	# non max suppress boxes
	for c in range(C):
		for i in range(len(boxes)):
			boxes[i].class_num = c
		boxes = sorted(boxes, key = prob_compare, reverse = True)
		for i in range(len(boxes)):
			boxi = boxes[i]
			if boxi.probs[c] == 0: continue
			for j in range(i + 1, len(boxes)):
				boxj = boxes[j]
				if box_iou(boxi, boxj) >= .4:
					boxes[j].probs[c] = 0.


	colors = meta['colors']
	labels = meta['labels']
	if type(im) is not np.ndarray:
		imgcv = cv2.imread(im)
	else: imgcv = im
	h, w, _ = imgcv.shape
	textBuff = "["
	for b in boxes:
		max_indx = np.argmax(b.probs)
		max_prob = b.probs[max_indx]
		label = labels[max_indx]
		if max_prob > threshold:
			left  = int ((b.x - b.w/2.) * w)
			right = int ((b.x + b.w/2.) * w)
			top   = int ((b.y - b.h/2.) * h)
			bot   = int ((b.y + b.h/2.) * h)
			if left  < 0    :  left = 0
			if right > w - 1: right = w - 1
			if top   < 0    :   top = 0
			if bot   > h - 1:   bot = h - 1
			thick = int((h+w)/300)
			mess = '{}'.format(label)
			if self.FLAGS.json:
				line = 	('{"label":"%s",'
						'"topleft":{"x":%d,"y":%d},'
						'"bottomright":{"x":%d,"y":%d}},\n') % \
						(mess, left, top, right, bot)
				textBuff += line
				continue

			cv2.rectangle(imgcv,
				(left, top), (right, bot),
				colors[max_indx], thick)
			cv2.putText(imgcv, mess, (left, top - 12),
				0, 1e-3 * h, colors[max_indx],thick//3)

	# Removing trailing comma+newline adding json list terminator.
	textBuff = textBuff[:-2] + "]"
	outfolder = os.path.join(self.FLAGS.test, 'out')
	img_name = os.path.join(outfolder, im.split('/')[-1])
	if self.FLAGS.json:
		textFile = os.path.splitext(img_name)[0] + ".json"
		with open(textFile, 'w') as f:
			f.write(textBuff)
		return

	if not save: return imgcv
	cv2.imwrite(img_name, imgcv)
Esempio n. 5
0
def postprocess(self, net_out, im, save=True):
    """
	Takes net output, draw predictions, save to disk
	"""
    meta, FLAGS = self.meta, self.FLAGS
    threshold, sqrt = FLAGS.threshold, meta['sqrt'] + 1
    C, B, S = meta['classes'], meta['num'], meta['side']
    colors, labels = meta['colors'], meta['labels']

    boxes = []
    SS = S * S  # number of grid cells
    prob_size = SS * C  # class probabilities
    conf_size = SS * B  # confidences for each grid cell
    #net_out = net_out[0]
    probs = net_out[0:prob_size]
    confs = net_out[prob_size:(prob_size + conf_size)]
    cords = net_out[(prob_size + conf_size):]
    probs = probs.reshape([SS, C])
    confs = confs.reshape([SS, B])
    cords = cords.reshape([SS, B, 4])

    for grid in range(SS):
        for b in range(B):
            bx = BoundBox(C)
            bx.c = confs[grid, b]
            bx.x = (cords[grid, b, 0] + grid % S) / S
            bx.y = (cords[grid, b, 1] + grid // S) / S
            bx.w = cords[grid, b, 2]**sqrt
            bx.h = cords[grid, b, 3]**sqrt
            p = probs[grid, :] * bx.c
            p *= (p > threshold)
            bx.probs = p
            boxes.append(bx)

    # non max suppress boxes
    for c in range(C):
        for i in range(len(boxes)):
            boxes[i].class_num = c
        boxes = sorted(boxes, key=prob_compare, reverse=True)
        for i in range(len(boxes)):
            boxi = boxes[i]
            if boxi.probs[c] == 0: continue
            for j in range(i + 1, len(boxes)):
                boxj = boxes[j]
                if box_iou(boxi, boxj) >= .4:
                    boxes[j].probs[c] = 0.

    if type(im) is not np.ndarray:
        imgcv = cv2.imread(im)
    else:
        imgcv = im
    h, w, _ = imgcv.shape
    textBuff = "["
    for b in boxes:
        max_indx = np.argmax(b.probs)
        max_prob = b.probs[max_indx]
        label = self.meta['labels'][max_indx]
        if max_prob > threshold:
            left = int((b.x - b.w / 2.) * w)
            right = int((b.x + b.w / 2.) * w)
            top = int((b.y - b.h / 2.) * h)
            bot = int((b.y + b.h / 2.) * h)
            if left < 0: left = 0
            if right > w - 1: right = w - 1
            if top < 0: top = 0
            if bot > h - 1: bot = h - 1
            thick = int((h + w) // 300)
            mess = '{}'.format(label)
            if self.FLAGS.json:
                line =  ('{"label":"%s",'
                  '"topleft":{"x":%d,"y":%d},'
                  '"bottomright":{"x":%d,"y":%d}},\n') % \
                  (mess, left, top, right, bot)
                textBuff += line
                continue

            cv2.rectangle(imgcv, (left, top), (right, bot),
                          self.meta['colors'][max_indx], thick)
            cv2.putText(imgcv, mess, (left, top - 12), 0, 1e-3 * h,
                        self.meta['colors'][max_indx], thick // 3)

    # Removing trailing comma+newline adding json list terminator.
    textBuff = textBuff[:-2] + "]"
    if self.FLAGS.json:
        textFile = os.path.splitext(img_name)[0] + ".json"
        with open(textFile, 'w') as f:
            f.write(textBuff)
        return

    if not save: return imgcv
    outfolder = os.path.join(self.FLAGS.test, 'out')
    img_name = os.path.join(outfolder, im.split('/')[-1])
    cv2.imwrite(img_name, imgcv)
Esempio n. 6
0
def postprocess(self, net_out, im, save=True):
    """
	Takes net output, draw net_out, save to disk
	"""
    # meta
    meta = self.meta
    H, W, _ = meta['out_size']
    threshold = meta['thresh']
    C, B = meta['classes'], meta['num']
    anchors = meta['anchors']
    net_out = net_out.reshape([H, W, B, -1])

    boxes = list()
    for row in range(H):
        for col in range(W):
            for b in range(B):
                bx = BoundBox(C)
                bx.x, bx.y, bx.w, bx.h, bx.c = net_out[row, col, b, :5]
                bx.c = expit(bx.c)
                bx.x = (col + expit(bx.x)) / W
                bx.y = (row + expit(bx.y)) / H
                bx.w = math.exp(bx.w) * anchors[2 * b + 0] / W
                bx.h = math.exp(bx.h) * anchors[2 * b + 1] / H
                classes = net_out[row, col, b, 5:]
                bx.probs = _softmax(classes) * bx.c
                bx.probs *= bx.probs > threshold
                boxes.append(bx)

    # non max suppress boxes
    for c in range(C):
        for i in range(len(boxes)):
            boxes[i].class_num = c
        boxes = sorted(boxes, key=prob_compare)
        for i in range(len(boxes)):
            boxi = boxes[i]
            if boxi.probs[c] == 0: continue
            for j in range(i + 1, len(boxes)):
                boxj = boxes[j]
                if box_iou(boxi, boxj) >= .4:
                    boxes[j].probs[c] = 0.

    colors = meta['colors']
    labels = meta['labels']
    if type(im) is not np.ndarray:
        imgcv = cv2.imread(im)
    else:
        imgcv = im
    h, w, _ = imgcv.shape
    for b in boxes:
        max_indx = np.argmax(b.probs)
        max_prob = b.probs[max_indx]
        label = 'object' * int(C < 2)
        label += labels[max_indx] * int(C > 1)
        if max_prob > threshold:
            left = int((b.x - b.w / 2.) * w)
            right = int((b.x + b.w / 2.) * w)
            top = int((b.y - b.h / 2.) * h)
            bot = int((b.y + b.h / 2.) * h)
            if left < 0: left = 0
            if right > w - 1: right = w - 1
            if top < 0: top = 0
            if bot > h - 1: bot = h - 1
            thick = int((h + w) / 300)
            cv2.rectangle(imgcv, (left, top), (right, bot), colors[max_indx],
                          thick)
            mess = '{}'.format(label)
            cv2.putText(imgcv, mess, (left, top - 12), 0, 1e-3 * h,
                        colors[max_indx], thick // 3)

    if not save: return imgcv
    outfolder = os.path.join(self.FLAGS.test, 'out')
    img_name = os.path.join(outfolder, im.split('/')[-1])
    cv2.imwrite(img_name, imgcv)


# def _postprocess(self, net_out, im, save = True):
# 	"""
# 	Takes net output, draw net_out, save to disk
# 	"""
# 	# meta
# 	meta = self.meta
# 	H, W, _ = meta['out_size']
# 	threshold = meta['thresh']
# 	C, B = meta['classes'], meta['num']
# 	anchors = meta['anchors']
# 	net_out = net_out.reshape([H, W, B, -1])

# 	boxes = list()
# 	for row in range(H):
# 		for col in range(W):
# 			for b in range(B):
# 				bx = BoundBox(C)
# 				bx.x, bx.y, bx.w, bx.h, bx.c = net_out[row, col, b, :5]
# 				bx.c = expit(bx.c)
# 				bx.x = (col + expit(bx.x)) / W
# 				bx.y = (row + expit(bx.y)) / H
# 				bx.w = math.exp(bx.w) * anchors[2 * b + 0] / W
# 				bx.h = math.exp(bx.h) * anchors[2 * b + 1] / H
# 				p = net_out[row, col, b, 5:] * bx.c
# 				mi = np.argmax(p)
# 				if p[mi] < threshold*2: continue
# 				bx.ind = mi; bx.pi = p[mi]
# 				boxes.append(bx)

# 	# non max suppress boxes
# 	boxes = sorted(boxes, cmp = prob_compare2)
# 	for i in range(len(boxes)):
# 		boxi = boxes[i]
# 		if boxi.pi == 0: continue
# 		for j in range(i + 1, len(boxes)):
# 			boxj = boxes[j]
# 			areaj = boxj.w * boxj.h
# 			if box_intersection(boxi, boxj)/areaj >= .4:
# 					boxes[j].pi = 0.

# 	colors = meta['colors']
# 	labels = meta['labels']
# 	if type(im) is not np.ndarray:
# 		imgcv = cv2.imread(im)
# 	else: imgcv = im
# 	h, w, _ = imgcv.shape
# 	for b in boxes:
# 		if b.pi > 0.:
# 			label = labels[b.ind]
# 			left  = int ((b.x - b.w/2.) * w)
# 			right = int ((b.x + b.w/2.) * w)
# 			top   = int ((b.y - b.h/2.) * h)
# 			bot   = int ((b.y + b.h/2.) * h)
# 			if left  < 0    :  left = 0
# 			if right > w - 1: right = w - 1
# 			if top   < 0    :   top = 0
# 			if bot   > h - 1:   bot = h - 1
# 			thick = int((h+w)/300)
# 			cv2.rectangle(imgcv,
# 				(left, top), (right, bot),
# 				colors[b.ind], thick)
# 			mess = '{}'.format(label)
# 			cv2.putText(imgcv, mess, (left, top - 12),
# 				0, 1e-3 * h, colors[b.ind], thick // 3)

# 	if not save: return imgcv
# 	outfolder = os.path.join(self.FLAGS.test, 'out')
# 	img_name = os.path.join(outfolder, im.split('/')[-1])
# 	cv2.imwrite(img_name, imgcv)
Esempio n. 7
0
def postprocess(self, net_out, im, save=True):
    """
	Takes net output, draw predictions, save to disk
	"""
    meta, FLAGS = self.meta, self.FLAGS
    threshold, sqrt = FLAGS.threshold, meta['sqrt'] + 1
    C, B, S = meta['classes'], meta['num'], meta['side']
    colors, labels = meta['colors'], meta['labels']

    boxes = []
    SS = S * S  # number of grid cells
    prob_size = SS * C  # class probabilities
    conf_size = SS * B  # confidences for each grid cell
    #net_out = net_out[0]
    probs = net_out[0:prob_size]
    confs = net_out[prob_size:(prob_size + conf_size)]
    cords = net_out[(prob_size + conf_size):]
    probs = probs.reshape([SS, C])
    confs = confs.reshape([SS, B])
    cords = cords.reshape([SS, B, 4])

    for grid in range(SS):
        for b in range(B):
            bx = BoundBox(C)
            bx.c = confs[grid, b]
            bx.x = (cords[grid, b, 0] + grid % S) / S
            bx.y = (cords[grid, b, 1] + grid // S) / S
            bx.w = cords[grid, b, 2]**sqrt
            bx.h = cords[grid, b, 3]**sqrt
            p = probs[grid, :] * bx.c
            p *= (p > threshold)
            bx.probs = p
            boxes.append(bx)

    # non max suppress boxes
    for c in range(C):
        for i in range(len(boxes)):
            boxes[i].class_num = c
        boxes = sorted(boxes, key=prob_compare)
        for i in range(len(boxes)):
            boxi = boxes[i]
            if boxi.probs[c] == 0: continue
            for j in range(i + 1, len(boxes)):
                boxj = boxes[j]
                if box_iou(boxi, boxj) >= .4:
                    boxes[j].probs[c] = 0.

    if type(im) is not np.ndarray:
        imgcv = cv2.imread(im)
    else:
        imgcv = im
    h, w, _ = imgcv.shape
    for b in boxes:
        max_indx = np.argmax(b.probs)
        max_prob = b.probs[max_indx]
        label = self.meta['labels'][max_indx]
        if max_prob > _thresh.get(label, threshold):
            left = int((b.x - b.w / 2.) * w)
            right = int((b.x + b.w / 2.) * w)
            top = int((b.y - b.h / 2.) * h)
            bot = int((b.y + b.h / 2.) * h)
            if left < 0: left = 0
            if right > w - 1: right = w - 1
            if top < 0: top = 0
            if bot > h - 1: bot = h - 1
            thick = int((h + w) // 150)
            cv2.rectangle(imgcv, (left, top), (right, bot),
                          self.meta['colors'][max_indx], thick)
            mess = '{}'.format(label)
            cv2.putText(imgcv, mess, (left, top - 12), 0, 1e-3 * h,
                        self.meta['colors'][max_indx], thick // 3)

    if not save: return imgcv
    outfolder = os.path.join(FLAGS.test, 'out')
    img_name = os.path.join(outfolder, im.split('/')[-1])
    cv2.imwrite(img_name, imgcv)
Esempio n. 8
0
def postprocess(self, net_out, im, save = True, check= False):
	"""
	Takes net output, draw net_out, save to disk
	"""
	# meta
	meta = self.meta
	H, W, _ = meta['out_size']
	threshold = meta['thresh']
	C, B = meta['classes'], meta['num']
	anchors = meta['anchors']
	net_out = net_out.reshape([H, W, B, -1])

	boxes = list()
	for row in range(H):
		for col in range(W):
			for b in range(B):
				bx = BoundBox(C)
				bx.x, bx.y, bx.w, bx.h, bx.c = net_out[row, col, b, :5]
				bx.c = expit(bx.c)
				bx.x = (col + expit(bx.x)) / W
				bx.y = (row + expit(bx.y)) / H
				bx.w = math.exp(bx.w) * anchors[2 * b + 0] / W
				bx.h = math.exp(bx.h) * anchors[2 * b + 1] / H
				classes = net_out[row, col, b, 5:]
				bx.probs = _softmax(classes) * bx.c
				bx.probs *= bx.probs > threshold
				boxes.append(bx)

	# non max suppress boxes
	for c in range(C):
		for i in range(len(boxes)):
			boxes[i].class_num = c
		boxes = sorted(boxes, key = prob_compare)
		for i in range(len(boxes)):
			boxi = boxes[i]
			if boxi.probs[c] == 0: continue
			for j in range(i + 1, len(boxes)):
				boxj = boxes[j]
				if box_iou(boxi, boxj) >= .4:
					boxes[j].probs[c] = 0.


	colors = meta['colors']
	labels = meta['labels']
	if type(im) is not np.ndarray:
		imgcv = cv2.imread(im)
	else: imgcv = im
	h, w, _ = imgcv.shape
	resultsForJSON = []	
	for b in boxes:
		max_indx = np.argmax(b.probs)
		max_prob = b.probs[max_indx]
		label = 'object' * int(C < 2)
		label += labels[max_indx] * int(C>1)
		if max_prob > threshold:
			left  = int ((b.x - b.w/2.) * w)
			right = int ((b.x + b.w/2.) * w)
			top   = int ((b.y - b.h/2.) * h)
			bot   = int ((b.y + b.h/2.) * h)
			if left  < 0    :  left = 0
			if right > w - 1: right = w - 1
			if top   < 0    :   top = 0
			if bot   > h - 1:   bot = h - 1
			thick = int((h+w)/300)
			mess = '{}'.format(label)
			#print(("label", mess, "confidence", max_prob, "topleft ","x ", left, "y ", top, "bottomright", " x",  right, "y", bot))
			if self.FLAGS.json:
				resultsForJSON.append({"label": mess, "confidence": float('%.2f' % max_prob), "topleft": {"x": left, "y": top}, "bottomright": {"x": right, "y": bot}})
				continue
			cv2.rectangle(imgcv, 
				(left, top), (right, bot), 
				colors[max_indx], thick)
			cv2.putText(imgcv, mess, (left, top - 12), 
				0, 1e-3 * h, colors[max_indx],thick//3)

	outfolder = os.path.join(self.FLAGS.test, 'out') 
	img_name = os.path.join(outfolder, im.split('/')[-1])
	if(check==False):
		if self.FLAGS.json:
			textJSON = json.dumps(resultsForJSON)
			textFile = os.path.splitext(img_name)[0] + ".json"
			with open(textFile, 'w') as f:
				f.write(textJSON)
			return	

	if not save: return imgcv
	
	cv2.imwrite(img_name, imgcv)