-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinference1.py
243 lines (215 loc) · 9.91 KB
/
inference1.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
import argparse
import json
import os
import shutil
import numpy as np
import jittor as jt
import jittor.nn as nn
jt.flags.use_cuda = 1
from PIL import Image
import jittor.transform as transforms
from tqdm import tqdm
import src.resnet as resnet_model
from src.singlecropdataset import InferImageFolder
from src.utils import hungarian
import random
def parse_args():
parser = argparse.ArgumentParser(description='Inference')
parser.add_argument('--mode', type=str, required=True)
parser.add_argument('--dump_path',
type=str,
default=None,
help='The path to save results.')
parser.add_argument('--match_file',
type=str,
default=None,
help='The matching file for test set.')
parser.add_argument('--data_path',
type=str,
default=None,
help='The path to ImagenetS dataset.')
parser.add_argument('--pretrained',
type=str,
default=None,
help='The model checkpoint file.')
parser.add_argument('-a',
'--arch',
metavar='ARCH',
help='The model architecture.')
parser.add_argument('-c',
'--num-classes',
default=50,
type=int,
help='The number of classes.')
parser.add_argument('--max_res', default=1000, type=int, help="Maximum resolution for evaluation. 0 for disable.")
parser.add_argument('--method',
default='example submission',
help='Method name in method description file(.txt).')
parser.add_argument('--train_data',
default='null',
help='Training data in method description file(.txt).')
parser.add_argument(
'--train_scheme',
default='null',
help='Training scheme in method description file(.txt), \
e.g., SSL, Sup, SSL+Sup.')
parser.add_argument(
'--link',
default='null',
help='Paper/project link in method description file(.txt).')
parser.add_argument(
'--description',
default='null',
help='Method description in method description file(.txt).')
args = parser.parse_args()
return args
def main_worker(args):
# build model
if 'resnet' in args.arch:
model = resnet_model.__dict__[args.arch](
hidden_mlp=0, output_dim=0, nmb_prototypes=0, train_mode='finetune', num_classes=args.num_classes)
else:
raise NotImplementedError()
checkpoint = jt.load(args.pretrained)["state_dict"]
for k in list(checkpoint.keys()):
if k not in model.state_dict().keys():
del checkpoint[k]
model.load_state_dict(checkpoint)
print("=> loaded model '{}'".format(args.pretrained))
model.eval()
# build dataset
# assert args.mode in ['validation', 'test', 'train']
data_path = os.path.join(args.data_path, args.mode)
validation_segmentation = os.path.join(args.data_path,
'validation-segmentation')
normalize = transforms.ImageNormalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
dataset = InferImageFolder(root=data_path,
transform=transforms.Compose([
transforms.Resize(256),
transforms.ToTensor(),
normalize,
]))
dataloader = dataset.set_attrs(
batch_size=1,
num_workers=16)
dump_path = os.path.join(args.dump_path, 'result')
# dump_path = args.dump_path
targets = []
predictions = []
txt1_path = './label.txt'
txt1_path_fen = './label_fen.txt'
for images, path, height, width in tqdm(dataloader):
path = path[0]
cate = path.split('/')[-2]
name = path.split('/')[-1].split('.')[0]
if not os.path.exists(os.path.join(dump_path, cate)):
os.makedirs(os.path.join(dump_path, cate))
with jt.no_grad():
H = height.item()
W = width.item()
###2,3,4
output = model(images)
flag = 0.5
# if random.random() < flag:
images_h = jt.flip(images, dim=(3,))
output_h = model(images_h)
output = (output + jt.flip(output_h, dim=(3,))) / 2
# else:
# images_v = jt.flip(images, dim=(2, ))
# output_v = model(images_v)
# output = (output + jt.flip(output_v, dim=(2, ))) / 2
####
###512###
images_512 = nn.resize(images, (512, 512), mode='bilinear', align_corners=False)
output2 = model(images_512)
images_512_h = jt.flip(images_512, dim=(3,))
output_512_h = model(images_512_h)
output2 = (output2 + jt.flip(output_512_h, dim=(3,))) / 2
####output 384 output2 512
if H * W > args.max_res * args.max_res and args.max_res > 0:
output = nn.interpolate(output, (args.max_res, int(args.max_res * W / H)), mode="bilinear",
align_corners=False)
output2 = nn.interpolate(output2, (args.max_res, int(args.max_res * W / H)), mode="bilinear",
align_corners=False)
output = jt.argmax((output + output2) / 2, dim=1, keepdims=True)[0]
out1, out2 = jt.argmax((output + output2) / 2, dim=1, keepdims=True)
prediction = nn.interpolate(output.float(), (H, W), mode="nearest").long()
else:
output = nn.interpolate(output, (H, W), mode="bilinear", align_corners=False)
output2 = nn.interpolate(output2, (H, W), mode="bilinear", align_corners=False)
out1, out2 = jt.argmax((output + output2) / 2, dim=1, keepdims=True)
prediction = jt.argmax((output + output2) / 2, dim=1, keepdims=True)[0]
result = []
# label = -1000
for i in range(1, 51):
result.append(out2[out1 == i].mean())
if max(result) <= 0:
label = -2
fen = 0
else:
label = result.index(max(result))
fen = float(result[label])
with open(txt1_path, 'a') as f:
f.write(os.path.join(cate, name + '.JPEG') + ' ' + str(label) + '\n')
with open(txt1_path_fen, 'a') as f:
f.write(os.path.join(cate, name + '.JPEG') + ' ' + str(label)+ ' ' + str(fen) + '\n')
# if H * W > args.max_res * args.max_res and args.max_res > 0:
# output = nn.interpolate(output, (args.max_res, int(args.max_res * W / H)), mode="bilinear",
# align_corners=False)
# output = jt.argmax(output, dim=1, keepdims=True)[0]
# prediction = nn.interpolate(output.float(), (H, W), mode="nearest").long()
# else:
# output = nn.interpolate(output, (H, W), mode="bilinear", align_corners=False)
# prediction = jt.argmax(output, dim=1, keepdims=True)[0]
prediction = prediction.squeeze(0).squeeze(0)
res = jt.zeros((prediction.shape[0], prediction.shape[1], 3))
res[:, :, 0] = prediction % 256
res[:, :, 1] = prediction // 256
res = res.cpu().numpy()
res = Image.fromarray(res.astype(np.uint8))
res.save(os.path.join(dump_path, cate, name + '.png'))
if args.mode == 'validation':
target = Image.open(os.path.join(validation_segmentation, cate, name + '.png'))
target = np.array(target).astype(np.int32)
target = target[:, :, 1] * 256 + target[:, :, 0]
# Prepare for matching (target)
target_unique = np.unique(target.reshape(-1))
target_unique = target_unique - 1
target_unique = target_unique.tolist()
if -1 in target_unique:
target_unique.remove(-1)
targets.append(target_unique)
# Prepare for matching (prediction)
prediction_unique = np.unique(prediction.cpu().numpy().reshape(-1))
prediction_unique = prediction_unique - 1
prediction_unique = prediction_unique.tolist()
if -1 in prediction_unique:
prediction_unique.remove(-1)
predictions.append(prediction_unique)
# jt.clean_graph()
# jt.sync_all()
# jt.gc()
if args.mode == 'validation':
_, match = hungarian(targets, predictions, num_classes=args.num_classes)
match = {k + 1: v + 1 for k, v in match.items()}
match[0] = 0
with open(os.path.join(dump_path, 'match.json'), 'w') as f:
f.write(json.dumps(match))
# elif args.mode == 'test':
# assert os.path.exists(args.match_file)
# shutil.copyfile(args.match_file, os.path.join(dump_path, 'match.json'))
#
# method = 'Method name: {}\n'.format(args.method) + \
# 'Training data: {}\nTraining scheme: {}\n'.format(
# args.train_data, args.train_scheme) + \
# 'Networks: {}\nPaper/Project link: {}\n'.format(
# args.arch, args.link) + \
# 'Method description: {}'.format(args.description)
# with open(os.path.join(dump_path, 'method.txt'), 'w') as f:
# f.write(method)
# zip for submission
# shutil.make_archive(os.path.join(args.dump_path, args.mode), 'zip', root_dir=dump_path)
if __name__ == '__main__':
args = parse_args()
main_worker(args=args)