Object Detection
vision
File size: 11,123 Bytes
780c589
 
df8cf63
780c589
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
###########################################################################
# Computer vision - Embedded person tracking demo software by HyperbeeAI. #
# Copyrights © 2023 Hyperbee.AI Inc. All rights reserved. [email protected] #
###########################################################################
# Author: Zylo117

"""
COCO-Style Evaluations

put images here datasets/your_project_name/val_set_name/*.jpg
put annotations here datasets/your_project_name/annotations/instances_{val_set_name}.json
put weights here /path/to/your/weights/*.pth
change compound_coef

"""

import json
import os
import numpy as np

import argparse
import torch
from tqdm import tqdm
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from torch.utils.data import DataLoader
import torchvision
import torchvision.transforms as transforms
import time

from models import mnv2_SSDlite
from library.ssd import conv_model_fptunc2fpt, conv_model_fpt2qat, conv_model_qat2hw, collate_fn, PredsPostProcess, round_floats
from dataloader import CocoDetection, input_fxpt_normalize

#from library.ssd import generateAnchorsInOrigImage, collate_fn, point_form, prepareHeadDataforLoss_fast, plot_image_mnv2_2xSSDlite, sampleRandomPicsFromCOCO, saveOutputs ,PredsPostProcess, calculatemAP, batchNormAdaptation, round_floats

ap = argparse.ArgumentParser()
ap.add_argument('-m', '--mode', type=str, default='qat', help='Mode of the model, allowed modes: fpt_unc, fpt, qat')
ap.add_argument('--nms_threshold', type=float, default=0.5, help='non max supression threshold')
ap.add_argument('--conf_threshold', type=float, default=0.5, help='confidence treshold, predictions below this level will be discarded')
ap.add_argument('-dp', '--data_path', type=str, default=None, help='/path/to/images')
ap.add_argument('-ap', '--json_path', type=str, default=None, help='/path/to/annotations.json')
ap.add_argument('-wp', '--weights_path', type=str, default=None, help='/path/to/weights')

args = ap.parse_args()

mode = args.mode
nms_threshold = args.nms_threshold
conf_threshold = args.conf_threshold
data_path = args.data_path
json_path = args.json_path
weights_path = args.weights_path

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def evaluate_coco(model, DATA_PATH, JSON_PATH , nmsIoUTreshold = 0.5, PredMinConfTreshold = 0.5, HW_mode = False):
    
    if HW_mode:
        act_8b_mode = True
    else:
        act_8b_mode = False

    transform = transforms.Compose([transforms.ToTensor(), input_fxpt_normalize(act_8b_mode=act_8b_mode)])
    targetFileName = 'resized.json'
    dataset = CocoDetection(root=DATA_PATH, annFile=JSON_PATH, transform=transform, scaleImgforCrop= None)

    dataset.createResizedAnnotJson(targetFileName=targetFileName)
    resizedFilePath = os.path.join(os.path.split(JSON_PATH)[0],targetFileName)
    cocoGt=COCO(resizedFilePath)
    os.remove(resizedFilePath)

    seq_sampler = torch.utils.data.SequentialSampler(dataset)
    data_loader = DataLoader(dataset,
                              sampler=seq_sampler,
                              batch_size=1,
                              collate_fn=collate_fn,
                              drop_last=False)
    print(f"Dataset Length: {len(dataset)}, Number of Batches: {len(data_loader)}")
    
    ANCHORS_HEAD1 = [(11.76, 28.97),
                     (20.98, 52.03),
                     (29.91, 77.24),
                     (38.97, 106.59)]

    ANCHORS_HEAD2 = [(52.25, 144.77),
                    (65.86, 193.05),
                    (96.37, 254.09),
                    (100.91, 109.82),
                    (140, 350)]
    
    predsPostProcess = PredsPostProcess(512, ANCHORS_HEAD1, ANCHORS_HEAD2)

    
    dataDictList =[]
    imgIDS = []
    for i, data in enumerate(tqdm(data_loader)):
        imageBatch, targetBatch , idxBatch = data

        imageStack = torch.stack(imageBatch).detach().to(device)
        imageStack.requires_grad_(True)
        predBatch = model(imageStack)
        
        if HW_mode:
            BBs1 = predBatch[0].detach() / 128.0
            CFs1 = predBatch[1].detach() / 128.0
            BBs2 = predBatch[2].detach() / 128.0
            CFs2 = predBatch[3].detach() / 128.0
        else:
            BBs1 = predBatch[0].detach()
            CFs1 = predBatch[1].detach()
            BBs2 = predBatch[2].detach()
            CFs2 = predBatch[3].detach()

        for imgNum in range(imageStack.shape[0]):
            img = imageStack[imgNum,:,:,:]
            target = targetBatch[imgNum]
            image_id = int(idxBatch[imgNum])
            imgIDS.append(image_id)

            pred = (BBs1[imgNum,:,:,:].unsqueeze(0), CFs1[imgNum,:,:,:].unsqueeze(0), 
                    BBs2[imgNum,:,:,:].unsqueeze(0), CFs2[imgNum,:,:,:].unsqueeze(0))

            boxes, confidences = predsPostProcess.getPredsInOriginal(pred)

            nms_picks   = torchvision.ops.nms(boxes, confidences, nmsIoUTreshold) 
            boxes_to_draw = boxes[nms_picks]
            confs_to_draw = confidences[nms_picks]
            confMask = (confs_to_draw > PredMinConfTreshold)

            # Inputs to mAP algorithm
            if (confMask.any()):

                # pred boxes -> [xmin,ymin,xmax,ymax], tensor shape[numpred,4]
                bbox = boxes_to_draw[confMask]
                scores = confs_to_draw[confMask]
                # Convert BB to coco annot format -> [xmin,ymin,width, height]
                bbox[:,2] = bbox[:,2] - bbox[:,0]
                bbox[:,3] = bbox[:,3] - bbox[:,1]
                

                bbox = bbox.tolist() # pred boxes -> [xmin,ymin,xmax,ymax], shape[numpred,4]
                score = scores.tolist()
                category_id = np.ones_like(score,dtype=int).tolist()

                for j in range(len(bbox)):
                    box = {"image_id":image_id, "category_id":category_id[j], "bbox":bbox[j],"score":score[j]}
                    dataDictList.append(round_floats(box))

    if (len(dataDictList)):        
        # Evavluate and Accumulate mAP for remained baches, if any
        cocoDT = json.dumps(dataDictList)

        # Write detections to .json file
        with open('cocoDT.json', 'w') as outfile:
            outfile.write(cocoDT) 

        # Load detections
        cocoDt=cocoGt.loadRes('cocoDT.json')
        os.remove("cocoDT.json")

        # running evaluation
        annType = 'bbox'
        cocoEval = COCOeval(cocoGt,cocoDt,annType)
        cocoEval.params.catIds = 1
        cocoEval.params.imgIds  = imgIDS
        cocoEval.evaluate()
        cocoEval.accumulate()
        
        print('')
        cocoEval.summarize()
    else:
        raise Exception('the model does not provide any valid output, check model architecture and the data input')


if __name__ == '__main__':
    model = mnv2_SSDlite()
    
    layer_bits_dictionary = {}
    layer_bits_dictionary['conv1' ] = 8;
    layer_bits_dictionary['epw_conv2' ] = 8;
    layer_bits_dictionary['dw_conv2' ]  = 8;
    layer_bits_dictionary['ppw_conv2' ] = 8;

    layer_bits_dictionary['epw_conv3' ] = 8;
    layer_bits_dictionary['dw_conv3' ]  = 8;
    layer_bits_dictionary['ppw_conv3' ] = 8;

    layer_bits_dictionary['epw_conv4' ] = 8;
    layer_bits_dictionary['dw_conv4' ]  = 8;
    layer_bits_dictionary['ppw_conv4' ] = 8;

    layer_bits_dictionary['epw_conv5']  = 8;
    layer_bits_dictionary['dw_conv5']   = 8;
    layer_bits_dictionary['ppw_conv5']  = 8;

    layer_bits_dictionary['epw_conv6']  = 8;
    layer_bits_dictionary['dw_conv6']   = 8;
    layer_bits_dictionary['ppw_conv6']  = 8;

    layer_bits_dictionary['epw_conv7']  = 8;
    layer_bits_dictionary['dw_conv7']   = 8;
    layer_bits_dictionary['ppw_conv7']  = 8;

    layer_bits_dictionary['epw_conv8']  = 8;
    layer_bits_dictionary['dw_conv8']   = 8;
    layer_bits_dictionary['ppw_conv8']  = 8;

    layer_bits_dictionary['epw_conv9']  = 8;
    layer_bits_dictionary['dw_conv9']   = 8;
    layer_bits_dictionary['ppw_conv9']  = 8;

    layer_bits_dictionary['epw_conv10'] = 8;
    layer_bits_dictionary['dw_conv10']  = 8;
    layer_bits_dictionary['ppw_conv10'] = 8;

    layer_bits_dictionary['epw_conv11'] = 8;
    layer_bits_dictionary['dw_conv11']  = 8;
    layer_bits_dictionary['ppw_conv11'] = 8;

    layer_bits_dictionary['epw_conv12'] = 8;
    layer_bits_dictionary['dw_conv12']  = 8;
    layer_bits_dictionary['ppw_conv12'] = 8;

    layer_bits_dictionary['epw_conv13'] = 8;
    layer_bits_dictionary['dw_conv13']  = 8;
    layer_bits_dictionary['ppw_conv13'] = 8;

    layer_bits_dictionary['epw_conv14'] = 8;
    layer_bits_dictionary['dw_conv14']  = 8;
    layer_bits_dictionary['ppw_conv14'] = 8;

    layer_bits_dictionary['epw_conv15'] = 8;
    layer_bits_dictionary['dw_conv15']  = 8;
    layer_bits_dictionary['ppw_conv15'] = 8;

    layer_bits_dictionary['epw_conv16'] = 8;
    layer_bits_dictionary['dw_conv16']  = 8;
    layer_bits_dictionary['ppw_conv16'] = 8;

    layer_bits_dictionary['epw_conv17'] = 8;
    layer_bits_dictionary['dw_conv17']  = 8;
    layer_bits_dictionary['ppw_conv17'] = 8;

    layer_bits_dictionary['epw_conv18'] = 8;
    layer_bits_dictionary['dw_conv18']  = 8;
    layer_bits_dictionary['ppw_conv18'] = 8;

    layer_bits_dictionary['head1_dw_classification'] = 8;
    layer_bits_dictionary['head1_pw_classification'] = 8;
    layer_bits_dictionary['head1_dw_regression'] = 8;
    layer_bits_dictionary['head1_pw_regression'] = 8;

    layer_bits_dictionary['head2_dw_classification'] = 8;
    layer_bits_dictionary['head2_pw_classification'] = 8;
    layer_bits_dictionary['head2_dw_regression'] = 8;
    layer_bits_dictionary['head2_pw_regression'] = 8;

    # Convert model to appropriate mode before loading weights
    HW_mode = False
    if mode == 'fpt_unc':
        model.to(device)
        
    elif mode == 'fpt':
        model = conv_model_fptunc2fpt(model)
        model.to(device)
        
    elif mode == 'qat':
        model = conv_model_fptunc2fpt(model)
        model.to(device)
        model = conv_model_fpt2qat(model, layer_bits_dictionary)
        model.to(device)
        
    elif mode == 'hw':
        HW_mode = True
        model = conv_model_fptunc2fpt(model)
        model.to(device)
        model = conv_model_fpt2qat(model, layer_bits_dictionary)
        model.to(device)
        model = conv_model_qat2hw(model)
        model.to(device)

    else:
        raise Exception('Invalid model mode is selected, select from: fpt_unc, fpt, qat, hw')


    weights = torch.load(weights_path, map_location=torch.device('cpu'))
    model.load_state_dict(weights['state_dict'], strict=True)

    model.requires_grad_(False)
    model.eval()
    
    if mode == 'qat' or mode == 'hw':
        print(''*5)
        print('*'*120)
        print('qat or hardware mode is selected, please make sure you configured layer_bits_dictionary in "coco_eval.py" accordingly!!!')
        print('*'*120)
        print('')
        time.sleep(5)

    evaluate_coco(model, DATA_PATH=data_path, JSON_PATH=json_path , nmsIoUTreshold=nms_threshold, 
                  PredMinConfTreshold=conf_threshold, HW_mode = HW_mode)