def __getitem__(self, i): """ idx must be between 0 to len-1 assuming flow[0] contains flow in x direction and flow[1] contains flow in y """ image_path1 = self.total_image[i][0] image_path2 = self.total_image[i][1] backward_flow_path = self.total_flow[i] consistency_path = self.total_consistency[i] # print('image_path is ',image_path1) # print('backward_flow_path ',backward_flow_path) # print('consistency_path ',consistency_path) img1 = Image.open(image_path1) img2 = Image.open(image_path2) img1 = self.transform(img1) img2 = self.transform(img2) # mask is numpy and shape is HXWX3 mask = read_weights_file(consistency_path) mask = torch.from_numpy(mask).permute(2, 0, 1) flow = read(backward_flow_path) originalflow = torch.from_numpy(flow) # img_flow = Image.fromarray(np.uint8(flow)) flow = torch.from_numpy(flow).permute(2, 0, 1).float() # this is for transpose. flow[0, :, :] *= float(flow.shape[1]) / originalflow.shape[1] flow[1, :, :] *= float(flow.shape[2]) / originalflow.shape[2] # [3,400,640] [3,400,640] [3,400,640],[ 2, 400, 640] return (img1, img2, flow, mask)
def __getitem__(self, i): """ idx must be between 0 to len-1 assuming flow[0] contains flow in x direction and flow[1] contains flow in y """ image_path1 = self.imagelist[i] image_path2 = self.imagelist[i + 1] backward_flow_path = self.flowlist[i] consistency_path = self.consistencylist[i] img1 = Image.open(os.path.join(self.image_path, image_path1)) img2 = Image.open(os.path.join(self.image_path, image_path2)) img1 = self.transform(img1) img2 = self.transform(img2) # mask is numpy and shape is HXWX3 mask = read_weights_file( os.path.join(self.consistency_path, consistency_path)) mask = torch.from_numpy(mask).permute(2, 0, 1).unsqueeze(0) flow = read(os.path.join(self.flow_path, backward_flow_path)) originalflow = torch.from_numpy(flow) # img_flow = Image.fromarray(np.uint8(flow)) flow = torch.from_numpy(flow).to(device).permute(2, 0, 1).float() # this is for transpose. flow[0, :, :] *= float(flow.shape[1]) / originalflow.shape[1] flow[1, :, :] *= float(flow.shape[2]) / originalflow.shape[2] print('It now processes image {} !!!'.format( os.path.join(self.image_path, image_path1))) return (img1, img2, mask, flow)
def __getitem__(self, idx): """ idx must be between 0 to len-1 assuming flow[0] contains flow in x direction and flow[1] contains flow in y """ num = toString7(idx) img1 = io.imread(self.path + num + "-img_0.png") img2 = io.imread(self.path + num + "-img_1.png") mask = io.imread(self.path + num + "-mb_01.png") img1 = torch.from_numpy(transform.resize( img1, (360, 640))).to(device).permute(2, 0, 1).float() img2 = torch.from_numpy(transform.resize( img2, (360, 640))).to(device).permute(2, 0, 1).float() mask = torch.from_numpy(transform.resize( mask, (360, 640))).to(device).float() flow = read(self.path + num + "-flow_01.flo") # bilinear interpolation is default originalflow = torch.from_numpy(flow) flow = torch.from_numpy(transform.resize( flow, (360, 640))).to(device).permute(2, 0, 1).float() flow[0, :, :] *= float(flow.shape[1]) / originalflow.shape[1] flow[1, :, :] *= float(flow.shape[2]) / originalflow.shape[2] if self.transform: # complete later pass #IMG2 should be at t in IMG1 is at T-1 return (img1, img2, mask, flow)
def _load_cache(self, ): subdirs = os.listdir(self.flow_dir) subdirs.sort() for i, sub in enumerate(subdirs): # for i, sub in enumerate(subdirs[0:2]): frame_dir = os.path.join(self.rgb_dir, sub) flow_dir = os.path.join(self.flow_dir, sub) occlusion_dir = os.path.join(self.occlusion_dir, sub) num_of_thissub = len(os.listdir(flow_dir)) assert (len(os.listdir(occlusion_dir)) == num_of_thissub) assert (len(os.listdir(frame_dir)) == (num_of_thissub + 1)) for j in range(num_of_thissub): try: rgb1_fn = os.path.join(frame_dir, 'frame_%.4d.png' % (j + 1)) rgb2_fn = os.path.join(frame_dir, 'frame_%.4d.png' % (j + 2)) flow_fn = os.path.join(flow_dir, 'frame_%.4d.flo' % (j + 1)) occlusion_fn = os.path.join(occlusion_dir, 'frame_%.4d.png' % (j + 1)) # rgb image rgb1 = io.imread(rgb1_fn) rgb2 = io.imread(rgb2_fn) rgb1 = transform.resize(rgb1, (self.wh[1], self.wh[0]), order=3) rgb2 = transform.resize(rgb2, (self.wh[1], self.wh[0]), order=3) # mask # mask = io.imread(occlusion_fn, as_gray=True) # mask = transform.resize(mask, (self.wh[1], self.wh[0])).astype(np.float32)/255 # print(mask.max()) # mask = io.imread(occlusion_fn, as_gray=True) # mask = transform.resize(mask, (self.wh[1], self.wh[0]), order=3).astype(np.float32) # # mask = (1-mask/255.0) mask = cv2.imread(occlusion_fn, 0) mask = cv2.resize(mask, (self.wh[0], self.wh[1])).astype( np.float32) / 255.0 mask = 1 - mask # flow flow = read(flow_fn) flow = transform.resize(flow, (self.wh[1], self.wh[0])) flow[:, :, 0] *= self.wh[1] / flow.shape[0] flow[:, :, 1] *= self.wh[0] / flow.shape[1] except Exception as e: print('%s during add frame_.4%d' % (str(e), j + 1)) continue self.rgb_list.append( (rgb1.astype(np.float32), rgb2.astype(np.float32))) self.occulusion_list.append(mask.astype(np.float32)) self.flow_list.append(flow.astype(np.float32)) print('MPI ViedoSeq:%s added with %d tuples' % (sub, num_of_thissub))
def __getitem__(self, idx): frame = io.imread(self.frames_path + self.frames_list[idx]) / 255.0 if idx == 0: flow = np.zeros(frame.shape[:2] + (2, )) mask = np.zeros(frame.shape[:2] + (1, )) else: flow = read(self.flows_path + self.flows_list[idx - 1]) mask = io.imread(self.masks_path + self.masks_list[idx - 1]) / 255.0 mask = 1.0 - mask.reshape(mask.shape + (1, )) offset = 5 if idx - offset < 0 or idx == self.length - 1: lt_flow = [] lt_mask = [] else: #print(self.frames_path + self.frames_list[idx]) #print(self.fc5_path + self.lt_data_list[idx-offset]) data = np.load(self.fc5_path + self.lt_data_list[idx - offset], allow_pickle=True) #print(data.shape) lt_flow = data[0, :, :, :2] lt_mask = data[0, :, :, 2] lt_mask = lt_mask.reshape(lt_mask.shape + (1, )) lt_flow = torch.from_numpy(lt_flow).to(device).permute(2, 0, 1).float() lt_mask = torch.from_numpy(lt_mask).to(device).permute(2, 0, 1).float() #print(lt_flow.shape) #print(lt_mask.shape) #blah #print(frame.shape) #print(flow.shape) #print(mask.shape) frame = torch.from_numpy(frame).to(device).permute(2, 0, 1).float() flow = torch.from_numpy(flow).to(device).permute(2, 0, 1).float() mask = torch.from_numpy(mask).to(device).permute(2, 0, 1).float() return (frame, mask, flow, [lt_flow, lt_mask])
def __getitem__(self, idx): """ idx must be between 0 to len-1 assuming flow[0] contains flow in x direction and flow[1] contains flow in y """ for i in range(0, len(self.numlist)): folder = self.dirlist[i] path = self.path + "clean/" + folder + "/" occpath = self.path + "occlusions/" + folder + "/" flowpath = self.path + "flow/" + folder + "/" if (idx < (self.numlist[i] - 1)): num1 = toString4(idx + 1) num2 = toString4(idx + 2) img1 = io.imread(path + "frame_" + num1 + ".png") img2 = io.imread(path + "frame_" + num2 + ".png") mask = io.imread(occpath + "frame_" + num1 + ".png") img1 = torch.from_numpy(transform.resize( img1, (360, 640))).to(device).permute(2, 0, 1).float() img2 = torch.from_numpy(transform.resize( img2, (360, 640))).to(device).permute(2, 0, 1).float() mask = torch.from_numpy(transform.resize( mask, (360, 640))).to(device).float() flow = read(flowpath + "frame_" + num1 + ".flo") # bilinear interpolation is default originalflow = torch.from_numpy(flow) flow = torch.from_numpy(transform.resize( flow, (360, 640))).to(device).permute(2, 0, 1).float() flow[0, :, :] *= float(flow.shape[1]) / originalflow.shape[1] flow[1, :, :] *= float(flow.shape[2]) / originalflow.shape[2] # print(flow.shape) #y,x,2 # print(img1.shape) break idx -= self.numlist[i] - 1 if self.transform: # complete later pass #IMG2 should be at t in IMG1 is at T-1 return (img1, img2, mask, flow)