-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.py
77 lines (70 loc) · 2.56 KB
/
utils.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
# coding=utf-8
# Copyright 2022 Gen Luo. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import yaml
import time
import numpy as np
import torch
def set_seed(seed=0):
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
@torch.no_grad()
def throughput(model,img_size=224,bs=1):
with torch.no_grad():
x = torch.randn(bs, 3, img_size, img_size).cuda()
batch_size=x.shape[0]
# model=create_model('vit_base_patch16_224_in21k', checkpoint_path='./ViT-B_16.npz', drop_path_rate=0.1)
model.eval()
for i in range(50):
model(x)
torch.cuda.synchronize()
print(f"throughput averaged with 30 times")
tic1 = time.time()
for i in range(30):
model(x)
torch.cuda.synchronize()
tic2 = time.time()
print(f"batch_size {batch_size} throughput {30 * batch_size / (tic2 - tic1)}")
MB = 1024.0 * 1024.0
print('memory:', torch.cuda.max_memory_allocated() / MB)
@torch.no_grad()
def save(method, dataset, model, acc, ep):
model.eval()
model = model.cpu()
trainable = {}
for n, p in model.named_parameters():
if 'adapter' in n or 'head' in n:
trainable[n] = p.data
torch.save(trainable, './models/%s/%s.pt'%(method, dataset))
with open('./models/%s/%s.log'%(method, dataset), 'w') as f:
f.write(str(ep)+' '+str(acc))
def load(method, dataset, model):
model = model.cpu()
st = torch.load('./models/%s/%s.pt'%(method, dataset))
model.load_state_dict(st, False)
return model
def get_config(method, dataset_name,few_shot=False):
if few_shot:
config_name = './configs/%s_few_shot/%s.yaml' % (method, dataset_name)
else:
config_name = './configs/%s/%s.yaml' % (method, dataset_name)
with open(config_name, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
return config