@@ -33,21 +33,22 @@ class TSMINFReader(DataReader):
33
33
"""
34
34
Data reader for video dataset of jpg folder.
35
35
"""
36
-
37
36
def __init__ (self , name , mode , cfg , material = None ):
38
37
super (TSMINFReader , self ).__init__ (name , mode , cfg )
39
38
name = name .upper ()
40
- self .seg_num = cfg [name ]['seg_num ' ]
41
- self .seglen = cfg [name ]['seglen' ]
42
- self .short_size = cfg [name ]['short_size' ]
43
- self .target_size = cfg [name ]['target_size' ]
44
- self .batch_size = cfg [name ]['batch_size' ]
39
+ self .num_seg = cfg [name ]['num_seg ' ]
40
+ self .seglen = cfg [name ]['seglen' ]
41
+ self .short_size = cfg [name ]['short_size' ]
42
+ self .target_size = cfg [name ]['target_size' ]
43
+ self .batch_size = cfg [name ]['batch_size' ]
45
44
self .reader_threads = cfg [name ]['reader_threads' ]
46
- self .buf_size = cfg [name ]['buf_size' ]
47
- self .video_path = cfg [name ]['frame_list' ]
45
+ self .buf_size = cfg [name ]['buf_size' ]
46
+ self .video_path = cfg [name ]['frame_list' ]
48
47
49
- self .img_mean = np .array (cfg [name ]['image_mean' ]).reshape ([3 , 1 , 1 ]).astype (np .float32 )
50
- self .img_std = np .array (cfg [name ]['image_std' ]).reshape ([3 , 1 , 1 ]).astype (np .float32 )
48
+ self .img_mean = np .array (cfg [name ]['image_mean' ]).reshape (
49
+ [3 , 1 , 1 ]).astype (np .float32 )
50
+ self .img_std = np .array (cfg [name ]['image_std' ]).reshape (
51
+ [3 , 1 , 1 ]).astype (np .float32 )
51
52
52
53
self .material = material
53
54
@@ -56,16 +57,16 @@ def create_reader(self):
56
57
batch loader for TSN
57
58
"""
58
59
_reader = self ._inference_reader_creator_longvideo (
59
- self .video_path ,
60
- self .mode ,
61
- seg_num = self .seg_num ,
62
- seglen = self .seglen ,
63
- short_size = self .short_size ,
64
- target_size = self .target_size ,
65
- img_mean = self .img_mean ,
66
- img_std = self .img_std ,
67
- num_threads = self .reader_threads ,
68
- buf_size = self .buf_size )
60
+ self .video_path ,
61
+ self .mode ,
62
+ num_seg = self .num_seg ,
63
+ seglen = self .seglen ,
64
+ short_size = self .short_size ,
65
+ target_size = self .target_size ,
66
+ img_mean = self .img_mean ,
67
+ img_std = self .img_std ,
68
+ num_threads = self .reader_threads ,
69
+ buf_size = self .buf_size )
69
70
70
71
def _batch_reader ():
71
72
batch_out = []
@@ -81,9 +82,10 @@ def _batch_reader():
81
82
82
83
return _batch_reader
83
84
84
-
85
- def _inference_reader_creator_longvideo (self , video_path , mode , seg_num , seglen ,
86
- short_size , target_size , img_mean , img_std , num_threads , buf_size ):
85
+ def _inference_reader_creator_longvideo (self , video_path , mode , num_seg ,
86
+ seglen , short_size , target_size ,
87
+ img_mean , img_std , num_threads ,
88
+ buf_size ):
87
89
"""
88
90
inference reader for video
89
91
"""
@@ -94,7 +96,7 @@ def reader():
94
96
def image_buf (image_id_path_buf ):
95
97
"""
96
98
image_buf reader
97
- """
99
+ """
98
100
try :
99
101
img_path = image_id_path_buf [1 ]
100
102
img = Image .open (img_path ).convert ("RGB" )
@@ -103,61 +105,68 @@ def image_buf(image_id_path_buf):
103
105
image_id_path_buf [2 ] = None
104
106
105
107
frame_len = len (video_path )
106
- read_thread_num = seg_num
108
+ read_thread_num = num_seg
107
109
for i in range (0 , frame_len , read_thread_num ):
108
- image_list_part = video_path [i : i + read_thread_num ]
110
+ image_list_part = video_path [i :i + read_thread_num ]
109
111
image_id_path_buf_list = []
110
112
for k in range (len (image_list_part )):
111
113
image_id_path_buf_list .append ([k , image_list_part [k ], None ])
112
114
113
-
114
- with concurrent .futures .ThreadPoolExecutor (max_workers = read_thread_num ) as executor :
115
- executor .map (lambda image_id_path_buf : image_buf (image_id_path_buf ), image_id_path_buf_list )
115
+ with concurrent .futures .ThreadPoolExecutor (
116
+ max_workers = read_thread_num ) as executor :
117
+ executor .map (
118
+ lambda image_id_path_buf : image_buf (image_id_path_buf ),
119
+ image_id_path_buf_list )
116
120
imgs_seg_list = [x [2 ] for x in image_id_path_buf_list ]
117
-
121
+
118
122
# add the fault-tolerant for bad image
119
123
for k in range (len (image_id_path_buf_list )):
120
124
img_buf = image_id_path_buf_list [k ][2 ]
121
125
pad_id = 1
122
- while pad_id < seg_num and img_buf is None :
123
- img_buf = imgs_seg_list [(k + pad_id )% seg_num ][2 ]
126
+ while pad_id < num_seg and img_buf is None :
127
+ img_buf = imgs_seg_list [(k + pad_id ) % num_seg ][2 ]
124
128
if img_buf is None :
125
- logger .info ("read img erro from {} to {}" .format (i , i + read_thread_num ))
129
+ print ("read img erro from {} to {}" .format (
130
+ i , i + read_thread_num ))
126
131
exit (0 )
127
132
else :
128
133
imgs_seg_list [k ] = img_buf
129
- for pad_id in range (len (imgs_seg_list ), seg_num ):
134
+ for pad_id in range (len (imgs_seg_list ), num_seg ):
130
135
imgs_seg_list .append (imgs_seg_list [- 1 ])
131
- yield imgs_seg_list
136
+ yield imgs_seg_list
132
137
133
138
134
- def inference_imgs_transform (imgs_list , mode , seg_num , seglen , short_size ,\
139
+ def inference_imgs_transform (imgs_list , mode , num_seg , seglen , short_size ,\
135
140
target_size , img_mean , img_std ):
136
141
"""
137
142
inference_imgs_transform
138
- """
139
- imgs_ret = imgs_transform (imgs_list , mode , seg_num , seglen , short_size ,
140
- target_size , img_mean , img_std )
143
+ """
144
+ imgs_ret = imgs_transform (imgs_list , mode , num_seg , seglen ,
145
+ short_size , target_size , img_mean ,
146
+ img_std )
141
147
label_ret = 0
142
148
143
149
return imgs_ret , label_ret
144
150
145
- mapper = functools .partial (
146
- inference_imgs_transform ,
147
- mode = mode ,
148
- seg_num = seg_num ,
149
- seglen = seglen ,
150
- short_size = short_size ,
151
- target_size = target_size ,
152
- img_mean = img_mean ,
153
- img_std = img_std )
151
+ mapper = functools .partial (inference_imgs_transform ,
152
+ mode = mode ,
153
+ num_seg = num_seg ,
154
+ seglen = seglen ,
155
+ short_size = short_size ,
156
+ target_size = target_size ,
157
+ img_mean = img_mean ,
158
+ img_std = img_std )
154
159
155
- return paddle .reader .xmap_readers (mapper , reader , num_threads , buf_size , order = True )
160
+ return paddle .reader .xmap_readers (mapper ,
161
+ reader ,
162
+ num_threads ,
163
+ buf_size ,
164
+ order = True )
156
165
157
166
158
167
def imgs_transform (imgs ,
159
168
mode ,
160
- seg_num ,
169
+ num_seg ,
161
170
seglen ,
162
171
short_size ,
163
172
target_size ,
@@ -186,7 +195,7 @@ def imgs_transform(imgs,
186
195
imgs = np_imgs
187
196
imgs -= img_mean
188
197
imgs /= img_std
189
- imgs = np .reshape (imgs , (seg_num , seglen * 3 , target_size , target_size ))
198
+ imgs = np .reshape (imgs , (num_seg , seglen * 3 , target_size , target_size ))
190
199
191
200
return imgs
192
201
@@ -260,10 +269,10 @@ def _sample_crop_size(im_size):
260
269
'crop_h' : crop_pair [1 ],
261
270
'offset_w' : w_offset ,
262
271
'offset_h' : h_offset
263
- }
264
-
272
+ }
273
+
265
274
return crop_info
266
-
275
+
267
276
crop_info = _sample_crop_size (im_size )
268
277
crop_w = crop_info ['crop_w' ]
269
278
crop_h = crop_info ['crop_h' ]
@@ -355,4 +364,3 @@ def group_scale(imgs, target_size):
355
364
resized_imgs .append (img .resize ((ow , oh ), Image .BILINEAR ))
356
365
357
366
return resized_imgs
358
-
0 commit comments