-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_embeddings_ours.py
193 lines (150 loc) · 6.84 KB
/
get_embeddings_ours.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import os
import sys
import numpy as np
import config as cfg
import models.PointNetVlad as PNV
import torch
from loading_pointclouds import *
from torch.backends import cudnn
from tqdm import tqdm
#os.environ["CUDA_VISIBLE_DEVICES"] = "0"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
cudnn.enabled = True
parser = argparse.ArgumentParser()
parser.add_argument('--log_dir', default='log/', help='Log dir [default: log]')
parser.add_argument('--results_dir', default='results/',
help='results dir [default: results/]')
parser.add_argument('--positives_per_query', type=int, default=2,
help='Number of potential positives in each training tuple [default: 2]')
parser.add_argument('--negatives_per_query', type=int, default=18,
help='Number of definite negatives in each training tuple [default: 18]')
parser.add_argument('--max_epoch', type=int, default=100,
help='Epoch to run [default: 100]')
parser.add_argument('--batch_num_queries', type=int, default=2,
help='Batch Size during training [default: 2]')
parser.add_argument('--learning_rate', type=float, default=0.000005,
help='Initial learning rate [default: 0.000005]')
parser.add_argument('--momentum', type=float, default=0.9,
help='Initial learning rate [default: 0.9]')
parser.add_argument('--optimizer', default='adam',
help='adam or momentum [default: adam]')
parser.add_argument('--decay_step', type=int, default=200000,
help='Decay step for lr decay [default: 200000]')
parser.add_argument('--decay_rate', type=float, default=0.7,
help='Decay rate for lr decay [default: 0.7]')
parser.add_argument('--margin_1', type=float, default=0.5,
help='Margin for hinge loss [default: 0.5]')
parser.add_argument('--margin_2', type=float, default=0.2,
help='Margin for hinge loss [default: 0.2]')
parser.add_argument('--loss_function', default='quadruplet', choices=[
'triplet', 'quadruplet'], help='triplet or quadruplet [default: quadruplet]')
parser.add_argument('--loss_not_lazy', action='store_false',
help='If present, do not use lazy variant of loss')
parser.add_argument('--loss_ignore_zero_batch', action='store_true',
help='If present, mean only batches with loss > 0.0')
parser.add_argument('--triplet_use_best_positives', action='store_true',
help='If present, use best positives, otherwise use hardest positives')
parser.add_argument('--resume', action='store_true',
help='If present, restore checkpoint and resume training')
parser.add_argument('--dataset_folder', default='../../dataset/',
help='PointNetVlad Dataset Folder')
FLAGS = parser.parse_args()
cfg.BATCH_NUM_QUERIES = FLAGS.batch_num_queries
#cfg.EVAL_BATCH_SIZE = 12
cfg.NUM_POINTS = 256
cfg.TRAIN_POSITIVES_PER_QUERY = FLAGS.positives_per_query
cfg.TRAIN_NEGATIVES_PER_QUERY = FLAGS.negatives_per_query
cfg.MAX_EPOCH = FLAGS.max_epoch
cfg.BASE_LEARNING_RATE = FLAGS.learning_rate
cfg.MOMENTUM = FLAGS.momentum
cfg.OPTIMIZER = FLAGS.optimizer
cfg.DECAY_STEP = FLAGS.decay_step
cfg.DECAY_RATE = FLAGS.decay_rate
cfg.MARGIN1 = FLAGS.margin_1
cfg.MARGIN2 = FLAGS.margin_2
cfg.FEATURE_OUTPUT_DIM = 256
cfg.LOSS_FUNCTION = FLAGS.loss_function
cfg.TRIPLET_USE_BEST_POSITIVES = FLAGS.triplet_use_best_positives
cfg.LOSS_LAZY = FLAGS.loss_not_lazy
cfg.LOSS_IGNORE_ZERO_BATCH = FLAGS.loss_ignore_zero_batch
cfg.LOG_DIR = FLAGS.log_dir
if not os.path.exists(cfg.LOG_DIR):
os.mkdir(cfg.LOG_DIR)
LOG_FOUT = open(os.path.join(cfg.LOG_DIR, 'log_train.txt'), 'w')
LOG_FOUT.write(str(FLAGS) + '\n')
cfg.RESULTS_FOLDER = FLAGS.results_dir
print("cfg.RESULTS_FOLDER:"+str(cfg.RESULTS_FOLDER))
cfg.DATASET_FOLDER = FLAGS.dataset_folder
cfg.BN_INIT_DECAY = 0.5
cfg.BN_DECAY_DECAY_RATE = 0.5
BN_DECAY_DECAY_STEP = float(cfg.DECAY_STEP)
cfg.BN_DECAY_CLIP = 0.99
HARD_NEGATIVES = {}
TRAINING_LATENT_VECTORS = []
TOTAL_ITERATIONS = 0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f'cfg.BATCH_NUM_QUERIES : {cfg.BATCH_NUM_QUERIES}')
def get_bn_decay(batch):
bn_momentum = cfg.BN_INIT_DECAY * \
(cfg.BN_DECAY_DECAY_RATE **
(batch * cfg.BATCH_NUM_QUERIES // BN_DECAY_DECAY_STEP))
return min(cfg.BN_DECAY_CLIP, 1 - bn_momentum)
def log_string(out_str):
LOG_FOUT.write(out_str + '\n')
LOG_FOUT.flush()
print(out_str)
# learning rate halfed every 5 epoch
def get_learning_rate(epoch):
learning_rate = cfg.BASE_LEARNING_RATE * ((0.9) ** (epoch // 5))
learning_rate = max(learning_rate, 0.00001) # CLIP THE LEARNING RATE!
return learning_rate
def get_feature_representation(filename, model):
model.eval()
queries = load_pc_files([filename],True)
queries = np.expand_dims(queries, axis=1)
with torch.no_grad():
q = torch.from_numpy(queries).float()
q = q.to(device)
output = model(q)
output = output.detach().cpu().numpy()
output = np.squeeze(output)
model.train()
return output
def train():
learning_rate = get_learning_rate(0)
model = PNV.PointNetVlad(global_feat=True, feature_transform=True,
max_pool=False, output_dim=cfg.FEATURE_OUTPUT_DIM, num_points=cfg.NUM_POINTS)
model = model.to(device)
parameters = filter(lambda p: p.requires_grad, model.parameters())
if cfg.OPTIMIZER == 'momentum':
optimizer = torch.optim.SGD(
parameters, learning_rate, momentum=cfg.MOMENTUM)
elif cfg.OPTIMIZER == 'adam':
optimizer = torch.optim.Adam(parameters, learning_rate)
else:
optimizer = None
exit(0)
model_path = cfg.RESULTS_FOLDER + "checkpoints.pth.tar"
print("Loading model from ", model_path)
checkpoint = torch.load(model_path)
print(checkpoint.keys())
saved_state_dict = checkpoint['state_dict']
starting_epoch = checkpoint['epoch']
model.load_state_dict(saved_state_dict)
optimizer.load_state_dict(checkpoint['optimizer'])
data_path = "data/"
files = sorted(os.listdir(data_path))
train_file_idxs = np.arange(0, len(files))
print(f'Length of train ids : {len(train_file_idxs)}')
queries = []
for i in tqdm(train_file_idxs):
path = os.path.join(data_path, files[i])
query = get_feature_representation(path, model)
queries.append(query)
queries = np.array(queries)
save_file = cfg.RESULTS_FOLDER + "embeddings.npy"
np.save(save_file, queries)
print(queries.shape)
train()