def get_accuracy(net, inputs, net_config, threshold=0.9):
    bbox_list, conf_list = forward(net, inputs, net_config)
    anno = inputs['anno']
    image = inputs['raw']
    count_anno = 0.0
    for r in anno:
        count_anno += 1
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]

    all_rects = [[[] for x in range(net_config["grid_width"])]
                 for y in range(net_config["grid_height"])]
    for n in range(len(bbox_list)):
        for k in range(net_config["grid_height"] * net_config["grid_width"]):
            y = int(k / net_config["grid_width"])
            x = int(k % net_config["grid_width"])
            bbox = bbox_list[n][k]
            conf = conf_list[n][k, 1].flatten()[0]
            abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
            abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
            w = bbox[2, 0, 0]
            h = bbox[3, 0, 0]
            if conf < threshold:
                continue
            all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    acc_rects = stitch_rects(all_rects, net_config)
    count_cover = 0.0
    count_error = 0.0
    count_pred = 0.0
    for rect in acc_rects:
        if rect.true_confidence < threshold:
            continue
        else:
            count_pred += 1
            x1 = rect.cx - rect.width / 2.
            x2 = rect.cx + rect.width / 2.
            y1 = rect.cy - rect.height / 2.
            y2 = rect.cy + rect.height / 2.
            iscover = False
            for r in anno:
                if overlap_union(x1, y1, x2, y2, r.x1, r.y1, r.x2,
                                 r.y2) >= 0.5:  # 0.2 is for bad annotation
                    iscover = True
                    break
            if iscover:
                count_cover += 1
            else:
                count_error += 1

    #         cv2.rectangle(image, (int(x1),int(y1)),(int(x2),int(y2)),
    #                                     (255,0,0), 2)
    #         cv2.rectangle(image, (int(r.x1),int(r.y1)),(int(r.x2),int(r.y2)),
    #                                     (0,255,0), 2)
    # if count_pred!=0 and count_anno!=0:
    #     print 1-count_cover/count_pred, count_cover/count_anno
    # plt.imshow(image)
    # plt.show()

    return (count_cover, count_error, count_anno, count_pred)
Example #2
0
def get_accuracy(net, inputs, net_config, threshold=0.9):
    bbox_list, conf_list = forward(net, inputs, net_config, True)
    anno = inputs['anno']
    count_anno = 0.0
    for r in anno:
        count_anno += 1
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]

    all_rects = [[[] for x in range(net_config["grid_width"])]
                 for y in range(net_config["grid_height"])]
    for n in range(len(bbox_list)):
        for k in range(net_config["grid_height"] * net_config["grid_width"]):
            y = int(k / net_config["grid_width"])
            x = int(k % net_config["grid_width"])
            bbox = bbox_list[n][k]
            conf = conf_list[n][k, 1].flatten()[0]
            abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
            abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
            w = bbox[2, 0, 0]
            h = bbox[3, 0, 0]
            if conf < threshold:
                continue
            all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    acc_rects = stitch_rects(all_rects, net_config)
    count_cover = 0.0
    count_error = 0.0
    count_pred = 0.0
    for rect in acc_rects:
        if rect.true_confidence < threshold:
            continue
        else:
            count_pred += 1
            x1 = rect.cx - rect.width / 2.
            x2 = rect.cx + rect.width / 2.
            y1 = rect.cy - rect.height / 2.
            y2 = rect.cy + rect.height / 2.
            iscover = False
            for r in anno:
                if overlap_union(x1, y1, x2, y2, r.x1, r.y1, r.x2,
                                 r.y2) >= 0.5:
                    iscover = True
                    break
            if iscover:
                count_cover += 1
            else:
                count_error += 1

    return (count_cover, count_error, count_anno, count_pred)
def forward_test(net, inputs, net_config, enable_ip_split):
    net.phase = 'test'
    bbox_list, conf_list = forward(net,
                                   inputs,
                                   net_config,
                                   deploy=True,
                                   enable_ip_split=enable_ip_split)

    img = np.copy(inputs["raw"])
    all_rects = [[[] for x in range(net_config["grid_width"])]
                 for y in range(net_config["grid_height"])]
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]
    for n in range(len(bbox_list)):
        for k in range(net_config["grid_height"] * net_config["grid_width"]):
            y = int(k / net_config["grid_width"])
            x = int(k % net_config["grid_width"])
            bbox = bbox_list[n][k]
            conf = conf_list[n][k, 1].flatten()[0]
            abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
            abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
            w = bbox[2, 0, 0]
            h = bbox[3, 0, 0]
            if conf < 0.9:
                continue
            all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    acc_rects = stitch_rects(all_rects, net_config)

    for rect in acc_rects:
        if rect.true_confidence < 0.9:
            continue
        cv2.rectangle(
            img,
            (rect.cx - int(rect.width / 2), rect.cy - int(rect.height / 2)),
            (rect.cx + int(rect.width / 2), rect.cy + int(rect.height / 2)),
            (255, 0, 0), 2)
    return img
def convert_deploy_2_train(boot_deploy_list,
                           data_mean,
                           net_config,
                           max_heads=999999,
                           threshold=0.9,
                           jitter=True,
                           if_random=True):
    annos = []
    cnt = 0
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]
    for dic in boot_deploy_list:
        anno = Annotation()
        anno.imageName = dic["imname"]
        bbox_list = dic["bbox"]
        conf_list = dic["conf"]

        all_rects = [[[] for x in range(net_config["grid_width"])]
                     for y in range(net_config["grid_height"])]
        for n in range(len(bbox_list)):
            for k in range(net_config["grid_height"] *
                           net_config["grid_width"]):
                y = int(k / net_config["grid_width"])
                x = int(k % net_config["grid_width"])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k, 1].flatten()[0]
                abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
                abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
                w = bbox[2, 0, 0]
                h = bbox[3, 0, 0]
                if conf < threshold:
                    continue
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

        acc_rects = stitch_rects(all_rects, net_config)
        for rect in acc_rects:
            if rect.true_confidence >= threshold:
                r = AnnoRect()
                r.x1 = int(rect.cx - rect.width / 2.)
                r.x2 = int(rect.cx + rect.width / 2.)
                r.y1 = int(rect.cy - rect.height / 2.)
                r.y2 = int(rect.cy + rect.height / 2.)
                anno.rects.append(r)
        annos.append(anno)
        cnt += len(anno.rects)
        if cnt >= max_heads:
            break
    print 'deployed', len(annos), 'images with', cnt, 'heads'

    while True:
        if if_random:
            random.shuffle(annos)
        for anno in annos:
            if jitter:
                jit_image, jit_anno = annotation_jitter(
                    anno,
                    target_width=net_config["img_width"],
                    target_height=net_config["img_height"])
            else:
                jit_image = imread(anno.imageName)
                jit_anno = anno
            image = image_to_h5(jit_image, data_mean, image_scaling=1.0)
            boxes, box_flags = annotation_to_h5(jit_anno,
                                                net_config["grid_width"],
                                                net_config["grid_height"],
                                                net_config["region_size"],
                                                net_config["max_len"])
            yield {
                "num": len(annos),
                "imname": anno.imageName,
                "raw": jit_image,
                "image": image,
                "boxes": boxes,
                "box_flags": box_flags,
                'anno': jit_anno
            }
Example #5
0
def test(config):
    """Test the model and output to test/output."""

    net = apollocaffe.ApolloNet()

    net_config = config["net"]
    data_config = config["data"]
    solver = config["solver"]

    image_mean = load_data_mean(
        data_config["idl_mean"], net_config["img_width"],
        net_config["img_height"], image_scaling=1.0)

    input_gen_test = load_idl(data_config["test_idl"],
                                   image_mean, net_config, jitter=False)

    forward(net, input_gen_test.next(), config["net"])

    try:
        net.load(solver["weights"])
    except:
        pass

    net.phase = 'test'
    test_loss = []
    for i in range(solver["test_iter"]):
        input_test = input_gen_test.next()
        image = input_test["raw"]
        tic = time.time()
        bbox, conf = forward(net, input_test, config["net"], True)
        print "forward deploy time", time.time() - tic
        bbox_list = bbox
        conf_list = conf
        pix_per_w = 32
        pix_per_h = 32

        all_rects = [[[] for x in range(net_config['grid_width'])] for y in range(net_config['grid_height'])]
        for n in range(len(bbox_list)):
            for k in range(net_config['grid_width'] * net_config['grid_height']):
                y = int(k / net_config['grid_width'])
                x = int(k % net_config['grid_width'])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k,1].flatten()[0]
                abs_cx = pix_per_w/2 + pix_per_w*x + int(bbox[0,0,0])
                abs_cy = pix_per_h/2 + pix_per_h*y+int(bbox[1,0,0])
                w = bbox[2,0,0]
                h = bbox[3,0,0]
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))
        acc_rects = stitch_rects(all_rects)
        #print acc_rects

        for idx, rect in enumerate(acc_rects):
            if rect.true_confidence < 0.9:
                print 'rejected', rect.true_confidence
                continue
            else:
                print 'found', rect.true_confidence
                cv2.rectangle(image, (rect.cx-int(rect.width/2), rect.cy-int(rect.height/2)),
                                   (rect.cx+int(rect.width/2), rect.cy+int(rect.height/2)),
                                   (255,0,0),
                                   2)
        cv2.imwrite("test_output/img_out%s.jpg" % i, image)
Example #6
0
def test(config): 
    """ Takes the config, run test program
    """                
    
    data_mean = load_data_mean(config["data"]["idl_mean"], 
                               config["net"]["img_width"], 
                               config["net"]["img_height"], image_scaling=1.0)
    
    num_test_images = 599
    
    # Warning: load_idl returns an infinite generator. Calling list() before islice() will hang.
    test_list = list(itertools.islice(
            load_idl(config["data"]["test_idl"], data_mean, config["net"], False),
            0,
            num_test_images))
    img = np.copy(test_list[-1]["raw"])
    # plt.imshow(img)
    
    net = apollocaffe.ApolloNet()
    net.phase = 'test'
    forward(net, test_list[0], config["net"], True)
    net.load("data/snapshot/reinspect_hcs_800000.h5")
    
    annolist = al.AnnoList()
    net_config = config["net"]
    pix_per_w = net_config["img_width"]/net_config["grid_width"]
    pix_per_h = net_config["img_height"]/net_config["grid_height"]
    
    if config.has_key("conf_th"):
        conf_th = config["conf_th"]
    else:
        conf_th = 0.6
    
    mae = 0.
    for i in range(num_test_images):
        inputs = test_list[i]
        bbox_list, conf_list = forward(net, inputs, net_config, True)
        
        img = np.copy(inputs["raw"])
        all_rects = [[[] for x in range(net_config["grid_width"])] for y in range(net_config["grid_height"])]
        for n in range(len(bbox_list)):
            for k in range(net_config["grid_height"] * net_config["grid_width"]):
                y = int(k / net_config["grid_width"])
                x = int(k % net_config["grid_width"])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k,1].flatten()[0]
                # notice the output rect [cx, cy, w, h]
                # cx means center x-cord
                abs_cx = pix_per_w/2 + pix_per_w*x + int(bbox[0,0,0])
                abs_cy = pix_per_h/2 + pix_per_h*y + int(bbox[1,0,0])
                w = bbox[2,0,0]
                h = bbox[3,0,0]
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))
    
        acc_rects = stitch_rects(all_rects)
        
        display = True
        if display:
            for rect in acc_rects:
                if rect.true_confidence < conf_th:
                    continue
                cv2.rectangle(img, 
                              (rect.cx-int(rect.width/2), rect.cy-int(rect.height/2)), 
                              (rect.cx+int(rect.width/2), rect.cy+int(rect.height/2)), 
                              (255,0,0),
                              2)
#                cv2.circle(img, 
#                              (rect.cx, rect.cy), 
#                              ((rect.width + rect.height)/4), 
#                              (255,0,0),
#                              2)
            img_name = './data/tmp/%05d.jpg' % i
            plt.imsave(img_name, img)
            plt.figure(figsize=(15,10))
            plt.imshow(img)
            
        anno = al.Annotation()
        anno.imageName = inputs["imname"]
        # count 
        number = 0;
        for rect in acc_rects:
            r = al.AnnoRect()
            r.x1 = rect.cx - rect.width/2.
            r.x2 = rect.cx + rect.width/2.
            r.y1 = rect.cy - rect.height/2.
            r.y2 = rect.cy + rect.height/2.
            r.score = rect.true_confidence
            anno.rects.append(r)
            if r.score > conf_th:
                number += 1;
        annolist.append(anno)
        mae += abs(number - len(inputs["rects"]))
        print anno.imageName, number, len(inputs["rects"]), abs(number - len(inputs["rects"]))
    print mae / num_test_images
Example #7
0
def test(config):
    """ Takes the config, run test program
    """

    data_mean = load_data_mean(config["data"]["idl_mean"],
                               config["net"]["img_width"],
                               config["net"]["img_height"],
                               image_scaling=1.0)

    num_test_images = 599

    # Warning: load_idl returns an infinite generator. Calling list() before islice() will hang.
    test_list = list(
        itertools.islice(
            load_idl(config["data"]["test_idl"], data_mean, config["net"],
                     False), 0, num_test_images))
    img = np.copy(test_list[-1]["raw"])
    # plt.imshow(img)

    net = apollocaffe.ApolloNet()
    net.phase = 'test'
    forward(net, test_list[0], config["net"], True)
    net.load("data/snapshot/reinspect_hcs_800000.h5")

    annolist = al.AnnoList()
    net_config = config["net"]
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]

    if config.has_key("conf_th"):
        conf_th = config["conf_th"]
    else:
        conf_th = 0.6

    mae = 0.
    for i in range(num_test_images):
        inputs = test_list[i]
        bbox_list, conf_list = forward(net, inputs, net_config, True)

        img = np.copy(inputs["raw"])
        all_rects = [[[] for x in range(net_config["grid_width"])]
                     for y in range(net_config["grid_height"])]
        for n in range(len(bbox_list)):
            for k in range(net_config["grid_height"] *
                           net_config["grid_width"]):
                y = int(k / net_config["grid_width"])
                x = int(k % net_config["grid_width"])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k, 1].flatten()[0]
                # notice the output rect [cx, cy, w, h]
                # cx means center x-cord
                abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
                abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
                w = bbox[2, 0, 0]
                h = bbox[3, 0, 0]
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

        acc_rects = stitch_rects(all_rects)

        display = True
        if display:
            for rect in acc_rects:
                if rect.true_confidence < conf_th:
                    continue
                cv2.rectangle(img, (rect.cx - int(rect.width / 2),
                                    rect.cy - int(rect.height / 2)),
                              (rect.cx + int(rect.width / 2),
                               rect.cy + int(rect.height / 2)), (255, 0, 0), 2)


#                cv2.circle(img,
#                              (rect.cx, rect.cy),
#                              ((rect.width + rect.height)/4),
#                              (255,0,0),
#                              2)
            img_name = './data/tmp/%05d.jpg' % i
            plt.imsave(img_name, img)
            plt.figure(figsize=(15, 10))
            plt.imshow(img)

        anno = al.Annotation()
        anno.imageName = inputs["imname"]
        # count
        number = 0
        for rect in acc_rects:
            r = al.AnnoRect()
            r.x1 = rect.cx - rect.width / 2.
            r.x2 = rect.cx + rect.width / 2.
            r.y1 = rect.cy - rect.height / 2.
            r.y2 = rect.cy + rect.height / 2.
            r.score = rect.true_confidence
            anno.rects.append(r)
            if r.score > conf_th:
                number += 1
        annolist.append(anno)
        mae += abs(number - len(inputs["rects"]))
        print anno.imageName, number, len(
            inputs["rects"]), abs(number - len(inputs["rects"]))
    print mae / num_test_images
def main():
	config = json.load(open("config.json", 'r'))
	config["data"]["test_idl"] = "./data/brainwash/brainwash_test.idl"

	apollocaffe.set_random_seed(config["solver"]["random_seed"])
	apollocaffe.set_device(0)

	# Now lets load the data mean and the data.
	data_mean = load_data_mean(config["data"]["idl_mean"], 
						   config["net"]["img_width"], 
						   config["net"]["img_height"], image_scaling=1.0)

	num_test_images = 500
	display = True

	## Warning: load_idl returns an infinite generator. Calling list() before islice() will hang.
	test_list = list(itertools.islice(
			load_idl(config["data"]["test_idl"], data_mean, config["net"], False),
			0,
			num_test_images))

	# We can now load the snapshot weights.
	net = apollocaffe.ApolloNet()
	net.phase = 'test'
	import time; s = time.time()
	forward(net, test_list[0], config["net"], True) # define structure
	print time.time() - s
	net.load("./data/brainwash_800000.h5") # load pre-trained weights

	# We can now begin to run the model and visualize the results.
	annolist = al.AnnoList()
	net_config = config["net"]
	pix_per_w = net_config["img_width"]/net_config["grid_width"]
	pix_per_h = net_config["img_height"]/net_config["grid_height"]

	for i in range(10):
		inputs = test_list[i]
		timer = Timer()
		timer.tic()
		bbox_list, conf_list = forward(net, inputs, net_config, True)
		timer.toc()
		print ('Detection took {:.3f}s').format(timer.total_time)
		img = np.copy(inputs["raw"])
		png = np.copy(inputs["imname"])
		all_rects = [[[] for x in range(net_config["grid_width"])] for y in range(net_config["grid_height"])]
		for n in range(len(bbox_list)):
			for k in range(net_config["grid_height"] * net_config["grid_width"]):
				y = int(k / net_config["grid_width"])
				x = int(k % net_config["grid_width"])
				bbox = bbox_list[n][k]
				conf = conf_list[n][k,1].flatten()[0]
				abs_cx = pix_per_w/2 + pix_per_w*x + int(bbox[0,0,0])
				abs_cy = pix_per_h/2 + pix_per_h*y+int(bbox[1,0,0])
				w = bbox[2,0,0]
				h = bbox[3,0,0]
				all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))

		timer.tic()
		acc_rects = stitch_rects(all_rects)
		timer.toc()
		print ('Stitching detected bboxes took {:.3f}s').format(timer.total_time)		

		if display:
			visualize_detection(img, acc_rects)    
			
		anno = al.Annotation()
		anno.imageName = inputs["imname"]
		for rect in acc_rects:
			r = al.AnnoRect()
			r.x1 = rect.cx - rect.width/2.
			r.x2 = rect.cx + rect.width/2.
			r.y1 = rect.cy - rect.height/2.
			r.y2 = rect.cy + rect.height/2.
			r.score = rect.true_confidence
			anno.rects.append(r)
		annolist.append(anno)