-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_nce.py
187 lines (155 loc) · 4.36 KB
/
run_nce.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
import argparse
import json
import os
import torch
import transformers
from torch.utils.data import DataLoader
from transformers import AdamW
from src.nce.datasets import CityFlowNLDataset, CityFlowNLDatasetInference
from src.nce.learner import Learner
from src.nce.models import NCEModel
from src.utils.signature import print_signature
from src.utils.train_utils import seed_everything
os.environ["TOKENIZERS_PARALLELISM"] = "false"
print("Using Torch version:", torch.__version__)
print("Using Transformers version:", transformers.__version__)
parser = argparse.ArgumentParser(description="CVPR 2021 Challenge Track 5: Natural Language-Based Vehicle Retrieval")
parser.add_argument(
"--train_json",
default="data/data/my_train.json",
type=str,
help="path to the train data tracks",
)
parser.add_argument(
"--validation_json",
default="data/data/my_validation.json",
type=str,
help="path to the validation data tracks",
)
parser.add_argument(
"--test_tracks",
default="data/data/test-tracks_2.json",
type=str,
help="path to the test tracks",
)
parser.add_argument(
"--test_queries",
default="data/data/test-queries.json",
type=str,
help="path to the test queries",
)
parser.add_argument(
"--submission_path",
default="outputs/clip/results.json",
type=str,
help="path to the submission save path",
)
parser.add_argument(
"--output_dir",
default="outputs/clip/",
type=str,
help="path to directory for models saving",
)
parser.add_argument(
"--image_dir",
default="data/data/",
type=str,
help="path to the image directory",
)
parser.add_argument(
"--clip_model",
default="ViT-B/32",
type=str,
help="pretrained clip model name ",
)
parser.add_argument(
"--out_features",
default=256,
type=int,
help="embedding dim for both image and text",
)
parser.add_argument(
"--batch_size",
default=16,
type=int,
help="num examples per batch",
)
parser.add_argument(
"--accu_grad_step",
default=2,
type=int,
help="num accumulative gradient step",
)
parser.add_argument(
"--lr",
default=1e-5,
type=float,
help="learning rate",
)
parser.add_argument(
"--n_epochs",
default=10,
type=int,
help="num epochs required for training",
)
parser.add_argument(
"--seed",
default=1710,
type=int,
help="seed for reproceduce",
)
args = parser.parse_args()
if __name__ == "__main__":
seed_everything(seed=args.seed)
print_signature()
with open(args.train_json, "r") as f:
my_train = json.load(f)
with open(args.validation_json, "r") as f:
my_validation = json.load(f)
with open(args.test_tracks) as f:
test_tracks = json.load(f)
with open(args.test_queries) as f:
queries = json.load(f)
train_data, val_data = [], []
for k, v in my_train.items():
v["id"] = k
train_data.append(v)
for k, v in my_validation.items():
v["id"] = k
val_data.append(v)
for k, v in test_tracks.items():
v["id"] = k
test_data = list(test_tracks.values())
train_ds = CityFlowNLDataset(cityflow_path=args.image_dir, data=train_data, image_size=args.image_size, k_neg=5)
val_ds = CityFlowNLDatasetInference(
cityflow_path=args.image_dir,
data=val_data,
image_size=args.image_size,
)
train_dl = DataLoader(train_ds, batch_size=args.batch_size, shuffle=True)
val_dl = DataLoader(val_ds, batch_size=args.batch_size * 2, shuffle=False)
test_ds = CityFlowNLDatasetInference(
cityflow_path=args.image_dir,
data=test_data,
image_size=args.image_size,
)
test_dl = DataLoader(test_ds, batch_size=args.batch_size * 2, shuffle=False)
model = NCEModel(args.clip_model, args.out_features)
model.cuda()
learner = Learner(
epoch=args.n_epochs,
lr=args.lr,
accu_grad_step=args.accu_grad_step,
train_dl=train_dl,
val_dl=val_dl,
model=model,
optim=AdamW,
)
learner.train(output_dir=args.output_dir)
learner.model.load_state_dict(torch.load(os.path.join(args.output_dir, "ckpt.pth")))
learner.model.eval()
learner.eval_one_epoch(val_dl)
with torch.no_grad():
submits = learner.match_track2querry(queries, test_dl)
with open(args.submission_path, "w") as f:
json.dump(submits, f)