-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining_pre.py
313 lines (292 loc) · 9.97 KB
/
training_pre.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
# # import cv2 as cv
# # from backend.object_detector import Detector
# # import time as T
# # from dds_utils import (Results, merge_boxes_in_results,Region)
# #
# # import argparse
# # from video_processor import VideoProcessor
# # parser=argparse.ArgumentParser(description="diff_pq")
# # parser.add_argument('--video_path',type=str)
# # parser.add_argument('--qp',type=int)
# # args=parser.parse_args()
# # qp=args.qp
# #
# # video_path=args.video_path
# #
# # result = []
# # model = Detector()
# # times=[]
# #
# # frame_idx=0
# # with VideoProcessor(video_path,0) as video:
# # for frame in video:
# # starttime = T.time()
# # result.append(model.infer(frame))
# # # result.append([])
# # endtime = T.time()
# # times.append(float(endtime - starttime))
# # # 从0帧 开始
# # print("finish", frame_idx)
# # frame_idx+=1
# #
# # print(len(result))
# # final_results=Results()
# # for frame_idx in range(0,3000):
# # # print(self.l_results,self.result[frame_idx])
# # for label, conf, (x, y, w, h) in result[frame_idx]:
# # r = Region(frame_idx, x, y, w, h, conf, label,
# # 0, origin="mpeg")
# # # 从服务器传到client端都origin会重新命名为low-res, 函数get_first_phase_results
# # final_results.append(r)
# # result=merge_boxes_in_results(final_results.regions_dict, 0.3, 0.3)
# #
# #
# # import pickle
# # with open(f"dds_results540_{qp}.txt", "wb") as myprofile:
# # pickle.dump(result, myprofile)
# # # with open(f"time_{qp}_infer.txt","wb") as myprofile:
# # # pickle.dump(times,myprofile)
# #
# #
# #
# #
# #
# # ################################SR之后infer的结果
# # import cv2 as cv
# # from backend.object_detector import Detector
# # import time as T
# # from dds_utils import (Results, merge_boxes_in_results,Region)
# #
# # import argparse
# # from video_processor import VideoProcessor
# # from backend.server import Server
# # # parser=argparse.ArgumentParser(description="diff_pq")
# # # parser.add_argument('--video_path',type=str)
# # # parser.add_argument('--qp',type=int)
# # # args=parser.parse_args()
# # # qp=args.qp
# # #
# # # video_path=args.video_path
# #
# # result = []
# # model = Detector()
# # times=[]
# #
# # frame_idx=0
# #
# #
# # server=Server()
# # result,times_sr,times_infer=server.easy_perform_detection_edsr_on_video(video_path)
# #
# # final_results=Results()
# # for frame_idx in range(0,3000):
# # # print(self.l_results,self.result[frame_idx])
# # for label, conf, (x, y, w, h) in result[frame_idx]:
# # r = Region(frame_idx, x, y, w, h, conf, label,
# # 0, origin="mpeg")
# # # 从服务器传到client端都origin会重新命名为low-res, 函数get_first_phase_results
# # final_results.append(r)
# # result=merge_boxes_in_results(final_results.regions_dict, 0.3, 0.3)
# #
# #
# # import pickle
# # with open(f"dds_results540_{qp}_plusSR.txt", "wb") as myprofile:
# # pickle.dump(result, myprofile)
# # with open(f"time540_{qp}_sr.txt","wb") as myprofile:
# # pickle.dump(times_sr,myprofile)
# # # sr的时间和内容复杂度无关,但是
# # with open(f"time540x2_{qp}_infer.txt", "wb") as myprofile:
# # pickle.dump(times_infer, myprofile)
#
#
# #
# # ######################################################算不同qp
# # #
# import cv2 as cv
# from backend.object_detector import Detector
# import time as T
# from dds_utils import (Results, merge_boxes_in_results,Region)
#
# import argparse
# from video_processor import VideoProcessor
# import torchvision.models as models
# import time
# import torchvision.transforms as transforms
# import torch
# import pickle
# from torch.autograd import Variable
# # parser=argparse.ArgumentParser(description="diff_pq")
# # parser.add_argument('--video_path',type=str)
# # parser.add_argument('--qp',type=int)
# # args=parser.parse_args()
# # qp=args.qp
#
# # video_path=args.video_path
#
# result = []
# model = Detector()
# times=[]
#
# frame_idx=0
# vgg_model_4096 = models.vgg16(pretrained=True)
# new_classifier = torch.nn.Sequential(*list(vgg_model_4096.children())[-1])
# vgg_model_4096.classifier = new_classifier
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# vgg_model_4096.to(device)
# print(device,"device")
#
#
#
# feature_dict={}
# qp_list=[10,15,20,25,30,35,40,45,50]
# for qp in qp_list:
# #!!!!!!!!!!!!!!!!!!!!!!!!1每个新的dp都要更新啊!!!!!!!!!!!!
# feature_list = []
# video_path=f"/home/ubuntu/VideoAnalytics/workplace/DRL_SR_Infer/VA/dataset/video_test540_{qp}/video_test540_{qp}.mp4"
# print(video_path)
# with VideoProcessor(video_path,0) as video:
# for frame in video:
# tensor = transforms.ToTensor()(frame)
# tensor = torch.unsqueeze(tensor, dim=0)
# # tensor.to() 这个函数功能是产生一个新的tensor,并不会改变原数据。所以需要tensor=... 而模型可以直接moedl.to(device)
# tensor = tensor.to(device)
# feature = vgg_model_4096(Variable(tensor))
# feature = feature.detach().cpu().numpy().tolist()
# feature_list.append(feature[0])
# feature_dict[qp]=feature_list
#
#
#
# #
# import pickle
# with open(f"features_dict_long.txt", "wb") as myprofile:
# pickle.dump(feature_dict, myprofile)
#
#
# print("yes")
""" kkkkkkkkkkkkkkkkkkkkk """
from backend.server import Server
from backend.object_detector import Detector
from dds_utils import (Results, merge_boxes_in_results,Region)
from video_processor import VideoProcessor
import torchvision.models as models
import torchvision.transforms as transforms
import torch
import pickle
from torch.autograd import Variable
import time as T
import time
# bitrate_list=[200,300,450,750,1200,1850,2850,4300,6000,8000]
print(time.strftime("%Y-%m-%d %H-%M-%S",time.localtime()))
T_begin=time.time()
bitrate_list=[10910693, 6050811, 3196561, 1627969, 825260, 450706, 273220, 180834]
for bitrate in bitrate_list:
video_path=f"/home/ubuntu/VideoAnalytics/workplace/DRL_SR_Infer/VA/dataset_profile/video_test540_{bitrate}/video_test540_{bitrate}_360p.mp4"
result = []
model = Detector()
times=[]
frame_idx=0
with VideoProcessor(video_path,0) as video:
for frame in video:
starttime = T.time()
result.append(model.infer(frame))
# result.append([])
endtime = T.time()
times.append(float(endtime - starttime))
# 从0帧 开始
print("finish", frame_idx)
frame_idx+=1
print(len(result))
final_results=Results()
for frame_idx in range(0,3000):
# print(self.l_results,self.result[frame_idx])
for label, conf, (x, y, w, h) in result[frame_idx]:
r = Region(frame_idx, x, y, w, h, conf, label,
0, origin="mpeg")
# 从服务器传到client端都origin会重新命名为low-res, 函数get_first_phase_results
final_results.append(r)
result=merge_boxes_in_results(final_results.regions_dict, 0.3, 0.3)
with open(f"/home/ubuntu/VideoAnalytics/workplace/DRL_SR_Infer/VA/dataset_profile/constbit_dds_results540_{bitrate}_360p.txt", "wb") as myprofile:
pickle.dump(result, myprofile)
print(time.time()-T_begin)
# 1161.2982788085938
print("end 540 lr infer")
# for bitrate in bitrate_list:
# video_path = f"/home/ubuntu/VideoAnalytics/workplace/DRL_SR_Infer/VA/dataset_bit/video_test540_{bitrate}/video_test540_{bitrate}.mp4"
# result = []
# model = Detector()
# times=[]
#
#
#
# frame_idx=0
#
#
# server=Server()
# result,times_sr,times_infer=server.easy_perform_detection_edsr_on_video(video_path)
#
# final_results=Results()
# for frame_idx in range(0,3000):
# # print(self.l_results,self.result[frame_idx])
# for label, conf, (x, y, w, h) in result[frame_idx]:
# r = Region(frame_idx, x, y, w, h, conf, label,
# 0, origin="mpeg")
# # 从服务器传到client端都origin会重新命名为low-res, 函数get_first_phase_results
# final_results.append(r)
# result=merge_boxes_in_results(final_results.regions_dict, 0.3, 0.3)
#
#
#
# with open(f"constbit_dds_results540_{bitrate}_plusSR.txt", "wb") as myprofile:
# pickle.dump(result, myprofile)
#
#
#
# print("end SR+infer---------------------------")
#
# #
# # ######################################################算不同bitrate feature
#
#
# result = []
# model = Detector()
# times=[]
#
# frame_idx=0
# vgg_model_4096 = models.vgg16(pretrained=True)
# new_classifier = torch.nn.Sequential(*list(vgg_model_4096.children())[-1])
# vgg_model_4096.classifier = new_classifier
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# vgg_model_4096.to(device)
# print(device,"device")
#
#
#
# feature_dict={}
# bitrate_list=[200,300,450,750,1200,1850,2850,4300,6000,8000]
# for bitrate in bitrate_list:
# #!!!!!!!!!!!!!!!!!!!!!!!!1每个新的dp都要更新啊!!!!!!!!!!!!
# feature_list = []
# video_path=f"/home/ubuntu/VideoAnalytics/workplace/DRL_SR_Infer/VA/dataset_bit/video_test540_{bitrate}/video_test540_{bitrate}.mp4"
# print(video_path)
# with VideoProcessor(video_path,0) as video:
# for frame in video:
# tensor = transforms.ToTensor()(frame)
# tensor = torch.unsqueeze(tensor, dim=0)
# # tensor.to() 这个函数功能是产生一个新的tensor,并不会改变原数据。所以需要tensor=... 而模型可以直接moedl.to(device)
# tensor = tensor.to(device)
# feature = vgg_model_4096(Variable(tensor))
# feature = feature.detach().cpu().numpy().tolist()
# feature_list.append(feature[0])
# feature_dict[bitrate]=feature_list
#
#
#
#
#
#
# with open(f"constbit_features_dict_long.txt", "wb") as myprofile:
# pickle.dump(feature_dict, myprofile)
#
#
# print("end feature---------------------------------")