def __getitem__(self, index): left = self.left[index] right = self.right[index] disp_L = self.disp_L[index] left_img = self.loader(left) right_img = self.loader(right) dataL = self.dploader(disp_L) print(np.ascontiguousarray(dataL, dtype=np.float32)) exit() if self.training: w, h = left_img.size th, tw = 256, 512 x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) #dataL = np.ascontiguousarray(dataL,dtype=np.float32)/256 dataL = np.ascontiguousarray(dataL, dtype=np.float32) dataL = dataL[y1:y1 + th, x1:x1 + tw] processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) #print("left_img: ", left_img.shape) #print("right_img: ", right_img.shape) #print("dataL: ", dataL.shape) #exit() return left_img, right_img, dataL else: w, h = left_img.size left_img = left_img.crop((w - 1232, h - 368, w, h)) right_img = right_img.crop((w - 1232, h - 368, w, h)) w1, h1 = left_img.size dataL = dataL.crop((w - 1232, h - 368, w, h)) dataL = np.ascontiguousarray(dataL, dtype=np.float32) #dataL = np.ascontiguousarray(dataL,dtype=np.float32)/256 processed = preprocess.get_transform(augment=False) #processed = get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, dataL
def __getitem__(self, index): left = self.left[index] right = self.right[index] disp_L = self.disp_L[index] left_img = self.loader(left) right_img = self.loader(right) dataL = self.dploader(disp_L) # if index == 0: # disp_crop_L = np.asarray(dataL) # print("disp_crop_L.shape") # torch.Size([4, 256, 512]) # [batchsize, h, w] # print(disp_crop_L.shape) # if index == 0: # print(dataL) # dataL.show() if self.training: w, h = left_img.size th, tw = 256, 512 x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) dataL = np.ascontiguousarray(dataL, dtype=np.float32) / 256 dataL = dataL[y1:y1 + th, x1:x1 + tw] processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, dataL else: w, h = left_img.size left_img = left_img.crop((w - 1232, h - 368, w, h)) right_img = right_img.crop((w - 1232, h - 368, w, h)) w1, h1 = left_img.size dataL = dataL.crop((w - 1232, h - 368, w, h)) dataL = np.ascontiguousarray(dataL, dtype=np.float32) / 256 processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, dataL
def main(): for left_image_path, right_image_path in zip(test_left_img, test_right_img): imgL = np.asarray(Image.open(left_image_path)) imgR = np.asarray(Image.open(right_image_path)) processed = preprocess.get_transform() imgL = processed(imgL).numpy() imgR = processed(imgR).numpy() imgL = np.reshape(imgL, [1, 3, imgL.shape[1], imgL.shape[2]]) imgR = np.reshape(imgR, [1, 3, imgR.shape[1], imgR.shape[2]]) w = imgL.shape[3] h = imgL.shape[2] dw = int(args.downsample_scale - (w%args.downsample_scale + (w%args.downsample_scale==0)*args.downsample_scale)) dh = int(args.downsample_scale - (h%args.downsample_scale + (h%args.downsample_scale==0)*args.downsample_scale)) top_pad = dh left_pad = dw imgL = np.lib.pad(imgL, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) imgR = np.lib.pad(imgR, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) disparity = test(imgL, imgR) disparity = disparity[0, top_pad:, :-left_pad].data.cpu().numpy() skimage.io.imsave(os.path.join(args.save_dir, left_image_path.split('/') [-1]), (disparity * 256).astype('uint16')) logging.info("Disparity for {} generated at: {}".format(left_image_path, os.path.join(args.save_dir, left_image_path.split('/')[-1])))
def main(): processed = preprocess.get_transform(augment=False) for inx in range(len(test_left_img)): imgL_o = (skimage.io.imread(test_left_img[inx]).astype('float32')) imgR_o = (skimage.io.imread(test_right_img[inx]).astype('float32')) imgL = processed(imgL_o).numpy() imgR = processed(imgR_o).numpy() imgL = np.reshape(imgL, [1, 3, imgL.shape[1], imgL.shape[2]]) imgR = np.reshape(imgR, [1, 3, imgR.shape[1], imgR.shape[2]]) # pad to (384, 1248) if args.dataset == '2015': top_pad = 384 - imgL.shape[2] left_pad = 1248 - imgL.shape[3] else:# pad to (384, 1248) top_pad = 1984 - imgL.shape[2] left_pad = 2880 - imgL.shape[3] imgL = np.lib.pad(imgL, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) imgR = np.lib.pad(imgR, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) start_time = time.time() pred_disp = test(imgL, imgR) print('time = %.2f' % (time.time() - start_time)) top_pad = 384 - imgL_o.shape[0] left_pad = 1248 - imgL_o.shape[1] img = pred_disp[top_pad:, :-left_pad] skimage.io.imsave(test_left_img[inx].split('/')[-1], (img * 256).astype('uint16'))
def __getitem__(self, index): left = self.left[index] right = self.right[index] disp_L= self.disp_L[index] left_img = self.loader(left) right_img = self.loader(right) dataL = self.dploader(disp_L) if self.training: w, h = left_img.size th, tw = 256,544 x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) dataL = np.ascontiguousarray(dataL,dtype=np.float32)/256 dataL = dataL[y1:y1 + th, x1:x1 + tw] processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, dataL else: w, h = left_img.size left_img = left_img.crop((w-1216, h-320, w, h)) right_img = right_img.crop((w-1216, h-320, w, h)) dataL = dataL.crop((w-1216, h-320 , w, h)) dataL = np.ascontiguousarray(dataL,dtype=np.float32)/256 processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, dataL
def main(): processed = preprocess.get_transform(augment=False) gt_fold = '' left_fold = '' lidar2_raw = '' gt = [img for img in os.listdir(gt_fold)] image = [img for img in os.listdir(left_fold)] lidar2 = [img for img in os.listdir(lidar2_raw)] gt_test = [gt_fold + img for img in gt] left_test = [left_fold + img for img in image] sparse2_test = [lidar2_raw + img for img in lidar2] left_test.sort() sparse2_test.sort() gt_test.sort() time_all = 0.0 for inx in range(len(left_test)): print(inx) imgL_o = skimage.io.imread(left_test[inx]) imgL_o = np.reshape(imgL_o, [imgL_o.shape[0], imgL_o.shape[1], 3]) imgL = processed(imgL_o).numpy() imgL = np.reshape(imgL, [1, 3, imgL_o.shape[0], imgL_o.shape[1]]) gtruth = skimage.io.imread(gt_test[inx]).astype(np.float32) gtruth = gtruth * 1.0 / 256.0 sparse = skimage.io.imread(sparse2_test[inx]).astype(np.float32) sparse = sparse * 1.0 / 256.0 mask = np.where(sparse > 0.0, 1.0, 0.0) mask = np.reshape(mask, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = np.reshape(sparse, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = processed(sparse).numpy() sparse = np.reshape(sparse, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) mask = processed(mask).numpy() mask = np.reshape(mask, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) output1 = '' + left_test[inx].split('/')[-1] pred, time_temp = test(imgL, sparse, mask) pred = np.where(pred <= 0.0, 0.9, pred) time_all = time_all + time_temp print(time_temp) pred_show = pred * 256.0 pred_show = pred_show.astype('uint16') res_buffer = pred_show.tobytes() img = Image.new("I", pred_show.T.shape) img.frombytes(res_buffer, 'raw', "I;16") img.save(output1) print("time: %.8f" % (time_all * 1.0 / 1000.0))
def __getitem__(self, index): left = self.left[index] right = self.right[index] disp_L = self.disp_L[index] left_img = self.loader(left) right_img = self.loader(right) dataL = self.dploader(disp_L) if self.training: w, h = left_img.size th, tw = 256, 512 x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) dataL = dataL[y1:y1 + th, x1:x1 + tw] processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) else: w, h = left_img.size # left_img = left_img.crop((w - 1232, h - 368, w, h)) # right_img = right_img.crop((w - 1232, h - 368, w, h)) left_img = left_img.crop((w - 1200, h - 352, w, h)) right_img = right_img.crop((w - 1200, h - 352, w, h)) w1, h1 = left_img.size # dataL1 = dataL[h - 368:h, w - 1232:w] dataL = dataL[h - 352:h, w - 1200:w] processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) dataL = torch.from_numpy(dataL).float() return left_img, right_img, dataL
def __getitem__(self, index): left = self.left[index] right = self.right[index] disp_L = self.disp_L[index] left_img = self.loader(left) right_img = self.loader(right) dataL, scaleL = self.dploader(disp_L) dataL = np.ascontiguousarray(dataL, dtype=np.float32) if self.training: #w, h = left_img.size #th, tw = 256, 512 #print('liyang',w) #print('liyang',h) #x1 = random.randint(0, w - tw) #y1 = random.randint(0, h - th) #left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) #right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) #dataL = dataL[y1:y1 + th, x1:x1 + tw] processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) #w, h = left_img.size #print('liyang',w) #print('liyang',h) #print('liyang',left_img.shape) #print('liyang_imgL=',left_img) return left_img, right_img, dataL else: w, h = left_img.size left_img = left_img.crop((w - 960, h - 544, w, h)) right_img = right_img.crop((w - 960, h - 544, w, h)) processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, dataL
def __getitem__(self, index): left_img = self.left_images[index] right_img = self.right_images[index] left_disp = self.left_disparity[index] left_img = self.loader(left_img) right_img = self.loader(right_img) left_disp, left_scale = self.dploader(left_disp) left_disp = np.ascontiguousarray(left_disp, dtype=np.float32) if self.training: w, h = left_img.size th, tw = DEFAULT_TRAIN_IMAGE_HEIGHT, DEFAULT_TRAIN_IMAGE_WIDTH x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) left_disp = left_disp[y1:y1 + th, x1:x1 + tw] processed = preprocess.get_transform() left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, left_disp else: w, h = left_img.size dw = w + (self.downsample_scale - (w%self.downsample_scale + (w%self.downsample_scale==0)*self.downsample_scale)) dh = h + (self.downsample_scale - (h%self.downsample_scale + (h%self.downsample_scale==0)*self.downsample_scale)) left_img = left_img.crop((w - dw, h - dh, w, h)) right_img = right_img.crop((w - dw, h - dh, w, h)) processed = preprocess.get_transform() left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img, left_disp, dw-w, dh-h
def data_load(left_img_dir,right_img_dir): left_img=Image.open(left_img_dir).convert('RGB') right_img=Image.open(right_img_dir).convert('RGB') w, h = left_img.size th, tw = 256, 512 # 变为256,512 x1 = random.randint(0, w - tw) y1 = random.randint(0, h - th) left_img = left_img.crop((x1, y1, x1 + tw, y1 + th)) right_img = right_img.crop((x1, y1, x1 + tw, y1 + th)) # dataL = dataL.crop((w - 1232, h - 368, w, h)) # dataL = np.ascontiguousarray(dataL, dtype=np.float32) / 256 processed = preprocess.get_transform(augment=False) left_img = processed(left_img) right_img = processed(right_img) return left_img, right_img
def __getitem__(self, idx): Limg_name = self.files[self.split + 'left'][idx].rstrip() Rimg_name = os.path.join(self.rightimages_base, Limg_name.split(os.sep)[-2], os.path.basename(Limg_name)[ :-15] + "rightImg8bit.png", ) # self.files[self.split + 'right'][idx].rstrip() lbl_path = os.path.join( self.annotations_base, Limg_name.split(os.sep)[-2], os.path.basename(Limg_name)[:-15] + "gtFine_labelIds.png", ) disp_path = os.path.join(self.disp_base, Limg_name.split(os.sep)[-2], os.path.basename(Limg_name)[:-15] + "disparity.png", ) imgL = Image.open(Limg_name).convert('RGB') imgR = Image.open(Rimg_name).convert('RGB') w, h = imgL.size lbl = scipy.misc.imread(lbl_path) # original label size: 1024*2048 disp = Image.open(disp_path) disp = np.ascontiguousarray(disp, dtype=np.float32) / 256 top = random.randint(0, h - self.new_h) left = random.randint(0, w - self.new_w) imgL = imgL.crop((left, top, self.new_w + left, self.new_h + top)) imgR = imgR.crop((left, top, self.new_w + left, self.new_h + top)) lbl = lbl[top:top + self.new_h, left:left + self.new_w] label = self.encode_segmap(np.array(lbl, dtype=np.uint8)) disp = disp[top:top + self.new_h, left:left + self.new_w] processed = preprocess.get_transform(augment=False) imgL = processed(imgL) imgR = processed(imgR) label = torch.from_numpy(label.copy()).long() # int64,256,512,tensor,cpu disp = torch.from_numpy(disp.copy()).float() return imgL, imgR, label, disp,Limg_name
def main(): processed = preprocess.get_transform(augment=False) num_imgs = len(test_left_img) for inx in range(num_imgs): imgL_o = (skimage.io.imread(test_left_img[inx]).astype('float32')) imgR_o = (skimage.io.imread(test_right_img[inx]).astype('float32')) imgL = processed(imgL_o).numpy() imgR = processed(imgR_o).numpy() imgL = np.reshape(imgL, [1, 3, imgL.shape[1], imgL.shape[2]]) imgR = np.reshape(imgR, [1, 3, imgR.shape[1], imgR.shape[2]]) if 'KITTI' in args.dataset: # pad to (384, 1248) top_pad = 384 - imgL.shape[2] left_pad = 1248 - imgL.shape[3] imgL = np.lib.pad(imgL, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) imgR = np.lib.pad(imgR, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) else: # pad to (576, 960) top_pad = 576 - imgL.shape[2] left_pad = 960 - imgL.shape[3] imgL = np.lib.pad(imgL, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) imgR = np.lib.pad(imgR, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) start_time = time.time() pred_disp = test(imgL, imgR) print('processed %d/%d, time = %.2f' % (inx, num_imgs, time.time() - start_time)) if 'KITTI' in args.dataset: # crop to original size top_pad = 384 - imgL_o.shape[0] left_pad = 1248 - imgL_o.shape[1] img = pred_disp[top_pad:, :-left_pad] else: # crop to original size top_pad = 576 - imgL_o.shape[0] #left_pad = 960 - imgL_o.shape[1] img = pred_disp[top_pad:, :] #img = pred_disp path = os.path.join( *test_left_img[inx].replace("\\", "/").split('/')[:-2], 'disparity_psm') file = test_left_img[inx].replace("\\", "/").split('/')[-1] if not os.path.exists(path): os.makedirs(path) skimage.io.imsave(os.path.join(path, file), (img * 256).astype('uint16'))
def main(): processed = preprocess.get_transform(augment=False) print("Start Testing!!!") # print(args.pad) total_time = 0 for inx in tqdm(range(len(test_up_img))): # read grey scale if args.real: imgU_o = np.tile( skimage.io.imread(test_up_img[inx], as_grey=True)[:, :, np.newaxis], (1, 1, 3)) * 255 imgD_o = np.tile( skimage.io.imread(test_down_img[inx], as_grey=True)[:, :, np.newaxis], (1, 1, 3)) * 255 else: imgU_o = (skimage.io.imread(test_up_img[inx])) imgD_o = (skimage.io.imread(test_down_img[inx])) # concatenate polar angle as equirectangular information -------- imgU_o = np.concatenate([imgU_o, equi_info], 2) imgD_o = np.concatenate([imgD_o, equi_info], 2) # Real World / Synthetic preprocessing -------------------------- if args.real: compose_trans = transforms.Compose([transforms.ToTensor()]) imgU = compose_trans(imgU_o).numpy() imgD = compose_trans(imgD_o).numpy() imgU = np.reshape(imgU, [1, 4, imgU.shape[1], imgU.shape[2]]) imgD = np.reshape(imgD, [1, 4, imgD.shape[1], imgD.shape[2]]) else: imgU = processed(imgU_o).numpy() imgD = processed(imgD_o).numpy() imgU = np.reshape(imgU, [1, 4, imgU.shape[1], imgU.shape[2]]) imgD = np.reshape(imgD, [1, 4, imgD.shape[1], imgD.shape[2]]) # Wide padding ----------------------------- LR_pad = 32 imgU = np.lib.pad(imgU, ((0, 0), (0, 0), (0, 0), (LR_pad, LR_pad)), mode='wrap') imgD = np.lib.pad(imgD, ((0, 0), (0, 0), (0, 0), (LR_pad, LR_pad)), mode='wrap') # Testing and count time ------------------- start_time = time.time() pred_disp = test(imgU, imgD) total_time += (time.time() - start_time) img = pred_disp[:, LR_pad:-LR_pad] # Save output ------------------------------ if args.outfile[-1] == '/': args.outfile = args.outfile[:-1] os.system('mkdir -p %s' % args.outfile) np.save( args.outfile + '/' + test_up_img[inx].split('/')[-1][:-4] + '.npy', img) # ------------------------------------------- # Print Total Time print("Total time: ", total_time, "Average time: ", total_time / len(test_up_img))
def main(): processed = preprocess.get_transform(augment=False) gt_fold = os.path.join('/home', 'tmt', 'CV_data', 'selection', 'depth_selection', 'val_selection_cropped', 'groundtruth_depth') left_fold = os.path.join('/home', 'tmt', 'CV_data', 'selection', 'depth_selection', 'val_selection_cropped', 'image') lidar2_raw = os.path.join('/home', 'tmt', 'CV_data', 'selection', 'depth_selection', 'val_selection_cropped', 'velodyne_raw') gt = [img for img in os.listdir(gt_fold)] # list of filenames in gt_fold image = [img for img in os.listdir(left_fold)] # list of filenames in image lidar2 = [img for img in os.listdir(lidar2_raw) ] # list of filenames in velodyne_raw gt_test = [os.path.join(gt_fold, img) for img in gt] # list of path to grundtruth files left_test = [os.path.join(left_fold, img) for img in image] # list of path to image files sparse2_test = [os.path.join(lidar2_raw, img) for img in lidar2] # list of path to velodyne_raw files left_test.sort() # RGB image file paths sparse2_test.sort() # LiDar file paths gt_test.sort() # Groudtruch file paths time_all = 0.0 test_image_paths = left_test num_testing_image = len( test_image_paths ) if args.num_testing_image == -1 else args.num_testing_image running_error = 0 pbar = tqdm(range(num_testing_image)) for idx in pbar: # process RGB image imgL_o = skimage.io.imread( test_image_paths[idx]) # (W, H, C) = (352, 1216, 3) imgL_o = np.reshape(imgL_o, [imgL_o.shape[0], imgL_o.shape[1], 3]) imgL = processed(imgL_o).numpy() imgL = np.reshape( imgL, [1, 3, imgL_o.shape[0], imgL_o.shape[1]]) #(3, 352, 1216) # process groundtruth image gtruth = skimage.io.imread(gt_test[idx]).astype(np.float32) gtruth = gtruth * 1.0 / 256.0 # process LiDAR image and mask sparse = skimage.io.imread(sparse2_test[idx]).astype( np.float32) # Lidar input sparse = sparse * 1.0 / 256.0 mask = np.where( sparse > 0.0, 1.0, 0.0) # if element > 0, assign element in mask as 1, else 0 mask = np.reshape(mask, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = np.reshape(sparse, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = processed(sparse).numpy() sparse = np.reshape(sparse, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) mask = processed(mask).numpy() mask = np.reshape(mask, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) # variables related to saved file path filename = os.path.basename(test_image_paths[idx]) saved_path = os.path.join(SAVED_DIR, filename) if not os.path.exists(SAVED_DIR): os.mkdir(SAVED_DIR) # run the model pred, testing_time = test(imgL, sparse, mask) pred = np.where(pred <= 0.0, 0.9, pred) time_all = time_all + testing_time running_error += rmse(gtruth, pred, 0.0) * 1000 mean_error = running_error / (idx + 1) pbar.set_description('Mean error: {:.4f}'.format(mean_error)) # to save image pred_show = pred * 256.0 pred_show = pred_show.astype('uint16') res_buffer = pred_show.tobytes() img = Image.new("I", pred_show.T.shape) img.frombytes(res_buffer, 'raw', "I;16") img.save(saved_path) pbar.set_postfix(mean_error=mean_error) print("time: %.8f" % (time_all * 1.0 / 1000.0))
def main(): processed = preprocess.get_transform(augment=False) m_rmse = 0 m_irmse = 0 m_mae = 0 m_imae = 0 m_absrel = 0 # gt_fold = '' # left_fold = '' # lidar2_raw ='' root = '/nfs-data/zhengk_data/kitti_depth/val_selection_cropped' gt_fold = root + '/groundtruth_depth/' left_fold = root + '/image/' lidar2_raw = root + '/velodyne_raw/' gt = [img for img in os.listdir(gt_fold)] image = [img for img in os.listdir(left_fold)] lidar2 = [img for img in os.listdir(lidar2_raw)] gt_test = [gt_fold + img for img in gt] left_test = [left_fold + img for img in image] sparse2_test = [lidar2_raw + img for img in lidar2] left_test.sort() sparse2_test.sort() gt_test.sort() time_all = 0.0 for inx in range(len(left_test)): # print(inx) imgL_o = skimage.io.imread(left_test[inx]) # skimage.io.imsave(inp, imgL_o) im = cv2.imread(left_test[inx]) imgL_o = np.reshape(imgL_o, [imgL_o.shape[0], imgL_o.shape[1], 3]) imgL = processed(imgL_o).numpy() imgL = np.reshape(imgL, [1, 3, imgL_o.shape[0], imgL_o.shape[1]]) gtruth = skimage.io.imread(gt_test[inx]).astype(np.float32) gtruth = gtruth * 1.0 / 256.0 sparse = skimage.io.imread(sparse2_test[inx]).astype(np.float32) sparse = sparse * 1.0 / 256.0 ''' gt = cv2.imread(gt_test[inx]) sp = cv2.imread(sparse2_test[inx]) com = np.vstack((im, gt, sp)) cv2.imshow('dd', com) cv2.waitKey(0) ''' mask = np.where(sparse > 0.0, 1.0, 0.0) mask = np.reshape(mask, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = np.reshape(sparse, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = processed(sparse).numpy() sparse = np.reshape(sparse, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) mask = processed(mask).numpy() mask = np.reshape(mask, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) output1 = '/home/zhengk/DeepLiDAR/output/' + left_test[inx].split( '/')[-1] pred, time_temp = test(imgL, sparse, mask) pred = np.where(pred <= 0.0, 0.9, pred) time_all = time_all + time_temp # print(time_temp) ''' pred_show = pred * 256.0 pred_show = pred_show.astype('uint16') res_buffer = pred_show.tobytes() img = Image.new("I",pred_show.T.shape) img.frombytes(res_buffer,'raw',"I;16") img.save(output1) ''' r = rmse(gtruth, pred) ir = irmse(gtruth, pred) m = mae(gtruth, pred) im = imae(gtruth, pred) a = absrel(gtruth, pred) m_rmse += r m_irmse += ir m_mae += m m_imae += im m_absrel += a # print(inx, " rmse: ",r, " absrel: ", a) print(inx) m_rmse /= len(left_test) m_irmse /= len(left_test) m_mae /= len(left_test) m_imae /= len(left_test) m_absrel /= len(left_test) print("***** average_rmse: ", m_rmse) print("***** average_irmse: ", m_irmse) print("***** average_mae: ", m_mae) print("***** average_imae: ", m_imae) print("***** average_absrel: ", m_absrel) print("time: %.8f" % (time_all * 1.0 / 1000.0))
def main(): name_save_idx = 0 print('POINT 0.0') #for left_image_path, right_image_path in zip(test_left_img, test_right_img): #for left_image_path, right_image_path in zip(image_2, image_3): for i in range(2, 3): name_save_idx += 1 #left_img_path = './unity_chair/rgb_chair_left/' #right_img_path = './unity_chair/rgb_chair_right/' left_img_path = './data/unity_chair/rgb_chair_left/' + '{:04d}'.format( i) + '.jpg' #CHECK TO CHANGE B4 RUNNING print('i:', i) right_img_path = './data/unity_chair/rgb_chair_right/' + '{:04d}'.format( i) + '.jpg' #CHECK TO CHANGE B4 RUNNING #imgL = cv2.imread(image_L_path,0) #imgR = cv2.imread(image_R_path,0) imgL = cv2.imread(left_img_path) imgR = cv2.imread(right_img_path) print('POINT 0.1') #imgL = np.asarray(Image.open(left_image_path)) #imgR = np.asarray(Image.open(right_image_path)) print('imgL:', imgL) print('imgR:', imgR) print('POINT 0.2') #print('IMGL SHAPE:',imgL.shape) #iprint('IMGR SHAPE:',imgR.shape) processed = preprocess.get_transform() print('POINT 1') imgL = processed(imgL).numpy() imgR = processed(imgR).numpy() print('IMG 1', imgL) print('IMGR 1', imgR) imgL = np.reshape(imgL, [1, 3, imgL.shape[1], imgL.shape[2]]) imgR = np.reshape(imgR, [1, 3, imgR.shape[1], imgR.shape[2]]) print('IMGL 2', imgL) print('IMGR 2', imgR) w = imgL.shape[3] h = imgL.shape[2] dw = int(args.downsample_scale - (w % args.downsample_scale + (w % args.downsample_scale == 0) * args.downsample_scale)) dh = int(args.downsample_scale - (h % args.downsample_scale + (h % args.downsample_scale == 0) * args.downsample_scale)) top_pad = dh left_pad = dw imgL = np.lib.pad(imgL, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) imgR = np.lib.pad(imgR, ((0, 0), (0, 0), (top_pad, 0), (0, left_pad)), mode='constant', constant_values=0) disparity = test(imgL, imgR) print('POINT 0') print('DISPARITY:', disparity) print('DISPARITY TYPE:', type(disparity)) print('DISPARITY SHAPE:', disparity.shape) #disparity = list(disparity) disparity = disparity.cpu().numpy() #disparity = np.array(disparity) print(disparity) print('DISPARITY SHAPE:', disparity.shape) print('TOP PAD:', top_pad) print('LEFT_PAD:', left_pad) #disparity = disparity[0, top_pad:, :-left_pad].data.cpu().numpy() print('POINT 1') print('NEED TO FIX ISSUE BELOW') print('DISPARITY AFTER PAD:', disparity) print(args.save_dir) #print(left_img_path.split(('/')[-1]), (disparity * 256).astype('uint16')) print('DISPARITY AFTER SPLIT:', disparity) #skimage.io.imsave(os.path.join(args.save_dir, left_img_path.split(('/')[-1]), (disparity * 256).astype('uint16'))) print('POINT 3') filename = 'unity_chair_estimated_disparity_pair_dp_' + '{:04d}'.format( name_save_idx) + '.png' #disparity = np.uint16(disparity) print('POINT 4') #img = Image.fromarray(disparity_comb, 'RGB') #i#img.save(filename) ('POINT 5') #plt.imsave('test.png',(disparity*256).astype('uint16')) cv2.imwrite(filename, disparity[0])
def main(): processed = preprocess.get_transform(augment=False) root = '/nfs-data/zhengk_data/kitti_depth/val_selection_cropped' gt_fold = root + '/groundtruth_depth/' left_fold = root + '/image/' lidar2_raw = root + '/velodyne_raw/' gt = [img for img in os.listdir(gt_fold)] image = [img for img in os.listdir(left_fold)] lidar2 = [img for img in os.listdir(lidar2_raw)] gt_test = [gt_fold + img for img in gt] left_test = [left_fold + img for img in image] sparse2_test = [lidar2_raw + img for img in lidar2] left_test.sort() sparse2_test.sort() gt_test.sort() time_all = 0.0 for inx in range(len(left_test)): # print(inx) imgL_o = skimage.io.imread(left_test[inx]) # skimage.io.imsave(inp, imgL_o) im = cv2.imread(left_test[inx]) imgL_o = np.reshape(imgL_o, [imgL_o.shape[0], imgL_o.shape[1], 3]) imgL = processed(imgL_o).numpy() imgL = np.reshape(imgL, [1, 3, imgL_o.shape[0], imgL_o.shape[1]]) gtruth = skimage.io.imread(gt_test[inx]).astype(np.float32) gtruth = gtruth * 1.0 / 256.0 sparse = skimage.io.imread(sparse2_test[inx]).astype(np.float32) sparse = sparse * 1.0 / 256.0 mask = np.where(sparse > 0.0, 1.0, 0.0) mask = np.reshape(mask, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = np.reshape(sparse, [imgL_o.shape[0], imgL_o.shape[1], 1]) sparse = processed(sparse).numpy() sparse = np.reshape(sparse, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) mask = processed(mask).numpy() mask = np.reshape(mask, [1, 1, imgL_o.shape[0], imgL_o.shape[1]]) output1 = '/home/zhengk/DeepLiDAR/my_output/' + left_test[inx].split( '/')[-1] pred, time_temp, confidence_mask = test(imgL, sparse, mask) print(type(pred), type(confidence_mask)) pred = np.where(pred <= 0.0, 0.9, pred) time_all = time_all + time_temp # print(time_temp) pred_show = pred * 256.0 pred_show = pred_show.astype('uint16') res_buffer = pred_show.tobytes() img = Image.new("I", pred_show.T.shape) img.frombytes(res_buffer, 'raw', "I;16") img.save(output1) ''' # show result compare one by one print(inx) # show color_lidar/confidence mask/pred sp = cv2.imread(sparse2_test[inx], -1) sp = sp * 1.0/256.0 #pprint.pprint(sp) #pprint.pprint(pred) #pprint.pprint(confidence_mask) #print("sp: ", sp.shape) #print("confidence_mask: ", confidence_mask.shape) #print("pred: ", pred.shape) c_sp = kitti_disp_to_color(sp, max_disp=100) c_pred = kitti_disp_to_color(pred, max_disp=100) # c_mask = confidence_mask.reshape(confidence_mask.shape[0], confidence_mask.shape[1], 1) c_mask = np.expand_dims(confidence_mask, axis=2) c_mask = cv2.cvtColor(c_mask, cv2.COLOR_GRAY2RGB) #pprint.pprint(c_pred) #pprint.pprint(c_mask) #print("rgb: ", im.shape) #print("c_sp: ", c_sp.shape) #print("c_mask: ", c_mask.shape) #print("c_pred: ", c_pred.shape) # com = np.hstack((c_sp, c_mask, c_pred)) # cv2.imshow('dd', com) #cv2.imshow("rgb ", im) #cv2.imshow('c_sp', c_sp) #cv2.imshow('confidence', c_mask) #cv2.imshow('c_pred', c_pred) #cv2.waitKey(0) ''' print("time: %.8f" % (time_all * 1.0 / 1000.0))
def __getitem__(self, idx): path = self.files[self.split+'left'][idx].rstrip() Limg_path = os.path.join(self.leftimages_base, path) Rimg_path = os.path.join(self.rightimages_base, path) lbl_path = os.path.join(self.annotations_base, path) """ #disp_path = os.path.join(self.disp_base, path) imgL = scipy.misc.imread(Limg_path) imgL = np.array(imgL, dtype=np.uint8) imgR = scipy.misc.imread(Rimg_path) imgR = np.array(imgR, dtype=np.uint8) lbl = scipy.misc.imread(lbl_path) # original label size: 1024*2048 #disp = Image.open(disp_path) #disp = np.ascontiguousarray(disp, dtype=np.float32) / 256. if random.random() < self.flip_rate: imgL = np.fliplr(imgL) imgR = np.fliplr(imgR) #disp = np.fliplr(disp) lbl = np.fliplr(lbl) h, w, _ = imgL.shape top = random.randint(0, h - self.new_h) left = random.randint(0, w - self.new_w) imgL = imgL[top:top + self.new_h, left:left + self.new_w] imgR = imgR[top:top + self.new_h, left:left + self.new_w] lbl = lbl[top:top + self.new_h, left:left + self.new_w] label = self.encode_segmap(np.array(lbl, dtype=np.uint8)) #disp = disp[top:top + self.new_h, left:left + self.new_w] # reduce mean if self.saliency_eval_depth == False: imgL = imgL[:, :, ::-1] # switch to BGR imgR = imgR[:, :, ::-1] # switch to BGR #imgL = np.transpose(imgL, (2, 0, 1)) / 255. #imgR = np.transpose(imgR, (2, 0, 1)) / 255. imgL = imgL.astype(np.float64) imgR = imgR.astype(np.float64) if self.saliency_eval_depth == False: imgL = imgL.astype(float) / 255.0 imgR = imgR.astype(float) / 255.0 else: imgL = ((imgL / 255 - 0.5) / 0.5) imgR = ((imgR / 255 - 0.5) / 0.5) imgL = imgL.transpose(2, 0, 1) # NHWC -> NCHW [3, h, w] imgR = imgR.transpose(2, 0, 1) # NHWC -> NCHW [3, h, w] # convert to tensor imgL = torch.from_numpy(imgL.copy()).float()#.copy() imgR = torch.from_numpy(imgR.copy()).float()#torch.from_numpy(imgR.copy()).float() label= torch.from_numpy(label.copy()).long()#int64,256,512,tensor,cpu #disp = torch.from_numpy(disp.copy()).float() """ imgL = Image.open(Limg_path).convert('RGB') w, h = imgL.size lbl = scipy.misc.imread(lbl_path) # original label size: 1024*2048 top = random.randint(0, h - self.new_h) left = random.randint(0, w - self.new_w) imgL = imgL.crop((left, top, self.new_w + left, self.new_h + top)) lbl = lbl[top:top + self.new_h, left:left + self.new_w] label = self.encode_segmap(np.array(lbl, dtype=np.uint8)) processed = preprocess.get_transform(augment=False) imgL = processed(imgL) label = torch.from_numpy(label.copy()).long() # int64,256,512,tensor,cpu return imgL,label #,disp