Beispiel #1
0
def test(train_data,
         test_data=None,
         n_hidden=None,
         learn_rate=0.1,
         n_epochs=500,
         verbose=True):
    """
    Uses <test_data> to evaluate classification accuracy of network trained on
    <train_data>.

    INPUTS:
    - train_data (list): list of train data, stored as tuples of input/target
      feature vectors
    - test_data (list): list of test data, in same format as train data
    - n_hidden (int): size of hidden layer
    - learn_rate (float): learning rate for grad. descent
    - n_epochs (int): total iterations over all train data
    - verbose (bool): prints test accuracy if True

    OUTPUTS:
    - returns test accuracy and trained weights/biases
    """

    if test_data is None:
        test_data = train_data  # use same dataset for train/test

    # classify test data using trained weights/biases
    w_out, b_out, w_h, b_h = train(train_data, n_hidden, learn_rate, n_epochs)
    n_correct, n_data = 0, len(test_data)
    for x, target in test_data:
        _, sigma_out = forward(x, w_out, b_out, w_h, b_h)
        pred = np.argmax(sigma_out)
        n_correct += target[pred][0]

    if verbose:
        print("Percent classified correctly: {:.2f}% ({}:{})".format(
            100 * n_correct / n_data, n_correct, n_data - n_correct))

    return n_correct, n_data, (w_out, b_out, w_h, b_h)
Beispiel #2
0
def classify(net, impath, regions_list, image_mean):
    im = imread(impath)
    batch_size = len(regions_list) / 4
    image_batch = np.zeros((batch_size, 3, 227, 227))
    for i in range(batch_size):
        x0 = int(regions_list[i * 4 + 0])
        y0 = int(regions_list[i * 4 + 1])
        x1 = int(regions_list[i * 4 + 2])
        y1 = int(regions_list[i * 4 + 3])
        sub_image = im[x0:x1, y0:y1, :]
        jit_image = cv2.resize(sub_image, (256, 256))
        image = image_to_h5(jit_image,
                            image_mean,
                            crop_size=227,
                            image_scaling=1.0)
        image_batch[i, :, :, :] = image
    input_en = {"image": image_batch}
    probs = forward(net, input_en, deploy=True)
    res = ''
    for i in range(probs.shape[0]):
        index, value = get_max_index(probs[i, :])
        res += "%d %.03f" % (index, value)
    return res
Beispiel #3
0
def main():

    torch.manual_seed(opt.random_seed)
    torch.cuda.manual_seed(opt.random_seed)


    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    # device = torch.device('cpu')

    cur_dir = os.getcwd()
    train_dataset = MultiSessionsGraph(cur_dir + '/datasets/' + opt.dataset, phrase='train', knn_phrase='neigh_data_'+str(opt.id))
    train_loader = DataLoader(train_dataset, batch_size=opt.batch_size, shuffle=True)
    test_dataset = MultiSessionsGraph(cur_dir + '/datasets/' + opt.dataset, phrase='test', knn_phrase='neigh_data_'+str(opt.id))
    test_loader = DataLoader(test_dataset, batch_size=opt.batch_size, shuffle=False)

    log_dir = cur_dir + '/log/' + str(opt.dataset) + '/' + time.strftime(
        "%Y-%m-%d %H:%M:%S", time.localtime())
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logging.warning('logging to {}'.format(log_dir))
    writer = SummaryWriter(log_dir)

    if opt.dataset == 'cikm16':
        n_node = 43097
    elif opt.dataset == 'yoochoose1_64':
        n_node = 17400
    else:
        n_node = 309

    model = GraphModel(opt, n_node=n_node).to(device)

    multigraph_parameters = list(map(id, model.group_graph.parameters()))
    srgnn_parameters = (p for p in model.parameters() if id(p) not in multigraph_parameters)
    parameters = [{"params": model.group_graph.parameters(), "lr": 0.001}, {"params": srgnn_parameters}]

    # best 0.1
    lambda1 = lambda epoch: 0.1 ** (epoch // 3)
    lambda2 = lambda epoch: 0.1 ** (epoch // 3)

    optimizer = torch.optim.Adam(parameters, lr=opt.lr, weight_decay=opt.l2)
    #scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=opt.lr_dc_step, gamma=opt.lr_dc)
    scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=[lambda1, lambda2])

    logging.warning(model)
    if not opt.predict:
        best_result20 = [0, 0]
        best_epoch20 = [0, 0]

        best_result10 = [0, 0]
        best_epoch10 = [0, 0]

        best_result5 = [0, 0]
        best_epoch5 = [0, 0]
        for epoch in range(opt.epoch):
            scheduler.step(epoch)
            print("Epoch ", epoch)
            forward(model, train_loader, device, writer, epoch, top_k=opt.top_k, optimizer=optimizer, train_flag=True)
            with torch.no_grad():
                mrr20, hit20, mrr10, hit10, mrr5, hit5 = forward(model, test_loader, device, writer, epoch, top_k=opt.top_k, train_flag=False)

            if hit20 >= best_result20[0]:
                best_result20[0] = hit20
                best_epoch20[0] = epoch
                # torch.save(model.state_dict(), log_dir+'/best_recall_params.pkl')
            if mrr20 >= best_result20[1]:
                best_result20[1] = mrr20
                best_epoch20[1] = epoch

            if hit10 >= best_result10[0]:
                best_result10[0] = hit10
                best_epoch10[0] = epoch
                # torch.save(model.state_dict(), log_dir+'/best_recall_params.pkl')
            if mrr10 >= best_result10[1]:
                best_result10[1] = mrr10
                best_epoch10[1] = epoch
                # torch.save(model.state_dict(), log_dir+'/best_mrr_params.pkl')

            if hit5 >= best_result5[0]:
                best_result5[0] = hit5
                best_epoch5[0] = epoch
                # torch.save(model.state_dict(), log_dir+'/best_recall_params.pkl')
            if mrr5 >= best_result5[1]:
                best_result5[1] = mrr5
                best_epoch5[1] = epoch

            print('Best Result:')
            print('\tMrr@%d:\t%.4f\tEpoch:\t%d' % (20, best_result20[1], best_epoch20[1]))
            print('\tRecall@%d:\t%.4f\tEpoch:\t%d\n' % (20, best_result20[0], best_epoch20[0]))
            print('\tMrr@%d:\t%.4f\tEpoch:\t%d' % (opt.top_k, best_result10[1], best_epoch10[1]))
            print('\tRecall@%d:\t%.4f\tEpoch:\t%d\n' % (opt.top_k, best_result10[0], best_epoch10[0]))
            print('\tMrr@%d:\t%.4f\tEpoch:\t%d' % (5, best_result5[1], best_epoch5[1]))
            print('\tRecall@%d:\t%.4f\tEpoch:\t%d' % (5, best_result5[0], best_epoch5[0]))
            print("-"*20)
        # print_txt(log_dir, opt, best_result, best_epoch, opt.top_k, note, save_config=True)
    else:
        log_dir = 'log/cikm16/2019-08-19 14:27:33'
        model.load_state_dict(torch.load(log_dir+'/best_mrr_params.pkl'))
        mrr, hit = forward(model, test_loader, device, writer, 0, top_k=opt.top_k, train_flag=False)
        best_result = [hit, mrr]
        best_epoch = [0, 0]
Beispiel #4
0
def main():
    assert opt.dataset in ['gowalla', 'lastfm']
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    cur_dir = os.getcwd()

    train_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset,
                                       phrase='train')
    train_loader = DataLoader(train_dataset,
                              batch_size=opt.batch_size,
                              shuffle=True)
    train_for_res, _ = load_data_valid(
        os.path.expanduser(
            os.path.normpath(cur_dir + '/../datasets/' + opt.dataset +
                             '/raw/train.txt.csv')), 0)
    max_train_item = max(max(max(train_for_res[0])), max(train_for_res[1]))
    max_train_user = max(train_for_res[2])

    test_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset,
                                      phrase='test1')
    test_loader = DataLoader(test_dataset,
                             batch_size=opt.batch_size,
                             shuffle=False)
    test_for_res = load_testdata(
        os.path.expanduser(
            os.path.normpath(cur_dir + '/../datasets/' + opt.dataset +
                             '/raw/test1.txt.csv')))
    max_item = max(max(max(test_for_res[0])), max(test_for_res[1]))
    max_user = max(test_for_res[2])
    pre_max_item = max_train_item
    pre_max_user = max_train_user

    log_dir = cur_dir + '/../log/' + str(opt.dataset) + '/paper200/' + str(
        opt) + '_fix_new_entropy(rank)_on_union+' + str(opt.u) + 'tanh*u_AGCN***GAG-win' + str(opt.win_size) \
              + '***concat3_linear_tanh_in_e2s_' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logging.warning('logging to {}'.format(log_dir))
    writer = SummaryWriter(log_dir)

    if opt.dataset == 'gowalla':
        n_item = 30000
        n_user = 33005
    else:
        n_item = 10000
        n_user = 984

    model = GNNModel(hidden_size=opt.hidden_size,
                     n_item=n_item,
                     n_user=n_user,
                     u=opt.u).to(device)

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=opt.lr,
                                 weight_decay=opt.l2)
    scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
                                                     milestones=[2, 3],
                                                     gamma=opt.lr_dc)

    logging.warning(model)

    # offline training on 'train' and test on 'test1'
    logging.warning('*********Begin offline training*********')
    updates_per_epoch = len(train_loader)
    updates_count = 0
    for train_epoch in tqdm(range(opt.epoch)):
        forward(model,
                train_loader,
                device,
                writer,
                train_epoch,
                optimizer=optimizer,
                train_flag=True,
                max_item_id=max_train_item,
                last_update=updates_count)
        scheduler.step()
        updates_count += updates_per_epoch
        with torch.no_grad():
            forward(model,
                    test_loader,
                    device,
                    writer,
                    train_epoch,
                    train_flag=False,
                    max_item_id=max_item)

    # reservoir construction with 'train'
    logging.warning(
        '*********Constructing the reservoir with offline training data*********'
    )
    res = Reservoir(train_for_res, opt.res_size)
    res.update(train_for_res)

    # test and online training on 'test2~5'
    logging.warning('*********Begin online training*********')
    now = time.asctime()
    for test_epoch in tqdm(range(1, 6)):
        if test_epoch != 1:
            test_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' +
                                              opt.dataset,
                                              phrase='test' + str(test_epoch))
            test_loader = DataLoader(test_dataset,
                                     batch_size=opt.batch_size,
                                     shuffle=False)

            test_for_res = load_testdata(
                os.path.expanduser(
                    os.path.normpath(cur_dir + '/../datasets/' + opt.dataset +
                                     '/raw/test' + str(test_epoch) +
                                     '.txt.csv')))
            pre_max_item = max_item
            pre_max_user = max_user
            max_item = max(max(max(test_for_res[0])), max(test_for_res[1]))
            max_user = max(test_for_res[2])

            # test on the current test set
            # no need to test on test1 because it's done in the online training part
            # epoch + 10 is a number only for the visualization convenience
            with torch.no_grad():
                forward(model,
                        test_loader,
                        device,
                        writer,
                        test_epoch + 10,
                        train_flag=False,
                        max_item_id=max_item)

        # reservoir sampling
        sampled_data = fix_new_entropy_on_union(cur_dir,
                                                now,
                                                opt,
                                                model,
                                                device,
                                                res.data,
                                                test_for_res,
                                                len(test_for_res[0]) //
                                                opt.win_size,
                                                pre_max_item,
                                                pre_max_user,
                                                ent='wass')

        # cast the sampled set to dataset
        sampled_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' +
                                             opt.dataset,
                                             phrase='sampled' + now,
                                             sampled_data=sampled_data)
        sampled_loader = DataLoader(sampled_dataset,
                                    batch_size=opt.batch_size,
                                    shuffle=True)

        # update with the sampled set
        forward(model,
                sampled_loader,
                device,
                writer,
                test_epoch + opt.epoch,
                optimizer=optimizer,
                train_flag=True,
                max_item_id=max_item,
                last_update=updates_count)

        updates_count += len(test_loader)

        scheduler.step()

        res.update(test_for_res)
        os.remove('../datasets/' + opt.dataset + '/processed/sampled' + now +
                  '.pt')
Beispiel #5
0
def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    cur_dir = os.getcwd()
    # custom dataset
    train_dataset = MultiSessionsGraph(cur_dir + '/datasets/' + opt.dataset,
                                       phrase='train')
    train_loader = DataLoader(train_dataset,
                              batch_size=opt.batch_size,
                              shuffle=True)
    test_dataset = MultiSessionsGraph(cur_dir + '/datasets/' + opt.dataset,
                                      phrase='test')
    test_loader = DataLoader(test_dataset,
                             batch_size=opt.batch_size,
                             shuffle=False)

    # log_dir = cur_dir + '/log/' + str(opt.dataset) + '/' + str(opt) + '_s2s3_linear_gat8-1_noleaky_' + time.strftime(
    #     "%Y-%m-%d %H:%M:%S", time.localtime())
    log_dir = cur_dir + '/log/' + str(opt.dataset) + '/' + 'model_log'
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logging.warning('logging to {}'.format(log_dir))
    writer = SummaryWriter(log_dir)

    if opt.dataset == 'diginetica':
        n_node = 43097
    elif opt.dataset == 'yoochoose1_64' or opt.dataset == 'yoochoose1_4':
        n_node = 37483
    else:
        n_node = 309

    model = GNNModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)
    # model = SortPoolModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)
    # model = Set2SetModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)
    # model = GINSet2SetModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)
    # model = VirtualNodeModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)
    # model = Set2SetATTModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)
    # model = VirtualNodeRNNModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=opt.lr,
                                 weight_decay=opt.l2)
    # optimizer = torch.optim.SGD(model.parameters(), lr=opt.lr, weight_decay=opt.l2, momentum=opt.momentum)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=opt.lr_dc_step,
                                                gamma=opt.lr_dc)

    logging.warning(model)

    for epoch in tqdm(range(opt.epoch)):
        scheduler.step()
        _, _ = forward(model,
                       train_loader,
                       device,
                       writer,
                       epoch,
                       top_k=opt.top_k,
                       optimizer=optimizer,
                       train_flag=True)
        with torch.no_grad():
            h, m = forward(model,
                           test_loader,
                           device,
                           writer,
                           epoch,
                           top_k=opt.top_k,
                           train_flag=False)
            print(h, m)
Beispiel #6
0
def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    cur_dir = os.getcwd()
    train_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset,
                                       phrase='train')
    train_loader = DataLoader(train_dataset,
                              batch_size=opt.batch_size,
                              shuffle=True)
    test_dataset = MultiSessionsGraph(cur_dir + '/../datasets/' + opt.dataset,
                                      phrase='test')
    test_loader = DataLoader(test_dataset,
                             batch_size=opt.batch_size,
                             shuffle=False)

    log_dir = cur_dir + '/../log/' + str(
        opt.dataset) + '/' + str(opt) + time.strftime("%Y-%m-%d %H:%M:%S",
                                                      time.localtime())
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logging.warning('logging to {}'.format(log_dir))
    writer = SummaryWriter(log_dir)

    if opt.dataset == 'diginetica':
        n_node = 43097
    elif opt.dataset == 'yoochoose1_64' or opt.dataset == 'yoochoose1_4':
        n_node = 37483
    else:
        n_node = 309

    model = GNNModel(hidden_size=opt.hidden_size,
                     n_node=n_node,
                     use_san=opt.use_san,
                     use_gat=opt.use_gat).to(device)

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=opt.lr,
                                 weight_decay=opt.l2)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=opt.lr_dc_step,
                                                gamma=opt.lr_dc)

    logging.warning(model)

    for epoch in tqdm(range(opt.epoch)):
        scheduler.step()
        forward(model,
                train_loader,
                device,
                writer,
                epoch,
                top_k=opt.top_k,
                optimizer=optimizer,
                train_flag=True)
        with torch.no_grad():
            forward(model,
                    test_loader,
                    device,
                    writer,
                    epoch,
                    top_k=opt.top_k,
                    train_flag=False)
Beispiel #7
0
def eval_ai_generate(dataloader, params, eval_batch_size, split='test'):
    ranks_json = []
    dialog_encoder.eval()
    batch_idx = 0
    with torch.no_grad():
        batch_size = 500 * (params['n_gpus'] / 8)
        batch_size = min([1, 2, 4, 5, 100, 1000, 200, 8, 10, 40, 50, 500, 20, 25, 250, 125], \
             key=lambda x: abs(x-batch_size) if x <= batch_size else float("inf"))
        print("batch size for evaluation", batch_size)
        for epochId, _, batch in batch_iter(dataloader, params):
            if epochId == 1:
                break

            tokens = batch['tokens']
            num_rounds = tokens.shape[1]
            num_options = tokens.shape[2]
            tokens = tokens.view(-1, tokens.shape[-1])
            segments = batch['segments']
            segments = segments.view(-1, segments.shape[-1])
            sep_indices = batch['sep_indices']
            sep_indices = sep_indices.view(-1, sep_indices.shape[-1])
            mask = batch['mask']
            mask = mask.view(-1, mask.shape[-1])
            hist_len = batch['hist_len']
            hist_len = hist_len.view(-1)

            # get image features
            features = batch['image_feat']
            spatials = batch['image_loc']
            image_mask = batch['image_mask']

            # expand the image features to match those of tokens etc.
            max_num_regions = features.shape[-2]
            features = features.unsqueeze(1).unsqueeze(1).expand(
                eval_batch_size, num_rounds, num_options, max_num_regions,
                2048).contiguous()
            spatials = spatials.unsqueeze(1).unsqueeze(1).expand(
                eval_batch_size, num_rounds, num_options, max_num_regions,
                5).contiguous()
            image_mask = image_mask.unsqueeze(1).unsqueeze(1).expand(
                eval_batch_size, num_rounds, num_options,
                max_num_regions).contiguous()

            features = features.view(-1, max_num_regions, 2048)
            spatials = spatials.view(-1, max_num_regions, 5)
            image_mask = image_mask.view(-1, max_num_regions)

            assert tokens.shape[0] == segments.shape[0] == sep_indices.shape[0] == mask.shape[0] == \
                hist_len.shape[0] == features.shape[0] == spatials.shape[0] == \
                    image_mask.shape[0] == num_rounds * num_options * eval_batch_size

            output = []
            assert (eval_batch_size * num_rounds *
                    num_options) // batch_size == (eval_batch_size * num_rounds
                                                   * num_options) / batch_size
            for j in range(
                (eval_batch_size * num_rounds * num_options) // batch_size):
                # create chunks of the original batch
                item = {}
                item['tokens'] = tokens[j * batch_size:(j + 1) * batch_size, :]
                item['segments'] = segments[j * batch_size:(j + 1) *
                                            batch_size, :]
                item['sep_indices'] = sep_indices[j * batch_size:(j + 1) *
                                                  batch_size, :]
                item['mask'] = mask[j * batch_size:(j + 1) * batch_size, :]
                item['hist_len'] = hist_len[j * batch_size:(j + 1) *
                                            batch_size]

                item['image_feat'] = features[j * batch_size:(j + 1) *
                                              batch_size, :, :]
                item['image_loc'] = spatials[j * batch_size:(j + 1) *
                                             batch_size, :, :]
                item['image_mask'] = image_mask[j * batch_size:(j + 1) *
                                                batch_size, :]

                _, _, _, _, nsp_scores = forward(dialog_encoder,
                                                 item,
                                                 params,
                                                 output_nsp_scores=True,
                                                 evaluation=True)
                # normalize nsp scores
                nsp_probs = F.softmax(nsp_scores, dim=1)
                assert nsp_probs.shape[-1] == 2
                output.append(nsp_probs[:, 0])

            # print("output shape",torch.cat(output,0).shape)
            output = torch.cat(output, 0).view(eval_batch_size, num_rounds,
                                               num_options)
            ranks = scores_to_ranks(output)
            ranks = ranks.squeeze(1)
            for i in range(eval_batch_size):
                ranks_json.append({
                    "image_id":
                    batch["image_id"][i].item(),
                    "round_id":
                    int(batch["round_id"][i].item()),
                    "ranks": [rank.item() for rank in ranks[i][:]],
                })

            batch_idx += 1
    return ranks_json
Beispiel #8
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 #9
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 #10
0
        batch['segments'] = segments
        batch['sep_indices'] = sep_indices
        batch['mask'] = mask
        batch['hist_len'] = hist_len
        batch['next_sentence_labels'] = nsp_labels

        batch['image_feat'] = features.contiguous()
        batch['image_loc'] = spatials.contiguous()
        batch['image_mask'] = image_mask.contiguous()
        batch['image_target'] = image_target.contiguous()
        batch['image_label'] = image_label.contiguous()

        print("token shape", tokens.shape)
        loss = 0
        nsp_loss = 0
        _, _, _, _, nsp_scores = forward(dialog_encoder, batch, \
        params, sample_size=None, output_nsp_scores=True, evaluation=True)
        logging.info("nsp scores: {}".format(nsp_scores))
        # calculate dense annotation ce loss
        nsp_scores = nsp_scores.view(-1, 80, 2)
        nsp_loss = F.cross_entropy(nsp_scores.view(-1, 2), nsp_labels.view(-1))
        nsp_scores = nsp_scores[:, :, 0]

        gt_relevance = batch['gt_relevance'].to(device)
        # shuffle the gt relevance scores as well
        gt_relevance = gt_relevance[:, option_indices]
        ce_loss = ce_loss_fct(F.log_softmax(nsp_scores, dim=1),
                              F.softmax(gt_relevance, dim=1))
        loss = ce_loss + params['nsp_loss_coeff'] * nsp_loss
        loss /= params['batch_multiply']
        loss.backward()
        scheduler.step()
Beispiel #11
0
def get_sentiment(word):
    x = get_encoded_data(word, enc)
    sentiment = forward(x, W, bias)
    print(sentiment)
    return sentiment[0][0]
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)
def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    cur_dir = os.getcwd()
    train_filename, test_filename = 'train.txt', 'test.txt'
    dataset_name = opt.dataset
    if 'yoochoose' in opt.dataset:
        dataset_name = 'yoochoose'
        train_filename = opt.dataset + '-' + train_filename
        test_filename = opt.dataset + '-' + test_filename

    train_dataset = MultiSessionsGraph(
        name=train_filename,
        raw_dir=cur_dir + '/../../../_data/' + dataset_name + '/processed/',
        save_dir=cur_dir + '/../../../_data/' + dataset_name + '/saved/',
        force_reload=True)
    num_train = len(train_dataset)
    train_sampler = SubsetRandomSampler(torch.arange(num_train))
    train_loader = GraphDataLoader(
        train_dataset,
        batch_size=opt.batch_size,
        # sampler=train_sampler,
        shuffle=True,
        drop_last=False)

    test_dataset = MultiSessionsGraph(
        name=test_filename,
        raw_dir=cur_dir + '/../../../_data/' + dataset_name + '/processed/',
        save_dir=cur_dir + '/../../../_data/' + dataset_name + '/saved/',
        force_reload=True)
    num_test = len(test_dataset)
    test_sampler = SubsetRandomSampler(torch.arange(num_test))
    test_loader = GraphDataLoader(
        test_dataset,
        batch_size=opt.batch_size,
        # sampler=test_sampler,
        shuffle=False,
        drop_last=False)

    log_dir = os.path.join(cur_dir, 'log', str(opt.dataset), str(opt))
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    logging.warning('logging to {}'.format(log_dir))
    writer = SummaryWriter(log_dir)

    if 'diginetica' in opt.dataset:
        n_node = 43097
    elif 'yoochoose' in opt.dataset:
        n_node = 37483
    else:
        n_node = 309

    model = GNNModel(hidden_size=opt.hidden_size, n_node=n_node).to(device)

    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=opt.lr,
                                 weight_decay=opt.l2)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=opt.lr_dc_step,
                                                gamma=opt.lr_dc)

    logging.warning(model)

    for epoch in tqdm(range(opt.epoch)):
        forward(model,
                train_loader,
                device,
                writer,
                epoch,
                top_k=opt.top_k,
                optimizer=optimizer,
                train_flag=True)
        with torch.no_grad():
            forward(model,
                    test_loader,
                    device,
                    writer,
                    epoch,
                    top_k=opt.top_k,
                    train_flag=False)
        scheduler.step()