-
Notifications
You must be signed in to change notification settings - Fork 0
/
finetune.py
221 lines (187 loc) · 7.02 KB
/
finetune.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
import os
import torch
import clip
import os
from tqdm import tqdm
import time
from PIL import Image
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize, RandomResizedCrop
from torchvision.datasets import ImageFolder
from utils import ModelWrapper, maybe_dictionarize_batch, cosine_lr
def _convert_to_rgb(image):
return image.convert('RGB')
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
"--data-location",
type=str,
default=os.path.expanduser('~/data'),
help="The root directory for the datasets.",
)
parser.add_argument(
"--model-location",
type=str,
default=os.path.expanduser('~/tmp123'),
help="Where to download the models.",
)
parser.add_argument(
"--batch-size",
type=int,
default=256,
)
parser.add_argument(
"--custom-template", action="store_true", default=False,
)
parser.add_argument(
"--workers",
type=int,
default=8,
)
parser.add_argument(
"--epochs",
type=int,
default=4,
)
parser.add_argument(
"--warmup-length",
type=int,
default=50,
)
parser.add_argument(
"--lr",
type=float,
default=1e-5,
)
parser.add_argument(
"--wd",
type=float,
default=0.1,
)
parser.add_argument(
"--model",
default='ViT-B/32',
help='Model to use -- you can try another like ViT-L/14'
)
parser.add_argument(
"--name",
default='finetune_cp',
help='Filename for the checkpoints.'
)
return parser.parse_args()
def zeroshot_classifier(model, classnames, templates, device):
print('Building zero-shot classifier.')
with torch.no_grad():
zeroshot_weights = []
for classname in tqdm(classnames):
texts = [template(classname) for template in templates] #format with class
texts = clip.tokenize(texts).to(device) #tokenize
class_embeddings = model.encode_text(texts)
class_embeddings /= class_embeddings.norm(dim=-1, keepdim=True)
class_embedding = class_embeddings.mean(dim=0)
class_embedding /= class_embedding.norm()
zeroshot_weights.append(class_embedding)
zeroshot_weights = torch.stack(zeroshot_weights, dim=1).to(device)
return 100*zeroshot_weights.t()
if __name__ == '__main__':
args = parse_arguments()
DEVICE = 'cuda'
template = [lambda c: f'a photo of a syringe containing the drug: "{c}".',]
base_model, preprocess = clip.load(args.model, 'cuda', jit=False)
train_preprocess = preprocess
traindir = os.path.join(args.data_location, 'train')
valdir = os.path.join(args.data_location, 'test')
normalize = Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711))
train_preprocess = Compose([
RandomResizedCrop(base_model.visual.input_resolution, scale=(0.9, 1.0), interpolation=Image.BICUBIC),
_convert_to_rgb,
ToTensor(),
normalize,
])
train_dataset = ImageFolder(
traindir, transform=train_preprocess)
train_loader = torch.utils.data.DataLoader(
train_dataset,
shuffle=True,
batch_size=args.batch_size,
num_workers=args.workers,
pin_memory=True,
)
test_dataset = ImageFolder(valdir, transform=preprocess)
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=args.batch_size,
num_workers=args.workers
)
idx_to_class = dict((v, k)
for k, v in train_dataset.class_to_idx.items())
classnames = [idx_to_class[i].replace(
'_', ' ') for i in range(len(idx_to_class))]
print('classnames are', classnames)
print(classnames)
clf = zeroshot_classifier(base_model, classnames, template, DEVICE)
NUM_CLASSES = len(classnames)
feature_dim = base_model.visual.output_dim
model = ModelWrapper(base_model, feature_dim, NUM_CLASSES, normalize=True, initial_weights=clf)
for p in model.parameters():
p.data = p.data.float()
model = model.cuda()
devices = [x for x in range(torch.cuda.device_count())]
model = torch.nn.DataParallel(model, device_ids=devices)
model_parameters = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.AdamW(model_parameters, lr=args.lr, weight_decay=args.wd)
num_batches = len(train_loader)
scheduler = cosine_lr(optimizer, args.lr, args.warmup_length, args.epochs * num_batches)
loss_fn = torch.nn.CrossEntropyLoss()
if os.path.exists(args.model_location):
model_path = os.path.join(args.model_location, f'{args.name}_0.pt')
print('Saving model to', model_path)
torch.save(model.module.state_dict(), model_path)
for epoch in range(args.epochs):
# Train
model.train()
end = time.time()
for i, batch in enumerate(train_loader):
step = i + epoch * num_batches
scheduler(step)
optimizer.zero_grad()
batch = maybe_dictionarize_batch(batch)
inputs, labels = batch['images'].to(DEVICE), batch['labels'].to(DEVICE)
data_time = time.time() - end
logits = model(inputs)
loss = loss_fn(logits, labels)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
batch_time = time.time() - end
end = time.time()
if i % 20 == 0:
percent_complete = 100.0 * i / len(train_loader)
print(
f"Train Epoch: {epoch} [{percent_complete:.0f}% {i}/{len(train_loader)}]\t"
f"Loss: {loss.item():.6f}\tData (t) {data_time:.3f}\tBatch (t) {batch_time:.3f}", flush=True
)
# #Evaluate
test_loader = test_loader
model.eval()
with torch.no_grad():
print('*'*80)
print('Starting eval')
correct, count = 0.0, 0.0
pbar = tqdm(test_loader)
for batch in pbar:
batch = maybe_dictionarize_batch(batch)
inputs, labels = batch['images'].to(DEVICE), batch['labels'].to(DEVICE)
logits = model(inputs)
loss = loss_fn(logits, labels)
pred = logits.argmax(dim=1, keepdim=True)
correct += pred.eq(labels.view_as(pred)).sum().item()
count += len(logits)
pbar.set_description(
f"Val loss: {loss.item():.4f} Acc: {100*correct/count:.2f}")
top1 = correct / count
print(f'Val acc at epoch {epoch}: {100*top1:.2f}')
if os.path.exists(args.model_location):
model_path = os.path.join(args.model_location, f'{args.name}_{epoch + 1}.pt')
print('Saving model to', model_path)
torch.save(model.module.state_dict(), model_path)