def flow_loader(path): if '.pfm' in path: data = readPFM(path)[0] data[:, :, 2] = 1 return data else: return read_flow(path)
def disparity_loader(path): if '.png' in path: data = Image.open(path) data = np.ascontiguousarray(data, dtype=np.float32) / 256 return data else: return readPFM(path)[0]
def read_pfm_file(flow_file): """ Read from .pfm file :param flow_file: name of the flow file :return: optical flow data in matrix """ (data, scale) = readPFM(flow_file) return data
def eval_f(fp): import warnings warnings.filterwarnings("ignore") # gt if '2015' in args.dataset: mask_gt = cv2.imread(maskp[fp], 0) flow_gt = read_flow(maskp[fp].replace('obj_map', 'flow_occ')).astype(np.float32) validmask = flow_gt[:, :, -1] == 1 bgmask_gt = np.logical_and(validmask, mask_gt == 0).astype(float) else: bgmask_gt = cv2.imread(maskp[fp], 0) > 0 shape = bgmask_gt.shape[:2] validmask = np.ones(shape).astype(bool) shape = bgmask_gt.shape[:2] # pred flowpath = flow_paths[fp] # KITTI if '2015' in args.dataset: idnum = int(flowpath.split('/')[-1].split('_')[-2]) if args.method == 'ours': maskp_pred = '%s/%s/pm-%06d_10.pfm' % (args.path, args.dataset, idnum) mask_pred = readPFM(maskp_pred)[0].astype(int) bgmask_pred = mask_pred == 0 elif args.method == 'detectron2': maskp_pred = '/data/gengshay/code/moseg/moseg-detectron2/%06d.png' % idnum mask_pred = cv2.imread(maskp_pred, 0) bgmask_pred = mask_pred == 0 elif args.method == 'mrflowss': bgmask_pred = np.load( '/home/gengshay/code/SF/mrflow/semantic_rigidity_cvpr2017/output_kitti_smodel/%06d_10.npy' % idnum) bgmask_pred = cv2.resize(bgmask_pred.astype(float), shape[::-1]).astype(bool) elif args.method == 'mrflowsk': bgmask_pred = cv2.imread( '/home/gengshay/code/SF/mrflow/semantic_rigidity_cvpr2017/output_kitti_kmodel/%06d_10.png' % idnum, 0).astype(bool) elif args.method == 'u2net': bgmask_pred = cv2.imread( '/data/gengshay/code/U-2-Net/test_data/u2net_results/%06d_10.png' % idnum, 0) < 128 elif args.method == 'rtn': bgmask_pred = ~cv2.imread( '/data/gengshay/code/learningrigidity_mod/output_kitti/%06d_10.png' % idnum, 0).astype(bool) elif args.method == 'fusionseg': bgmask_pred = ~np.load( '/data/gengshay/code/fusionseg/python/kitti/results_liu/joint_davis_val/%06d_10_mask.npy' % idnum).astype(bool) elif args.method == 'fusionsegm': bgmask_pred = ~np.load( '/data/gengshay/code/fusionseg/python/kitti/results_liu/motion/%06d_10_mask.npy' % idnum).astype(bool) elif args.method == 'cosnet': bgmask_pred = cv2.resize( cv2.imread( '/data/gengshay/code/COSNet/result/test/davis_iteration_conf/Results/kitti/%06d_10.png' % idnum, 0), shape[::-1]) < 128 elif args.method == 'matnet': bgmask_pred = cv2.imread( '/data/gengshay/code/MATNet/output/kitti_liu/%06d_10.png' % idnum, 0) < 128 elif args.method == 'cc': bgmask_pred = cv2.resize( np.load('/data/gengshay/code/cc/output/mask-joint/%03d.npy' % idnum)[0], shape[::-1]) == 1 elif args.method == 'ccm': bgmask_pred = cv2.resize( np.load('/data/gengshay/code/cc/output/mask-motion/%03d.npy' % idnum)[0], shape[::-1]) == 1 elif args.method == 'angle': maskp_pred = '%s/%s/pm-%06d_10.pfm' % (args.path, args.dataset, idnum) mask_pred = readPFM(maskp_pred)[0].astype(int) bgmask_pred = mask_pred == 0 else: exit() # Sintel else: passname = flowpath.split('/')[-1].split('_')[-4] seqname1 = flowpath.split('/')[-1].split('_')[-3] seqname2 = flowpath.split('/')[-1].split('_')[-2] framename = int(flowpath.split('/')[-1].split('_')[-1].split('.')[0]) if args.method == 'ours': maskp_pred = '%s/%s/pm-Sintel_%s_%s_%s_%02d.pfm' % ( args.path, args.dataset, passname, seqname1, seqname2, framename) mask_pred = readPFM(maskp_pred)[ 0] # label map in {1,...N} given N rigid body predictions bgmask_pred = mask_pred == 0 elif args.method == 'detectron2': maskp_pred = '/data/gengshay/code/moseg/smoseg-detectron2/Sintel_%s_%s_%s_%02d.png' % ( passname, seqname1, seqname2, framename) mask_pred = cv2.imread(maskp_pred, 0) bgmask_pred = mask_pred == 0 elif args.method == 'mrflowss': bgmask_pred = np.load( '/home/gengshay/code/SF/mrflow/semantic_rigidity_cvpr2017/output_sintel_smodel/Sintel_%s_%s_%s_%02d.npy' % (passname, seqname1, seqname2, framename)).astype(bool) elif args.method == 'mrflowsk': bgmask_pred = cv2.imread( '/home/gengshay/code/SF/mrflow/semantic_rigidity_cvpr2017/output_sintel_kmodel/Sintel_%s_%s_%s_%02d.png' % (passname, seqname1, seqname2, framename), 0).astype(bool) elif args.method == 'u2net': bgmask_pred = cv2.imread( '/data/gengshay/code/U-2-Net/test_data/u2net_results_sintel/Sintel_%s_%s_%s_%02d.png' % (passname, seqname1, seqname2, framename), 0) < 128 elif args.method == 'rtn': bgmask_pred = ~cv2.imread( '/data/gengshay/code/learningrigidity_mod/output_sintel/Sintel_%s_%s_%s_%02d.png' % (passname, seqname1, seqname2, framename), 0).astype(bool) elif args.method == 'fusionseg': bgmask_pred = ~np.load( '/data/gengshay/code/fusionseg/python/sintel/results/joint_davis_val/Sintel_%s_%s_%s_%02d_mask.npy' % (passname, seqname1, seqname2, framename)).astype(bool) elif args.method == 'fusionsegm': bgmask_pred = ~np.load( '/data/gengshay/code/fusionseg/python/sintel/results/motion/Sintel_%s_%s_%s_%02d_mask.npy' % (passname, seqname1, seqname2, framename)).astype(bool) elif args.method == 'cosnet': bgmask_pred = cv2.resize( cv2.imread( '/data/gengshay/code/COSNet/result/test/davis_iteration_conf/Results/sintel/Sintel_%s_%s_%s_%02d.png' % (passname, seqname1, seqname2, framename), 0), shape[::-1]) < 128 elif args.method == 'matnet': bgmask_pred = cv2.imread( '/data/gengshay/code/MATNet/output/sintel/Sintel_%s_%s_%s_%02d.png' % (passname, seqname1, seqname2, framename), 0) < 128 elif args.method == 'angle': maskp_pred = '%s/%s/pm-Sintel_%s_%s_%s_%02d.pfm' % ( args.path, args.dataset, passname, seqname1, seqname2, framename) mask_pred = readPFM(maskp_pred)[0] bgmask_pred = mask_pred == 0 else: exit() if args.method != 'ours' and args.method != 'detectron2': _, mask_pred = cv2.connectedComponentsWithAlgorithm( (1 - bgmask_pred.astype(np.uint8)), connectivity=8, ltype=cv2.CV_16U, ccltype=cv2.CCL_WU) # bg-iou try: bgiou = np.logical_and( bgmask_gt.astype(bool)[validmask], bgmask_pred[validmask]).sum() / np.logical_or( bgmask_gt.astype(bool)[validmask], bgmask_pred[validmask]).sum() except: pdb.set_trace() # obj-F (penalizes false positive) # defined in https://arxiv.org/abs/1902.03715 Sec.4 # F=2PR/P+R => 2/(1/P+1/R) # since P = sum(|c and g|) / sum(|c|) and R = sum(|c and g|) / sum(|g|), # where c is prediction and g is GT # 1/P+1/R = [sum(|c|) + sum(|g|)] / sum(|c and g|) # therefore F = 2*sum(|c and g|) / [sum(|c|) + sum(|g|)] if '2015' in args.dataset: gtlist = list(set(mask_gt.flatten())) M = len(gtlist) - 1 imatrix = np.zeros((M, mask_pred.max())) fmatrix = np.zeros((M, mask_pred.max())) for i in range(M): # for all bg instances objx_mask_gt = mask_gt == gtlist[i + 1] for j in range(mask_pred.max()): objx_mask_pred = mask_pred == j + 1 imatrix[i, j] = float( np.logical_and(objx_mask_gt, objx_mask_pred)[validmask].sum()) fmatrix[i, j] = imatrix[i, j] / (objx_mask_gt[validmask].sum() + objx_mask_pred[validmask].sum()) * 2 ridx, cidx = scipy.optimize.linear_sum_assignment(1 - fmatrix) objf = imatrix[ridx, cidx].sum() / ((mask_pred > 0)[validmask].sum() + (mask_gt > 0)[validmask].sum()) * 2 else: objf = 0. return bgiou, objf
import os import sys sys.path.insert(0, os.getcwd()) import cv2 import numpy as np import glob import argparse from utils.util_flow import readPFM parser = argparse.ArgumentParser(description='') parser.add_argument('--datapath', default='', help='dataset path') parser.add_argument('--outpath', default='', help='output path') args = parser.parse_args() for folder in glob.glob('%s/seq-*' % (args.datapath)): name = folder.split('seq-')[-1] dirpath = '%s/%s/' % (args.outpath, name) print(dirpath) if not os.path.isdir(dirpath): os.mkdir(dirpath) for filename in glob.glob('%s/pm*.pfm' % folder): frameid = filename.split('pm-')[-1].split('.pfm')[0] print(filename) mask_pred = readPFM(filename)[0].astype(int) > 0 shape = mask_pred.shape mask_pred = np.stack( [np.zeros(shape), np.zeros(shape), mask_pred.astype(int) * 128], -1) cv2.imwrite('%s/%s.png' % (dirpath, frameid), mask_pred)
def pfm_to_flo(pfm_file): flow_filename = pfm_file[0:pfm_file.find('.pfm')] + '.flo' (data, scale) = readPFM(pfm_file) flow = data[:, :, 0:2] write_flow(flow, flow_filename)
parser.add_argument('--imgpath', default='', help='dataset path') args = parser.parse_args() class Object(object): def has(self, name: str) -> bool: return name in self._fields def __getattr__(self, name: str) -> Any: if name == "_fields" or name not in self._fields: raise AttributeError("Cannot find field '{}' in the given Instances!".format(name)) return self._fields[name] frames=[] for i,path in enumerate(sorted(glob.glob('%s/pm*'%args.datapath))): print(path) pred = readPFM(path)[0] center_img = cv2.imread(path.replace('pm', 'mvis').replace('.pfm', '.jpg')) img = cv2.imread('%s/%s.png'%(args.imgpath,path.split('/')[-1].split('pm-')[1].split('.pfm')[0])) if img is None: img = cv2.imread('%s/%s.jpg'%(args.imgpath,path.split('/')[-1].split('pm-')[1].split('.pfm')[0])) shape = pred.shape[:2] num_instances = int(pred.max()) # if no object detected if num_instances==0: _, pred =cv2.connectedComponentsWithAlgorithm((1-(pred==0).astype(np.uint8)),connectivity=8,ltype=cv2.CV_16U,ccltype=cv2.CCL_WU) num_instances = pred.max() if num_instances>0: pred_masks = torch.zeros((num_instances,)+shape).bool() for k in range(num_instances):