forked from mfarhadi/TKD-Cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoss_evaluation.py
291 lines (216 loc) · 9.66 KB
/
Loss_evaluation.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
import queue
import threading
import time
import argparse
import time
from sys import platform
import torch
from models import * # set ONNX_EXPORT in models.py
from utils.datasets import *
from utils.utils import *
import argparse
import time
from sys import platform
from models import * # set ONNX_EXPORT in models.py
from utils.datasets import *
from utils.utils import *
from torch.autograd import Variable
import torch.optim as optim
from loss_preparation import TKD_loss
import torch.distributed as dist
import os
import scipy.io as sio
import numpy as np
import threading
global exitFlag
exitFlag=[False]
import os
from classes import *
def Argos(opt):
img_size = (320, 192) if ONNX_EXPORT else opt.img_size # (320, 192) or (416, 256) or (608, 352) for (height, width)
device = torch_utils.select_device(force_cpu=ONNX_EXPORT)
data = opt.data
data_dict = parse_data_cfg(data)
################ STUDENT ##########################
s_weights, half = opt.s_weights, opt.half
# Initialize model
s_model = Darknet(opt.s_cfg, img_size)
s_model.feture_index=[8,12]
# Load weights
if s_weights.endswith('.pt'): # pytorch format
s_model.load_state_dict(torch.load(s_weights, map_location=device)['model'])
else: # darknet format
_ = load_darknet_weights(s_model, s_weights)
# Eval mode
s_model.to(device).eval()
model=s_model
# Half precision
half = half and device.type != 'cpu' # half precision only supported on CUDA
if half:
s_model.half()
TKD_decoder = Darknet('cfg/TKD_decoder.cfg', img_size)
#if s_weights.endswith('.pt'): # pytorch format
TKD_decoder.load_state_dict(torch.load('weights/TKD.pt', map_location=device)['model'])
hyp = {'giou': 1.582, # giou loss gain
'cls': 27.76, # cls loss gain (CE=~1.0, uCE=~20)
'cls_pw': 1.446, # cls BCELoss positive_weight
'obj': 21.35, # obj loss gain (*=80 for uBCE with 80 classes)
'obj_pw': 3.941, # obj BCELoss positive_weight
'iou_t': 0.2635, # iou training threshold
'lr0': 0.002324, # initial learning rate (SGD=1E-3, Adam=9E-5)
'lrf': -4., # final LambdaLR learning rate = lr0 * (10 ** lrf)
'momentum': 0.97, # SGD momentum
'weight_decay': 0.0004569, # optimizer weight decay
'hsv_s': 0.5703, # image HSV-Saturation augmentation (fraction)
'hsv_v': 0.3174, # image HSV-Value augmentation (fraction)
'degrees': 1.113, # image rotation (+/- deg)
'translate': 0.06797, # image translation (+/- fraction)
'scale': 0.1059, # image scale (+/- gain)
'shear': 0.5768} # image shear (+/- deg)
TKD_decoder.hyp = hyp # attach hyperparameters to model
TKD_decoder.nc=int(data_dict['classes'])
TKD_decoder.arc = opt.arc
################ Teacher ##########################
o_weights, half = opt.o_weights, opt.half
# Initialize model
o_model = Darknet(opt.o_cfg, img_size)
# Load weights
if o_weights.endswith('.pt'): # pytorch format
o_model.load_state_dict(torch.load(o_weights, map_location=device)['model'])
else: # darknet format
_ = load_darknet_weights(o_model, o_weights)
# Eval mode
o_model.to(device).eval()
# Half precision
half = half and device.type != 'cpu' # half precision only supported on CUDA
if half:
o_model.half()
################## Oracle for inference ###################
Oracle_model = Darknet(opt.o_cfg, img_size)
# Load weights
if o_weights.endswith('.pt'): # pytorch format
Oracle_model.load_state_dict(torch.load(o_weights, map_location=device)['model'])
else: # darknet format
_ = load_darknet_weights(Oracle_model, o_weights)
# Eval mode
Oracle_model.to(device).eval()
# Half precision
half = half and device.type != 'cpu' # half precision only supported on CUDA
if half:
Oracle_model.half()
threadList = opt.source
threads = []
threadID = 1
students=[]
info=student(threadID,TKD_decoder,o_model,opt.source,opt,dist,device)
# Configure run
nc = 9 # number of classes
seen = 0
model.eval()
coco91class = coco80_to_coco91_class()
s = ('%20s' + '%10s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP', 'F1')
p, r, f1, mp, mr, map, mf1 = 0., 0., 0., 0., 0., 0., 0.
jdict, stats, ap, ap_class = [], [], [], []
iou_thres = 0.5
for source in info.source:
webcam = source == '0' or source.startswith('rtsp') or source.startswith('http')
streams = source == 'streams.txt'
model.eval()
info.TKD.eval().cuda()
# Set Dataloader
if streams:
torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=info.opt.img_size, half=info.opt.half)
elif webcam:
stream_img = True
dataset = LoadWebcam(source, img_size=info.opt.img_size, half=info.opt.half)
else:
save_img = True
dataset = LoadImages(source, img_size=info.opt.img_size, half=info.opt.half)
# Run inference
info.frame = torch.zeros([1, 3, info.opt.img_size, info.opt.img_size])
oracle_T = Oracle()
info.oracle.train().cuda()
counter=0
confidence=0.001
records=np.zeros((1000,2))
for path, img, im0s, vid_cap in dataset:
info.collecting = True
# Get detections
counter+=1
info.frame[0, :, 0:img.shape[1], :] = torch.from_numpy(img)
info.frame = info.frame.cuda()
pred, _, feature = model(info.frame)
info.TKD.img_size = info.frame.shape[-2:]
pred_TKD, p = info.TKD(feature)
Oracle_model.train()
T_out = Oracle_model(info.frame)
t1=time.time()
richOutput = [Variable(T_out[0].data, requires_grad=False), Variable(T_out[1].data, requires_grad=False)]
loss=0
for i in range(2):
loss += TKD_loss(p[i], richOutput[i], info.loss)
t2=time.time()
info.TKD.train()
pred= info.TKD(feature)
Oracle_model.eval()
labels,_=Oracle_model(info.frame)
t3=time.time()
labels = non_max_suppression(labels, confidence, 0.5)
labels=labels[0]
if labels is not None:
labels = labels[:, [4, 6, 0, 1, 2, 3]].round()
labels[:, 2:] = xyxy2xywh(labels[:, 2:])
labels[:, 2:] = labels[:, 2:] / 416
labels[:, 0] = labels[:, 0] * 0
if labels is not None:
loss, loss_items = compute_loss(pred, labels, info.TKD)
t4=time.time()
print(labels.shape[0],t2-t1,t4-t3)
records[labels.shape[0],:]=[t2-t1,t4-t3]
if counter%100==0:
if confidence<0.2:
confidence*=2
elif confidence<0.9:
confidence+=0.1
if labels.shape[0]==1:
break
info.TKD.eval()
file = open('loss_time'+'.txt', 'a')
for i in range(500):
if records[i,0]!=0:
file.write('\n'+str(i)+','+str(records[i,0]*1000)+','+str(records[i,1]*1000))
file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--s-cfg', type=str, default='cfg/yolov3-tiny.cfg', help='cfg file path')
parser.add_argument('--o-cfg', type=str, default='cfg/yolov3.cfg', help='cfg file path')
parser.add_argument('--data', type=str, default='data/coco.data', help='coco.data file path')
parser.add_argument('--s-weights', type=str, default='weights/yolov3-tiny.weights', help='path to weights file')
parser.add_argument('--o-weights', type=str, default='weights/yolov3.weights', help='path to weights file')
parser.add_argument('--source', type=str, default=['/media/common/DATAPART1/datasets/UCF_Crimes/Videos/Training_Normal_Videos_Anomaly/Normal_Videos425_x264.mp4'], help='source') # input file/folder, 0 for webcam
parser.add_argument('--output', type=str, default='output', help='output folder') # output folder
parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
parser.add_argument('--conf-thres', type=float, default=0.1, help='object confidence threshold')
parser.add_argument('--nms-thres', type=float, default=0.3, help='iou threshold for non-maximum suppression')
parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)')
parser.add_argument('--arc', type=str, default='defaultpw', help='yolo architecture') # defaultpw, uCE, uBCE
parser.add_argument('--half', action='store_true', help='half precision FP16 inference')
parser.add_argument("--backend", type=str, default='gloo',
help="Backend")
parser.add_argument('-s', "--send", action='store_true',
help="Send tensor (if not specified, will receive tensor)")
parser.add_argument("--master_addr", type=str,default='10.218.110.18',
help="IP address of master")
parser.add_argument("--use_helper_threads", action='store_true',
help="Use multiple threads")
parser.add_argument("--rank", type=int, default=1,
help="Rank of current worker")
parser.add_argument('-p', "--master_port", default=12345,
help="Port used to communicate tensors")
parser.add_argument("--intra_server_broadcast", action='store_true',
help="Broadcast within a server")
opt = parser.parse_args()
print(opt)
with torch.no_grad():
Argos(opt)