def conf_thresholding(data_dir, save_dir, score_threshold): # collect all trajectories tracker_id_score = dict() eval_dir = os.path.join(data_dir, 'data') seq_list, num_seq = load_list_from_folder(eval_dir) for seq_file in seq_list: seq_data, num_line = load_txt_file(seq_file) for data_line in seq_data: data_split = data_line.split(' ') score_tmp = float(data_split[-1]) id_tmp = int(data_split[1]) if id_tmp not in tracker_id_score.keys(): tracker_id_score[id_tmp] = list() tracker_id_score[id_tmp].append(score_tmp) # collect the ID to remove based on the confidence to_delete_id = list() for track_id, score_list in tracker_id_score.items(): average_score = sum(score_list) / float(len(score_list)) if average_score < score_threshold: to_delete_id.append(track_id) # remove the ID in the data folder for tracking evaluation save_dir_tmp = os.path.join(save_dir, 'data') mkdir_if_missing(save_dir_tmp) for seq_file in seq_list: seq_name = fileparts(seq_file)[1] seq_file_save = os.path.join(save_dir_tmp, seq_name + '.txt') seq_file_save = open(seq_file_save, 'w') seq_data, num_line = load_txt_file(seq_file) for data_line in seq_data: data_split = data_line.split(' ') id_tmp = int(float(data_split[1])) if id_tmp not in to_delete_id: seq_file_save.write(data_line + '\n') seq_file_save.close() # remove the ID in the trk with id folder for detection evaluation and tracking visualization trk_id_dir = os.path.join(data_dir, 'trk_withid') seq_dir_list, num_seq = load_list_from_folder(trk_id_dir) save_dir_tmp = os.path.join(save_dir, 'trk_withid') for seq_dir in seq_dir_list: frame_list, num_frame = load_list_from_folder(seq_dir) seq_name = fileparts(seq_dir)[1] save_frame_dir = os.path.join(save_dir_tmp, seq_name) mkdir_if_missing(save_frame_dir) for frame in frame_list: frame_index = fileparts(frame)[1] frame_file_save = os.path.join(save_frame_dir, frame_index + '.txt') frame_file_save = open(frame_file_save, 'w') frame_data, num_line = load_txt_file(frame) for data_line in frame_data: data_split = data_line.split(' ') id_tmp = int(data_split[-1]) if id_tmp not in to_delete_id: frame_file_save.write(data_line + '\n') frame_file_save.close()
def sweep_data(self): ''' sweep the data and return the dictionary of ground truth infomation for every images outputs: images_dict: a dictionary of info, keys are the image ids, values are a dictionary {'width':, 'height':, 'ids': a list of object ids contained} ''' print('loading annotations into memory from %s...' % self.anno_dir) anno_list, num_anno = load_list_from_folder(self.anno_dir, ext_filter=['.json'], recursive=True, depth=2) print('number of annotations has been loaded: %d' % num_anno) images_dict = {} for anno_tmp in anno_list: _, filename, _ = fileparts(anno_tmp) image_id = filename.split('_gt')[0] img_shape, class_ids = self.anno2mask(anno_tmp, run_fast=True) class_ids = list(set(class_ids)) image_data = { 'width': img_shape[0], 'height': img_shape[1], 'ids': class_ids } images_dict[image_id] = image_data return images_dict
def vis(result_sha, data_root, result_root): def show_image_with_boxes(img, objects_res, object_gt, calib, save_path, height_threshold=0): img2 = np.copy(img) for obj in objects_res: box3d_pts_2d, _ = compute_box_3d(obj, calib.P) color_tmp = tuple([int(tmp * 255) for tmp in colors[obj.id % max_color]]) img2 = draw_projected_box3d(img2, box3d_pts_2d, color=color_tmp) text = 'ID: %d' % obj.id if box3d_pts_2d is not None: img2 = cv2.putText(img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=color_tmp) img = Image.fromarray(img2) img = img.resize((width, height)) img.save(save_path) for seq in seq_list: image_dir = os.path.join(data_root, 'image_02/%s' % seq) calib_file = os.path.join(data_root, 'calib/%s.txt' % seq) result_dir = os.path.join(result_root, '%s/trk_withid/%s' % (result_sha, seq)) save_3d_bbox_dir = os.path.join(result_dir, '../../trk_image_vis/%s' % seq); mkdir_if_missing(save_3d_bbox_dir) # load the list images_list, num_images = load_list_from_folder(image_dir) print('number of images to visualize is %d' % num_images) start_count = 0 for count in range(start_count, num_images): image_tmp = images_list[count] if not is_path_exists(image_tmp): count += 1 continue image_index = int(fileparts(image_tmp)[1]) image_tmp = np.array(Image.open(image_tmp)) img_height, img_width, img_channel = image_tmp.shape result_tmp = os.path.join(result_dir, '%06d.txt'%image_index) # load the result if not is_path_exists(result_tmp): object_res = [] else: object_res = read_label(result_tmp) print('processing index: %d, %d/%d, results from %s' % (image_index, count+1, num_images, result_tmp)) calib_tmp = Calibration(calib_file) # load the calibration object_res_filtered = [] for object_tmp in object_res: if object_tmp.type not in type_whitelist: continue if hasattr(object_tmp, 'score'): if object_tmp.score < score_threshold: continue center = object_tmp.t object_res_filtered.append(object_tmp) num_instances = len(object_res_filtered) save_image_with_3dbbox_gt_path = os.path.join(save_3d_bbox_dir, '%06d.jpg' % (image_index)) show_image_with_boxes(image_tmp, object_res_filtered, [], calib_tmp, save_path=save_image_with_3dbbox_gt_path) print('number of objects to plot is %d' % (num_instances)) count += 1
def test_simple(params): """Test function.""" left = tf.placeholder(tf.float32, [2, args.input_height, args.input_width, 3]) model = MonodepthModel(params, "test", left, None) # SESSION config = tf.ConfigProto(allow_soft_placement=True) sess = tf.Session(config=config) # SAVER train_saver = tf.train.Saver() # INIT sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) coordinator = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coordinator) # RESTORE restore_path = checkpoint_path.split(".")[0] train_saver.restore(sess, restore_path) image_list, num_images = load_list_from_folder(images_dir) print('number of images loaded is %d' % num_images) for image_file_tmp in image_list: input_image = scipy.misc.imread(image_file_tmp, mode="RGB") original_height, original_width, num_channels = input_image.shape input_image = scipy.misc.imresize( input_image, [args.input_height, args.input_width], interp='lanczos') input_image = input_image.astype(np.float32) / 255 input_images = np.stack((input_image, np.fliplr(input_image)), 0) _, filename, ext = fileparts(image_file_tmp) print('processing frame %s' % filename) disp = sess.run(model.disp_left_est[0], feed_dict={left: input_images}) disp_pp = post_process_disparity(disp.squeeze()).astype(np.float32) # output_directory = os.path.dirname(args.image_path) # output_name = os.path.splitext(os.path.basename(args.image_path))[0] data_file_tmp = os.path.join(depth_save_dir, filename + '.npy') np.save(data_file_tmp, disp_pp) # np.save(os.path.join(output_directory, "{}_disp.npy".format(output_name)), disp_pp) disp_to_img = scipy.misc.imresize(disp_pp.squeeze(), [original_height, original_width]) save_file_tmp = os.path.join(depth_img_save_dir, filename + '.png') plt.imsave(save_file_tmp, disp_to_img, cmap='plasma') print('done!')
def generate_video_from_folder(images_dir, save_path, framerate=30, downsample=1, warning=True, debug=True): image_list, num_images = load_list_from_folder( images_dir, ext_filter=['.jpg', '.png', '.jpeg'], debug=debug) print('%d images loaded' % num_images) generate_video_from_list(image_list, save_path, framerate=framerate, downsample=downsample, warning=warning, debug=debug)
def rand_load_hdf5_from_folder(hdf5_src, dataname, debug=True): ''' randomly load a single hdf5 file from a hdf5 folder ''' if debug: assert is_path_exists(hdf5_src) and isfolder( hdf5_src), 'input hdf5 path does not exist: %s' % hdf5_src assert islist(dataname), 'dataset queried is not correct' assert all( isstring(dataset_tmp) for dataset_tmp in dataname), 'dataset queried is not correct' hdf5list, num_hdf5_files = load_list_from_folder(folder_path=hdf5_src, ext_filter='.hdf5') check_index = random.randrange(0, num_hdf5_files) hdf5_path_sample = hdf5list[check_index] hdf5_file = h5py.File(hdf5_path_sample, 'r') datadict = dict() for dataset in dataname: datadict[dataset] = np.array(hdf5_file[dataset]) return datadict
from __future__ import print_function import matplotlib; matplotlib.use('Agg') import os, numpy as np, time, sys from AB3DMOT_libs.model import AB3DMOT from xinshuo_io import load_list_from_folder, fileparts, mkdir_if_missing if __name__ == '__main__': if len(sys.argv) != 2: print('Usage: python main.py result_sha(e.g., pointrcnn_Car_test)') sys.exit(1) result_sha = sys.argv[1] save_root = './results' det_id2str = {1:'Pedestrian', 2:'Car', 3:'Cyclist'} seq_file_list, num_seq = load_list_from_folder(os.path.join('data/KITTI', result_sha)) total_time, total_frames = 0.0, 0 save_dir = os.path.join(save_root, result_sha); mkdir_if_missing(save_dir) eval_dir = os.path.join(save_dir, 'data'); mkdir_if_missing(eval_dir) seq_count = 0 for seq_file in seq_file_list: _, seq_name, _ = fileparts(seq_file) eval_file = os.path.join(eval_dir, seq_name + '.txt'); eval_file = open(eval_file, 'w') save_trk_dir = os.path.join(save_dir, 'trk_withid', seq_name); mkdir_if_missing(save_trk_dir) mot_tracker = AB3DMOT() seq_dets = np.loadtxt(seq_file, delimiter=',') # load detections, N x 15 # print(seq_file) # print(seq_dets) print(seq_dets.shape) # print(len(seq_dets.shape))
def generate_hdf5(data_src, save_dir, data_name='data', batch_size=1, ext_filter='png', label_src1=None, label_name1='label', label_preprocess_function1=identity, label_range1=None, label_src2=None, label_name2='label2', label_preprocess_function2=identity, label_range2=None, debug=True, vis=False): ''' # this function creates data in hdf5 format from a image path # input parameter # data_src: source of image data, which can be a list of image path, a txt file contains a list of image path, a folder contains a set of images, a list of numpy array image data # label_src: source of label data, which can be none, a file contains a set of labels, a dictionary of labels, a 1-d numpy array data, a list of label data # save_dir: where to store the hdf5 data # batch_size: how many image to store in a single hdf file # ext_filder: what format of data to use for generating hdf5 data ''' # parse input assert is_path_exists_or_creatable( save_dir), 'save path should be a folder to save all hdf5 files' mkdir_if_missing(save_dir) assert isstring( data_name), 'dataset name is not correct' # name for hdf5 data # convert data source to a list of numpy array image data if isfolder(data_src): print 'data is loading from %s with extension .%s' % (data_src, ext_filter) filelist, num_data = load_list_from_folder(data_src, ext_filter=ext_filter) datalist = None elif isfile(data_src): print 'data is loading from %s with extension .%s' % (data_src, ext_filter) filelist, num_data = load_list_from_file(data_src) datalist = None elif islist(data_src): if debug: assert all( isimage(data_tmp) for data_tmp in data_src ), 'input data source is not a list of numpy array image data' datalist = data_src num_data = len(datalist) filelist = None else: assert False, 'data source format is not correct.' if debug: assert (datalist is None and filelist is not None) or ( filelist is None and datalist is not None), 'data is not correct' if datalist is not None: assert len(datalist) == num_data, 'number of data is not equal' if filelist is not None: assert len(filelist) == num_data, 'number of data is not equal' # convert label source to a list of numpy array label if label_src1 is None: labeldict1 = None labellist1 = None elif isfile(label_src1): assert is_path_exists(label_src1), 'file not found' _, _, ext = fileparts(label_src1) assert ext == '.json', 'only json extension is supported' labeldict1 = json.load(label_src1) num_label1 = len(labeldict1) assert num_data == num_label1, 'number of data and label is not equal.' labellist1 = None elif isdict(label_src1): labeldict1 = label_src1 labellist1 = None elif isnparray(label_src1): if debug: assert label_src1.ndim == 1, 'only 1-d label is supported' labeldict1 = None labellist1 = label_src1 elif islist(label_src1): if debug: assert all( np.array(label_tmp).size == 1 for label_tmp in label_src1), 'only 1-d label is supported' labellist1 = label_src1 labeldict1 = None else: assert False, 'label source format is not correct.' assert isfunction(label_preprocess_function1 ), 'label preprocess function is not correct.' # convert label source to a list of numpy array label if label_src2 is None: labeldict2 = None labellist2 = None elif isfile(label_src2): assert is_path_exists(label_src2), 'file not found' _, _, ext = fileparts(label_src2) assert ext == '.json', 'only json extension is supported' labeldict2 = json.load(label_src2) num_label2 = len(labeldict2) assert num_data == num_label2, 'number of data and label is not equal.' labellist2 = None elif isdict(label_src2): labeldict2 = label_src2 labellist2 = None elif isnparray(label_src2): if debug: assert label_src2.ndim == 1, 'only 1-d label is supported' labeldict2 = None labellist2 = label_src2 elif islist(label_src2): if debug: assert all( np.array(label_tmp).size == 1 for label_tmp in label_src2), 'only 1-d label is supported' labellist2 = label_src2 labeldict2 = None else: assert False, 'label source format is not correct.' assert isfunction(label_preprocess_function2 ), 'label preprocess function is not correct.' # warm up if datalist is not None: size_data = datalist[0].shape else: size_data = imread(filelist[0]).shape if labeldict1 is not None: if debug: assert isstring(label_name1), 'label name is not correct' labels1 = np.zeros((batch_size, 1), dtype='float32') # label_value1 = [float(label_tmp_char) for label_tmp_char in labeldict1.values()] # label_range1 = np.array([min(label_value1), max(label_value1)]) if labellist1 is not None: labels1 = np.zeros((batch_size, 1), dtype='float32') # label_range1 = [np.min(labellist1), np.max(labellist1)] if label_src1 is not None and debug: assert label_range1 is not None, 'label range is not correct' assert (labeldict1 is not None and labellist1 is None) or ( labellist1 is not None and labeldict1 is None), 'label is not correct' if labeldict2 is not None: if debug: assert isstring(label_name2), 'label name is not correct' labels2 = np.zeros((batch_size, 1), dtype='float32') # label_value2 = [float(label_tmp_char) for label_tmp_char in labeldict2.values()] # label_range2 = np.array([min(label_value2), max(label_value2)]) if labellist2 is not None: labels2 = np.zeros((batch_size, 1), dtype='float32') # label_range2 = [np.min(labellist2), np.max(labellist2)] if label_src2 is not None and debug: assert label_range2 is not None, 'label range is not correct' assert (labeldict2 is not None and labellist2 is None) or ( labellist2 is not None and labeldict2 is None), 'label is not correct' # start generating count_hdf = 1 # count number of hdf5 file clock = Timer() datalist_batch = list() for i in xrange(num_data): clock.tic() if filelist is not None: imagefile = filelist[i] _, name, _ = fileparts(imagefile) img = imread(imagefile).astype('float32') max_value = np.max(img) if max_value > 1 and max_value <= 255: img = img / 255.0 # [rows,col,channel,numbers], scale the image data to (0, 1) if debug: min_value = np.min(img) assert min_value >= 0 and min_value <= 1, 'data is not in [0, 1]' if datalist is not None: img = datalist[i] if debug: assert size_data == img.shape datalist_batch.append(img) # process label if labeldict1 is not None: if debug: assert len(filelist) == len( labeldict1), 'file list is not equal to label dictionary' labels1[i % batch_size, 0] = float(labeldict1[name]) if labellist1 is not None: labels1[i % batch_size, 0] = float(labellist1[i]) if labeldict2 is not None: if debug: assert len(filelist) == len( labeldict2), 'file list is not equal to label dictionary' labels2[i % batch_size, 0] = float(labeldict2[name]) if labellist2 is not None: labels2[i % batch_size, 0] = float(labellist2[i]) # save to hdf5 if i % batch_size == 0: data = preprocess_image_caffe( datalist_batch, debug=debug, vis=vis ) # swap channel, transfer from list of HxWxC to NxCxHxW # write to hdf5 format if filelist is not None: save_path = os.path.join(save_dir, '%s.hdf5' % name) else: save_path = os.path.join(save_dir, 'image_%010d.hdf5' % count_hdf) h5f = h5py.File(save_path, 'w') h5f.create_dataset(data_name, data=data, dtype='float32') if (labeldict1 is not None) or (labellist1 is not None): # print(labels1) labels1 = label_preprocess_function1(data=labels1, data_range=label_range1, debug=debug) # print(labels1) h5f.create_dataset(label_name1, data=labels1, dtype='float32') labels1 = np.zeros((batch_size, 1), dtype='float32') if (labeldict2 is not None) or (labellist2 is not None): labels2 = label_preprocess_function2(data=labels2, data_range=label_range2, debug=debug) h5f.create_dataset(label_name2, data=labels2, dtype='float32') labels2 = np.zeros((batch_size, 1), dtype='float32') h5f.close() count_hdf = count_hdf + 1 del datalist_batch[:] if debug: assert len(datalist_batch) == 0, 'list has not been cleared' average_time = clock.toc() print( 'saving to %s: %d/%d, average time:%.3f, elapsed time:%s, estimated time remaining:%s' % (save_path, i + 1, num_data, average_time, convert_secs2time(average_time * i), convert_secs2time(average_time * (num_data - i)))) return count_hdf - 1, num_data
def generate_video_from_folder(images_dir, save_path, framerate=30, downsample=1, depth=1, reverse=False, display=True, warning=True, debug=True): image_list, num_images = load_list_from_folder(images_dir, ext_filter=['.jpg', '.png', '.jpeg'], depth=depth, debug=debug) if reverse: image_list = reverse_list(image_list, warning=warning, debug=debug) if display: print('%d images loaded' % num_images) generate_video_from_list(image_list, save_path, framerate=framerate, downsample=downsample, display=display, warning=warning, debug=debug)
import cv2, numpy as np, os from xinshuo_io import mkdir_if_missing, load_image, load_list_from_folder, save_image, fileparts from xinshuo_images import rgb2gray from flow_vis import flow_to_color data_dir = '/media/xinshuo/Data/Datasets/LRD/LRW/' images_dir = os.path.join(data_dir, 'centered122_rgb_images/ABOUT/train/ABOUT_00001') save_dir = os.path.join(data_dir, 'centered122_flow/ABOUT/train/ABOUT_00001'); mkdir_if_missing(save_dir) image_list, num_images = load_list_from_folder(images_dir) print('number of images loaded is %d' % num_images) test = cv2.imread(image_list[0]) # print(test.dtype) test = cv2.cvtColor(test, cv2.COLOR_BGR2GRAY) # print(test.dtype) # print(test) # zxc frame1 = load_image(image_list[0]) prvs = rgb2gray(frame1) # uint8 # print(prvs) hsv = np.zeros_like(frame1) hsv[..., 1] = 255 image_index = 1
##--------------------------------- Model Directory ----------------------------------## # model_path = os.path.join(root_dir, 'resnet50_imagenet.pth') # Path to trained weights file if train_dataset == 'coco': model_path = os.path.join( root_dir, 'models/mask_rcnn_coco.pth') # Path to trained weights file else: assert False, 'error' model = MaskRCNN(model_dir=save_dir, config=config) # Create model object. if config.GPU_COUNT: model = model.cuda() model.load_weights(model_path) # Load weights ##--------------------------------- Testing ----------------------------------## image_list, num_list = load_list_from_folder(images_dir, ext_filter=['.png', '.jpg'], depth=2) print_log('testing results on %d images' % num_list, log=log_file) count = 1 timer = Timer() timer.tic() for image_file_tmp in image_list: parent_dir, filename, _ = fileparts(image_file_tmp) video_dir = parent_dir.split('/')[-1] # print(video_dir) # zxc image = load_image(image_file_tmp) results = model.detect([image]) # inference, results is a dictionary if len(results) == 0: count += 1
def vis(cam_transform, data_root, result_root): def show_image_with_boxes(img, objects_res, object_gt, calib, save_path, height_threshold=0): img2 = np.copy(img) for obj in objects_res: box3d_pts_2d, _ = compute_box_3d(obj, calib.P) color_tmp = tuple( [int(tmp * 255) for tmp in colors[obj.id % max_color]]) img2 = draw_projected_box3d(img2, box3d_pts_2d, color=color_tmp) text = 'ID: %d' % obj.id if box3d_pts_2d is not None: img2 = cv2.putText( img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=color_tmp) img = Image.fromarray(img2) img = img.resize((width, height)) img = img.convert('RGB') img.save(save_path) for seq in seq_list: #image_dir = os.path.join(data_root, 'image_02/%s' % seq) #calib_file = os.path.join(data_root, 'calib/%s.txt' % seq) #result_dir = os.path.join(result_root, '%s/trk_withid/%s' % (result_sha, seq)) #save_3d_bbox_dir = os.path.join(result_dir, '../../trk_image_vis/%s' % seq); mkdir_if_missing(save_3d_bbox_dir) image_dir = '/home/ubuntu/xwp/datasets/multi_view_dataset/new/cam_sample/image_2' result_dir = '/home/ubuntu/xwp/datasets/multi_view_dataset/new/fuse_test/cam9+cam21/tracking_results/trk_withid/0000' calib_file = '/home/ubuntu/xwp/datasets/multi_view_dataset/346/calib/000000.txt' save_3d_bbox_dir = '/home/ubuntu/xwp/datasets/multi_view_dataset/new/fuse_test/cam9+cam21/trk_image_vis' mkdir_if_missing(save_3d_bbox_dir) # load the list images_list, num_images = load_list_from_folder(image_dir) print('number of images to visualize is %d' % num_images) cam_id = 'cam9' start_count = 8945 end_count = 8950 min_index = 8900 for count in range(start_count, end_count): image_tmp = images_list[count] if not is_path_exists(image_tmp): count += 1 continue image_index = int(fileparts(image_tmp)[1]) image_tmp = np.array(Image.open(image_tmp)) img_height, img_width, img_channel = image_tmp.shape result_tmp = os.path.join( result_dir, '%06d.txt' % (image_index - min_index)) # load the result if not is_path_exists(result_tmp): object_res = [] else: object_res = read_label(result_tmp) print('processing index: %d, %d/%d, results from %s' % (image_index, count + 1, num_images, result_tmp)) calib_tmp = Calibration(calib_file) # load the calibration object_res_filtered = [] cam_trans = cam_transform[cam_id] for object_tmp in object_res: if object_tmp.type not in type_whitelist: continue if hasattr(object_tmp, 'score'): if object_tmp.score < score_threshold: continue center = object_tmp.t #transform cord_p = np.zeros((1, 4)) cord_p[0][0] = object_tmp.t[2] cord_p[0][1] = object_tmp.t[0] cord_p[0][2] = -object_tmp.t[1] cord_p[0][3] = 1 rotation_y = object_tmp.ry * 180 / np.pi cam_matrix = cu.ClientSideBoundingBoxes.get_matrix(cam_trans) cam_to_world = np.dot(cam_matrix, np.transpose(cord_p)) ry_cam2world = cu.ry_filter_a( rotation_y - 90 + cam_trans.rotation.yaw) * np.pi / 180 object_tmp.t = [ cam_to_world[1][0], -cam_to_world[2][0], cam_to_world[0][0] ] object_tmp.ry = cu.ry_filter(ry_cam2world) #end object_res_filtered.append(object_tmp) num_instances = len(object_res_filtered) save_image_with_3dbbox_gt_path = os.path.join( save_3d_bbox_dir, '%06d.jpg' % (image_index)) show_image_with_boxes(image_tmp, object_res_filtered, [], calib_tmp, save_path=save_image_with_3dbbox_gt_path) print('number of objects to plot is %d' % (num_instances)) count += 1
from AB3DMOT_libs.model import AB3DMOT sys.path.append('/home/ubuntu/xwp/Xinshuo_pyToolbox') from xinshuo_io import load_list_from_folder, fileparts, mkdir_if_missing if __name__ == '__main__': # if len(sys.argv) != 2: # print('Usage: python main.py result_sha(e.g., pointrcnn_Car_test)') # sys.exit(1) #result_sha = sys.argv[1] save_root = '/home/ubuntu/xwp/datasets/multi_view_dataset/new/fuse_test/cam9+cam21' det_id2str = {1: 'Pedestrian', 2: 'Car', 3: 'Cyclist'} # seq_file_list, num_seq = load_list_from_folder(os.path.join('data/KITTI', result_sha)) seq_file_list, num_seq = load_list_from_folder( '/home/ubuntu/xwp/datasets/multi_view_dataset/new/fuse_test/cam9+cam21/label_test_tracking' ) total_time, total_frames = 0.0, 0 save_dir = os.path.join(save_root, 'tracking_results') mkdir_if_missing(save_dir) eval_dir = os.path.join(save_dir, 'data') mkdir_if_missing(eval_dir) seq_count = 0 for seq_file in seq_file_list: _, seq_name, _ = fileparts(seq_file) eval_file = os.path.join(eval_dir, seq_name + '.txt') eval_file = open(eval_file, 'w') save_trk_dir = os.path.join(save_dir, 'trk_withid', seq_name) mkdir_if_missing(save_trk_dir) mot_tracker = AB3DMOT()
def __init__(self, data_dir, obs_len=8, pred_len=12, skip=1, threshold=0.002, min_ped=0, delim='\t', \ phase='training', split='test'): """ Args: - data_dir: Directory containing dataset files in the format <frame_id> <ped_id> <x> <y> - obs_len: Number of time-steps in input trajectories - pred_len: Number of time-steps in output trajectories - skip: Number of frames to skip while making the dataset - threshold: Minimum error to be considered for non linear traj when using a linear predictor - min_ped: Minimum number of pedestrians that should be in a seqeunce - delim: Delimiter in the dataset files """ super(TrajectoryDataset, self).__init__() self.data_dir = data_dir self.obs_len, self.pred_len = obs_len, pred_len self.seq_len = self.obs_len + self.pred_len self.delim, self.skip = delim, skip all_files, _ = load_list_from_folder(self.data_dir) num_peds_in_seq = [] seq_list = [] seq_list_rel = [] loss_mask_list = [] non_linear_ped = [] seq_id_list = [] for path in all_files: print_str = 'load %s\r' % path sys.stdout.write(print_str) sys.stdout.flush() _, seq_name, _ = fileparts(path) data = read_file(path, delim) # as testing files only contains past, so add more windows if split == 'test': min_frame, max_frame = 0, 999 num_windows = int(max_frame - min_frame + 1 - skip * (self.seq_len - 1)) num_windows += (self.pred_len - 1) * skip + 1 else: frames = np.unique(data[:, 0]).tolist() min_frame, max_frame = frames[0], frames[-1] num_windows = int(max_frame - min_frame + 1 - skip * (self.seq_len - 1) ) # include all frames for past and future # loop through every windows for window_index in range(num_windows): start_frame = int(window_index + min_frame) end_frame = int(start_frame + self.seq_len * skip) # right-open, not including this frame # reduce window during testing, only evaluate every N windows if phase == 'testing': check_pass = check_eval_windows(start_frame + self.obs_len * skip, self.obs_len * skip, self.pred_len * skip, split=split) if not check_pass: continue # get data in current window curr_seq_data = [] for frame in range(start_frame, end_frame, skip): curr_seq_data.append(data[frame == data[:, 0], :]) curr_seq_data = np.concatenate(curr_seq_data, axis=0) # initialize data peds_in_curr_seq = np.unique(curr_seq_data[:, 1]) curr_seq_rel = np.zeros( (len(peds_in_curr_seq), 2, self.seq_len)) # objects x 2 x seq_len curr_seq = np.zeros((len(peds_in_curr_seq), 2, self.seq_len)) curr_loss_mask = np.zeros( (len(peds_in_curr_seq), self.seq_len)) # objects x seq_len id_frame_list = np.zeros( (len(peds_in_curr_seq), 3, self.seq_len)) # objects x 2 x seq_len id_frame_list.fill(-1) num_peds_considered = 0 _non_linear_ped = [] # loop through every object in this window for _, ped_id in enumerate(peds_in_curr_seq): curr_ped_seq = curr_seq_data[ curr_seq_data[:, 1] == ped_id, :] # frames x 4 (frames can less than seqlen) pad_front = int(curr_ped_seq[0, 0]) - start_frame pad_end = int(curr_ped_seq[-1, 0]) - start_frame + skip assert pad_end % skip == 0, 'error' frame_existing = curr_ped_seq[:, 0].tolist() # pad front and back data to make the trajectory complete if pad_end - pad_front != self.seq_len * skip: # pad end to_be_paded_end = int(self.seq_len - pad_end / skip) pad_end_seq = np.expand_dims(curr_ped_seq[-1, :], axis=0) pad_end_seq = np.repeat(pad_end_seq, to_be_paded_end, axis=0) frame_offset = np.zeros((to_be_paded_end, 4), dtype='float32') frame_offset[:, 0] = np.array( range(1, to_be_paded_end + 1)) pad_end_seq += frame_offset * skip # shift first columns for frame curr_ped_seq = np.concatenate( (curr_ped_seq, pad_end_seq), axis=0) # pad front to_be_paded_front = int(pad_front / skip) pad_front_seq = np.expand_dims(curr_ped_seq[0, :], axis=0) pad_front_seq = np.repeat(pad_front_seq, to_be_paded_front, axis=0) frame_offset = np.zeros((to_be_paded_front, 4), dtype='float32') frame_offset[:, 0] = np.array( range(-to_be_paded_front, 0)) pad_front_seq += frame_offset * skip curr_ped_seq = np.concatenate( (pad_front_seq, curr_ped_seq), axis=0) # set pad front and end to correct values pad_front = 0 pad_end = self.seq_len * skip # add edge case when the object reappears at a bad frame # in other words, missing intermediate frame if curr_ped_seq.shape[0] != (pad_end - pad_front) / skip: frame_all = list( range(int(curr_ped_seq[0, 0]), int(curr_ped_seq[-1, 0]) + skip, skip)) frame_missing, _ = remove_list_from_list( frame_all, curr_ped_seq[:, 0].tolist()) # pad all missing frames with zeros pad_seq = np.expand_dims(curr_ped_seq[-1, :], axis=0) pad_seq = np.repeat(pad_seq, len(frame_missing), axis=0) pad_seq.fill(0) pad_seq[:, 0] = np.array(frame_missing) pad_seq[:, 1] = ped_id # fill ID curr_ped_seq = np.concatenate((curr_ped_seq, pad_seq), axis=0) curr_ped_seq = curr_ped_seq[np.argsort( curr_ped_seq[:, 0])] assert pad_front == 0, 'error' assert pad_end == self.seq_len * skip, 'error' # make sure the seq_len frames are continuous, no jumping frames start_frame_now = int(curr_ped_seq[0, 0]) if curr_ped_seq[-1, 0] != start_frame_now + (self.seq_len - 1) * skip: continue # make sure that past data has at least one frame past_frame_list = [ *range(start_frame_now, start_frame_now + self.obs_len * skip, skip) ] common = find_unique_common_from_lists(past_frame_list, frame_existing, only_com=True) if len(common) == 0: continue # make sure that future GT data has at least one frame if phase != 'testing': gt_frame_list = [ *range(start_frame_now + self.obs_len * skip, start_frame_now + self.seq_len * skip, skip) ] common = find_unique_common_from_lists(gt_frame_list, frame_existing, only_com=True) if len(common) == 0: continue # only keep the state cache_tmp = np.transpose(curr_ped_seq[:, :2]) curr_ped_seq = np.transpose( curr_ped_seq[:, 2:]) # 2 x seq_len # Make coordinates relative rel_curr_ped_seq = np.zeros(curr_ped_seq.shape) rel_curr_ped_seq[:, 1:] = curr_ped_seq[:, 1:] - curr_ped_seq[:, : -1] _idx = num_peds_considered curr_seq[_idx, :, :] = curr_ped_seq curr_seq_rel[_idx, :, :] = rel_curr_ped_seq # record seqname, frame and ID information id_frame_list[_idx, :2, :] = cache_tmp id_frame_list[_idx, 2, :] = seqname2int(seq_name) # Linear vs Non-Linear Trajectory, only fit for the future part not past part _non_linear_ped.append( poly_fit(curr_ped_seq, pred_len, threshold)) # add mask onto padded dummay data frame_exist_index = np.array([ frame_tmp - start_frame_now for frame_tmp in frame_existing ]) frame_exist_index = (frame_exist_index / skip).astype('uint8') curr_loss_mask[_idx, frame_exist_index] = 1 num_peds_considered += 1 if num_peds_considered > min_ped: non_linear_ped += _non_linear_ped num_peds_in_seq.append(num_peds_considered) loss_mask_list.append(curr_loss_mask[:num_peds_considered]) seq_list.append(curr_seq[:num_peds_considered]) seq_list_rel.append(curr_seq_rel[:num_peds_considered]) seq_id_list.append(id_frame_list[:num_peds_considered]) self.num_seq = len(seq_list) seq_list = np.concatenate(seq_list, axis=0) # objects x 2 x seq_len seq_list_rel = np.concatenate(seq_list_rel, axis=0) loss_mask_list = np.concatenate(loss_mask_list, axis=0) non_linear_ped = np.asarray(non_linear_ped) seq_id_list = np.concatenate(seq_id_list, axis=0) # Convert numpy -> Torch Tensor self.obs_traj = torch.from_numpy(seq_list[:, :, :self.obs_len]).type( torch.float) self.pred_traj = torch.from_numpy(seq_list[:, :, self.obs_len:]).type( torch.float) self.obs_traj_rel = torch.from_numpy( seq_list_rel[:, :, :self.obs_len]).type(torch.float) self.pred_traj_rel = torch.from_numpy( seq_list_rel[:, :, self.obs_len:]).type(torch.float) self.loss_mask = torch.from_numpy(loss_mask_list).type(torch.float) self.non_linear_ped = torch.from_numpy(non_linear_ped).type( torch.float) cum_start_idx = [0] + np.cumsum(num_peds_in_seq).tolist() self.seq_start_end = [ (start, end) for start, end in zip(cum_start_idx, cum_start_idx[1:]) ] self.seq_id_list = torch.from_numpy(seq_id_list).type(torch.float)