-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
127 lines (104 loc) · 4.5 KB
/
test.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
import sys
import yaml
import argparse
from util import *
from src.models.Detection.Faster_RCNN import Faster_RCNN
from src.models.Segmentation.MaskRCNN import Mask_RCNN
from src.dataset.bdd_detetcion import BDDDetection
from src.dataset.bdd_instance_segmentation_drivable import BDDInstanceSegmentationDrivable
from src.config.defaults import cfg
from src.utils.DataLoaders import get_loader
from pytorch_lightning import Trainer
import pandas as pd
from pprint import pprint
import warnings
warnings.filterwarnings("ignore")
if __name__ == '__main__':
# Define the parser
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default="./data/fasterrcnn.yaml", help='data.yaml path')
parser.add_argument('--weights', type=str, default=None, help='train from checkpoint')
parser.add_argument('--pred', type=str, default='', help='Path to the predictions folder')
parser.add_argument('--gt', type=str, default='', help='Path to the ground truth folder')
parser.add_argument('--model', type=str, default='fasterrcnn', choices=['fasterrcnn', 'deeplab', 'maskrcnn', 'yolov5', 'yolov7'],
help='the model and task you want to perform')
parser.add_argument('--save-path', type=str, default='./mAP_results.csv',
help='Path and name of the file you want to export.')
# Fetch the params from the parser
args = parser.parse_args()
weights = args.weights # Check point to continue training
save_path = args.save_path
model_name = args.model # the name of the model: fastercnn, maskrcnn, deeplab
with open(args.data, 'r') as f:
data = yaml.safe_load(f) # data from .yaml file
obj_cls = data['classes'] # the classes we want to work one
relative_path = data['relative_path'] # relative path to the dataset
if model_name == "fasterrcnn":
## Load Model
try:
model = Faster_RCNN.load_from_checkpoint(weights)
except Exception as e:
print("Could not load the model weights. Please make sure you're providing the correct model weights.")
sys.exit()
bdd_params = {
'cfg': cfg,
'stage': 'test',
'relative_path': relative_path,
'obj_cls': obj_cls,
}
bdd = BDDDetection(**bdd_params)
dataloader_args = {
'dataset': bdd,
'batch_size': 1,
'shuffle': False,
'collate_fn': bdd.collate_fn,
'pin_memory': True,
'num_workers': 1
}
dataloader = get_loader(**dataloader_args)
trainer = Trainer(accelerator='gpu', devices=1)
pred = trainer.predict(model, dataloader)
print("Start Computing mAP for all classes.")
mAP = model.metric.compute()
elif model_name == 'maskrcnn':
try:
model = Mask_RCNN.load_from_checkpoint(weights)
except Exception as e:
print("Could not load the model weights. Please make sure you're providing the correct model weights.")
sys.exit()
bdd_params = {
'cfg': cfg,
'stage': 'test',
'relative_path': relative_path,
'obj_cls': obj_cls,
}
bdd = BDDInstanceSegmentationDrivable(**bdd_params)
dataloader_args = {
'dataset': bdd,
'batch_size': 1,
'shuffle': False,
'collate_fn': bdd.collate_fn,
'pin_memory': True,
'num_workers': 1
}
dataloader = get_loader(**dataloader_args)
trainer = Trainer(accelerator='gpu', devices=1)
pred = trainer.predict(model, dataloader)
print("Start Computing mAP for all classes.")
mAP = model.metric.compute()
elif model_name.startswith('yolo'):
"""
We evaluate Yolo models the same way
"""
# get the path where the *.txt file are stored. Those files are generated from the model
prediction_path = args.pred
# path to the ground truth folder, where we have the *.txt file. Those files are genearted from the prepate.py file
gt_path = args.gt
# check paths
assert os.path.exists(prediction_path), f"Predictions does not exists at {prediction_path}"
assert os.path.exists(gt_path), f"Predictions does not exists at {gt_path}"
# Evaluation
mAP = yolo_evaluation(prediction_path, gt_path)
# print
pprint(mAP)
export_map_json(mAP, json_file_name=f'mAP_{model_name}.json' , to_save_path=save_path)