Beispiel #1
0
def train(config):
    """Trains the ReInspect model using SGD with momentum
    and prints out the logging information."""

    net = apollocaffe.ApolloNet()

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

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

    input_gen = load_idl(data_config["train_idl"],
                              image_mean, net_config)
    input_gen_test = load_idl(data_config["test_idl"],
                                   image_mean, net_config)

    forward(net, input_gen.next(), config["net"])
    #net.draw_to_file(logging["schematic_path"])

    if solver["weights"]:
        net.load(solver["weights"])
    else:
        net.load(googlenet.weights_file())

    loss_hist = {"train": [], "test": []}
    loggers = [
        apollocaffe.loggers.TrainLogger(logging["display_interval"],
                                        logging["log_file"]),
        apollocaffe.loggers.TestLogger(solver["test_interval"],
                                       logging["log_file"]),
        apollocaffe.loggers.SnapshotLogger(logging["snapshot_interval"],
                                           logging["snapshot_prefix"]),
        ]
    for i in range(solver["start_iter"], solver["max_iter"]):
        if i % solver["test_interval"] == 0:
            net.phase = 'test'
            test_loss = []
            for _ in range(solver["test_iter"]):
                forward(net, input_gen_test.next(), config["net"], False)
                test_loss.append(net.loss)
            loss_hist["test"].append(np.mean(test_loss))
            net.phase = 'train'
        forward(net, input_gen.next(), config["net"])
        loss_hist["train"].append(net.loss)
        net.backward()
        learning_rate = (solver["base_lr"] *
                         (solver["gamma"])**(i // solver["stepsize"]))
        net.update(lr=learning_rate, momentum=solver["momentum"],
                   clip_gradients=solver["clip_gradients"])
        for logger in loggers:
            logger.log(i, {'train_loss': loss_hist["train"],
                           'test_loss': loss_hist["test"],
                           'apollo_net': net, 'start_iter': 0})
Beispiel #2
0
def train(config):
    net = apollocaffe.ApolloNet()

    net_config = config["net"]
    data_config = config["data"]
    logging = config["logging"]
    image_mean = load_data_mean(
        data_config["idl_mean"], net_config["img_width"], net_config["img_height"], image_scaling=1.0
    )
    input_gen = load_idl_list(data_config["train_idl"], image_mean, net_config)
    input_gen_test = load_idl_list(data_config["test_idl"], image_mean, net_config)

    forward(net, input_gen.next(), config["net"])
    net.draw_to_file(logging["schematic_path"])

    solver = config["solver"]
    if solver["weights"]:
        net.load(solver["weights"])
    else:
        net.load(googlenet.weights_file())

    train_loss_hist = []
    test_loss_hist = []
    loggers = [
        apollocaffe.loggers.TrainLogger(logging["display_interval"]),
        apollocaffe.loggers.TestLogger(solver["test_interval"]),
        apollocaffe.loggers.SnapshotLogger(logging["snapshot_interval"], logging["snapshot_prefix"]),
    ]
    for i in range(solver["start_iter"], solver["max_iter"]):
        if i % solver["test_interval"] == 0:
            net.phase = "test"
            for _ in range(solver["test_iter"]):
                forward(net, input_gen_test.next(), config["net"], False)
                test_loss_hist.append(net.loss)
            net.phase = "train"
        forward(net, input_gen.next(), config["net"])
        train_loss_hist.append(net.loss)
        net.backward()
        lr = solver["base_lr"] * (solver["gamma"]) ** (i // solver["stepsize"])
        net.update(lr=lr, momentum=solver["momentum"], clip_gradients=solver["clip_gradients"])
        for logger in loggers:
            logger.log(
                i, {"train_loss": train_loss_hist, "test_loss": test_loss_hist, "apollo_net": net, "start_iter": 0}
            )
Beispiel #3
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)
Beispiel #4
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)