forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
places365.py
315 lines (263 loc) · 10.5 KB
/
places365.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import sys
import time
import numpy as np
import cv2
from PIL import Image
import ailia
# import original modules
sys.path.append('../../util')
# logger
from logging import getLogger # noqa: E402
import webcamera_utils # noqa: E402
from image_utils import imread # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from arg_utils import get_base_parser, get_savepath, update_parser # noqa: E402
logger = getLogger(__name__)
# ======================
# Parameters 1
# ======================
IMAGE_PATH = 'input.jpg'
SAVE_IMAGE_PATH = 'output.png'
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'Resnet18 ImageNet classification model', IMAGE_PATH, None
)
parser.add_argument(
'--model', default='resnet18',
choices=['resnet18', 'alexnet', 'resnet50', 'densenet161', 'wideresnet18']
)
args = update_parser(parser)
# ======================
# Parameters 2
# ======================
ALEXNET_WEIGHT_PATH = 'alexnet_places365.onnx'
ALEXNET_MODEL_PATH = 'alexnet_places365.onnx.prototxt'
RESNET18_WEIGHT_PATH = 'resnet18_places365.onnx'
RESNET18_MODEL_PATH = 'resnet18_places365.onnx.prototxt'
RESNET50_WEIGHT_PATH = 'resnet50_places365.onnx'
RESNET50_MODEL_PATH = 'resnet50_places365.onnx.prototxt'
WIDERESNET18_WEIGHT_PATH = 'wideresnet18_places365.onnx'
WIDERESNET18_MODEL_PATH = 'wideresnet18_places365.onnx.prototxt'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/places365/'
# ======================
# Utils
# ======================
def softmax(x):
u = np.sum(np.exp(x))
return np.exp(x)/u
def get_model():
if args.model == 'resnet18':
model_path, weight_path = RESNET18_MODEL_PATH, RESNET18_WEIGHT_PATH
elif args.model == 'resnet50':
model_path, weight_path = RESNET50_MODEL_PATH, RESNET50_WEIGHT_PATH
elif args.model == 'alexnet':
model_path, weight_path = ALEXNET_MODEL_PATH, ALEXNET_WEIGHT_PATH
elif args.model == 'wideresnet18':
model_path, weight_path = WIDERESNET18_MODEL_PATH, WIDERESNET18_WEIGHT_PATH
else:
logger.info('Invalid model name.')
exit()
# model files check and download
check_and_download_models(weight_path, model_path, REMOTE_PATH)
# load model
model = ailia.Net(model_path, weight_path, env_id=args.env_id)
# get weight
weight = None
if args.model in ['wideresnet18']:
import onnx
from onnx import numpy_helper
weight = onnx.load(weight_path)
weight = weight.graph.initializer
weight = onnx.numpy_helper.to_array(weight[0])
return model, weight
def get_label_scene_category():
file_name = 'categories_places365.txt'
classes = list()
with open(file_name) as class_file:
for line in class_file:
classes.append(line.strip().split(' ')[0][3:])
classes = tuple(classes)
return classes
def get_label_indoor_and_outdoor():
labels_IO = None
if args.model in ['wideresnet18']:
file_name_IO = 'IO_places365.txt'
with open(file_name_IO) as f:
lines = f.readlines()
labels_IO = []
for line in lines:
items = line.rstrip().split()
labels_IO.append(int(items[-1]) -1) # 0 is indoor, 1 is outdoor
labels_IO = np.array(labels_IO)
return labels_IO
def get_label_scene_attribute():
labels_attribute, W_attribute = None, None
if args.model in ['wideresnet18']:
file_name_attribute = 'labels_sunattribute.txt'
with open(file_name_attribute) as f:
lines = f.readlines()
labels_attribute = [item.rstrip() for item in lines]
file_name_W = 'W_sceneattribute_wideresnet18.npy'
W_attribute = np.load(file_name_W)
return labels_attribute, W_attribute
def apply_centre_crop(img):
if args.model in ['wideresnet18']:
img = cv2.resize(img, (224, 224))
else:
img = cv2.resize(img, (256, 256))
pad = int((256-224)/2)
img = img[pad:-pad, pad:-pad, :]
img = img / 255
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
for i in range(3):
img[:, :, i] = (img[:, :, i]-mean[i])/std[i]
img = img.transpose(2, 0, 1)
img = img[np.newaxis, :, :, :]
return img
def returnCAM(feature_conv, weight_softmax, class_idx):
# generate the class activation maps upsample to 256x256
size_upsample = (256, 256)
nc, h, w = feature_conv.shape
output_cam = []
for idx in class_idx:
cam = weight_softmax[class_idx].dot(feature_conv.reshape((nc, h*w)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
# ======================
# Main functions
# ======================
def recognize_from_image(net, weight, classes, labels_IO, labels_attribute, W_attribute):
# input image loop
for image_path in args.input:
# prepare input data
logger.info(image_path)
img = Image.open(image_path)
img = apply_centre_crop(np.array(img))
output = net.predict({'input_img': img})
logit = output[0].astype(np.float32)
if args.model in ['wideresnet18']:
out_layer4 = np.squeeze(output[1].astype(np.float32))
out_avgpool = np.squeeze(output[2].astype(np.float32))
h_x = softmax(logit[0])
idx = np.argsort(-h_x)
probs = h_x[idx]
logger.info('prediction on {}'.format(image_path))
# output the IO prediction
if args.model in ['wideresnet18']:
io_image = np.mean(labels_IO[idx[:10]]) # vote for the indoor or outdoor
print('--TYPE OF ENVIRONMENT:')
if io_image < 0.5:
print('\tindoor')
else:
print('\toutdoor')
# output the prediction of scene category
print('--SCENE CATEGORIES:')
for i in range(0, 5):
print('\t{:.3f} -> {}'.format(probs[i], classes[idx[i]]))
if args.model in ['wideresnet18']:
# output the scene attributes
responses_attribute = W_attribute.dot(out_avgpool)
idx_a = np.argsort(responses_attribute)
print('--SCENE ATTRIBUTES:')
print('\t', ', '.join([labels_attribute[idx_a[i]] for i in range(-1,-10,-1)]))
if args.savepath is not None:
# generate class activation mapping
logger.info('Class activation map is saved as {}'.format(args.savepath))
CAMs = returnCAM(out_layer4, weight, [idx[0]])
# render the CAM and output
img = cv2.imread(image_path)
height, width, _ = img.shape
heatmap = cv2.applyColorMap(cv2.resize(CAMs[0],(width, height)), cv2.COLORMAP_JET)
result = heatmap * 0.4 + img * 0.5
cv2.imwrite(args.savepath, result)
logger.info('Script finished successfully.')
def recognize_from_video(net, weight, classes, labels_IO, labels_attribute, W_attribute):
capture = webcamera_utils.get_capture(args.video)
# create video writer if savepath is specified as video format
writer = None
if args.model in ['wideresnet18']:
if args.savepath != SAVE_IMAGE_PATH and args.savepath is not None:
f_h = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
f_w = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
save_h, save_w = f_h, f_w
writer = webcamera_utils.get_writer(args.savepath, save_h, save_w)
frame_shown = False
frame_i = 0
while (True):
ret, frame = capture.read()
if (cv2.waitKey(1) & 0xFF == ord('q')) or not ret:
break
if frame_shown and cv2.getWindowProperty('frame', cv2.WND_PROP_VISIBLE) == 0:
break
# prepare input data
org = frame
frame = Image.fromarray(frame)
img = apply_centre_crop(np.array(frame))
output = net.predict({'input_img': img})
logit = output[0].astype(np.float32)
if args.model in ['wideresnet18']:
out_layer4 = np.squeeze(output[1].astype(np.float32))
out_avgpool = np.squeeze(output[2].astype(np.float32))
h_x = softmax(logit[0])
idx = np.argsort(-h_x)
probs = h_x[idx]
logger.info('prediction on index_{}'.format(frame_i))
frame_i += 1
# output the IO prediction
if args.model in ['wideresnet18']:
io_image = np.mean(labels_IO[idx[:10]]) # vote for the indoor or outdoor
print('--TYPE OF ENVIRONMENT:')
if io_image < 0.5:
print('\tindoor')
else:
print('\toutdoor')
# output the prediction of scene category
print('--SCENE CATEGORIES:')
for i in range(0, 5):
print('\t{:.3f} -> {}'.format(probs[i], classes[idx[i]]))
if args.model in ['wideresnet18']:
# output the scene attributes
responses_attribute = W_attribute.dot(out_avgpool)
idx_a = np.argsort(responses_attribute)
print('--SCENE ATTRIBUTES:')
print('\t', ', '.join([labels_attribute[idx_a[i]] for i in range(-1,-10,-1)]))
if writer is not None:
# generate class activation mapping
logger.info('Class activation map is saved.')
CAMs = returnCAM(out_layer4, weight, [idx[0]])
# render the CAM and output
height, width, _ = org.shape
heatmap = cv2.applyColorMap(cv2.resize(CAMs[0],(width, height)), cv2.COLORMAP_JET)
result = heatmap * 0.4 + org * 0.5
result = result.astype(np.uint8)
cv2.imshow('Class activation map', result)
frame_shown = True
writer.write(result)
capture.release()
cv2.destroyAllWindows()
if writer is not None:
writer.release()
logger.info('Script finished successfully.')
def main():
# net initialize
net, weight = get_model()
# get label
classes = get_label_scene_category()
labels_IO = get_label_indoor_and_outdoor()
labels_attribute, W_attribute = get_label_scene_attribute()
if args.video is not None:
# video mode
recognize_from_video(net, weight, classes, labels_IO, labels_attribute, W_attribute)
else:
# image mode
recognize_from_image(net, weight, classes, labels_IO, labels_attribute, W_attribute)
if __name__ == '__main__':
main()