def load_sfdata(data_path, current_file, self_guassian_normalize=True): A = current_file #filename = pjoin(data_path, 'frames_finalpass', A) filename = pjoin(data_path, A) #print ("[****] limg: {}".format(filename)) """ if using Pillow Image, the loaded image will have RGBA for PNG in Scene Flow dataset """ #left = Image.open(filename) #left.show() #left = np.asarray(Image.open(filename), dtype=np.float32, order='C') left = cv2.imread(filename)[:, :, ::-1].astype( np.float32) # change BRG to RGB via `::-1`; #print ('[???] left image shape = ', left.shape) #pfm.show_uint8(left) #filename = pjoin(data_path, 'frames_finalpass/' + A[:-13] + 'right/' + A[len(A)-8:]) filename = pjoin(data_path, A[:-13] + 'right/' + A[len(A) - 8:]) #print ("[****] rimg: {}".format(filename)) right = cv2.imread(filename)[:, :, ::-1].astype(np.float32) #pfm.show_uint8(right) #filename = pjoin(data_path, 'disparity/' + A[0:-4] + '.pfm') #print ("[???]", current_file, type(current_file)) pos = A.find('/') tmp_len = len('frames_finalpass') filename = pjoin( data_path, A[0:pos] + '/disparity' + A[pos + 1 + tmp_len:-4] + '.pfm') #print ("[****] ldisp: {}".format(filename)) disp_left = pfm.readPFM(filename) disp_left[disp_left == np.inf] = 0 # set zero as a invalid disparity value; #print ('[***] disp_left shape = ', disp_left.shape) #pfm.show(disp_left) #print ("[???] ",data_path + 'disparity/' + A[0:-13] + 'right/' + A[len(A)-8:-4] + '.pfm' ) filename = pjoin( data_path, A[0:pos] + '/disparity' + A[pos + 1 + tmp_len:-13] + 'right/' + A[len(A) - 8:-4] + '.pfm') #print ("[****] rdisp: {}".format(filename)) disp_right = pfm.readPFM(filename) #pfm.show(disp_right) height, width = left.shape[:2] temp_data = np.zeros([8 + 6 + 1, height, width], 'float32') temp_data[0], temp_data[1], temp_data[2] = normalize_rgb_via_mean_std( left, is_mean_std=self_guassian_normalize) temp_data[3], temp_data[4], temp_data[5] = normalize_rgb_via_mean_std( right, is_mean_std=self_guassian_normalize) #temp_data[6:7, :, :] = width * 2 temp_data[6, :, :] = disp_left temp_data[7, :, :] = disp_right # save for tensorboard visualization temp_data[8, :, :] = left[:, :, 0] / 255.0 #R temp_data[9, :, :] = left[:, :, 1] / 255.0 #G temp_data[10, :, :] = left[:, :, 2] / 255.0 #B temp_data[11, :, :] = right[:, :, 0] / 255.0 #R temp_data[12, :, :] = right[:, :, 1] / 255.0 #G temp_data[13, :, :] = right[:, :, 2] / 255.0 #B return temp_data
def load_kitti2012_data(file_path, current_file): """ load current file from the list""" filename = pjoin(file_path, 'colored_0/' + current_file) #filename = pjoin(file_path, 'image_0/' + current_file) print("limg: {}".format(filename)) left = np.asarray(Image.open(filename), dtype=np.float32, order="C") #filename = pjoin(file_path, 'image_1/' + current_file) filename = pjoin(file_path, 'colored_1/' + current_file) print("rimg: {}".format(filename)) right = np.asarray(Image.open(filename), dtype=np.float32, order='C') filename = pjoin(file_path, 'disp_occ_pfm/' + current_file[0:-4] + '.pfm') print("ldisp: {}".format(filename)) disp_left = pfm.readPFM(filename) height, width = disp_left.shape[:2] #disp_left[disp_left == np.inf] = width*2 #set 2*width as a invalid disparity value; disp_left[disp_left == np.inf] = 0 # set zero as a invalid disparity value; temp_data = np.zeros([8 + 6 + 1, height, width], 'float32') temp_data[0], temp_data[1], temp_data[2] = normalize_rgb_via_mean_std(left) temp_data[3], temp_data[4], temp_data[5] = normalize_rgb_via_mean_std( right) temp_data[6, :, :] = disp_left # save for tensorboard visualization temp_data[8, :, :] = left[:, :, 0] / 255.0 #R temp_data[9, :, :] = left[:, :, 1] / 255.0 #G temp_data[10, :, :] = left[:, :, 2] / 255.0 #B temp_data[11, :, :] = right[:, :, 0] / 255.0 #R temp_data[12, :, :] = right[:, :, 1] / 255.0 #G temp_data[13, :, :] = right[:, :, 2] / 255.0 #B return temp_data
def eval_bad_x(self): self.model.eval() file_path = self.args.data_path file_list = self.args.test_list f = open(file_list, 'r') filelist = [l.rstrip() for l in f.readlines()] avg_err = .0 avg_rate = .0 if not os.path.exists(self.args.resultDir): os.makedirs(self.args.resultDir) print('makedirs {}'.format(self.args.resultDir)) print("[***]To test %d imgs" % len(filelist)) for index in range(len(filelist)): current_file = filelist[index] if self.kitti2015: dispname = pjoin( file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') if os.path.isfile(savename): disp = pfm.readPFM(savename) else: savename = pjoin(self.args.resultDir, "disp-pfm/" + current_file[0:-4] + '.pfm') disp = pfm.readPFM(savename) elif self.kitti2012: dispname = pjoin( file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') if os.path.isfile(savename): disp = pfm.readPFM(savename) else: savename = pjoin(self.args.resultDir, "disp-pfm/" + current_file[0:-4] + '.pfm') disp = pfm.readPFM(savename) else: raise Exception("No KT dataset found, so do nothing!!!") error, rate = get_epe_rate(dispGT, disp, self.max_disp, self.args.threshold) avg_err += error avg_rate += rate avg_err = avg_err / len(filelist) avg_rate = avg_rate / len(filelist) print( "===> Total {} Frames ==> AVG EPE Error: {:.4f}, AVG Bad-{:.1f} Error: {:.4f}" .format(len(filelist), avg_err, self.args.threshold, avg_rate)) print('{} testing finished!'.format(self.model_name))
def load_kitti2012_gray_data(file_path, current_file, self_guassian_normalize=True): """ load current file from the list""" imglname = pjoin(file_path, 'image_0/' + current_file) left = np.asarray(Image.open(imglname), dtype=np.float32, order="C") #print ("left shape = ", left.shape) imgrname = pjoin(file_path, 'image_1/' + current_file) right = np.asarray(Image.open(imgrname), dtype=np.float32, order='C') #print ("right shape = ", right.shape) displname = pjoin(file_path, 'disp_occ_pfm/' + current_file[0:-4] + '.pfm') disp_left = pfm.readPFM(displname) #print ("disp shape = ", disp_left.shape) #print ("limg: {}, rimg: {}, ldisp: {}".format(imglname, imgrname, displname)) height, width = disp_left.shape[:2] #disp_left[disp_left == np.inf] = width*2 #set 2*width as a invalid disparity value; disp_left[disp_left == np.inf] = 0 # set zero as a invalid disparity value; temp_data = np.zeros([8 + 6 + 1, height, width], 'float32') tmp_l = normalize_gray_via_mean_std(left, is_mean_std=self_guassian_normalize) # copy left gray image to each of the three ones temp_data[0] = tmp_l temp_data[1] = tmp_l temp_data[2] = tmp_l # copy right gray image to each of the three ones tmp_r = normalize_gray_via_mean_std(right, is_mean_std=self_guassian_normalize) temp_data[3] = tmp_r temp_data[4] = tmp_r temp_data[5] = tmp_r temp_data[6, :, :] = disp_left # save for tensorboard visualization rgb_lname = pjoin(file_path, 'colored_0/' + current_file) left_rgb = np.asarray(Image.open(rgb_lname), dtype=np.float32, order="C") rgb_rname = pjoin(file_path, 'colored_1/' + current_file) right_rgb = np.asarray(Image.open(rgb_rname), dtype=np.float32, order="C") # save for tensorboard visualization temp_data[8, :, :] = left_rgb[:, :, 0] / 255.0 #R temp_data[9, :, :] = left_rgb[:, :, 1] / 255.0 #G temp_data[10, :, :] = left_rgb[:, :, 2] / 255.0 #B temp_data[11, :, :] = right_rgb[:, :, 0] / 255.0 #R temp_data[12, :, :] = right_rgb[:, :, 1] / 255.0 #G temp_data[13, :, :] = right_rgb[:, :, 2] / 255.0 #B return temp_data
def load_kitti2015_data(file_path, current_file, is_semantic=True, self_guassian_normalize=True): """ load current file from the list""" filename = pjoin(file_path, 'image_0/' + current_file) #print ("limg: {}".format(filename)) left = np.asarray(Image.open(filename), dtype=np.float32, order="C") filename = pjoin(file_path, 'image_1/' + current_file) #print ("rimg: {}".format(filename)) right = np.asarray(Image.open(filename), dtype=np.float32, order="C") filename = file_path + 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm' #print ("ldisp: {}".format(filename)) disp_left = pfm.readPFM(filename) height, width = disp_left.shape[:2] #disp_left[disp_left == np.inf] = width*2 # set 2*width as a invalid disparity value; disp_left[disp_left == np.inf] = 0 # set zero as a invalid disparity value; temp_data = np.zeros([8 + 6 + 1, height, width], 'float32') temp_data[0], temp_data[1], temp_data[2] = normalize_rgb_via_mean_std( left, is_mean_std=self_guassian_normalize) temp_data[3], temp_data[4], temp_data[5] = normalize_rgb_via_mean_std( right, is_mean_std=self_guassian_normalize) temp_data[6, :, :] = disp_left # save for tensorboard visualization temp_data[8, :, :] = left[:, :, 0] / 255.0 #R temp_data[9, :, :] = left[:, :, 1] / 255.0 #G temp_data[10, :, :] = left[:, :, 2] / 255.0 #B temp_data[11, :, :] = right[:, :, 0] / 255.0 #R temp_data[12, :, :] = right[:, :, 1] / 255.0 #G temp_data[13, :, :] = right[:, :, 2] / 255.0 #B # semantic segmentaion label if is_semantic: # uint8 gray png image filename = pjoin(file_path, '../data_semantics/training/semantic/' + current_file) #print ("semantic label: {}".format(filename)) semantic_label = np.asarray(Image.open(filename), dtype=np.float32, order="C") #pfm.show(semantic_label) temp_data[14, :, :] = semantic_label return temp_data
def test(self): self.model.eval() file_path = self.args.data_path file_list_txt = self.args.test_list f = open(file_list_txt, 'r') if self.virtual_kitti2: filelist = get_virtual_kitti2_filelist(file_list_txt) else: filelist = [l.rstrip() for l in f.readlines() if not l.rstrip().startswith('#')] crop_width = self.args.crop_width crop_height = self.args.crop_height if not os.path.exists(self.args.resultDir): os.makedirs(self.args.resultDir) print ('makedirs {}'.format(self.args.resultDir)) avg_err = 0 avg_rate1 = 0 avg_rate3 = 0 for index in range(len(filelist)): #for index in range(1): current_file = filelist[index] if self.kitti2015: leftname = pjoin(file_path, 'image_0/' + current_file) if index % 20 == 0: print ("limg: {}".format(leftname)) rightname = pjoin(file_path, 'image_1/' + current_file) dispname = pjoin(file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT= None savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') elif self.kitti2012: #leftname = pjoin(file_path, 'image_0/' + current_file) leftname = pjoin(file_path, 'colored_0/' + current_file) rightname = pjoin(file_path, 'colored_1/' + current_file) dispname = pjoin(file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT= None savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') #disp = Image.open(dispname) #disp = np.asarray(disp) / 256.0 elif self.virtual_kitti2: A = current_file # e.g., /media/ccjData2/datasets/Virtual-KITTI-V2/vkitti_2.0.3_rgb/Scene01/15-deg-left/frames/rgb/Camera_0/rgb_00001.jpg leftname = pjoin(file_path, "vkitti_2.0.3_rgb/" + A) rightname = pjoin(file_path, "vkitti_2.0.3_rgb/" + A[:-22] + 'Camera_1/' + A[-13:]) #load depth GT and change it to disparity GT: depth_png_filename = pjoin(file_path, "vkitti_2.0.3_depth/" + A[:-26] + 'depth/Camera_0/depth_' + A[-9:-4] + ".png") #print ("imgl = ", leftname, ", imgr = ", rightname, ", depth_left = ", depth_png_filename) #NOTE: The depth map in centimeters can be directly loaded depth_left = cv2.imread(depth_png_filename, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH) height, width = depth_left.shape[:2] # Intrinsi: f_x = f_y = 725.0087 # offset(i.e., distance between stereo views): B = 0.532725 m = 53.2725 cm B = 53.2725 # in centimeters; f = 725.0087 # in pixels # set zero as a invalid disparity value; dispGT = np.zeros([height, width], 'float32') mask = depth_left > 0 dispGT[mask] = f*B/ depth_left[mask] # d = fB/z #pfm.show(dispGT, title='dispGT') savename = pjoin(self.args.resultDir, '%04d.pfm'%(index)) elif self.eth3d or self.middlebury: leftname = pjoin(file_path, current_file + '/im0.png') rightname = pjoin(file_path, current_file + '/im1.png') print ("limg: {}".format(leftname)) H, W = cv2.imread(leftname).shape[:2] dispname = pjoin(file_path, current_file + '/disp0GT.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT= None savename = pjoin(self.args.resultDir, current_file + '.pfm') # due to eth3d images have diverse image size; # so we process each of them seperately; ds = self.ds # e.g., == 64, encoder sacle; crop_width = W + (ds - W % ds) % ds crop_height = H + (ds - H % ds) % ds print ("input HxW=%dx%d, padding size=%dx%d" %(H,W,crop_height, crop_width)) else: A = current_file leftname = pjoin(file_path, A) rightname = pjoin(file_path, A[:-13] + 'right/' + A[len(A)-8:]) # check disparity GT exists or not!!! pos = A.find('/') tmp_len = len('frames_finalpass') dispname = pjoin(file_path, A[0:pos] + '/disparity' + A[pos+1+tmp_len:-4] + '.pfm') #print ("[****] ldisp: {}".format(dispname)) if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT= None savename = pjoin(self.args.resultDir, str(index) + '.pfm') input1, input2, height, width = test_transform( load_test_data(leftname, rightname, is_data_augment=self.is_data_augment), crop_height, crop_width) if self.cuda: input1 = input1.cuda() input2 = input2.cuda() with torch.no_grad(): prediction = self.model(input1, input2) disp = prediction.cpu().detach().numpy() # in full size if height <= crop_height and width <= crop_width: disp = disp[0, crop_height - height: crop_height, crop_width-width: crop_width] else: disp = disp[0, :, :] # for KT benchmark submission; if dispGT is None: tmp_savename = savename[:-4] + '.png' print ("savd ", tmp_savename) skimage.io.imsave(tmp_savename, (disp * 256).astype('uint16')) #sys.exit() if any([self.kitti2015, self.kitti2012, self.eth3d, self.middlebury, index %250 == 0]): pfm.save(savename, disp) #print ('saved ', savename) if 0 and dispGT is not None: left = np.asarray(Image.open(leftname))[:,:,:3].astype(np.float32) right = np.asarray(Image.open(rightname))[:,:,:3].astype(np.float32) #print ("[???]", left.shape) pfm.save(savename[:-4] + '-iml.pfm', left) pfm.save(savename[:-4] + '-imr.pfm', right) pfm.save(savename[:-4] + '-gt.pfm', dispGT.astype(np.float32)) if dispGT is not None: #error, rate1, rate3 = get_epe_rate2(dispGT, disp, self.args.max_disp, threshold=1.0, threshold2=3.0) error, rate1, rate3 = get_epe_rate(dispGT, disp, threshold=1.0, threshold2=3.0) avg_err += error avg_rate1 += rate1 avg_rate3 += rate3 if index % 250 == 0: print("===> Frame {}: ".format(index) + leftname + " ==> EPE Error: {:.4f}, Bad-{:.1f} Error: {:.4f}, Bad-{:.1f} Error: {:.4f}".format( error, 1.0, rate1, 3.0, rate3)) # save kt15 color #if self.kitti2015: if any([self.kitti2015, self.kitti2012, self.eth3d, self.middlebury, index%250==0]): """ disp """ tmp_dir = pjoin(self.args.resultDir, "dispColor") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) if self.eth3d or self.middlebury: tmp_dispname = pjoin(tmp_dir, current_file + '.png') elif self.kitti2012 or self.kitti2015: tmp_dispname = pjoin(tmp_dir, current_file[0:-4] + '.png') else: tmp_dispname = pjoin(tmp_dir, '%04d.png'%(index)) cv2.imwrite(tmp_dispname, KT15FalseClr.writeKT15FalseColor(np.ascontiguousarray(disp)).astype(np.uint8)[:,:,::-1]) if index < 1: print ('saved ', tmp_dispname) if dispGT is not None: #If KT benchmark submission, then No dispGT; """ err-disp """ tmp_dir = pjoin(self.args.resultDir, "errDispColor") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) if self.eth3d or self.middlebury: tmp_errdispname = pjoin(tmp_dir, current_file + '.png') elif self.kitti2015 or self.kitti2012: tmp_errdispname = pjoin(tmp_dir, current_file[0:-4] + '.png') else: tmp_errdispname = pjoin(tmp_dir, '%04d.png'%(index)) cv2.imwrite(tmp_errdispname, KT15LogClr.writeKT15ErrorDispLogColor(np.ascontiguousarray(disp), np.ascontiguousarray(dispGT)).astype(np.uint8)[:,:,::-1]) if index < 1: print ('saved ', tmp_errdispname) if dispGT is not None: avg_err /= len(filelist) avg_rate1 /= len(filelist) avg_rate3 /= len(filelist) print("===> Total {} Frames ==> AVG EPE Error: {:.4f}, AVG Bad-{:.1f} Error: {:.4f}, AVG Bad-{:.1f} Error: {:.4f}".format( len(filelist), avg_err, 1.0, avg_rate1, 3.0, avg_rate3)) """ save as csv file, Excel file format """ csv_file = os.path.join(self.args.resultDir, 'bad-err.csv') print ("write ", csv_file, "\n") timeStamp = datetime.now().strftime('%Y-%m-%d_%H:%M:%S') messg = timeStamp + ',{},bad-1.0,{:.4f},bad-3.0,{:.4f},epe,{:.4f},fileDir={},for log,{:.3f}(epe); {:.3f}%(bad1); {:.3f}%(bad3)\n'.format( self.data_type_str, avg_rate1, avg_rate3, avg_err, self.args.resultDir, avg_err, avg_rate1*100.0, avg_rate3*100.0) with open( csv_file, 'w') as fwrite: fwrite.write(messg)
def test(self, py=None): self.model.eval() if not os.path.exists(self.args.resultDir): os.makedirs(self.args.resultDir) print('makedirs {}'.format(self.args.resultDir)) dispScale = 1.0 avg_err = .0 avg_rate = .0 img_num = self.test_loader_len print("[****] To test %d images !!!" % img_num) for iteration, batch_data in enumerate(self.testing_data_loader): batch_size = batch_data[0].size()[0] assert batch_size == 1 features = batch_data[0] print("[???] features size:", features.size()) height = batch_data[1][0].item() width = batch_data[2][0].item() crop_height = batch_data[3][0].item() crop_width = batch_data[4][0].item() current_file = batch_data[5][0] disp_name = batch_data[6][0] print(height, crop_height, current_file, disp_name) if os.path.isfile(disp_name): dispGT = pfm.readPFM(disp_name) dispGT[dispGT == np.inf] = .0 else: dispGT = None if self.kitti2015 or self.kitti2012: savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') elif self.eth3d or self.middlebury: savename = pjoin(self.args.resultDir, current_file + '.pfm') else: # scene flow dataset savename = pjoin(self.args.resultDir, str(iteration) + '.pfm') if self.cuda: features = features.cuda() with torch.no_grad(): if self.model_name == 'MS-GCNet': disp = self.model(features) elif self.model_name == 'MS-PSMNet': disp = self.model(features) else: raise Exception( "No suitable model found ... Wrong name: {}".format( self.model_name)) #about memory del features disp = disp.cpu().detach().numpy() * dispScale if height <= crop_height and width <= crop_width: #disp = disp[0, crop_height - height: crop_height, crop_width-width: crop_width] disp = disp[0, crop_height - height:crop_height, 0:width] else: disp = disp[0, :, :] #save to uint16 png files #skimage.io.imsave(savename, (disp * 256).astype('uint16')) if any([ self.kitti2015, self.kitti2012, self.eth3d, self.middlebury, iteration % 50 == 0 ]): pfm.save(savename, disp) #print ('saved ', savename) if dispGT is not None: if self.eth3d: self.args.threshold = 1.0 elif self.middlebury: self.args.threshold = 1.0 # for trainingH; elif self.kitti2012 or self.kitti2015: self.args.threshold = 3.0 else: # Scene Flow self.args.threshold = 1.0 error, rate = get_epe_rate(dispGT, disp, self.max_disp, self.args.threshold) avg_err += error avg_rate += rate if iteration % 200 == 0: message_info = "===> Frame {}: ".format( iteration ) + current_file + " ==> EPE Error: {:.4f}, Bad-{:.1f} Error: {:.4f}".format( error, self.args.threshold, rate) """ adapted from Mateo's code """ if py is not None: gc.collect() memoryUse = py.memory_info( )[0] / 2.**20 # memory use in MB...I think message_info += ', memeory: {:.2f} MB'.format( memoryUse) print(message_info) # save kt15 color #if self.kitti2015: if any( [self.kitti2015, self.kitti2012, self.eth3d, self.middlebury]): """ disp """ tmp_dir = pjoin(self.args.resultDir, "dispColor") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp_dispname = pjoin(tmp_dir, current_file[0:-4] + '.png') cv2.imwrite( tmp_dispname, KT15FalseClr.writeKT15FalseColor( np.ascontiguousarray(disp)).astype( np.uint8)[:, :, ::-1]) if iteration % 50 == 0: print('saved ', tmp_dispname) if dispGT is not None: #If KT benchmark submission, then No dispGT; """ err-disp """ tmp_dir = pjoin(self.args.resultDir, "errDispColor") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp_errdispname = pjoin(tmp_dir, current_file[0:-4] + '.png') cv2.imwrite( tmp_errdispname, KT15LogClr.writeKT15ErrorDispLogColor( np.ascontiguousarray(disp), np.ascontiguousarray(dispGT)).astype( np.uint8)[:, :, ::-1]) if iteration % 50 == 0: print('saved ', tmp_errdispname) if dispGT is not None: avg_err = avg_err / img_num avg_rate = avg_rate / img_num print( "===> Total {} Frames ==> AVG EPE Error: {:.4f}, AVG Bad-{:.1f} Error: {:.4f}" .format(img_num, avg_err, self.args.threshold, avg_rate)) print('{} testing finished!'.format(self.model_name))
img_num = len(filelist) print("[***]To test %d imgs" % img_num) for index in range(img_num): current_file = filelist[index] # updated by CCJ for image path: if opt.kitti2015: leftname = pjoin(file_path, 'image_0/' + current_file) if index < 1: print("limg: {}".format(leftname)) rightname = pjoin(file_path, 'image_1/' + current_file) dispname = pjoin(file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT = None savename = pjoin(opt.resultDir, current_file[0:-4] + '.pfm') elif opt.kitti2012: # KITTI 2012 #leftname = pjoin(file_path, 'image_0/' + current_file) leftname = pjoin(file_path, 'colored_0/' + current_file) rightname = pjoin(file_path, 'colored_1/' + current_file) dispname = pjoin(file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else:
def test(self): self.model.eval() file_path = self.args.data_path file_list = self.args.test_list f = open(file_list, 'r') filelist = [l.rstrip() for l in f.readlines()] crop_width = self.args.crop_width crop_height = self.args.crop_height if not os.path.exists(self.args.resultDir): os.makedirs(self.args.resultDir) print('makedirs {}'.format(self.args.resultDir)) avg_err = 0 avg_rate = 0 for index in range(len(filelist)): #for index in range(1): current_file = filelist[index] if self.kitti2015: leftname = pjoin(file_path, 'image_0/' + current_file) if index % 20 == 0: print("limg: {}".format(leftname)) rightname = pjoin(file_path, 'image_1/' + current_file) dispname = pjoin( file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT = None savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') elif self.kitti2012: #leftname = pjoin(file_path, 'image_0/' + current_file) leftname = pjoin(file_path, 'colored_0/' + current_file) rightname = pjoin(file_path, 'colored_1/' + current_file) dispname = pjoin( file_path, 'disp_occ_0_pfm/' + current_file[0:-4] + '.pfm') if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT = None savename = pjoin(self.args.resultDir, current_file[0:-4] + '.pfm') #disp = Image.open(dispname) #disp = np.asarray(disp) / 256.0 else: A = current_file leftname = pjoin(file_path, A) rightname = pjoin(file_path, A[:-13] + 'right/' + A[len(A) - 8:]) # check disparity GT exists or not!!! pos = A.find('/') tmp_len = len('frames_finalpass') dispname = pjoin( file_path, A[0:pos] + '/disparity' + A[pos + 1 + tmp_len:-4] + '.pfm') #print ("[****] ldisp: {}".format(dispname)) if os.path.isfile(dispname): dispGT = pfm.readPFM(dispname) dispGT[dispGT == np.inf] = .0 else: dispGT = None savename = pjoin(self.args.resultDir, str(index) + '.pfm') input1, input2, height, width = test_transform( load_test_data(leftname, rightname), crop_height, crop_width) if self.cuda: input1 = input1.cuda() input2 = input2.cuda() with torch.no_grad(): prediction1, prediction = self.model(input1, input2) disp = prediction.cpu().detach().numpy() # in full size disp1 = prediction1.cpu().detach().numpy() # in half size if height <= crop_height and width <= crop_width: disp = disp[0, crop_height - height:crop_height, crop_width - width:crop_width] disp1 = disp1[0, crop_height // 2 - height // 2:crop_height // 2, crop_width // 2 - width // 2:crop_width // 2] else: disp = disp[0, :, :] disp1 = disp1[0, :, :] #skimage.io.imsave(savename, (disp * 256).astype('uint16')) if self.kitti2015 or self.kitti2012 or index % 50 == 0: pfm.save(savename, disp) #print ('saved ', savename) if 0 and dispGT is not None: left = np.asarray(Image.open(leftname))[:, :, :3].astype( np.float32) right = np.asarray(Image.open(rightname))[:, :, :3].astype( np.float32) #print ("[???]", left.shape) pfm.save(savename[:-4] + '-iml.pfm', left) pfm.save(savename[:-4] + '-imr.pfm', right) pfm.save(savename[:-4] + '-gt.pfm', dispGT.astype(np.float32)) pfm.save(savename[:-4] + '-half.pfm', disp1) if dispGT is not None: error, rate = get_epe_rate(dispGT, disp, self.args.max_disp, self.args.threshold) avg_err += error avg_rate += rate if index % 20 == 0: print("===> Frame {}: ".format(index) + leftname + " ==> EPE Error: {:.4f}, Bad-{:.1f} Error: {:.4f}". format(error, self.args.threshold, rate)) # save kt15 color if self.kitti2015: """ disp """ tmp_dir = pjoin(self.args.resultDir, "dispColor") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp_dispname = pjoin(tmp_dir, current_file[0:-4] + '.png') cv2.imwrite( tmp_dispname, KT15FalseClr.writeKT15FalseColor( np.ascontiguousarray(disp)).astype( np.uint8)[:, :, ::-1]) if index % 20 == 0: print('saved ', tmp_dispname) if dispGT is not None: #If KT benchmark submission, then No dispGT; """ err-disp """ tmp_dir = pjoin(self.args.resultDir, "errDispColor") if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) tmp_errdispname = pjoin(tmp_dir, current_file[0:-4] + '.png') cv2.imwrite( tmp_errdispname, KT15LogClr.writeKT15ErrorDispLogColor( np.ascontiguousarray(disp), np.ascontiguousarray(dispGT)).astype( np.uint8)[:, :, ::-1]) if index % 20 == 0: print('saved ', tmp_errdispname) if dispGT is not None: avg_err = avg_err / len(filelist) avg_rate = avg_rate / len(filelist) print( "===> Total {} Frames ==> AVG EPE Error: {:.4f}, AVG Bad-{:.1f} Error: {:.4f}" .format(len(filelist), avg_err, self.args.threshold, avg_rate))