質問編集履歴
2
初心者なもので、、、コードをちゃんと書いてませんでした。どうぞよろしくお願いします。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
python
|
1
|
+
python 他クラスから2クラス検出にしたいですがどこをどう変えればよいかわかりません。
|
test
CHANGED
@@ -2,8 +2,6 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
ここに質問の内容を詳しく書いてください。
|
6
|
-
|
7
5
|
|
8
6
|
|
9
7
|
pythonを使ってディープラーニングをやっているのですが、2クラスだけ(Backgroundを含めずに)の検出にしたい場合、
|
1
初心者なもので、、、コードをちゃんと書いてませんでした。どうぞよろしくお願いします。
test
CHANGED
File without changes
|
test
CHANGED
@@ -17,3 +17,435 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
https://github.com/guoruoqian/DetNet_pytorch/blob/master/demo.py
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
```ここに言語を入力
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
for target_size in cfg.TEST.SCALES:
|
28
|
+
|
29
|
+
im_scale = float(target_size) / float(im_size_min)
|
30
|
+
|
31
|
+
# Prevent the biggest axis from being more than MAX_SIZE
|
32
|
+
|
33
|
+
if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE:
|
34
|
+
|
35
|
+
im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max)
|
36
|
+
|
37
|
+
im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,
|
38
|
+
|
39
|
+
interpolation=cv2.INTER_LINEAR)
|
40
|
+
|
41
|
+
im_scale_factors.append(im_scale)
|
42
|
+
|
43
|
+
processed_ims.append(im)
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
# Create a blob to hold the input images
|
48
|
+
|
49
|
+
blob = im_list_to_blob(processed_ims)
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
return blob, np.array(im_scale_factors)
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
if __name__ == '__main__':
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
args = parse_args()
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
print('Called with args:')
|
68
|
+
|
69
|
+
print(args)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
args.cfg_file = "cfgs/{}.yml".format(args.net)
|
74
|
+
|
75
|
+
if args.cfg_file is not None:
|
76
|
+
|
77
|
+
cfg_from_file(args.cfg_file)
|
78
|
+
|
79
|
+
if args.set_cfgs is not None:
|
80
|
+
|
81
|
+
cfg_from_list(args.set_cfgs)
|
82
|
+
|
83
|
+
if not os.path.exists(args.result_dir):
|
84
|
+
|
85
|
+
os.mkdir(args.result_dir)
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
print('Using config:')
|
90
|
+
|
91
|
+
pprint.pprint(cfg)
|
92
|
+
|
93
|
+
np.random.seed(cfg.RNG_SEED)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
# train set
|
98
|
+
|
99
|
+
# -- Note: Use validation set and disable the flipped to enable faster loading.
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
if args.exp_name is not None:
|
104
|
+
|
105
|
+
input_dir = args.load_dir + "/" + args.net + "/" + args.dataset + '/' + args.exp_name
|
106
|
+
|
107
|
+
else:
|
108
|
+
|
109
|
+
input_dir = args.load_dir + "/" + args.net + "/" + args.dataset
|
110
|
+
|
111
|
+
if not os.path.exists(input_dir):
|
112
|
+
|
113
|
+
raise Exception('There is no input directory for loading network from ' + input_dir)
|
114
|
+
|
115
|
+
load_name = os.path.join(input_dir,
|
116
|
+
|
117
|
+
'fpn_{}_{}_{}.pth'.format(args.checksession, args.checkepoch, args.checkpoint))
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
classes = np.asarray(['__background__',
|
122
|
+
|
123
|
+
'aeroplane', 'bicycle', 'bird', 'boat',
|
124
|
+
|
125
|
+
'bottle', 'bus', 'car', 'cat', 'chair',
|
126
|
+
|
127
|
+
'cow', 'diningtable', 'dog', 'horse',
|
128
|
+
|
129
|
+
'motorbike', 'person', 'pottedplant',
|
130
|
+
|
131
|
+
'sheep', 'sofa', 'train', 'tvmonitor'])
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
if args.net == 'detnet59':
|
136
|
+
|
137
|
+
fpn = detnet(classes, 59, pretrained=False, class_agnostic=args.class_agnostic)
|
138
|
+
|
139
|
+
else:
|
140
|
+
|
141
|
+
print("network is not defined")
|
142
|
+
|
143
|
+
pdb.set_trace()
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
fpn.create_architecture()
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
checkpoint = torch.load(load_name)
|
152
|
+
|
153
|
+
fpn.load_state_dict(checkpoint['model'])
|
154
|
+
|
155
|
+
if 'pooling_mode' in checkpoint.keys():
|
156
|
+
|
157
|
+
cfg.POOLING_MODE = checkpoint['pooling_mode']
|
158
|
+
|
159
|
+
print('load model successfully!')
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
# pdb.set_trace()
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
print("load checkpoint %s" % (load_name))
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
# initilize the tensor holder here.
|
172
|
+
|
173
|
+
im_data = torch.FloatTensor(1)
|
174
|
+
|
175
|
+
im_info = torch.FloatTensor(1)
|
176
|
+
|
177
|
+
num_boxes = torch.LongTensor(1)
|
178
|
+
|
179
|
+
gt_boxes = torch.FloatTensor(1)
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
# ship to cuda
|
184
|
+
|
185
|
+
if args.cuda:
|
186
|
+
|
187
|
+
im_data = im_data.cuda()
|
188
|
+
|
189
|
+
im_info = im_info.cuda()
|
190
|
+
|
191
|
+
num_boxes = num_boxes.cuda()
|
192
|
+
|
193
|
+
gt_boxes = gt_boxes.cuda()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
# make variable
|
198
|
+
|
199
|
+
im_data = Variable(im_data, volatile=True)
|
200
|
+
|
201
|
+
im_info = Variable(im_info, volatile=True)
|
202
|
+
|
203
|
+
num_boxes = Variable(num_boxes, volatile=True)
|
204
|
+
|
205
|
+
gt_boxes = Variable(gt_boxes, volatile=True)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
if args.cuda:
|
210
|
+
|
211
|
+
cfg.CUDA = True
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
if args.cuda:
|
216
|
+
|
217
|
+
fpn.cuda()
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
fpn.eval()
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
start = time.time()
|
226
|
+
|
227
|
+
max_per_image = 100
|
228
|
+
|
229
|
+
thresh = 0.05
|
230
|
+
|
231
|
+
vis = True
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
imglist = os.listdir(args.image_dir)
|
236
|
+
|
237
|
+
num_images = len(imglist)
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
print('Loaded Photo: {} images.'.format(num_images))
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
for i in range(num_images):
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
# Load the demo image
|
250
|
+
|
251
|
+
im_file = os.path.join(args.image_dir, imglist[i])
|
252
|
+
|
253
|
+
# im = cv2.imread(im_file)
|
254
|
+
|
255
|
+
im = np.array(Image.open(im_file))
|
256
|
+
|
257
|
+
if len(im.shape) == 2:
|
258
|
+
|
259
|
+
im = im[:, :, np.newaxis]
|
260
|
+
|
261
|
+
im = np.concatenate((im, im, im), axis=2)
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
blobs, im_scales = _get_image_blob(im)
|
266
|
+
|
267
|
+
assert len(im_scales) == 1, "Only single-image batch implemented"
|
268
|
+
|
269
|
+
im_blob = blobs
|
270
|
+
|
271
|
+
im_info_np = np.array([[im_blob.shape[1], im_blob.shape[2], im_scales[0]]], dtype=np.float32)
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
im_data_pt = torch.from_numpy(im_blob)
|
276
|
+
|
277
|
+
im_data_pt = im_data_pt.permute(0, 3, 1, 2)
|
278
|
+
|
279
|
+
im_info_pt = torch.from_numpy(im_info_np)
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
im_data.data.resize_(im_data_pt.size()).copy_(im_data_pt)
|
284
|
+
|
285
|
+
im_info.data.resize_(im_info_pt.size()).copy_(im_info_pt)
|
286
|
+
|
287
|
+
gt_boxes.data.resize_(1, 1, 5).zero_()
|
288
|
+
|
289
|
+
num_boxes.data.resize_(1).zero_()
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
# pdb.set_trace()
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
det_tic = time.time()
|
298
|
+
|
299
|
+
rois, cls_prob, bbox_pred, \
|
300
|
+
|
301
|
+
_, _, _, _, _ = fpn(im_data, im_info, gt_boxes, num_boxes)
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
scores = cls_prob.data
|
306
|
+
|
307
|
+
boxes = (rois[:, :, 1:5] / im_scales[0]).data
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
if cfg.TEST.BBOX_REG:
|
312
|
+
|
313
|
+
# Apply bounding-box regression deltas
|
314
|
+
|
315
|
+
box_deltas = bbox_pred.data
|
316
|
+
|
317
|
+
if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:
|
318
|
+
|
319
|
+
# Optionally normalize targets by a precomputed mean and stdev
|
320
|
+
|
321
|
+
if args.class_agnostic:
|
322
|
+
|
323
|
+
box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_STDS).cuda() \
|
324
|
+
|
325
|
+
+ torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
|
326
|
+
|
327
|
+
box_deltas = box_deltas.view(1, -1, 4)
|
328
|
+
|
329
|
+
else:
|
330
|
+
|
331
|
+
box_deltas = box_deltas.view(-1, 4) * torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_STDS).cuda() \
|
332
|
+
|
333
|
+
+ torch.FloatTensor(cfg.TRAIN.BBOX_NORMALIZE_MEANS).cuda()
|
334
|
+
|
335
|
+
box_deltas = box_deltas.view(1, -1, 4 * len(classes))
|
336
|
+
|
337
|
+
pred_boxes = bbox_transform_inv(boxes, box_deltas, 1)
|
338
|
+
|
339
|
+
pred_boxes = clip_boxes(pred_boxes, im_info.data, 1)
|
340
|
+
|
341
|
+
else:
|
342
|
+
|
343
|
+
# Simply repeat the boxes, once for each class
|
344
|
+
|
345
|
+
pred_boxes = np.tile(boxes, (1, scores.size[1]))
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
scores = scores.squeeze()
|
350
|
+
|
351
|
+
pred_boxes = pred_boxes.squeeze()
|
352
|
+
|
353
|
+
# _t['im_detect'].tic()
|
354
|
+
|
355
|
+
det_toc = time.time()
|
356
|
+
|
357
|
+
detect_time = det_toc - det_tic
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
misc_tic = time.time()
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
if vis:
|
366
|
+
|
367
|
+
im2show = np.copy(im[:, :, ::-1])
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
for j in xrange(1, len(classes)):
|
372
|
+
|
373
|
+
inds = torch.nonzero(scores[:, j] > thresh).view(-1)
|
374
|
+
|
375
|
+
if inds.numel() > 0:
|
376
|
+
|
377
|
+
cls_scores = scores[:, j][inds]
|
378
|
+
|
379
|
+
_, order = torch.sort(cls_scores, 0, True)
|
380
|
+
|
381
|
+
if args.class_agnostic:
|
382
|
+
|
383
|
+
cls_boxes = pred_boxes[inds, :]
|
384
|
+
|
385
|
+
else:
|
386
|
+
|
387
|
+
cls_boxes = pred_boxes[inds][:, j * 4:(j + 1) * 4]
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
cls_dets = torch.cat((cls_boxes, cls_scores.unsqueeze(1)), 1)
|
392
|
+
|
393
|
+
cls_dets = cls_dets[order]
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
if args.soft_nms:
|
398
|
+
|
399
|
+
np_dets = cls_dets.cpu().numpy().astype(np.float32)
|
400
|
+
|
401
|
+
keep = soft_nms(np_dets, method=cfg.TEST.SOFT_NMS_METHOD) # np_dets will be changed
|
402
|
+
|
403
|
+
keep = torch.from_numpy(keep).type_as(cls_dets).int()
|
404
|
+
|
405
|
+
cls_dets = torch.from_numpy(np_dets).type_as(cls_dets)
|
406
|
+
|
407
|
+
else:
|
408
|
+
|
409
|
+
keep = nms(cls_dets, cfg.TEST.NMS)
|
410
|
+
|
411
|
+
cls_dets = cls_dets[keep.view(-1).long()]
|
412
|
+
|
413
|
+
cls_dets = cls_dets.cpu().numpy()
|
414
|
+
|
415
|
+
else:
|
416
|
+
|
417
|
+
cls_dets = np.array([])
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
if vis:
|
422
|
+
|
423
|
+
im2show = vis_detections(im2show, classes[j], cls_dets, thresh=0.5)
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
misc_toc = time.time()
|
428
|
+
|
429
|
+
nms_time = misc_toc - misc_tic
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
sys.stdout.write('im_detect: {:d}/{:d} {:.3f}s {:.3f}s \r' \
|
434
|
+
|
435
|
+
.format(i + 1, num_images, detect_time, nms_time))
|
436
|
+
|
437
|
+
sys.stdout.flush()
|
438
|
+
|
439
|
+
|
440
|
+
|
441
|
+
if vis:
|
442
|
+
|
443
|
+
# cv2.imshow('test', im2show)
|
444
|
+
|
445
|
+
# cv2.waitKey(0)
|
446
|
+
|
447
|
+
result_path = os.path.join(args.result_dir, imglist[i][:-4] + "_det.jpg")
|
448
|
+
|
449
|
+
cv2.imwrite(result_path, im2show)
|
450
|
+
|
451
|
+
```
|