-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
720 lines (639 loc) · 31.5 KB
/
evaluate.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import math
import networkx as nx
import matplotlib.pyplot as plt
from itertools import product
import math
import csv
import torch
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
import os.path as osp
import numpy as np
from data import COCODetection, get_label_map, MEANS, COLORS
from yolact import Yolact
from utils.augmentations import BaseTransform, FastBaseTransform, Resize
from utils.functions import MovingAverage, ProgressBar
from layers.box_utils import jaccard, center_size, mask_iou
from utils import timer
from utils.functions import SavePath
from layers.output_utils import postprocess, undo_image_transformation
import pycocotools
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from data import cfg, set_cfg, set_dataset
from math import sqrt
from timeit import default_timer as timer
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
import argparse
import time
import random
import cProfile
import pickle
import json
import os
from collections import defaultdict
from pathlib import Path
from collections import OrderedDict
from PIL import Image
import matplotlib.pyplot as plt
import cv2
import networkx as nx
import matplotlib.pyplot as plt
from itertools import product
import math
import csv
import torch
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
import os.path as osp
import numpy as np
from COCOParser import COCOParser
from gin import GIN
from gcn import GCN
from ginlaf import LAFNet
from collections import defaultdict
import json
import numpy as np
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def parse_args(argv=None):
parser = argparse.ArgumentParser(
description='YOLACT COCO Evaluation')
parser.add_argument('--trained_model',
default='weights/ssd300_mAP_77.43_v2.pth', type=str,
help='Trained state_dict file path to open. If "interrupt", this will open the interrupt file.')
parser.add_argument('--top_k', default=5, type=int,
help='Further restrict the number of predictions to parse')
parser.add_argument('--cuda', default=True, type=str2bool,
help='Use cuda to evaulate model')
parser.add_argument('--fast_nms', default=True, type=str2bool,
help='Whether to use a faster, but not entirely correct version of NMS.')
parser.add_argument('--cross_class_nms', default=False, type=str2bool,
help='Whether compute NMS cross-class or per-class.')
parser.add_argument('--display_masks', default=True, type=str2bool,
help='Whether or not to display masks over bounding boxes')
parser.add_argument('--display_bboxes', default=True, type=str2bool,
help='Whether or not to display bboxes around masks')
parser.add_argument('--display_text', default=True, type=str2bool,
help='Whether or not to display text (class [score])')
parser.add_argument('--display_scores', default=True, type=str2bool,
help='Whether or not to display scores in addition to classes')
parser.add_argument('--display', dest='display', action='store_true',
help='Display qualitative results instead of quantitative ones.')
parser.add_argument('--shuffle', dest='shuffle', action='store_true',
help='Shuffles the images when displaying them. Doesn\'t have much of an effect when display is off though.')
parser.add_argument('--ap_data_file', default='results/ap_data.pkl', type=str,
help='In quantitative mode, the file to save detections before calculating mAP.')
parser.add_argument('--resume', dest='resume', action='store_true',
help='If display not set, this resumes mAP calculations from the ap_data_file.')
parser.add_argument('--max_images', default=-1, type=int,
help='The maximum number of images from the dataset to consider. Use -1 for all.')
parser.add_argument('--output_coco_json', dest='output_coco_json', action='store_true',
help='If display is not set, instead of processing IoU values, this just dumps detections into the coco json file.')
parser.add_argument('--bbox_det_file', default='results/bbox_detections.json', type=str,
help='The output file for coco bbox results if --coco_results is set.')
parser.add_argument('--mask_det_file', default='results/mask_detections.json', type=str,
help='The output file for coco mask results if --coco_results is set.')
parser.add_argument('--config', default=None,
help='The config object to use.')
parser.add_argument('--output_web_json', dest='output_web_json', action='store_true',
help='If display is not set, instead of processing IoU values, this dumps detections for usage with the detections viewer web thingy.')
parser.add_argument('--web_det_path', default='web/dets/', type=str,
help='If output_web_json is set, this is the path to dump detections into.')
parser.add_argument('--no_bar', dest='no_bar', action='store_true',
help='Do not output the status bar. This is useful for when piping to a file.')
parser.add_argument('--display_lincomb', default=False, type=str2bool,
help='If the config uses lincomb masks, output a visualization of how those masks are created.')
parser.add_argument('--benchmark', default=False, dest='benchmark', action='store_true',
help='Equivalent to running display mode but without displaying an image.')
parser.add_argument('--no_sort', default=False, dest='no_sort', action='store_true',
help='Do not sort images by hashed image ID.')
parser.add_argument('--seed', default=None, type=int,
help='The seed to pass into random.seed. Note: this is only really for the shuffle and does not (I think) affect cuda stuff.')
parser.add_argument('--mask_proto_debug', default=False, dest='mask_proto_debug', action='store_true',
help='Outputs stuff for scripts/compute_mask.py.')
parser.add_argument('--no_crop', default=False, dest='crop', action='store_false',
help='Do not crop output masks with the predicted bounding box.')
parser.add_argument('--image', default=None, type=str,
help='A path to an image to use for display.')
parser.add_argument('--image_dir', default=None, type=str,
help='A path to an image to use for display.')
parser.add_argument('--name', default=None, type=str,
help='A path to an image to use for display.')
parser.add_argument('--images', default=None, type=str,
help='An input folder of images and output folder to save detected images. Should be in the format input->output.')
parser.add_argument('--video', default=None, type=str,
help='A path to a video to evaluate on. Passing in a number will use that index webcam.')
parser.add_argument('--video_multiframe', default=1, type=int,
help='The number of frames to evaluate in parallel to make videos play at higher fps.')
parser.add_argument('--score_threshold', default=0, type=float,
help='Detections with a score under this threshold will not be considered. This currently only works in display mode.')
parser.add_argument('--dataset', default="/content/test.json", type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--dir', default="./content", type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--coco_images_dir', default="/content/images", type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--gcn_model', default=None, type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--model', default="GCN", type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--file_res', default="/content/", type=str,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--GCN', default=True, type=str2bool,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--detect', default=False, dest='detect', action='store_true',
help='Don\'t evauluate the mask branch at all and only do object detection. This only works for --display and --benchmark.')
parser.add_argument('--display_fps', default=False, dest='display_fps', action='store_true',
help='When displaying / saving video, draw the FPS on the frame')
parser.add_argument('--nb_label', default=1, type=int,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--beta', default=0.1, type=float,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.add_argument('--emulate_playback', default=False, dest='emulate_playback', action='store_true',
help='When saving a video, emulate the framerate that you\'d get running in real-time mode.')
parser.add_argument('--display_scene', default=True, type=str2bool,
help='Whether or not to display score in addition to classes')
parser.add_argument('--object_detection', default=True, type=str2bool,
help='Activate or not the object detection pipeline')
parser.add_argument('--hidden', default=1024, type=int,
help='If specified, override the dataset specified in the config with this one (example: coco2017_dataset).')
parser.set_defaults(no_bar=False, display=False, resume=False, output_coco_json=False, output_web_json=False,
shuffle=False,
benchmark=False, no_sort=False, no_hash=False, mask_proto_debug=False, crop=True, detect=False,
display_fps=False,
emulate_playback=False)
global args
args = parser.parse_args(argv)
if args.output_web_json:
args.output_coco_json = True
if args.seed is not None:
random.seed(args.seed)
iou_thresholds = [x / 100 for x in range(50, 100, 5)]
coco_cats = {} # Call prep_coco_cats to fill this
coco_cats_inv = {}
color_cache = defaultdict(lambda: {})
def sort_res_scene(dets_out, gcn, img, h, w ,nb_label,beta, content, undo_transform=True):
"""
Note: If undo_transform=False then im_h and im_w are allowed to be None.
"""
if undo_transform:
img_numpy = undo_image_transformation(img, w, h)
img_gpu = torch.Tensor(img_numpy).cuda()
else:
img_gpu = img / 255.0
h, w, _ = img.shape
# with timer.env('Postprocess'):
save = cfg.rescore_bbox
cfg.rescore_bbox = True
t = postprocess(dets_out, w, h, visualize_lincomb=args.display_lincomb,
crop_masks=args.crop,
score_threshold=args.score_threshold)
cfg.rescore_bbox = save
# with timer.env('Copy'):
idx = t[1].argsort(0, descending=True)[:args.top_k]
if cfg.eval_mask_branch:
# Masks are drawn on the GPU, so don't copy
masks = t[3][idx]
classes, scores, boxes = [x[idx].cpu().numpy() for x in t[:3]]
num_dets_to_consider = min(args.top_k, classes.shape[0])
for j in range(num_dets_to_consider):
if scores[j] < args.score_threshold:
num_dets_to_consider = j
break
if num_dets_to_consider == 0:
# print("ERROR")
res = [-1, 0, -1]
return res
else:
# print("DETECTED OBJECT: {}".format(num_dets_to_consider))
position_ = []
class_ = []
information = []
# Create nodes and edges list
nodes = []
edges = []
edges_ = []
sets = []
n = 0
for j in reversed(range(num_dets_to_consider)):
x1, y1, x2, y2 = boxes[j, :]
# print(f'x: {x1} y: {y1} w: {x2} h:{y2}')
score = scores[j]
class_id = classes[j]
# print(f'Class: {class_id}')
# Create Graph
pos = np.array([[x1, y1]])
posi = [x1, y1]
if n == 0:
position = pos
else:
position = np.concatenate((position, pos), axis=0)
# Compute the object diagonal
diagonal = float(math.sqrt(x2 * x2 + y2 * y2))
info = [x1, y1, x2, y2, class_id, diagonal]
position_.append(posi)
information.append(info)
n = n + 1
length = len(position_)
min_value = min(position_)
min_index = position_.index(min_value)
information = sorted(information)
if(length > nb_label):
for i in range(0, length - 1):
for n in range((i + 1), length):
# Computhe distance between objects to define corresponding weight for specific edge
distance = (math.sqrt((position[i][0] - position[n][0]) ** 2 + (position[i][1] - position[n][1]) ** 2))
# print(distance)
edges.append({"source": i, "target": n, "attributes": {"weight": distance}})
for i in range(0, length):
nodes.append({"id": i, "attributes": {"class": float(information[i][4]), "size": information[i][5]}})
distances = []
if i < length:
for n in range((i + 1), length):
# Computhe distance between objects to define corresponding weight for specific edge
dist = (math.sqrt((information[i][0] - information[n][0]) ** 2 + (
information[i][1] - information[n][1]) ** 2))
distances.append(dist)
# print("value i: {} and j: {} distance: {}".format(i,j, distances))
else:
dist = 0
distance.append(dist)
if (len(distances) > 0):
min_value = min(distances)
min_index = distances.index(min_value)
else:
min_value = 1000
min_index = 0
if i < (length - 1):
idx = min_index + 1 + i
edges_.append({"source": i, "target": idx, "attributes": {"weight": min_value}})
# print(edges_)
for j in range(len(distances)):
if ((distances[j] <= (min_value + beta * min_value)) and j != min_index):
if j < i:
idx = j
else:
idx = j + 1
edges_.append({"source": i, "target": idx, "attributes": {"weight": distances[j]}})
node_attributes = []
for node in nodes:
node_attributes.append([node["attributes"][key] for key in node["attributes"]])
# for node in nodes:
# node_attributes.append(node["attributes"]["class"])
# # Imprime uniquement le premier attribut de chaque noeud (le nombre de classe)
# print(f'NODE: {node["attributes"]["class"]}')
s = []
t = []
edge_index = []
edge_attributes = []
for edge in edges_:
source = int(edge["source"])
target = int(edge["target"])
# edge_index.append([source, target])
s.append(source)
t.append(target)
edge_attributes.append([edge["attributes"][key] for key in edge["attributes"]])
edge_index = [s, t]
# Tensorised
node_attributes = torch.Tensor(node_attributes)
position_ = torch.Tensor(position_)
edge_index = torch.Tensor(edge_index)
edge_index = edge_index.to(torch.long)
batch = torch.zeros([len(node_attributes)], dtype=torch.long)
num_node_features = 2
num_edge_features = 1
num_classes = 2
# batch = 1
start = timer()
outg = gcn(node_attributes, edge_index, batch)
res_gcn = outg.argmax(dim=1)
end = timer()
time = end-start
if(res_gcn==0):
# print("INDOOR: {}".format(res_gcn))
res_="INDOOR"
else:
# print("OUTDOOR: {}".format(res_gcn))
res_ = "OUTDOOR"
# print("Content : {}".format(content))
res = [res_gcn, time, content]
return res
else:
# print("DEFAUT")
res = [-1, 0, -1]
return res
return res
def sort_res_scene_(annotations, gcn,nb_label,beta, content, undo_transform=True):
"""
Note: If undo_transform=False then im_h and im_w are allowed to be None.
"""
num_dets_to_consider = len(annotations)
length = len(annotations)
print(f'Number annot in fct: {num_dets_to_consider}')
if length == 0:
# print("ERROR")
res = [-1, 0, -1]
return res
else:
# print("DETECTED OBJECT: {}".format(num_dets_to_consider))
position_ = []
class_ = []
information = []
# Create nodes and edges list
nodes = []
edges = []
edges_ = []
sets = []
j = 0
for ann in annotations:
# Extract the bounding boxe information
bbox = ann['bbox']
x, y, w, h = [int(b) for b in bbox]
# print(f'x: {x} y: {y} w: {w} h:{h}')
pos = np.array([[x, y]])
posi = [x, y]
if j == 0:
position = pos
else:
position = np.concatenate((position, pos), axis=0)
# Compute the object diagonal
diagonal = float(math.sqrt(w * w + h * h))
# Extract the object class information
class_id = ann["category_id"]
class_name = coco.load_cats(class_id)[0]["name"]
# Add node to the Graph G
# nodes.append({"id": j, "attributes": {"class": float(class_id), "size": diagonal}})
info = [x, y, w, h, int(class_id), class_name, diagonal]
position_.append(posi)
information.append(info)
j += 1
length = len(position_)
min_value = min(position_)
min_index = position_.index(min_value)
information = sorted(information)
if(length > nb_label):
for i in range(0, length - 1):
for n in range((i + 1), length):
# Computhe distance between objects to define corresponding weight for specific edge
distance = (math.sqrt((position[i][0] - position[n][0]) ** 2 + (position[i][1] - position[n][1]) ** 2))
# print(distance)
edges.append({"source": i, "target": n, "attributes": {"weight": distance}})
for i in range(0, length):
nodes.append({"id": i, "attributes": {"class": float(information[i][4]), "size": information[i][5]}})
distances = []
if i < length:
for n in range((i + 1), length):
# Computhe distance between objects to define corresponding weight for specific edge
dist = (math.sqrt((information[i][0] - information[n][0]) ** 2 + (
information[i][1] - information[n][1]) ** 2))
distances.append(dist)
# print("value i: {} and j: {} distance: {}".format(i,j, distances))
else:
dist = 0
distance.append(dist)
if (len(distances) > 0):
min_value = min(distances)
min_index = distances.index(min_value)
else:
min_value = 1000
min_index = 0
if i < (length - 1):
idx = min_index + 1 + i
edges_.append({"source": i, "target": idx, "attributes": {"weight": min_value}})
# print(edges_)
for j in range(len(distances)):
if ((distances[j] <= (min_value + beta * min_value)) and j != min_index):
if j < i:
idx = j
else:
idx = j + 1
edges_.append({"source": i, "target": idx, "attributes": {"weight": distances[j]}})
node_attributes = []
# for node in nodes:
# node_attributes.append([node["attributes"][key] for key in node["attributes"]])
# print(f'NODE: {[node["attributes"][key] for key in node["attributes"]]}')
# print(f'length node: {len(nodes)}')
for node in nodes:
# Extraire l'attribut "class" du dictionnaire des attributs de chaque nœud
class_value = node["attributes"]["class"]
# Ajouter cette valeur à la liste node_attributes
node_attributes.append(int(class_value))
# Afficher la longueur et les valeurs stockées
# print(f'NODE: {len(node_attributes)}')
# print(f'Node attributes (classes): {node_attributes}')
# ct=0
# for node in nodes:
# node_attributes.append([node["attributes"]["class"]])
# # Imprime uniquement le premier attribut de chaque noeud (le nombre de classe)
# print(f'NODE: {node["attributes"]["class"]}')
# ct += 1
# print(f'counter: {ct}')
# node_attributes.append(nodes["attributes"]["class"])
print(f'end annotation: {len(node_attributes)}')
s = []
t = []
edge_index = []
edge_attributes = []
for edge in edges_:
source = int(edge["source"])
target = int(edge["target"])
# edge_index.append([source, target])
s.append(source)
t.append(target)
edge_value = edge["attributes"]["weight"]
edge_attributes.append(edge_value)
edge_index = [s, t]
# Tensorised
node_attributes = torch.Tensor(node_attributes)
position_ = torch.Tensor(position_)
edge_index = torch.Tensor(edge_index)
edge_index = edge_index.to(torch.long)
batch = torch.zeros([len(node_attributes)], dtype=torch.long)
# batch = 1
num_node_features = 2
num_edge_features = 1
num_classes = 2
start = timer()
outg = gcn(node_attributes, edge_index, batch)
res_gcn = outg.argmax(dim=1)
end = timer()
time = end-start
if(res_gcn==0):
# print("INDOOR: {}".format(res_gcn))
res_="INDOOR"
else:
# print("OUTDOOR: {}".format(res_gcn))
res_ = "OUTDOOR"
# print("Content : {}".format(content))
res = [res_gcn, time, content]
return res
else:
# print("DEFAUT")
res = [-1, 0, -1]
return res
return res
def evaluate_(gcn, coco, content, nb_label, beta):
# net.detect.use_fast_nms = args.fast_nms
# net.detect.use_cross_class_nms = args.cross_class_nms
# cfg.mask_proto_debug = args.mask_proto_debug
# TODO Currently we do not support Fast Mask Re-scroing in evalimage, evalimages, and evalvideo
if args.GCN is True:
img_ids = coco.get_imgIds()
nb_images=len(img_ids)
nb_pred = 0;
cpt_time = 0.0;
cpt_good_pred = 0.0;
for im in range(int(nb_images*0.9), nb_images):
# Select images
selected_img_ids = img_ids[im]
ann_ids = coco.get_annIds(selected_img_ids)
currentlines = content[im].split(",")
content_ = int(currentlines[1])
# Get annotations of image number img
annotations = coco.load_anns(ann_ids)
print(f'length annotation: {len(annotations)}')
result = sort_res_scene_(annotations, gcn, nb_label, beta, content_, undo_transform=False)
if result[0] != -1:
nb_pred = nb_pred+1
cpt_time = cpt_time + result[1]
if result[0].item() == result[2]:
cpt_good_pred = cpt_good_pred +1
av_time = cpt_time/nb_pred
precision = cpt_good_pred/nb_pred
print('Average: %5.7f s, Precision %5.5f' % (av_time, precision))
print('Total time: %5.7f s, Total prediction %5.5f' % (cpt_time, cpt_good_pred ))
def evaluate(net: Yolact, gcn, coco, content,image_dir,nb_label, beta):
net.detect.use_fast_nms = args.fast_nms
net.detect.use_cross_class_nms = args.cross_class_nms
cfg.mask_proto_debug = args.mask_proto_debug
# TODO Currently we do not support Fast Mask Re-scroing in evalimage, evalimages, and evalvideo
if args.GCN is True:
img_ids = coco.get_imgIds()
nb_images=len(img_ids)
nb_pred = 0;
cpt_time = 0.0;
cpt_good_pred = 0.0;
print(f'evaluate process, nb image : {nb_images}')
for im in range(int(nb_images*0.9), nb_images):
# Select images
selected_img_ids = img_ids[im]
currentlines = content[im].split(",")
content_ = int(currentlines[1])
name = str(selected_img_ids).zfill(12)
image_name = image_dir + name + ".jpg"
print(image_name)
frame = torch.from_numpy(cv2.imread(image_name)).cuda().float()
batch = FastBaseTransform()(frame.unsqueeze(0))
preds = net(batch)
# img, h, w
h, w, _ = frame.shape
result = sort_res_scene(preds, gcn, frame,h, w,nb_label, beta, content_, undo_transform=False)
if result[0] != -1:
nb_pred = nb_pred+1
cpt_time = cpt_time + result[1]
if result[0].item() == result[2]:
cpt_good_pred = cpt_good_pred +1
av_time = cpt_time/nb_pred
precision = cpt_good_pred/nb_pred
print('Average: %5.7f s, Precision %5.5f' % (av_time, precision))
print('Total time: %5.7f s, Total prediction %5.5f' % (cpt_time, cpt_good_pred ))
def prep_coco_cats():
""" Prepare inverted table for category id lookup given a coco cats object. """
for coco_cat_id, transformed_cat_id_p1 in get_label_map().items():
transformed_cat_id = transformed_cat_id_p1 - 1
coco_cats[transformed_cat_id] = coco_cat_id
coco_cats_inv[coco_cat_id] = transformed_cat_id
if __name__ == '__main__':
parse_args()
if args.config is not None:
set_cfg(args.config)
# if args.config is None:
# model_path = SavePath.from_str(args.trained_model)
# # TODO: Bad practice? Probably want to do a name lookup instead.
# args.config = model_path.model_name + '_config'
# print('Config not specified. Parsed %s from the file name.\n' % args.config)
# set_cfg(args.config)
if args.display_scene:
# model_gcn = model.load_state_dict(torch.load('model2_maxpool.pth'))
state_dict = torch.load(args.gcn_model)
num_node_features = 2
num_edge_features = 1
hidden_channels = 32
hidden_channels = args.hidden
num_classes = 2
# batch_= 64
if(args.model=='GIN'):
print("GIN PROCESS")
hidden_channels = 1024
model_gcn = GIN(num_node_features,hidden_channels,num_classes)
if (args.model == 'GCN'):
print("GCN PROCESS")
hidden_channels = 1024
model_gcn = GCN(num_node_features, hidden_channels, num_classes)
if (args.model == 'GINLAF'):
print("GINLAF PROCESS")
hidden_channels = 32
model_gcn = LAFNet(num_node_features, hidden_channels, num_classes)
# model_gcn=model_gcn.load_state_dict(state_dict)
# model_gcn = torch.load('model2_maxpool_.pth')
model_gcn.load_weights(args.gcn_model)
print("LOADED GCN MODEL")
coco_annotations_file = args.dataset
coco_images_dir = args.dir
coco = COCOParser(coco_annotations_file, coco_images_dir)
print("LOADED COCO")
if args.detect:
cfg.eval_mask_branch = False
with torch.no_grad():
if not os.path.exists('results'):
os.makedirs('results')
if args.cuda:
cudnn.fastest = True
torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
torch.set_default_tensor_type('torch.FloatTensor')
if args.object_detection:
print('Loading model...', end='')
net = Yolact()
net.load_weights(args.trained_model)
net.eval()
print(' Done.')
if args.cuda:
net = net.cuda()
# files_ = args.file_res + 'tests.txt'
files_ = args.file_res
file = open(files_)
# read the content of the file opened
content = file.readlines()
if args.display_scene:
print("EXECUTE Graph Neural Network:")
criterion = torch.nn.CrossEntropyLoss()
model_gcn.eval()
model_gcn = model_gcn.cuda()
# model, data = model.to(device), data.to(device)
print("Minimum number of label classes: {} and beta value: {}".format(args.nb_label, args.beta))
evaluate(net, model_gcn, coco, content, args.image_dir, args.nb_label, args.beta)
else:
print('Not Loading object detection model...', end='')
# files_ = args.file_res + 'tests.txt'
files_ = args.file_res
file = open(files_)
# read the content of the file opened
content = file.readlines()
if args.display_scene:
print("EXECUTE Graph Neural Network:")
criterion = torch.nn.CrossEntropyLoss()
model_gcn.eval()
model_gcn = model_gcn.cuda()
# model, data = model.to(device), data.to(device)
print("Minimum number of label classes: {} and beta value: {}".format(args.nb_label, args.beta))
evaluate_(model_gcn, coco, content, args.nb_label, args.beta)