質問編集履歴

2

URLを追加しました

2020/04/13 05:54

投稿

ikayakioishii
ikayakioishii

スコア8

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  Githubに公開されているPeeking into the futureの論文のコード(next-prediction)を実行してみたところ,AssertErrorが発生し,エラー文に何も出ておらず,解決への糸口が見つからないため,手助けいただきたいです.
6
6
 
7
+ Github 再現実験手順URL:https://github.com/JunweiLiang/next-prediction/blob/master/code/prepare_data/README.md
8
+
9
+ 対象のコードURL:https://github.com/JunweiLiang/next-prediction/blob/master/code/prepare_data/step4_generate_traj.py
10
+
7
11
 
8
12
 
9
13
  ### 発生している問題・エラーメッセージ

1

コードが違っていたので,直しました.

2020/04/13 05:54

投稿

ikayakioishii
ikayakioishii

スコア8

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
 
42
42
 
43
- """Given a list of images, run scene semantic segmentation using deeplab."""
43
+ """Generate trajectory files and scene, person box, other box, activity files."""
44
44
 
45
45
 
46
46
 
@@ -48,17 +48,25 @@
48
48
 
49
49
  # pylint: disable=g-bad-import-order
50
50
 
51
+ import argparse
52
+
51
53
  import os
52
54
 
53
- import argparse
55
+ import operator
54
-
56
+
55
- import tensorflow as tf
57
+ import numpy as np
56
-
58
+
57
- from PIL import Image
59
+ import cPickle as pickle
58
60
 
59
61
  from tqdm import tqdm
60
62
 
61
- import numpy as np
63
+ from glob import glob
64
+
65
+ from utils import activity2id
66
+
67
+ from utils import actev_scene2imgsize
68
+
69
+ from utils import get_scene
62
70
 
63
71
 
64
72
 
@@ -66,73 +74,251 @@
66
74
 
67
75
  parser = argparse.ArgumentParser()
68
76
 
69
- parser.add_argument("imglst")
77
+ parser.add_argument("npzpath")
70
-
78
+
71
- parser.add_argument("model_path", help="path to the model .pb file")
79
+ parser.add_argument("split_path")
72
80
 
73
81
  parser.add_argument("out_path")
74
82
 
75
- parser.add_argument("--every", type=int, default=1,
76
-
77
- help="scene semantic segmentation doesn't have to be"
78
-
79
- " run on every frame")
80
-
81
- parser.add_argument("--down_rate", default=8.0, type=float,
82
-
83
- help="down-size how many times")
84
-
85
- parser.add_argument("--keep_full", action="store_true",
86
-
87
- help="get 512x288 feature")
88
-
89
-
90
-
91
- # ---- gpu stuff. Now only one gpu is used
92
-
93
- parser.add_argument("--gpuid", default=0, type=int)
94
-
95
-
96
-
97
- # For running parallel jobs, set --job 4 --curJob k, where k=1/2/3/4
98
-
99
- parser.add_argument("--job", type=int, default=1, help="total job")
100
-
101
- parser.add_argument("--curJob", type=int, default=1,
102
-
103
- help="this script run job Num")
104
-
105
-
106
-
107
- # ade20k -> 150 + 1 (bg) classes
108
-
109
- # city -> 18 + 1
110
-
111
-
112
-
113
-
114
-
115
- def resize_seg_map(seg, down_rate, keep_full=False):
116
-
117
- img_ = Image.fromarray(seg.astype(dtype=np.uint8))
118
-
119
- w_, h_ = img_.size
120
-
121
- neww, newh = int(w_ / down_rate), int(h_ / down_rate)
122
-
123
- if keep_full:
124
-
125
- neww, newh = 512, 288
126
-
127
-
128
-
129
- newimg = img_.resize((neww, newh)) # neareast neighbor
130
-
131
-
132
-
133
- newdata = np.array(newimg)
134
-
135
- return newdata
83
+ parser.add_argument("--drop_frame", default=1, type=int,
84
+
85
+ help="drop frame to match different fps, assuming "
86
+
87
+ "the virat fps is 30fps, so to get 2.5fps, "
88
+
89
+ "need to drop 12 frames every time")
90
+
91
+
92
+
93
+ parser.add_argument("--scene_feat_path",
94
+
95
+ help="the scene segmentation output path,"
96
+
97
+ "under it should be frame_name.npy")
98
+
99
+
100
+
101
+ # the following are the output paths
102
+
103
+ parser.add_argument("--scene_map_path",
104
+
105
+ help="frameidx mapping to actual scene feature file output")
106
+
107
+
108
+
109
+ parser.add_argument("--person_box_path",
110
+
111
+ help="Person box output")
112
+
113
+
114
+
115
+ parser.add_argument("--other_box_path",
116
+
117
+ help="Other object box output")
118
+
119
+
120
+
121
+ parser.add_argument("--activity_path",
122
+
123
+ help="activity annotation output")
124
+
125
+
126
+
127
+ # for ETH/UCY you need to write your own video size mapping
128
+
129
+ # In the PeekingFuture paper we resize ETH/UCY to 720x576 to extract features
130
+
131
+ scene2imgsize = actev_scene2imgsize
132
+
133
+
134
+
135
+ actid2name = {activity2id[n]: n for n in activity2id}
136
+
137
+
138
+
139
+
140
+
141
+ def resize_xy(xy, vname, resize_w, resize_h):
142
+
143
+ """Resize the xy coordinates."""
144
+
145
+ x_, y_ = xy
146
+
147
+ w, h = scene2imgsize[get_scene(vname)]
148
+
149
+ diff_w = resize_w / float(w)
150
+
151
+ diff_h = resize_h / float(h)
152
+
153
+ x_ *= diff_w
154
+
155
+ y_ *= diff_h
156
+
157
+
158
+
159
+ # normalize coordinates?
160
+
161
+ return [x_, y_]
162
+
163
+
164
+
165
+
166
+
167
+ def resize_box(box, vname, resize_w, resize_h):
168
+
169
+ """Resize the box coordintates."""
170
+
171
+ x1, y1, x2, y2 = [float(o) for o in box]
172
+
173
+
174
+
175
+ w, h = scene2imgsize[get_scene(vname)]
176
+
177
+ diff_w = resize_w / float(w)
178
+
179
+ diff_h = resize_h / float(h)
180
+
181
+
182
+
183
+ x1 *= diff_w
184
+
185
+ x2 *= diff_w
186
+
187
+ y1 *= diff_h
188
+
189
+ y2 *= diff_h
190
+
191
+ return [x1, y1, x2, y2]
192
+
193
+
194
+
195
+
196
+
197
+ # frame_lst is [(videoname,frameidx)], assume sorted by the frameidx
198
+
199
+ def get_nearest(frame_lst_, frame_idx):
200
+
201
+ """Since we don't run scene seg on every frame, we want to find the nearest one."""
202
+
203
+ frame_idxs = np.array([i_ for _, i_ in frame_lst_])
204
+
205
+ cloests_idx = (np.abs(frame_idxs - frame_idx)).argmin()
206
+
207
+ vname, closest_frame_idx = frame_lst_[cloests_idx]
208
+
209
+ return vname, closest_frame_idx, cloests_idx
210
+
211
+
212
+
213
+
214
+
215
+ def get_act_list(act_data, frameidx, bgid):
216
+
217
+ """Given a frameidx, get this person' activities."""
218
+
219
+
220
+
221
+ # act_data is a list of sorted (start,end,actclassid)
222
+
223
+ # return current act list,
224
+
225
+ current_act_list = [(actid, e - frameidx) for s, e, actid in act_data
226
+
227
+ if (frameidx >= s) and (frameidx <= e)]
228
+
229
+ current_act_list.sort(key=operator.itemgetter(1)) # dist to current act's end
230
+
231
+ current_actid_list_ = [actid for actid, _ in current_act_list]
232
+
233
+ current_dist_list_ = [dist for _, dist in current_act_list]
234
+
235
+
236
+
237
+ if not current_act_list:
238
+
239
+ current_actid_list_, current_dist_list_ = [bgid], [-1]
240
+
241
+
242
+
243
+ future_act_list = [(actid, s - frameidx) for s, e, actid in act_data
244
+
245
+ if frameidx < s]
246
+
247
+ future_act_list.sort(key=operator.itemgetter(1))
248
+
249
+
250
+
251
+ if not future_act_list:
252
+
253
+ return (current_actid_list_, current_dist_list_, [bgid], [-1])
254
+
255
+
256
+
257
+ # only the nearest future activity?
258
+
259
+ # smallest_dist = future_act_list[0][1]
260
+
261
+ # future_act_list = [(actid,dist) for actid, dist in future_act_list
262
+
263
+ # if dist == smallest_dist]
264
+
265
+
266
+
267
+ future_actid_list_ = [actid for actid, _ in future_act_list]
268
+
269
+ future_dist_list_ = [dist for _, dist in future_act_list]
270
+
271
+
272
+
273
+ return (current_actid_list_, current_dist_list_,
274
+
275
+ future_actid_list_, future_dist_list_)
276
+
277
+
278
+
279
+
280
+
281
+ def check_traj(newdata_, vname):
282
+
283
+ """Check and filter data."""
284
+
285
+ checkdata = np.array(newdata_, dtype="float")
286
+
287
+ frames_ = np.unique(checkdata[:, 0]).tolist()
288
+
289
+ checked_data_ = []
290
+
291
+ for frame_ in frames_:
292
+
293
+ # all personid in this frame
294
+
295
+ this_frame_data = checkdata[frame_ == checkdata[:, 0], :] # [K,4]
296
+
297
+ ped_ids = this_frame_data[:, 1]
298
+
299
+ unique_ped_ids, unique_idxs = np.unique(ped_ids, return_index=True)
300
+
301
+ if len(ped_ids) != len(unique_ped_ids):
302
+
303
+ tqdm.write("\twarning, %s frame %s has duplicate person annotation person"
304
+
305
+ " ids: %s/%s, removed the duplicate ones"
306
+
307
+ % (vname, frame_, len(unique_ped_ids), len(ped_ids)))
308
+
309
+
310
+
311
+ this_frame_data = this_frame_data[unique_idxs]
312
+
313
+
314
+
315
+ for f_, p_, x_, y_ in this_frame_data:
316
+
317
+ checked_data_.append((f_, p_, x_, y_))
318
+
319
+ checked_data_.sort(key=operator.itemgetter(0))
320
+
321
+ return checked_data_
136
322
 
137
323
 
138
324
 
@@ -144,133 +330,121 @@
144
330
 
145
331
 
146
332
 
147
- input_size = 513 # the model's input size, has to be this
148
-
149
-
150
-
151
- # load the model graph
152
-
153
- print("loading model...")
154
-
155
- graph = tf.Graph()
156
-
157
- with graph.as_default():
158
-
159
- gd = tf.GraphDef()
160
-
161
- with tf.gfile.GFile(args.model_path, "rb") as f:
162
-
163
- sg = f.read()
164
-
165
- gd.ParseFromString(sg)
166
-
167
- tf.import_graph_def(gd, name="")
168
-
169
-
170
-
171
- input_tensor = graph.get_tensor_by_name("ImageTensor:0")
172
-
173
- output_tensor = graph.get_tensor_by_name("SemanticPredictions:0")
174
-
175
-
176
-
177
- print("loaded.")
178
-
179
-
180
-
181
- if not os.path.exists(args.out_path):
182
-
183
- os.makedirs(args.out_path)
184
-
185
-
186
-
187
- imgs = [one.strip()
188
-
189
- for one in open(args.imglst, "r").readlines()][::args.every]
190
-
191
-
192
-
193
- tfconfig = tf.ConfigProto()
194
-
195
- tfconfig.gpu_options.allow_growth = True
196
-
197
- tfconfig.gpu_options.visible_device_list = "%s" % (
198
-
199
- ",".join(["%s" % i for i in [args.gpuid]]))
200
-
201
-
202
-
203
- with graph.as_default():
204
-
205
- with tf.Session(graph=graph, config=tfconfig) as sess:
206
-
207
- count = 0
208
-
209
- for img in tqdm(imgs):
210
-
211
- count += 1
212
-
213
- if (count % args.job) != (args.curJob - 1):
214
-
215
- continue
216
-
217
- imgname = os.path.splitext(os.path.basename(img))[0]
218
-
219
- ori_img = Image.open(img)
220
-
221
-
222
-
223
- w, h = ori_img.size
224
-
225
- resize_r = 1.0 * input_size / max(w, h)
226
-
227
- target_size = (int(resize_r * w), int(resize_r * h))
228
-
229
- resize_img = ori_img.convert("RGB").resize(target_size, Image.ANTIALIAS)
230
-
231
-
232
-
233
- seg_map, = sess.run([output_tensor],
234
-
235
- feed_dict={input_tensor: [np.asarray(resize_img)]})
236
-
237
- seg_map = seg_map[0] # single image input test
238
-
239
-
240
-
241
- # print seg_map.shape
242
-
243
- # print seg_map
244
-
245
- """
246
-
247
- (288, 513)
248
-
249
- [[ 8 8 8 ... 8 8 8]
250
-
251
- [ 8 8 8 ... 8 8 8]
252
-
253
- [ 8 8 8 ... 8 8 8]
254
-
255
- ...
256
-
257
- [11 11 11 ... 11 11 11]
258
-
259
- [11 11 11 ... 11 11 11]
260
-
261
- [11 11 11 ... 11 11 11]]
262
-
263
- """
264
-
265
-
266
-
267
- seg_map = resize_seg_map(seg_map, args.down_rate, args.keep_full)
268
-
269
- targetfile = os.path.join(args.out_path, "%s.npy" % imgname)
270
-
271
- np.save(targetfile, seg_map)
272
-
273
-
333
+ # Hard coded for ActEV experiment.
334
+
335
+ # :P
336
+
337
+ args.resize = True
338
+
339
+ args.resize_h = 1080
340
+
341
+ args.resize_w = 1920
342
+
343
+
344
+
345
+ filelst = {
346
+
347
+ "train": [os.path.splitext(os.path.basename(line.strip()))[0]
348
+
349
+ for line in open(os.path.join(args.split_path,
350
+
351
+ "train.lst"), "r").readlines()],
352
+
353
+ "val": [os.path.splitext(os.path.basename(line.strip()))[0]
354
+
355
+ for line in open(os.path.join(args.split_path,
356
+
357
+ "val.lst"), "r").readlines()],
358
+
359
+ "test": [os.path.splitext(os.path.basename(line.strip()))[0]
360
+
361
+ for line in open(os.path.join(args.split_path,
362
+
363
+ "test.lst"), "r").readlines()],
364
+
365
+ }
366
+
367
+
368
+
369
+ for split in tqdm(filelst, ascii=True):
370
+
371
+ out_path = os.path.join(args.out_path, split)
372
+
373
+
374
+
375
+ if not os.path.exists(out_path):
376
+
377
+ os.makedirs(out_path)
378
+
379
+
380
+
381
+ if not os.path.exists(os.path.join(args.person_box_path, split)):
382
+
383
+ os.makedirs(os.path.join(args.person_box_path, split))
384
+
385
+
386
+
387
+ if not os.path.exists(os.path.join(args.other_box_path, split)):
388
+
389
+ os.makedirs(os.path.join(args.other_box_path, split))
390
+
391
+
392
+
393
+ if not os.path.exists(os.path.join(args.activity_path, split)):
394
+
395
+ os.makedirs(os.path.join(args.activity_path, split))
396
+
397
+
398
+
399
+ scene_map_path = os.path.join(args.scene_map_path, split)
400
+
401
+ if not os.path.exists(scene_map_path):
402
+
403
+ os.makedirs(scene_map_path)
404
+
405
+
406
+
407
+ for videoname in tqdm(filelst[split]):
408
+
409
+ npzfile = os.path.join(args.npzpath, "%s.npz" % videoname)
410
+
411
+
412
+
413
+ data = np.load(npzfile, allow_pickle=True)
414
+
415
+
416
+
417
+ # each frame's all boxes, for getting other boxes
418
+
419
+ frameidx2boxes = data["frameidx2boxes"]
420
+
421
+
422
+
423
+ # personId -> all related activity with timespan, sorted by timespan start
424
+
425
+ # (start, end, act_classid)
426
+
427
+ personid2acts = data["personid2acts"]
428
+
429
+
430
+
431
+ # load all the frames for this video first
432
+
433
+ frame_lst = glob(os.path.join(args.scene_feat_path,
434
+
435
+ "%s_F_*.npy"%videoname))
436
+
437
+ assert frame_lst
438
+
439
+ frame_lst = [(os.path.basename(frame),
440
+
441
+ int(os.path.basename(frame).split(".")[0].split("_F_")[-1]))
442
+
443
+ for frame in frame_lst]
444
+
445
+
446
+
447
+ ・・・
274
448
 
275
449
  ```
276
450