zgcr654321
commited on
Commit
•
c1ca0dc
1
Parent(s):
9502263
Upload 15 files
Browse files- accv2022/generate_testa_dataset_result/__pycache__/accv2022testadataset.cpython-38.pyc +0 -0
- accv2022/generate_testa_dataset_result/__pycache__/test_config.cpython-38.pyc +0 -0
- accv2022/generate_testa_dataset_result/accv2022testadataset.py +363 -0
- accv2022/generate_testa_dataset_result/test.py +166 -0
- accv2022/generate_testa_dataset_result/test.sh +1 -0
- accv2022/generate_testa_dataset_result/test_config.py +55 -0
- accv2022/generate_testa_dataset_result/testa_pred_results.csv +0 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/__pycache__/train_config.cpython-38.pyc +0 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/checkpoints/latest.pth +3 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/checkpoints/vit_large_patch16-acc90.693.pth +3 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/log/train.info.log +0 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/test.sh +1 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/test_config.py +58 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/train.sh +1 -0
- accv2022/vit_large_patch16_lion_for_mae_pretrain/train_config.py +142 -0
accv2022/generate_testa_dataset_result/__pycache__/accv2022testadataset.cpython-38.pyc
ADDED
Binary file (8.72 kB). View file
|
|
accv2022/generate_testa_dataset_result/__pycache__/test_config.cpython-38.pyc
ADDED
Binary file (1.88 kB). View file
|
|
accv2022/generate_testa_dataset_result/accv2022testadataset.py
ADDED
@@ -0,0 +1,363 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import json
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
from tqdm import tqdm
|
9 |
+
|
10 |
+
from torch.utils.data import Dataset
|
11 |
+
|
12 |
+
import torch
|
13 |
+
import torch.nn.functional as F
|
14 |
+
import torchvision.transforms as transforms
|
15 |
+
|
16 |
+
|
17 |
+
class Opencv2PIL:
|
18 |
+
|
19 |
+
def __init__(self):
|
20 |
+
pass
|
21 |
+
|
22 |
+
def __call__(self, sample):
|
23 |
+
'''
|
24 |
+
sample must be a dict,contains 'image'、'label' keys.
|
25 |
+
'''
|
26 |
+
path, image = sample['path'], sample['image']
|
27 |
+
|
28 |
+
image = Image.fromarray(np.uint8(image))
|
29 |
+
|
30 |
+
return {
|
31 |
+
'path': path,
|
32 |
+
'image': image,
|
33 |
+
}
|
34 |
+
|
35 |
+
|
36 |
+
class PIL2Opencv:
|
37 |
+
|
38 |
+
def __init__(self):
|
39 |
+
pass
|
40 |
+
|
41 |
+
def __call__(self, sample):
|
42 |
+
'''
|
43 |
+
sample must be a dict,contains 'image'、'label' keys.
|
44 |
+
'''
|
45 |
+
path, image = sample['path'], sample['image']
|
46 |
+
|
47 |
+
image = np.asarray(image).astype(np.float32)
|
48 |
+
|
49 |
+
return {
|
50 |
+
'path': path,
|
51 |
+
'image': image,
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
class TorchResize:
|
56 |
+
|
57 |
+
def __init__(self, resize=224):
|
58 |
+
self.Resize = transforms.Resize(int(resize))
|
59 |
+
|
60 |
+
def __call__(self, sample):
|
61 |
+
'''
|
62 |
+
sample must be a dict,contains 'image'、'label' keys.
|
63 |
+
'''
|
64 |
+
path, image = sample['path'], sample['image']
|
65 |
+
|
66 |
+
image = self.Resize(image)
|
67 |
+
|
68 |
+
return {
|
69 |
+
'path': path,
|
70 |
+
'image': image,
|
71 |
+
}
|
72 |
+
|
73 |
+
|
74 |
+
class TorchCenterCrop:
|
75 |
+
|
76 |
+
def __init__(self, resize=224):
|
77 |
+
self.CenterCrop = transforms.CenterCrop(int(resize))
|
78 |
+
|
79 |
+
def __call__(self, sample):
|
80 |
+
'''
|
81 |
+
sample must be a dict,contains 'image'、'label' keys.
|
82 |
+
'''
|
83 |
+
path, image = sample['path'], sample['image']
|
84 |
+
|
85 |
+
image = self.CenterCrop(image)
|
86 |
+
|
87 |
+
return {
|
88 |
+
'path': path,
|
89 |
+
'image': image,
|
90 |
+
}
|
91 |
+
|
92 |
+
|
93 |
+
class TorchMeanStdNormalize:
|
94 |
+
|
95 |
+
def __init__(self, mean, std):
|
96 |
+
self.to_tensor = transforms.ToTensor()
|
97 |
+
self.Normalize = transforms.Normalize(mean=mean, std=std)
|
98 |
+
|
99 |
+
def __call__(self, sample):
|
100 |
+
'''
|
101 |
+
sample must be a dict,contains 'image'、'label' keys.
|
102 |
+
'''
|
103 |
+
path, image = sample['path'], sample['image']
|
104 |
+
image = self.to_tensor(image)
|
105 |
+
image = self.Normalize(image)
|
106 |
+
# 3 H W ->H W 3
|
107 |
+
image = image.permute(1, 2, 0)
|
108 |
+
image = image.numpy()
|
109 |
+
|
110 |
+
return {
|
111 |
+
'path': path,
|
112 |
+
'image': image,
|
113 |
+
}
|
114 |
+
|
115 |
+
|
116 |
+
class ClassificationCollater:
|
117 |
+
|
118 |
+
def __init__(self):
|
119 |
+
pass
|
120 |
+
|
121 |
+
def __call__(self, data):
|
122 |
+
paths = [s['path'] for s in data]
|
123 |
+
images = [s['image'] for s in data]
|
124 |
+
|
125 |
+
images = np.array(images).astype(np.float32)
|
126 |
+
|
127 |
+
images = torch.from_numpy(images).float()
|
128 |
+
# B H W 3 ->B 3 H W
|
129 |
+
images = images.permute(0, 3, 1, 2)
|
130 |
+
|
131 |
+
return {
|
132 |
+
'path': paths,
|
133 |
+
'image': images,
|
134 |
+
}
|
135 |
+
|
136 |
+
|
137 |
+
def load_state_dict(saved_model_path,
|
138 |
+
model,
|
139 |
+
excluded_layer_name=(),
|
140 |
+
loading_new_input_size_position_encoding_weight=False):
|
141 |
+
'''
|
142 |
+
saved_model_path: a saved model.state_dict() .pth file path
|
143 |
+
model: a new defined model
|
144 |
+
excluded_layer_name: layer names that doesn't want to load parameters
|
145 |
+
loading_new_input_size_position_encoding_weight: default False, for vit net, loading a position encoding layer with new input size, set True
|
146 |
+
only load layer parameters which has same layer name and same layer weight shape
|
147 |
+
'''
|
148 |
+
if not saved_model_path:
|
149 |
+
print('No pretrained model file!')
|
150 |
+
return
|
151 |
+
|
152 |
+
saved_state_dict = torch.load(saved_model_path,
|
153 |
+
map_location=torch.device('cpu'))
|
154 |
+
|
155 |
+
not_loaded_save_state_dict = []
|
156 |
+
filtered_state_dict = {}
|
157 |
+
for name, weight in saved_state_dict.items():
|
158 |
+
if name in model.state_dict() and not any(
|
159 |
+
excluded_name in name for excluded_name in excluded_layer_name
|
160 |
+
) and weight.shape == model.state_dict()[name].shape:
|
161 |
+
filtered_state_dict[name] = weight
|
162 |
+
else:
|
163 |
+
not_loaded_save_state_dict.append(name)
|
164 |
+
|
165 |
+
position_encoding_already_loaded = False
|
166 |
+
if 'position_encoding' in filtered_state_dict.keys():
|
167 |
+
position_encoding_already_loaded = True
|
168 |
+
|
169 |
+
# for vit net, loading a position encoding layer with new input size
|
170 |
+
if loading_new_input_size_position_encoding_weight and not position_encoding_already_loaded:
|
171 |
+
# assert position_encoding_layer name are unchanged for model and saved_model
|
172 |
+
# assert class_token num are unchanged for model and saved_model
|
173 |
+
# assert embedding_planes are unchanged for model and saved_model
|
174 |
+
model_num_cls_token = model.cls_token.shape[1]
|
175 |
+
model_embedding_planes = model.position_encoding.shape[2]
|
176 |
+
model_encoding_shape = int(
|
177 |
+
(model.position_encoding.shape[1] - model_num_cls_token)**0.5)
|
178 |
+
encoding_layer_name, encoding_layer_weight = None, None
|
179 |
+
for name, weight in saved_state_dict.items():
|
180 |
+
if 'position_encoding' in name:
|
181 |
+
encoding_layer_name = name
|
182 |
+
encoding_layer_weight = weight
|
183 |
+
break
|
184 |
+
save_model_encoding_shape = int(
|
185 |
+
(encoding_layer_weight.shape[1] - model_num_cls_token)**0.5)
|
186 |
+
|
187 |
+
save_model_cls_token_weight = encoding_layer_weight[:, 0:
|
188 |
+
model_num_cls_token, :]
|
189 |
+
save_model_position_weight = encoding_layer_weight[:,
|
190 |
+
model_num_cls_token:, :]
|
191 |
+
save_model_position_weight = save_model_position_weight.reshape(
|
192 |
+
-1, save_model_encoding_shape, save_model_encoding_shape,
|
193 |
+
model_embedding_planes).permute(0, 3, 1, 2)
|
194 |
+
save_model_position_weight = F.interpolate(save_model_position_weight,
|
195 |
+
size=(model_encoding_shape,
|
196 |
+
model_encoding_shape),
|
197 |
+
mode='bicubic',
|
198 |
+
align_corners=False)
|
199 |
+
save_model_position_weight = save_model_position_weight.permute(
|
200 |
+
0, 2, 3, 1).flatten(1, 2)
|
201 |
+
model_encoding_layer_weight = torch.cat(
|
202 |
+
(save_model_cls_token_weight, save_model_position_weight), dim=1)
|
203 |
+
|
204 |
+
filtered_state_dict[encoding_layer_name] = model_encoding_layer_weight
|
205 |
+
not_loaded_save_state_dict.remove('position_encoding')
|
206 |
+
|
207 |
+
if len(filtered_state_dict) == 0:
|
208 |
+
print('No pretrained parameters to load!')
|
209 |
+
else:
|
210 |
+
print(
|
211 |
+
f'load/model weight nums:{len(filtered_state_dict)}/{len(model.state_dict())}'
|
212 |
+
)
|
213 |
+
print(f'not loaded save layer weight:\n{not_loaded_save_state_dict}')
|
214 |
+
model.load_state_dict(filtered_state_dict, strict=False)
|
215 |
+
|
216 |
+
return
|
217 |
+
|
218 |
+
|
219 |
+
class ACCV2022TestaDataset(Dataset):
|
220 |
+
'''
|
221 |
+
ACCV2022 Dataset:https://www.cvmart.net/race/10412/des
|
222 |
+
'''
|
223 |
+
|
224 |
+
def __init__(self,
|
225 |
+
root_dir,
|
226 |
+
set_name='testa',
|
227 |
+
transform=None,
|
228 |
+
broken_list_path=None):
|
229 |
+
assert set_name in ['testa'], 'Wrong set name!'
|
230 |
+
set_dir = os.path.join(root_dir, set_name)
|
231 |
+
|
232 |
+
broken_list = set()
|
233 |
+
if broken_list_path:
|
234 |
+
with open(broken_list_path, 'r') as load_f:
|
235 |
+
broken_list = json.load(load_f)
|
236 |
+
broken_list = set(broken_list)
|
237 |
+
print(f'Broken image num:{len(broken_list)}')
|
238 |
+
|
239 |
+
self.image_path_list = []
|
240 |
+
for per_image_name in tqdm(os.listdir(set_dir)):
|
241 |
+
per_image_path = os.path.join(set_dir, per_image_name)
|
242 |
+
if per_image_name in broken_list:
|
243 |
+
continue
|
244 |
+
self.image_path_list.append(per_image_path)
|
245 |
+
|
246 |
+
self.transform = transform
|
247 |
+
|
248 |
+
print(f'Dataset Size:{len(self.image_path_list)}')
|
249 |
+
|
250 |
+
def __len__(self):
|
251 |
+
return len(self.image_path_list)
|
252 |
+
|
253 |
+
def __getitem__(self, idx):
|
254 |
+
path = self.image_path_list[idx]
|
255 |
+
image = self.load_image(idx)
|
256 |
+
|
257 |
+
sample = {
|
258 |
+
'path': path,
|
259 |
+
'image': image,
|
260 |
+
}
|
261 |
+
|
262 |
+
if self.transform:
|
263 |
+
sample = self.transform(sample)
|
264 |
+
|
265 |
+
return sample
|
266 |
+
|
267 |
+
def load_image(self, idx):
|
268 |
+
image = cv2.imdecode(
|
269 |
+
np.fromfile(self.image_path_list[idx], dtype=np.uint8),
|
270 |
+
cv2.IMREAD_COLOR)
|
271 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
272 |
+
|
273 |
+
return image.astype(np.float32)
|
274 |
+
|
275 |
+
|
276 |
+
if __name__ == '__main__':
|
277 |
+
import os
|
278 |
+
import random
|
279 |
+
import numpy as np
|
280 |
+
import torch
|
281 |
+
seed = 0
|
282 |
+
# for hash
|
283 |
+
os.environ['PYTHONHASHSEED'] = str(seed)
|
284 |
+
# for python and numpy
|
285 |
+
random.seed(seed)
|
286 |
+
np.random.seed(seed)
|
287 |
+
# for cpu gpu
|
288 |
+
torch.manual_seed(seed)
|
289 |
+
torch.cuda.manual_seed(seed)
|
290 |
+
torch.cuda.manual_seed_all(seed)
|
291 |
+
|
292 |
+
import os
|
293 |
+
import sys
|
294 |
+
|
295 |
+
BASE_DIR = os.path.dirname(
|
296 |
+
os.path.dirname(
|
297 |
+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
298 |
+
sys.path.append(BASE_DIR)
|
299 |
+
|
300 |
+
from tools.path import accv2022_dataset_path, accv2022_broken_list_path
|
301 |
+
|
302 |
+
import torchvision.transforms as transforms
|
303 |
+
from tqdm import tqdm
|
304 |
+
|
305 |
+
accv2022testadataset = ACCV2022TestaDataset(
|
306 |
+
root_dir=accv2022_dataset_path,
|
307 |
+
set_name='testa',
|
308 |
+
transform=transforms.Compose([
|
309 |
+
Opencv2PIL(),
|
310 |
+
TorchResize(resize=256),
|
311 |
+
TorchCenterCrop(resize=224),
|
312 |
+
PIL2Opencv(),
|
313 |
+
# TorchMeanStdNormalize(mean=[0.485, 0.456, 0.406],
|
314 |
+
# std=[0.229, 0.224, 0.225]),
|
315 |
+
]),
|
316 |
+
broken_list_path=accv2022_broken_list_path)
|
317 |
+
|
318 |
+
count = 0
|
319 |
+
for per_sample in tqdm(accv2022testadataset):
|
320 |
+
print(per_sample['image'].shape, type(per_sample['image']),
|
321 |
+
per_sample['path'])
|
322 |
+
|
323 |
+
# temp_dir = './temp'
|
324 |
+
# if not os.path.exists(temp_dir):
|
325 |
+
# os.makedirs(temp_dir)
|
326 |
+
|
327 |
+
# color = [random.randint(0, 255) for _ in range(3)]
|
328 |
+
# image = np.ascontiguousarray(per_sample['image'], dtype=np.uint8)
|
329 |
+
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
330 |
+
# image_name = per_sample['path'].split('/')[-1]
|
331 |
+
# text = f'image_name:{image_name}'
|
332 |
+
# cv2.putText(image,
|
333 |
+
# text, (30, 30),
|
334 |
+
# cv2.FONT_HERSHEY_PLAIN,
|
335 |
+
# 1.5,
|
336 |
+
# color=color,
|
337 |
+
# thickness=1)
|
338 |
+
|
339 |
+
# cv2.imencode('.jpg', image)[1].tofile(
|
340 |
+
# os.path.join(temp_dir, f'idx_{count}.jpg'))
|
341 |
+
|
342 |
+
if count < 5:
|
343 |
+
count += 1
|
344 |
+
else:
|
345 |
+
break
|
346 |
+
|
347 |
+
from torch.utils.data import DataLoader
|
348 |
+
collater = ClassificationCollater()
|
349 |
+
train_loader = DataLoader(accv2022testadataset,
|
350 |
+
batch_size=128,
|
351 |
+
shuffle=True,
|
352 |
+
num_workers=4,
|
353 |
+
collate_fn=collater)
|
354 |
+
|
355 |
+
count = 0
|
356 |
+
for data in tqdm(train_loader):
|
357 |
+
paths, images = data['path'], data['image']
|
358 |
+
print(images.shape)
|
359 |
+
print(images.dtype)
|
360 |
+
if count < 5:
|
361 |
+
count += 1
|
362 |
+
else:
|
363 |
+
break
|
accv2022/generate_testa_dataset_result/test.py
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import warnings
|
4 |
+
|
5 |
+
BASE_DIR = os.path.dirname(
|
6 |
+
os.path.dirname(os.path.dirname(os.path.dirname(
|
7 |
+
os.path.abspath(__file__)))))
|
8 |
+
sys.path.append(BASE_DIR)
|
9 |
+
warnings.filterwarnings('ignore')
|
10 |
+
|
11 |
+
import argparse
|
12 |
+
import collections
|
13 |
+
import numpy as np
|
14 |
+
import os
|
15 |
+
import random
|
16 |
+
import csv
|
17 |
+
|
18 |
+
from tqdm import tqdm
|
19 |
+
from thop import profile
|
20 |
+
from thop import clever_format
|
21 |
+
import torch
|
22 |
+
import torch.nn as nn
|
23 |
+
import torch.backends.cudnn as cudnn
|
24 |
+
|
25 |
+
from torch.utils.data import DataLoader
|
26 |
+
|
27 |
+
|
28 |
+
def set_seed(seed):
|
29 |
+
# for hash
|
30 |
+
os.environ['PYTHONHASHSEED'] = str(seed)
|
31 |
+
# for python and numpy
|
32 |
+
random.seed(seed)
|
33 |
+
np.random.seed(seed)
|
34 |
+
# for cpu gpu
|
35 |
+
torch.manual_seed(seed)
|
36 |
+
torch.cuda.manual_seed(seed)
|
37 |
+
torch.cuda.manual_seed_all(seed)
|
38 |
+
# for cudnn
|
39 |
+
cudnn.benchmark = False
|
40 |
+
cudnn.deterministic = True
|
41 |
+
|
42 |
+
|
43 |
+
def compute_macs_and_params(config, model):
|
44 |
+
assert isinstance(config.input_image_size, int) == True or isinstance(
|
45 |
+
config.input_image_size,
|
46 |
+
list) == True, 'Illegal input_image_size type!'
|
47 |
+
|
48 |
+
if isinstance(config.input_image_size, int):
|
49 |
+
macs_input = torch.randn(1, 3, config.input_image_size,
|
50 |
+
config.input_image_size).cpu()
|
51 |
+
elif isinstance(config.input_image_size, list):
|
52 |
+
macs_input = torch.randn(1, 3, config.input_image_size[0],
|
53 |
+
config.input_image_size[1]).cpu()
|
54 |
+
|
55 |
+
model = model.cpu()
|
56 |
+
|
57 |
+
macs, params = profile(model, inputs=(macs_input, ), verbose=False)
|
58 |
+
macs, params = clever_format([macs, params], '%.3f')
|
59 |
+
|
60 |
+
return macs, params
|
61 |
+
|
62 |
+
|
63 |
+
def test_classification(test_loader, model, config):
|
64 |
+
if hasattr(config, 'use_ema_model') and config.use_ema_model:
|
65 |
+
model = config.ema_model.ema_model
|
66 |
+
|
67 |
+
# switch to evaluate mode
|
68 |
+
model.eval()
|
69 |
+
|
70 |
+
test_results = collections.OrderedDict()
|
71 |
+
with torch.no_grad():
|
72 |
+
model_on_cuda = next(model.parameters()).is_cuda
|
73 |
+
for _, data in tqdm(enumerate(test_loader)):
|
74 |
+
paths, images = data['path'], data['image']
|
75 |
+
if model_on_cuda:
|
76 |
+
images = images.cuda()
|
77 |
+
|
78 |
+
torch.cuda.synchronize()
|
79 |
+
|
80 |
+
outputs = model(images)
|
81 |
+
torch.cuda.synchronize()
|
82 |
+
|
83 |
+
_, topk_indexes = torch.topk(outputs,
|
84 |
+
k=1,
|
85 |
+
dim=1,
|
86 |
+
largest=True,
|
87 |
+
sorted=True)
|
88 |
+
topk_indexes = torch.squeeze(topk_indexes, dim=-1)
|
89 |
+
|
90 |
+
for per_image_path, per_image_pred_index in zip(
|
91 |
+
paths, topk_indexes):
|
92 |
+
image_name = per_image_path.split('/')[-1]
|
93 |
+
written_index = f'{per_image_pred_index:0>4d}'
|
94 |
+
test_results[image_name] = written_index
|
95 |
+
|
96 |
+
return test_results
|
97 |
+
|
98 |
+
|
99 |
+
def parse_args():
|
100 |
+
parser = argparse.ArgumentParser(
|
101 |
+
description='PyTorch Classification Testing')
|
102 |
+
parser.add_argument('--work-dir',
|
103 |
+
type=str,
|
104 |
+
help='path for get testing config')
|
105 |
+
|
106 |
+
return parser.parse_args()
|
107 |
+
|
108 |
+
|
109 |
+
def main():
|
110 |
+
assert torch.cuda.is_available(), 'need gpu to train network!'
|
111 |
+
torch.cuda.empty_cache()
|
112 |
+
|
113 |
+
args = parse_args()
|
114 |
+
sys.path.append(args.work_dir)
|
115 |
+
from test_config import config
|
116 |
+
config.gpus_type = torch.cuda.get_device_name()
|
117 |
+
config.gpus_num = torch.cuda.device_count()
|
118 |
+
|
119 |
+
set_seed(config.seed)
|
120 |
+
|
121 |
+
local_rank = int(os.environ['LOCAL_RANK'])
|
122 |
+
# start init process
|
123 |
+
torch.distributed.init_process_group(backend='nccl', init_method='env://')
|
124 |
+
torch.cuda.set_device(local_rank)
|
125 |
+
config.group = torch.distributed.new_group(list(range(config.gpus_num)))
|
126 |
+
|
127 |
+
torch.distributed.barrier()
|
128 |
+
|
129 |
+
batch_size, num_workers = config.batch_size, config.num_workers
|
130 |
+
assert config.batch_size % config.gpus_num == 0, 'config.batch_size is not divisible by config.gpus_num!'
|
131 |
+
assert config.num_workers % config.gpus_num == 0, 'config.num_workers is not divisible by config.gpus_num!'
|
132 |
+
batch_size = int(config.batch_size // config.gpus_num)
|
133 |
+
num_workers = int(config.num_workers // config.gpus_num)
|
134 |
+
|
135 |
+
test_loader = DataLoader(config.test_dataset,
|
136 |
+
batch_size=batch_size,
|
137 |
+
shuffle=False,
|
138 |
+
pin_memory=True,
|
139 |
+
num_workers=num_workers,
|
140 |
+
collate_fn=config.test_collater)
|
141 |
+
|
142 |
+
model = config.model
|
143 |
+
|
144 |
+
macs, params = compute_macs_and_params(config, model)
|
145 |
+
print(f'model: {config.network}, macs: {macs}, params: {params}')
|
146 |
+
|
147 |
+
model = model.cuda()
|
148 |
+
|
149 |
+
model = nn.parallel.DistributedDataParallel(model,
|
150 |
+
device_ids=[local_rank],
|
151 |
+
output_device=local_rank)
|
152 |
+
|
153 |
+
test_results = test_classification(test_loader, model, config)
|
154 |
+
|
155 |
+
if local_rank == 0:
|
156 |
+
with open(f"{config.set_name}_pred_results.csv", "w",
|
157 |
+
encoding='utf-8') as csvfile:
|
158 |
+
writer = csv.writer(csvfile)
|
159 |
+
for per_image_name, per_image_pred in test_results.items():
|
160 |
+
writer.writerow([str(per_image_name), str(per_image_pred)])
|
161 |
+
|
162 |
+
return
|
163 |
+
|
164 |
+
|
165 |
+
if __name__ == '__main__':
|
166 |
+
main()
|
accv2022/generate_testa_dataset_result/test.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OMP_NUM_THREADS=1 CUDA_VISIBLE_DEVICES=0 python -m torch.distributed.run --nproc_per_node=1 --master_addr 127.0.1.11 --master_port 10011 test.py --work-dir ./
|
accv2022/generate_testa_dataset_result/test_config.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
BASE_DIR = os.path.dirname(
|
5 |
+
os.path.dirname(os.path.dirname(os.path.dirname(
|
6 |
+
os.path.abspath(__file__)))))
|
7 |
+
sys.path.append(BASE_DIR)
|
8 |
+
|
9 |
+
from tools.path import accv2022_dataset_path, accv2022_broken_list_path
|
10 |
+
|
11 |
+
from simpleAICV.classification import backbones
|
12 |
+
from accv2022testadataset import ACCV2022TestaDataset, Opencv2PIL, TorchResize, TorchCenterCrop, TorchMeanStdNormalize, ClassificationCollater, load_state_dict
|
13 |
+
|
14 |
+
import torch
|
15 |
+
import torchvision.transforms as transforms
|
16 |
+
|
17 |
+
|
18 |
+
class config:
|
19 |
+
'''
|
20 |
+
for resnet,input_image_size = 224;for darknet,input_image_size = 256
|
21 |
+
'''
|
22 |
+
network = 'vit_large_patch16'
|
23 |
+
num_classes = 5000
|
24 |
+
input_image_size = 224
|
25 |
+
scale = 256 / 224
|
26 |
+
set_name = 'testa'
|
27 |
+
|
28 |
+
model = backbones.__dict__[network](**{
|
29 |
+
'image_size': 224,
|
30 |
+
'global_pool': True,
|
31 |
+
'num_classes': num_classes,
|
32 |
+
})
|
33 |
+
|
34 |
+
# load pretrained model or not
|
35 |
+
trained_model_path = '/root/code/SimpleAICV_pytorch_training_examples_on_ImageNet_COCO_ADE20K/pretrained_models/vit_finetune_on_accv2022_from_mae_pretrain/vit_large_patch16-acc90.693.pth'
|
36 |
+
load_state_dict(trained_model_path, model)
|
37 |
+
|
38 |
+
test_dataset = ACCV2022TestaDataset(
|
39 |
+
root_dir=accv2022_dataset_path,
|
40 |
+
set_name=set_name,
|
41 |
+
transform=transforms.Compose([
|
42 |
+
Opencv2PIL(),
|
43 |
+
TorchResize(resize=input_image_size * scale),
|
44 |
+
TorchCenterCrop(resize=input_image_size),
|
45 |
+
TorchMeanStdNormalize(mean=[0.485, 0.456, 0.406],
|
46 |
+
std=[0.229, 0.224, 0.225]),
|
47 |
+
]),
|
48 |
+
broken_list_path=accv2022_broken_list_path)
|
49 |
+
test_collater = ClassificationCollater()
|
50 |
+
|
51 |
+
seed = 0
|
52 |
+
# batch_size is total size
|
53 |
+
batch_size = 16
|
54 |
+
# num_workers is total workers
|
55 |
+
num_workers = 20
|
accv2022/generate_testa_dataset_result/testa_pred_results.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/__pycache__/train_config.cpython-38.pyc
ADDED
Binary file (3.43 kB). View file
|
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/checkpoints/latest.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d88acaa0c254898e6c1286b30797e32d325c3e8bddc61e84764d8a7a06154a92
|
3 |
+
size 3677028335
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/checkpoints/vit_large_patch16-acc90.693.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3705a212f69ca5a4c66bc7897eb50ea368aa475c27cd4ce0c07807518ab051c6
|
3 |
+
size 1233796787
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/log/train.info.log
ADDED
The diff for this file is too large to render.
See raw diff
|
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/test.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OMP_NUM_THREADS=1 CUDA_VISIBLE_DEVICES=0,1 python -m torch.distributed.run --nproc_per_node=2 --master_addr 127.0.1.0 --master_port 10000 ../../../tools/test_classification_model.py --work-dir ./
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/test_config.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
BASE_DIR = os.path.dirname(
|
5 |
+
os.path.dirname(os.path.dirname(os.path.dirname(
|
6 |
+
os.path.abspath(__file__)))))
|
7 |
+
sys.path.append(BASE_DIR)
|
8 |
+
|
9 |
+
from tools.path import accv2022_dataset_path, accv2022_broken_list_path
|
10 |
+
|
11 |
+
from simpleAICV.classification import backbones
|
12 |
+
from simpleAICV.classification import losses
|
13 |
+
from simpleAICV.classification.datasets.accv2022traindataset import ACCV2022TrainDataset
|
14 |
+
from simpleAICV.classification.common import Opencv2PIL, TorchResize, TorchCenterCrop, TorchMeanStdNormalize, ClassificationCollater, load_state_dict
|
15 |
+
|
16 |
+
import torch
|
17 |
+
import torchvision.transforms as transforms
|
18 |
+
|
19 |
+
|
20 |
+
class config:
|
21 |
+
'''
|
22 |
+
for resnet,input_image_size = 224;for darknet,input_image_size = 256
|
23 |
+
'''
|
24 |
+
network = 'vit_large_patch16'
|
25 |
+
num_classes = 5000
|
26 |
+
input_image_size = 224
|
27 |
+
scale = 256 / 224
|
28 |
+
|
29 |
+
model = backbones.__dict__[network](**{
|
30 |
+
'image_size': 224,
|
31 |
+
'global_pool': True,
|
32 |
+
'num_classes': num_classes,
|
33 |
+
})
|
34 |
+
|
35 |
+
# load pretrained model or not
|
36 |
+
trained_model_path = ''
|
37 |
+
load_state_dict(trained_model_path, model)
|
38 |
+
|
39 |
+
test_criterion = losses.__dict__['CELoss']()
|
40 |
+
|
41 |
+
test_dataset = ACCV2022TrainDataset(
|
42 |
+
root_dir=accv2022_dataset_path,
|
43 |
+
set_name='train',
|
44 |
+
transform=transforms.Compose([
|
45 |
+
Opencv2PIL(),
|
46 |
+
TorchResize(resize=input_image_size * scale),
|
47 |
+
TorchCenterCrop(resize=input_image_size),
|
48 |
+
TorchMeanStdNormalize(mean=[0.485, 0.456, 0.406],
|
49 |
+
std=[0.229, 0.224, 0.225]),
|
50 |
+
]),
|
51 |
+
broken_list_path=accv2022_broken_list_path)
|
52 |
+
test_collater = ClassificationCollater()
|
53 |
+
|
54 |
+
seed = 0
|
55 |
+
# batch_size is total size
|
56 |
+
batch_size = 256
|
57 |
+
# num_workers is total workers
|
58 |
+
num_workers = 16
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/train.sh
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OMP_NUM_THREADS=1 CUDA_VISIBLE_DEVICES=0,1 python -m torch.distributed.run --nproc_per_node=2 --master_addr 127.0.1.0 --master_port 10000 ../../../tools/train_classification_model.py --work-dir ./
|
accv2022/vit_large_patch16_lion_for_mae_pretrain/train_config.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
BASE_DIR = os.path.dirname(
|
5 |
+
os.path.dirname(os.path.dirname(os.path.dirname(
|
6 |
+
os.path.abspath(__file__)))))
|
7 |
+
sys.path.append(BASE_DIR)
|
8 |
+
|
9 |
+
from tools.path import accv2022_dataset_path, accv2022_broken_list_path
|
10 |
+
|
11 |
+
from simpleAICV.classification import backbones
|
12 |
+
from simpleAICV.classification import losses
|
13 |
+
from simpleAICV.classification.datasets.accv2022traindataset import ACCV2022TrainDataset
|
14 |
+
from simpleAICV.classification.common import Opencv2PIL, TorchRandomResizedCrop, TorchRandomHorizontalFlip, RandAugment, TorchResize, TorchCenterCrop, TorchMeanStdNormalize, RandomErasing, ClassificationCollater, MixupCutmixClassificationCollater, load_state_dict
|
15 |
+
|
16 |
+
import torch
|
17 |
+
import torchvision.transforms as transforms
|
18 |
+
|
19 |
+
|
20 |
+
class config:
|
21 |
+
network = 'vit_large_patch16'
|
22 |
+
num_classes = 5000
|
23 |
+
input_image_size = 224
|
24 |
+
scale = 256 / 224
|
25 |
+
|
26 |
+
model = backbones.__dict__[network](**{
|
27 |
+
'image_size': 224,
|
28 |
+
'drop_path_prob': 0.1,
|
29 |
+
'global_pool': True,
|
30 |
+
'num_classes': num_classes,
|
31 |
+
})
|
32 |
+
|
33 |
+
# load pretrained model or not
|
34 |
+
trained_model_path = '/root/code/SimpleAICV_pytorch_training_examples_on_ImageNet_COCO_ADE20K/pretrained_models/vit_mae_pretrain_on_accv2022_from_imagenet1k_pretrain/vit_large_patch16_224_mae_pretrain_model-loss0.424_encoder.pth'
|
35 |
+
load_state_dict(trained_model_path,
|
36 |
+
model,
|
37 |
+
loading_new_input_size_position_encoding_weight=True)
|
38 |
+
|
39 |
+
train_criterion = losses.__dict__['OneHotLabelCELoss']()
|
40 |
+
test_criterion = losses.__dict__['CELoss']()
|
41 |
+
|
42 |
+
train_dataset = ACCV2022TrainDataset(
|
43 |
+
root_dir=accv2022_dataset_path,
|
44 |
+
set_name='train',
|
45 |
+
transform=transforms.Compose([
|
46 |
+
Opencv2PIL(),
|
47 |
+
TorchRandomResizedCrop(resize=input_image_size),
|
48 |
+
TorchRandomHorizontalFlip(prob=0.5),
|
49 |
+
RandAugment(magnitude=9,
|
50 |
+
num_layers=2,
|
51 |
+
resize=input_image_size,
|
52 |
+
mean=[0.485, 0.456, 0.406],
|
53 |
+
integer=True,
|
54 |
+
weight_idx=None,
|
55 |
+
magnitude_std=0.5,
|
56 |
+
magnitude_max=None),
|
57 |
+
TorchMeanStdNormalize(mean=[0.485, 0.456, 0.406],
|
58 |
+
std=[0.229, 0.224, 0.225]),
|
59 |
+
RandomErasing(prob=0.25, mode='pixel', max_count=1),
|
60 |
+
]),
|
61 |
+
broken_list_path=accv2022_broken_list_path)
|
62 |
+
|
63 |
+
test_dataset = ACCV2022TrainDataset(
|
64 |
+
root_dir=accv2022_dataset_path,
|
65 |
+
set_name='train',
|
66 |
+
transform=transforms.Compose([
|
67 |
+
Opencv2PIL(),
|
68 |
+
TorchResize(resize=input_image_size * scale),
|
69 |
+
TorchCenterCrop(resize=input_image_size),
|
70 |
+
TorchMeanStdNormalize(mean=[0.485, 0.456, 0.406],
|
71 |
+
std=[0.229, 0.224, 0.225]),
|
72 |
+
]),
|
73 |
+
broken_list_path=accv2022_broken_list_path)
|
74 |
+
|
75 |
+
train_collater = MixupCutmixClassificationCollater(
|
76 |
+
use_mixup=True,
|
77 |
+
mixup_alpha=0.8,
|
78 |
+
cutmix_alpha=1.0,
|
79 |
+
cutmix_minmax=None,
|
80 |
+
mixup_cutmix_prob=1.0,
|
81 |
+
switch_to_cutmix_prob=0.5,
|
82 |
+
mode='batch',
|
83 |
+
correct_lam=True,
|
84 |
+
label_smoothing=0.1,
|
85 |
+
num_classes=5000)
|
86 |
+
test_collater = ClassificationCollater()
|
87 |
+
|
88 |
+
seed = 0
|
89 |
+
# batch_size is total size
|
90 |
+
batch_size = 128
|
91 |
+
# num_workers is total workers
|
92 |
+
num_workers = 20
|
93 |
+
accumulation_steps = 32
|
94 |
+
|
95 |
+
optimizer = (
|
96 |
+
'Lion',
|
97 |
+
{
|
98 |
+
'lr':
|
99 |
+
4e-4,
|
100 |
+
'global_weight_decay':
|
101 |
+
False,
|
102 |
+
# if global_weight_decay = False
|
103 |
+
# all bias, bn and other 1d params weight set to 0 weight decay
|
104 |
+
'weight_decay':
|
105 |
+
5e-2,
|
106 |
+
# lr_layer_decay only support vit style model
|
107 |
+
'lr_layer_decay':
|
108 |
+
0.65,
|
109 |
+
'lr_layer_decay_block':
|
110 |
+
model.blocks,
|
111 |
+
'block_name':
|
112 |
+
'blocks',
|
113 |
+
'no_weight_decay_layer_name_list': [
|
114 |
+
'position_encoding',
|
115 |
+
'cls_token',
|
116 |
+
],
|
117 |
+
},
|
118 |
+
)
|
119 |
+
|
120 |
+
scheduler = (
|
121 |
+
'CosineLR',
|
122 |
+
{
|
123 |
+
'warm_up_epochs': 5,
|
124 |
+
'min_lr': 1e-6,
|
125 |
+
},
|
126 |
+
)
|
127 |
+
|
128 |
+
epochs = 100
|
129 |
+
print_interval = 10
|
130 |
+
|
131 |
+
sync_bn = False
|
132 |
+
use_amp = True
|
133 |
+
use_compile = False
|
134 |
+
compile_params = {
|
135 |
+
# 'default': optimizes for large models, low compile-time and no extra memory usage.
|
136 |
+
# 'reduce-overhead': optimizes to reduce the framework overhead and uses some extra memory, helps speed up small models, model update may not correct.
|
137 |
+
# 'max-autotune': optimizes to produce the fastest model, but takes a very long time to compile and may failed.
|
138 |
+
'mode': 'default',
|
139 |
+
}
|
140 |
+
|
141 |
+
use_ema_model = False
|
142 |
+
ema_model_decay = 0.9999
|