-
Notifications
You must be signed in to change notification settings - Fork 2
/
finetune_age.py
294 lines (252 loc) · 9.2 KB
/
finetune_age.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import argparse
import os
import torch
import clip
import os
from tqdm import tqdm
import time
import wandb
import random
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torchvision.transforms.functional as TF
import copy
from timm.data.transforms_factory import transforms_imagenet_train
from datasets.imagenet import ImageNet98p, ImageNet
from datasets.maskbasedataset import MaskBaseDataset, get_transforms, grid_image, AgeDataset
from utils import ModelWrapper, maybe_dictionarize_batch, cosine_lr, get_model_from_sd, get_model_from_sd_modified
from zeroshot import zeroshot_classifier
from openai_imagenet_template import openai_imagenet_template
import datasets.maskbasedataset
from pytorch_metric_learning import losses
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('model/'),
help="Where to download the models.",
)
parser.add_argument(
"--batch-size",
type=int,
default=128,
)
parser.add_argument(
"--custom-template", action="store_true", default=False,
)
parser.add_argument(
"--workers",
type=int,
default=4,
)
parser.add_argument(
"--epochs",
type=int,
default=20,
)
parser.add_argument(
"--warmup-length",
type=int,
default=500,
)
parser.add_argument(
"--lr",
type=float,
default=1e-5, ## 0.00001
)
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.'
)
parser.add_argument(
"--timm-aug", action="store_true", default=False,
)
parser.add_argument(
"--random-seed",
type=int,
default=42,
)
parser.add_argument(
"--i",
type=int,
default=0,
)
parser.add_argument(
"--loss-fn",
default='CrossEntropyLoss',
help='Loss function used in training'
)
return parser.parse_args()
if __name__ == '__main__':
args = parse_arguments()
DEVICE = 'cuda'
if args.random_seed != -1 :
torch.manual_seed(args.random_seed)
torch.cuda.manual_seed(args.random_seed)
torch.cuda.manual_seed_all(args.random_seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(args.random_seed)
random.seed(args.random_seed)
if args.custom_template:
template = [lambda x : f"a photo of a {x}."]
else:
template = openai_imagenet_template
base_model, preprocess = clip.load(args.model, 'cuda', jit=False)
dataset = AgeDataset(
data_dir='/opt/ml/input/data/train/images'
)
# Data Load
train_set, val_set= dataset.split_dataset(val_ratio=0.2, random_seed=args.random_seed)
train_set.dataset = copy.deepcopy(dataset)
# Augmentation
transform = get_transforms()
train_set.dataset.set_transform(transform['train'])
val_set.dataset.set_transform(transform['val'])
## 학습 이미지 저장 (Augmentation이 잘 적용됐는지 확인)
SAVE_IMG = False
if SAVE_IMG :
image_data = np.transpose(train_set[0][0], (1, 2, 0))
mean=(0.548, 0.504, 0.479)
std=(0.237, 0.247, 0.246)
image_data = MaskBaseDataset.denormalize_image(np.array(image_data), mean, std)
print(image_data.shape)
img_pil = TF.to_pil_image(image_data)
os.makedirs('temp', exist_ok=True)
img_pil.save('temp/temp.png')
exit()
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=args.batch_size,
num_workers=args.workers,
shuffle=True,
pin_memory=True,
drop_last=True,
)
val_loader = torch.utils.data.DataLoader(
val_set,
batch_size=args.batch_size,
num_workers=args.workers,
shuffle=False,
pin_memory=True,
drop_last=True,
)
class_names = ['young', 'middle', 'old']
clf = zeroshot_classifier(base_model, class_names, template, DEVICE)
NUM_CLASSES = len(class_names)
feature_dim = base_model.visual.output_dim
#############모델 load#############
base_model, preprocess = clip.load('ViT-B/32', 'cpu', jit=False)
model_path = os.path.join(args.model_location, f'model_{args.i}.pt')
state_dict = torch.load(model_path, map_location=torch.device('cpu'))
model = get_model_from_sd_modified(state_dict, base_model, NUM_CLASSES, 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)
if args.loss_fn == 'CrossEntropyLoss':
loss_fn = torch.nn.CrossEntropyLoss()
elif args.loss_fn == 'ContrastiveLoss':
loss_fn = losses.ContrastiveLoss(pos_margin=1, neg_margin=1)
print("Loss Function : ", args.loss_fn)
wandb.init(name=f'Age_{args.name}_i{args.i}', config={"batch_size": args.batch_size,
"lr" : args.lr,
"epochs" : args.epochs,
"name" : args.name,
"criterion_name" : loss_fn})
for epoch in range(args.epochs):
# Train
correct, count = 0.0, 0.0
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()
pred = logits.argmax(dim=1, keepdim=True)
correct += pred.eq(labels.view_as(pred)).sum().item()
count += len(logits)
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}\t Acc: {100*correct/count:.2f} \tData (t) {data_time:.3f}\tBatch (t) {batch_time:.3f}", flush=True
)
wandb.log({
"epoch" : epoch,
"Train loss": loss.item(),
"Train acc" : 100*correct/count
})
correct, count = 0.0, 0.0
# #Evaluate
test_loader = val_loader
model.eval()
with torch.no_grad():
print('*'*80)
print('Starting eval')
correct, count = 0.0, 0.0
pbar = tqdm(test_loader)
figure = None
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}")
if figure is None:
inputs_np = torch.clone(inputs).detach().cpu().permute(0, 2, 3, 1).numpy()
inputs_np = MaskBaseDataset.denormalize_image(inputs_np, dataset.mean, dataset.std)
figure = grid_image(inputs_np, labels, pred, n=25, shuffle=True) # 16
if count !=0 :
top1 = correct / count
print(f'Val acc at epoch {epoch+1}: {100*top1:.2f}')
if (epoch+1) % 5 == 0 :
model_path = os.path.join(args.model_location, f'{args.name}{args.i}_epoch{epoch+1}.pt')
print('Saving model to', model_path)
torch.save(model.module.state_dict(), model_path)
wandb.log({
"epoch" : epoch+1,
"Valid loss": loss.item(),
"Valid acc" : 100*top1,
"Valid fig" : wandb.Image(figure)
})
wandb.finish()