Exemple #1
0
def convert_matlab_normals_to_numpy_files(mat_files_dir, dst_dir):
    file_list = os.listdir(mat_files_dir)
    file_list = [
        x for x in file_list if x.endswith('mat') and x.find('nm') == -1
    ]
    file_list.sort()
    all_valid_depth_masks = []
    all_normals = []
    for _ind, file_name in enumerate(file_list):
        file_path = os.path.join(mat_files_dir, file_name)
        dt = scipy.io.loadmat(file_path)
        raw_normals = [dt['nx'], dt['ny'], dt['nz']]
        raw_normals = np.stack(raw_normals).astype(np.float32)
        valid_depth = dt['depthValid'].astype(np.float32)
        raw_normals = np.transpose(raw_normals, (1, 2, 0))
        all_valid_depth_masks.append(valid_depth)
        all_normals.append(raw_normals)

    # make filenames
    all_filenames = [x.replace('.mat', '') for x in file_list]
    # concat
    all_normals = np.stack(all_normals)
    all_valid_depth_masks = np.stack(all_valid_depth_masks)
    assert all_normals.shape[0] == all_valid_depth_masks.shape[0]
    assert len(all_filenames) == all_normals.shape[0]
    assert all_normals.shape[0] == 1449
    save_dict = {
        'all_normals': all_normals,
        'all_valid_depth_masks': all_valid_depth_masks,
        'all_filenames': all_filenames,
    }
    dst_file = os.path.join(dst_dir, 'all_normals.pklz')
    save_pickle(dst_file, save_dict)
    print('Wrote', dst_file)
Exemple #2
0
def pickle_data(train_list, dev_list, test_list, data_type='lap14'):
    token_vocab_file = os.path.join(vocab_dir, data_type,
                                    config['token_vocab_file'])
    char_vocab_file = os.path.join(vocab_dir, data_type,
                                   config['char_vocab_file'])
    pos_vocab_file = os.path.join(vocab_dir, data_type,
                                  config['pos_vocab_file'])
    dep_type_vocab_file = os.path.join(vocab_dir, data_type,
                                       config['dep_type_vocab_file'])

    token_vocab = Vocab.load(token_vocab_file)
    char_vocab = Vocab.load(char_vocab_file)
    pos_vocab = Vocab.load(pos_vocab_file)
    dep_vocab = Vocab.load(dep_type_vocab_file)

    processed_train = construct_instance(train_list, token_vocab, char_vocab,
                                         pos_vocab, dep_vocab, True)
    processed_dev = construct_instance(dev_list, token_vocab, char_vocab,
                                       pos_vocab, dep_vocab, False)
    processed_test = construct_instance(test_list, token_vocab, char_vocab,
                                        pos_vocab, dep_vocab, False)

    print('Saving pickle to ', inst_pl_file)
    print('Saving sent size Train: %d, Dev: %d, Test:%d' %
          (len(processed_train), len(processed_dev), len(processed_test)))
    save_pickle(inst_pl_file, [
        processed_train, processed_dev, processed_test, token_vocab,
        char_vocab, pos_vocab, dep_vocab
    ])
Exemple #3
0
def main(args):
    torch.cuda.set_device(args.gpu)

    crit = nn.NLLLoss(ignore_index=-1)

    # Dataset and Loader
    dataset_val = Dataset(args,
                          split_name=args.split_name,
                          batch_per_gpu=args.batchsize)
    loader_val = torchdata.DataLoader(dataset_val,
                                      batch_size=args.batchsize,
                                      shuffle=False,
                                      collate_fn=user_scattered_collate,
                                      num_workers=5,
                                      drop_last=True)

    # Network Builders
    builder = ModelBuilder()
    print('Loading encoder from: %s' % (args.weights_encoder))
    print('Loading decoder from: %s' % (args.weights_decoder))
    net_encoder = builder.build_encoder(arch=args.arch_encoder,
                                        fc_dim=args.fc_dim,
                                        weights=args.weights_encoder)
    net_decoder = builder.build_decoder(arch=args.arch_decoder,
                                        fc_dim=args.fc_dim,
                                        num_class=dataset_val.num_classes,
                                        weights=args.weights_decoder,
                                        use_softmax=True)
    segmentation_module = SegmentationModule(net_encoder, net_decoder, crit)

    segmentation_module.cuda()

    # Main loop
    if args.dataset.startswith('nyuv2sn'):
        metrics = evaluate_surface_normals(segmentation_module, loader_val,
                                           args)
    else:
        metrics = evaluate_segmentation(segmentation_module, loader_val, args)

    save_pickle(args.result_file, metrics)

    print('Evaluation Done!')
Exemple #4
0
def pickle_data():
    token_vocab = Vocab.load(token_vocab_file)
    char_vocab = Vocab.load(char_vocab_file)
    ent_type_vocab = Vocab.load(ent_type_vocab_file)
    ent_ref_vocab = Vocab.load(ent_ref_vocab_file)
    tri_type_vocab = Vocab.load(tri_type_vocab_file)
    arg_type_vocab = Vocab.load(arg_type_vocab_file)
    action_vocab = Vocab.load(action_vocab_file)
    pos_vocab = Vocab.load(pos_vocab_file)

    processed_train = construct_instance(train_list, token_vocab, char_vocab, ent_type_vocab, ent_ref_vocab, tri_type_vocab,
                       arg_type_vocab, action_vocab, pos_vocab)
    processed_dev = construct_instance(dev_list, token_vocab, char_vocab, ent_type_vocab, ent_ref_vocab, tri_type_vocab,
                       arg_type_vocab, action_vocab, pos_vocab, False)
    processed_test = construct_instance(test_list, token_vocab, char_vocab, ent_type_vocab, ent_ref_vocab, tri_type_vocab,
                       arg_type_vocab, action_vocab, pos_vocab, False)

    print('Saving pickle to ', inst_pl_file)
    print('Saving sent size Train: %d, Dev: %d, Test:%d'%(len(processed_train),len(processed_dev),len(processed_test)))
    save_pickle(inst_pl_file, [processed_train, processed_dev, processed_test, token_vocab, char_vocab, ent_type_vocab,
                               ent_ref_vocab, tri_type_vocab, arg_type_vocab, action_vocab,
                               pos_vocab])

    scale = np.sqrt(3.0 / embedd_dim)
    vocab_dict = token_vocab.tok2idx
    table = np.empty([len(vocab_dict), embedd_dim], dtype=np.float32)
    oov = 0
    for word, index in vocab_dict.items():
        if word in embedd_dict:
            embedding = embedd_dict[word]
        elif word.lower() in embedd_dict:
            embedding = embedd_dict[word.lower()]
        else:
            embedding = np.random.uniform(-scale, scale, [1, embedd_dim]).astype(np.float32)
            oov += 1
        table[index, :] = embedding

    np.save(vec_npy_file, table)
    print('pretrained embedding oov: %d' % oov)
    print()