-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
150 lines (110 loc) · 5.66 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
import argparse
import os
from tqdm import tqdm
import torch
from torch.utils.data import DataLoader
from src.models import Uni_Retrieval
from src.models import I2ITestDataset, I2ITrainDataset, I2MTestDataset, I2MTrainDataset, I2TTestDataset, I2TTrainDataset
from src.utils import setup_seed, save_loss
def parse_args():
parser = argparse.ArgumentParser(description='Parse args for Uni-Retrieval Train.')
# project settings
parser.add_argument('--resume', default='', type=str, help='load model checkpoint from given path')
parser.add_argument('--device', default='cuda:0')
parser.add_argument('--seed', default=42, type=int)
parser.add_argument('--num_workers', default=6, type=int)
parser.add_argument('--gram_encoder_path', default='pretrained/vgg_normalised.pth', type=str, help='load vgg from given path')
parser.add_argument('--style_prompt_path', default='pretrained/style_cluster.npy', type=str, help='load vgg from given path')
# data settings
parser.add_argument("--type", type=str, default='style2image', help='choose train test2image or style2image.')
parser.add_argument("--root_json_path", type=str, default='imagenet/test.json')
parser.add_argument("--other_json_path", type=str, default='imagenet/test.json')
parser.add_argument("--root_file_path", type=str, default='imagenet/')
parser.add_argument("--other_file_path", type=str, default='imagenet-s/')
parser.add_argument("--batch_size", type=int, default=16)
# model settings
parser.add_argument('--n_banks', type=int, default=4)
parser.add_argument('--bank_dim', type=int, default=1024)
parser.add_argument('--n_prompts', type=int, default=4)
parser.add_argument('--prompt_dim', type=int, default=1024)
# optimizer settings
parser.add_argument('--clip_ln_lr', type=float, default=1e-5)
parser.add_argument('--prompt_lr', type=float, default=1e-5)
args = parser.parse_args()
return args
def train(args, model, dataloader, optimizer):
model.train()
best_loss = 10000000
losses = []
epoches = []
count = 0
if args.type == 'text2image':
for epoch in range(args.epochs):
temp_loss = []
for data in enumerate(tqdm(dataloader)):
caption = model.tokenizer(data[1][0]).to(args.device, non_blocking=True)
image = data[1][1].to(args.device, non_blocking=True)
negative_image = data[1][2].to(args.device, non_blocking=True)
text_feature = model(caption, dtype='text')
image_feature = model(image, dtype='image')
negative_feature = model(negative_image, dtype='image')
loss = model.get_loss(image_feature, text_feature, negative_feature, optimizer)
temp_loss.append(loss)
print("loss: {:.6f}".format(loss))
if len(temp_loss)!=0:
res = round(sum(temp_loss)/len(temp_loss), 6)
print("epoch_{} loss is {}.".format(epoch, res))
losses.append(res)
epoches.append(epoch)
if res<best_loss:
best_loss = res
save_obj = model.state_dict()
torch.save(save_obj, os.path.join(args.output_dir, '{}_epoch{}.pth'.format(args.type, epoch)))
count = 0
else:
count +=1
if best_loss < 0.0001 or count >= 5:
break
else: # style2image retrival
for epoch in range(args.epochs):
temp_loss = []
for data in enumerate(tqdm(dataloader)):
original_image = data[1][0].to(args.device, non_blocking=True)
retrival_image = data[1][1].to(args.device, non_blocking=True)
negative_image = data[1][2].to(args.device, non_blocking=True)
original_feature = model(original_image, dtype='image')
retrival_feature = model(retrival_image, dtype='image')
negative_feature = model(negative_image, dtype='image')
loss = model.get_loss(original_feature, retrival_feature, negative_feature, optimizer)
temp_loss.append(loss)
print("loss: {:.6f}".format(loss))
if len(temp_loss)!=0:
res = round(sum(temp_loss)/len(temp_loss), 6)
print("epoch_{} loss is {}.".format(epoch, res))
losses.append(res)
epoches.append(epoch)
if res<best_loss:
best_loss = res
save_obj = model.state_dict()
torch.save(save_obj, os.path.join(args.output_dir, '{}_epoch{}.pth'.format(args.type, epoch)))
count = 0
else:
count +=1
if best_loss < 0.0001 or count >= 5:
break
return losses, epoches
if __name__ == "__main__":
args = parse_args()
setup_seed(args.seed)
model = Uni_Retrieval(args)
model = model.to(args.device)
if args.resume:
model.load_state_dict(torch.load(args.resume))
optimizer = torch.optim.Adam([
{'params': model.openclip.parameters(), 'lr': args.clip_ln_lr},
{'params': [model.prompt_lr], 'lr': args.prompt_lr}])
train_dataset = I2TTrainDataset(args.root_file_path, args.root_json_path, model.pre_process_train, model.tokenizer)
train_loader = DataLoader(dataset=train_dataset, batch_size=args.batch_size, num_workers=args.num_workers,
pin_memory=True, prefetch_factor=4, shuffle=False, drop_last=True)
loss, epochs = train(args, model, train_loader, optimizer)
save_loss(loss, epochs, args.out_path)