예제 #1
0
def make_normal_labels(mat_files_dir, dst_lab_files_dir, vocab_file):
    '''
      Creates png files with surface normal labels. Used for training models.
    '''
    # f = h5py.File(vocab_file, 'r')
    vocab = scipy.io.loadmat(vocab_file)
    codebook = vocab['vocabs'][0][0]['normals'][0][0]
    codebook = codebook.astype(np.float64)
    normal_info = load_pickle(
        '/data/bw462/nyu/surfacenormal_metadata/all_normals.pklz')
    assert NUM_NORMALS_CLASSES == codebook.shape[0]
    # whether to create Left-Right Flipped ground truth. This is beta tested only.
    flip = False  # flip = True is beta
    iterator = zip(normal_info['all_normals'],
                   normal_info['all_valid_depth_masks'],
                   normal_info['all_filenames'])
    for ind, (raw_normals, valid_depth, file_name) in enumerate(iterator):
        normal_labels = assign_normals_to_codebook(raw_normals, codebook)
        assert normal_labels.min() >= 0 and normal_labels.max(
        ) < NUM_NORMALS_CLASSES
        # now account for valid_depth mask
        normal_labels[valid_depth == 0] = IGNORE_LABEL_NORMALS
        dst_name = file_name + '_SN%d.png' % (NUM_NORMALS_CLASSES)
        dst_file_path = os.path.join(dst_lab_files_dir, dst_name)
        im = Image.fromarray(normal_labels.astype(np.uint8))
        im.save(dst_file_path)
        if ind % 100 == 0:
            print('Wrote %s' % (dst_file_path))
        if flip:
            # first we do a horizontal flip
            flipped_normals = raw_normals[:, ::-1, :]

            # now we need to do a rotation
            # flip the "X" axis
            flipped_normals[:, :, 0] *= -1.
    def load_normals_file(self):
        normals_file = self.normals_file
        normals_data = load_pickle(normals_file)
        filename_to_id = \
            {normals_data['all_filenames'][x]: x for x in range(len(normals_data['all_filenames']))}

        def get_normal_and_valid_depth(filename):
            id = filename_to_id[filename]
            return normals_data['all_normals'][id], normals_data['all_valid_depth_masks'][id]

        return get_normal_and_valid_depth
예제 #3
0
    model = load_infersent_model(MODEL_PATH,
                                 bsize=args.bsize,
                                 word_emb_dim=args.word_emb_dim,
                                 enc_lstm_dim=args.enc_lstm_dim,
                                 version=args.version)

    sentence_embeddings = []
    prefix = os.path.dirname(args.filepath)
    if args.streaming:
        output_path = os.path.join(
            prefix, f'sentence_embeddings_{args.version}_streaming.pkl')
    else:
        output_path = os.path.join(prefix,
                                   f'sentence_embeddings_{args.version}.pkl')

    sentences = load_pickle(args.filepath)
    flattened_sentences = [
        utterance for conversation in sentences for utterance in conversation
    ]

    print('Encoding sentences ...')
    flattened_embeddings = model.encode(flattened_sentences,
                                        tokenize=True,
                                        bsize=64)
    print('InferSent encoding done.')

    idx = 0
    sent_idx = 0
    for conversation in sentences:
        idx += 1
        conversation_embeddings = []
예제 #4
0
 def __init__(self, vocab_file, gt_file=None):
     super(NormalsEvaluator, self).__init__(vocab_file)
     if gt_file is not None:
         self.gt_data = load_pickle(gt_file)
     self.metrics = None
예제 #5
0
    argparser.add_argument('--step',
                           type=int,
                           default=100,
                           help="Number of steps for saving output")
    argparser.add_argument('--version',
                           type=int,
                           default=1,
                           help="Which model version of inferSent to use. "
                           "V1 has been trained on GloVe. "
                           "V2 has been trained on fastText.")
    args = argparser.parse_args()

    train_dir = os.path.join(args.dataset, 'train')
    train_file_path = os.path.join(
        train_dir, f'sentence_embeddings_{args.version}_streaming.pkl')
    train_embeddings = load_pickle(train_file_path)
    flattened_train_embeddings = [
        utterance for conversation in train_embeddings
        for utterance in conversation
    ]

    if args.fixeddim:
        pca_model_file_path = os.path.join(
            train_dir, f'v{args.version}_PCA_model_{args.ndim}.pkl')
        if args.savepca:
            pca_embeddings = fit_pca(flattened_train_embeddings, args.ndim)
            dump_pickle(pca_embeddings, pca_model_file_path)
        else:
            pca_embeddings = load_pickle(pca_model_file_path)
    else:
        pca_model_file_path = os.path.join(