-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
161 lines (138 loc) · 3.87 KB
/
train.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
import os
import argparse
from loguru import logger
from src.utils.utils import setup_model
from src.engine.engines import Engine
import wandb
# main function to call from workflow
def main():
# set up arg parser
parser = argparse.ArgumentParser(description="Train Vision Prompt CLIP")
# check cuda availability
parser.add_argument(
"--model",
type=str,
default="VPT-CLIP-Shallow",
help="For Saving and loading the current Model",
)
parser.add_argument(
"--backbone",
type=str,
default="ViT-B16",
help="For Saving and loading the current Model",
)
parser.add_argument(
"--data",
type=str,
default="vtab-caltech101",
help="For Saving and loading the current Model",
)
parser.add_argument(
"--type",
type=str,
default="vision",
help="Specify the type of inference, vision or vision-language",
)
parser.add_argument(
"--shots",
type=int,
default=8,
help="Specify the number of shots, -1 for all shots",
)
parser.add_argument(
"--seed",
type=int,
default=0,
help="Specify the seed",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
help="For Saving and loading the current Model",
)
parser.add_argument(
"--evluate",
type=bool,
default=False,
help="Whether to train or not",
)
parser.add_argument(
"--save_model",
type=bool,
default=False,
help="Whether to save the model or not",
)
parser.add_argument(
"--wandb",
type=bool,
default=False,
help="Whether to save the model or not",
)
args = parser.parse_args()
print(args)
# set up cfg and args
(
model,
train_loader,
test_loader,
dataset_config,
) = setup_model(args)
# setup engine
engine = Engine(
model=model,
device=args.device,
train_loader=train_loader,
test_loader=test_loader,
configs=dataset_config,
)
# train the model
model_path = "./src/logs/{}/{}/epochs{}/model.pth".format(
dataset_config.DATA.NAME,
dataset_config.MODEL.TYPE,
dataset_config.SOLVER.TOTAL_EPOCH,
)
log_dir = "./src/logs/{}/{}/shots{}/epochs{}/".format(
dataset_config.DATA.NAME,
dataset_config.MODEL.TYPE,
dataset_config.DATA.SHOTS,
dataset_config.SOLVER.TOTAL_EPOCH,
)
# print log path
# check if directory exists
if not os.path.exists(log_dir):
os.makedirs(log_dir)
logger.add(log_dir + "output.log", rotation="10 MB")
if args.wandb:
name = "{}-{}-{}-{}-{}".format(
dataset_config.DATA.NAME,
dataset_config.MODEL.TYPE,
dataset_config.DATA.SHOTS,
dataset_config.SOLVER.TOTAL_EPOCH,
args.seed,
)
inference_type = "Head" if args.type == "vision" else "Contrastive Prediction"
wandb.init(
project="PEFT_CLIP",
name=name,
config={
"model": args.model,
"dataset": args.data[5:],
"backbone": args.backbone,
"shots": dataset_config.DATA.SHOTS,
"epochs": dataset_config.SOLVER.TOTAL_EPOCH
+ dataset_config.SOLVER.WARMUP_EPOCH,
"lr": dataset_config.SOLVER.BASE_LR,
"type": inference_type,
"seed": args.seed,
},
)
if not args.evluate or not os.path.exists(model_path):
# evluate the model
engine.train(save_model=args.save_model)
# engine.evaluate()
elif args.evluate:
# evaluate the model
engine.evaluate()
if __name__ == "__main__":
main()