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_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"])

    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'
            for _ in range(solver["test_iter"]):
                forward(net, input_gen_test.next(), config["net"], False)
                loss_hist["test"].append(net.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):
    """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 #3
0
def forward_test(net, config, thresholds_list, enable_ip_split):
    """Trains the ReInspect model using SGD with momentum
	and prints out the logging information."""

    # # # init arguments # # #
    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)

    # # # load image data # # #
    test_gen = load_idl(data_config["boost_test_idl"],
                        image_mean,
                        net_config,
                        jitter=False,
                        if_random=False)

    net.phase = 'test'
    cc_dict = {}
    ce_dict = {}
    ca_dict = {}
    cp_dict = {}
    for threshold in thresholds_list:
        cc_dict[threshold] = []
        ce_dict[threshold] = []
        ca_dict[threshold] = []
        cp_dict[threshold] = []
    for _ in range(solver["test_iter"]):
        input_en = test_gen.next()
        bbox_list, conf_list = forward(net,
                                       input_en,
                                       net_config,
                                       enable_ip_split=enable_ip_split)
        for threshold in thresholds_list:
            (cc, ce, ca, cp) = get_accuracy(input_en['anno'], bbox_list,
                                            conf_list, threshold)
            cc_dict[threshold].append(cc)
            ce_dict[threshold].append(ce)
            ca_dict[threshold].append(ca)
            cp_dict[threshold].append(cp)
    for threshold in thresholds_list:
        precision = np.sum(cc_dict[threshold]) / np.sum(cp_dict[threshold])
        recall = np.sum(cc_dict[threshold]) / np.sum(ca_dict[threshold])
        f1 = 2 * precision * recall / (precision + recall)
        print threshold, "%.03f %.03f %.03f " % (1 - precision, recall, f1)
Beispiel #4
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_test = load_idl(data_config["test_idl"],
                              image_mean,
                              net_config,
                              jitter=False)

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

    if solver["weights"]:
        net.load(solver["weights"])
    else:
        raise Exception('weights not specified!')

    i = '1'
    output_file = open('data/new1_lstm_hidden' + i + '_mean.m', 'w')
    output_file.write('new1_lstm_hidden' + i + ' = [')
    for _ in range(20):
        input_gen_test.next()
    for input_en in input_gen_test:
        # plt.imshow(input_en['raw'])
        # plt.show()
        forward(net, input_en, config["net"])
        lstm_hidden0 = net.blobs['lstm_hidden' + i].data  # shape: (300, 1024)
        box_flags = net.blobs['box_flags'].data  # shape: (300, 1, 5, 1)
        N = lstm_hidden0.shape[0]
        for n in range(N):
            output_file.write(str(box_flags[n, 0, int(i), 0]) + ' ')
            for c in range(lstm_hidden0.shape[1]):
                output_file.write(str(lstm_hidden0[n, c]) + ' ')
            output_file.write('\n')
        break
    output_file.write('];')
    output_file.close()
Beispiel #5
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}
            )
def train(config):
    """Trains the ReInspect model using SGD with momentum
    and prints out the logging information."""

    # # # init arguments # # #
    net_config = config["net"]
    data_config = config["data"]
    solver = config["solver"]
    logging = config["logging"]
    MMD_config = config["MMD"]

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

    # # # load image data # # #
    re_train_gen = load_idl(data_config["reinspect_train_idl"], image_mean,
                            net_config)
    re_test_gen = load_idl(data_config["reinspect_test_idl"],
                           image_mean,
                           net_config,
                           jitter=False,
                           if_random=False)
    boost_test_gen = load_idl(data_config["boost_test_idl"],
                              image_mean,
                              net_config,
                              jitter=False,
                              if_random=False)
    boost_imname_list = load_imname_list(data_config['boost_idl'])

    # # # init apollocaffe # # #
    # source net
    src_net_ = apollocaffe.ApolloNet()
    net_config["ignore_label"] = -1
    forward(src_net_, re_test_gen.next(), net_config, enable_ip_split=False)
    if solver["weights"]:
        src_net_.load(solver["weights"])
    else:
        src_net_.load(googlenet.weights_file())

    # transform inner product layer of src_net
    src_net = apollocaffe.ApolloNet()
    net_convert_ip_2_ip_split(src_net_, src_net, re_test_gen.next(),
                              net_config)

    # boost net with MMD loss layer
    boost_net = apollocaffe.ApolloNet()
    net_config["ignore_label"] = 0
    forward(boost_net, boost_test_gen.next(), net_config)
    add_MMD_loss_layer(boost_net, src_net, MMD_config)
    boost_net.copy_params_from(src_net)

    # reinspect net with MMD loss layer
    re_net = apollocaffe.ApolloNet()
    net_config["ignore_label"] = 1
    forward(re_net, re_test_gen.next(), net_config)
    add_MMD_loss_layer(re_net, src_net, MMD_config)

    # # # init log # # #
    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"]),
    ]

    # # #  boost # # #
    for i in range(solver["start_iter"], solver["max_iter"]):
        # # test and evaluation
        if i % solver["test_interval"] == 0:
            boost_net.phase = 'test'
            test_loss = []
            test_loss2 = []
            cc_list = []
            ce_list = []
            ca_list = []
            cp_list = []
            for _ in range(solver["test_iter"]):
                input_en = boost_test_gen.next()
                mmd_stat(boost_net, re_test_gen, net_config, MMD_config)
                exit()
                (cc, ce, ca, cp) = get_accuracy(boost_net, input_en,
                                                net_config)
                add_MMD_loss_layer(boost_net, src_net, MMD_config)
                test_loss.append(boost_net.loss)
                cc_list.append(cc)
                ce_list.append(ce)
                ca_list.append(ca)
                cp_list.append(cp)
            loss_hist["test"].append(np.mean(test_loss))
            precision = np.sum(cc_list) / np.sum(cp_list)
            recall = np.sum(cc_list) / np.sum(ca_list)
            print 'hungarian loss:', np.mean(test_loss)
            print input_en['imname']
            for layers, loss_weight in zip(MMD_config['layers'],
                                           MMD_config['loss_weights']):
                if loss_weight == 0:
                    continue
                print boost_net.blobs[layers[0] + '_loss'].diff[0], '*',
                for layer in layers:
                    print boost_net.blobs[layer + '_loss'].data[0],
                print ''
            print 'iterate:  %6.d error, recall, F1: (%.3f %.3f) -> %.3f' % (
                i, 1 - precision, recall, 2 * precision * recall /
                (precision + recall))

        # # deploy for subsequent training, select boost_iter images from boost_iter_max images
        if i % solver["boost_interval"] == 0:
            boost_deploy_list = []
            random.shuffle(boost_imname_list)
            for imname in boost_imname_list[:solver[
                    "boost_iter_max"]]:  # not all images are needed for boost training
                input_en = generate_input_en(imname, image_mean, net_config)
                (bbox, conf) = forward(boost_net,
                                       input_en,
                                       net_config,
                                       deploy=True)
                add_MMD_loss_layer(boost_net, src_net, MMD_config)
                mmd_losses = []
                for layers, loss_weight in zip(MMD_config['layers'],
                                               MMD_config['loss_weights']):
                    if loss_weight == 0:
                        continue
                    mmd_losses += [
                        loss_weight * boost_net.blobs[x + '_loss'].data[0]
                        for x in layers
                    ]
                boost_deploy_list.append({
                    'imname': imname,
                    'bbox': bbox,
                    'conf': conf,
                    'MMDLoss': np.mean(mmd_losses)
                })
            boost_deploy_list = sorted(
                boost_deploy_list,
                key=lambda x: x['MMDLoss'],
                reverse=solver['reverse'])[:solver['boost_iter']]
            thres = 0.9
            boot_train_gen = convert_deploy_2_train(
                boost_deploy_list,
                image_mean,
                net_config,
                max_heads=solver['boost_iter_max_heads'],
                threshold=thres,
                if_random=solver['random'])
            boot_train_num = boot_train_gen.next()['num']

        if i % solver["boost_interval"] >= boot_train_num:
            continue

        # # train # #
        learning_rate = (solver["base_lr"] *
                         (solver["gamma"])**(i // solver["stepsize"]))

        # feed new data to source net to aid "add_MMD_loss_layer"
        forward(src_net, re_test_gen.next(), net_config)

        # train on reinspect dataset
        re_net.phase = "train"
        re_net.copy_params_from(boost_net)
        for _ in range(solver['Old_over_New']):
            forward(re_net, re_train_gen.next(), net_config)
            add_MMD_loss_layer(re_net, src_net, MMD_config)
            if not math.isnan(re_net.loss):
                re_net.backward()
            re_net.update(lr=learning_rate,
                          momentum=solver["momentum"],
                          clip_gradients=solver["clip_gradients"])

        boost_net.copy_params_from(re_net)

        # train on boost dataset
        boost_net.phase = 'train'
        forward(boost_net, boot_train_gen.next(), net_config)
        add_MMD_loss_layer(boost_net, src_net, MMD_config)
        loss_hist["train"].append(boost_net.loss)
        if not math.isnan(
                boost_net.loss):  # loss may be "nan", caused by ignore label.
            boost_net.backward()
        boost_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': boost_net,
                    'start_iter': 0
                })
Beispiel #7
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, jitter=False)
    input_gen_test = load_idl(data_config["test_idl"],
                                   image_mean, net_config, jitter=False)

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

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

    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 = []
            #save the weights
            net.save(solver["weights"])

            for x in range(solver["test_iter"]):
                test_input_data = input_gen_test.next()
                tic = time.time()
                forward(net, test_input_data, config["net"], False)
                print "Forward pass", time.time() - tic
                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 #8
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)

def updatefig(*args):
    new_frame = forward_multi(nets_list,
                              [input_gen.next() for input_gen in input_gens],
                              config["net"])
    im.set_array(new_frame)
    output_video.write(new_frame)
    return im,


if __name__ == "__main__":
    # load config
    config = json.load(open("config.json", 'r'))
    data_mean = load_data_mean(config["data"]["idl_mean"],
                               config["net"]["img_width"],
                               config["net"]["img_height"],
                               image_scaling=1.0)

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

    # model and video source
    global src_list
    # src_list = [ ("./multi_scene_data/pre_data/video_640_480/second_carteen_02.mov",
    # 		[('./data/brainwash_800000.h5',False),
    # 		 ("./tmp/saved/second_carteen_3_1800.h5", True)]),
    # 	   ("./multi_scene_data/pre_data/video_640_480/laoximen.mov",
    # 		[('./data/brainwash_800000.h5',False),
    # 		 ("./tmp/saved/laoximen_3_1100.h5", True)]),
    # 	  ("./multi_scene_data/pre_data/video_640_480/tianmulu_03.mov",
Beispiel #10
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
Beispiel #11
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)
Beispiel #13
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,
                              jitter=False)

    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"]):
        # # test and evaluation
        if i % solver["test_interval"] == 0:
            net.phase = 'test'
            test_loss = []
            test_loss2 = []
            cc_list = []
            ce_list = []
            ca_list = []
            cp_list = []
            for _ in range(solver["test_iter"]):
                input_en = input_gen_test.next()
                forward(net, input_en, net_config, False)
                test_loss.append(net.loss)
                (count_cover, count_error, count_anno,
                 count_pred) = get_accuracy(net, input_en, net_config)
                cc_list.append(count_cover)
                ce_list.append(count_error)
                ca_list.append(count_anno)
                cp_list.append(count_pred)
            loss_hist["test"].append(np.mean(test_loss))
            precision = np.sum(cc_list) / np.sum(cp_list)
            recall = np.sum(cc_list) / np.sum(ca_list)
            print 'hungarian loss:', np.mean(test_loss)
            print 'iterate:  %6.d error, recall, F1: (%.3f %.3f) -> %.3f' % (
                i, 1 - precision, recall, 2 * precision * recall /
                (precision + recall))
            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
                })