1
+ import torch
2
+ import time
3
+ import numpy as np
4
+ from model .model import parsingNet
5
+ import torchvision .transforms as transforms
6
+ import cv2
7
+ from matplotlib import pyplot as plt
8
+ from PIL import Image
9
+
10
+
11
+ img_transforms = transforms .Compose ([
12
+ transforms .Resize ((288 , 800 )),
13
+ transforms .ToTensor (),
14
+ transforms .Normalize ((0.485 , 0.456 , 0.406 ), (0.229 , 0.224 , 0.225 )),
15
+ ])
16
+
17
+ def resize (x , y ):
18
+ global cap
19
+ cap .set (3 ,x )
20
+ cap .set (4 ,y )
21
+
22
+ def test_practical_without_readtime ():
23
+ global cap
24
+ for i in range (10 ):
25
+ _ ,img = cap .read ()
26
+ img = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
27
+ img2 = Image .fromarray (img )
28
+ x = img_transforms (img2 )
29
+ x = x .unsqueeze (0 ).cuda ()+ 1
30
+ y = net (x )
31
+
32
+ print ("pracrical image input size:" ,img .shape )
33
+ print ("pracrical tensor input size:" ,x .shape )
34
+ t_all = []
35
+ for i in range (100 ):
36
+ _ ,img = cap .read ()
37
+
38
+ img = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
39
+ img2 = Image .fromarray (img )
40
+ x = img_transforms (img2 )
41
+ x = x .unsqueeze (0 ).cuda ()+ 1
42
+
43
+ t1 = time .time ()
44
+ y = net (x )
45
+ t2 = time .time ()
46
+ t_all .append (t2 - t1 )
47
+
48
+ print ("practical with out read time:" )
49
+ print ('\t average time:' , np .mean (t_all ) / 1 )
50
+ print ('\t average fps:' ,1 / np .mean (t_all ))
51
+
52
+ # print('fastest time:', min(t_all) / 1)
53
+ # print('fastest fps:',1 / min(t_all))
54
+
55
+ # print('slowest time:', max(t_all) / 1)
56
+ # print('slowest fps:',1 / max(t_all))
57
+
58
+
59
+ def test_practical ():
60
+ global cap
61
+ for i in range (10 ):
62
+ _ ,img = cap .read ()
63
+ img = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
64
+ img2 = Image .fromarray (img )
65
+ x = img_transforms (img2 )
66
+ x = x .unsqueeze (0 ).cuda ()+ 1
67
+ y = net (x )
68
+
69
+ print ("pracrical image input size:" ,img .shape )
70
+ print ("pracrical tensor input size:" ,x .shape )
71
+ t_all = []
72
+ t_capture = []
73
+ t_preprocessing = []
74
+ t_net = []
75
+ for i in range (100 ):
76
+ t1 = time .time ()
77
+
78
+ t3 = time .time ()
79
+ _ ,img = cap .read ()
80
+ t4 = time .time ()
81
+
82
+ t5 = time .time ()
83
+ img = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
84
+ img2 = Image .fromarray (img )
85
+ x = img_transforms (img2 )
86
+ x = x .unsqueeze (0 ).cuda ()+ 1
87
+ t6 = time .time ()
88
+
89
+ y = net (x )
90
+ t2 = time .time ()
91
+ t_all .append (t2 - t1 )
92
+ t_capture .append (t4 - t3 )
93
+ t_preprocessing .append (t6 - t5 )
94
+ t_net .append (t2 - t6 )
95
+
96
+ print ("practical with read time:" )
97
+ print ('\t average time:' , np .mean (t_all ) / 1 )
98
+ print ('\t average fps:' ,1 / np .mean (t_all ))
99
+ print ('\t capture time:' , np .mean (t_capture ) / 1 )
100
+ print ('\t pre-processing time:' , np .mean (t_preprocessing ) / 1 )
101
+ print ('\t detect time:' , np .mean (t_net ) / 1 )
102
+
103
+ # print('fastest time:', min(t_all) / 1)
104
+ # print('fastest fps:',1 / min(t_all))
105
+
106
+ # print('slowest time:', max(t_all) / 1)
107
+ # print('slowest fps:',1 / max(t_all))
108
+
109
+ ###x = torch.zeros((1,3,288,800)).cuda() + 1
110
+ def test_theoretical ():
111
+ x = torch .zeros ((1 ,3 ,288 ,800 )).cuda () + 1
112
+ for i in range (10 ):
113
+ y = net (x )
114
+
115
+ t_all = []
116
+ for i in range (100 ):
117
+ t1 = time .time ()
118
+ y = net (x )
119
+ t2 = time .time ()
120
+ t_all .append (t2 - t1 )
121
+ print ("theortical" )
122
+ print ('\t average time:' , np .mean (t_all ) / 1 )
123
+ print ('\t average fps:' ,1 / np .mean (t_all ))
124
+
125
+ # print('fastest time:', min(t_all) / 1)
126
+ # print('fastest fps:',1 / min(t_all))
127
+
128
+ # print('slowest time:', max(t_all) / 1)
129
+ # print('slowest fps:',1 / max(t_all))
130
+
131
+
132
+
133
+
134
+ if __name__ == "__main__" :
135
+ ###captrue data from camera or video
136
+ #cap = cv2.VideoCapture("video.mp4") #uncommen to activate a video input
137
+ cap = cv2 .VideoCapture (0 ) #uncommen to activate a camera imput
138
+ #resize(480, 640) #ucommen to change input size
139
+
140
+
141
+ # torch.backends.cudnn.deterministic = False
142
+ torch .backends .cudnn .benchmark = True
143
+ net = parsingNet (pretrained = False , backbone = '18' ,cls_dim = (100 + 1 ,56 ,4 ),use_aux = False ).cuda ()
144
+ # net = parsingNet(pretrained = False, backbone='18',cls_dim = (200+1,18,4),use_aux=False).cuda()
145
+ net .eval ()
146
+
147
+
148
+ test_practical_without_readtime ()
149
+ test_practical ()
150
+ cap .release ()
151
+ test_theoretical ()
0 commit comments