import os import torch from torch.utils.data import DataLoader from torch.utils.data import Dataset class OpenFWI(Dataset): def __init__(self, root_dir='CurveVelA', dataset_type='train'): """ OpenFWI数据集 :param dataset_name: ['FlatVelA', 'FlatVelB', 'CurveVelA', 'CurveVelB'] :param dataset_type: ['train', 'val', 'test'] """ self.root_dir = root_dir self.dataset_type = dataset_type self.data_dir = os.path.join(root_dir, dataset_type) self.model_files = sorted([os.path.join(self.data_dir, 'model', f) for f in os.listdir( os.path.join(self.data_dir, 'model')) if f.endswith('.pt')]) self.time_vel_files = sorted([os.path.join(self.data_dir, 'time_vel', f) for f in os.listdir( os.path.join(self.data_dir, 'time_vel')) if f.endswith('.pt')]) self.migrate_files = sorted([os.path.join(self.data_dir, 'migrate', f) for f in os.listdir( os.path.join(self.data_dir, 'migrate')) if f.endswith('.pt')]) self.well_log_files = sorted([os.path.join(self.data_dir, 'well_log', f) for f in os.listdir( os.path.join(self.data_dir, 'well_log')) if f.endswith('.pt')]) self.horizens_files = sorted([os.path.join(self.data_dir, 'horizens', f) for f in os.listdir( os.path.join(self.data_dir, 'horizens')) if f.endswith('.pt')]) def __len__(self): return len(self.model_files) def __getitem__(self, idx): model_file = self.model_files[idx] time_vel_file = self.time_vel_files[idx] migrate_file = self.migrate_files[idx] well_log_file = self.well_log_files[idx] horizens_file = self.horizens_files[idx] model_file_tensor = torch.load(model_file) time_velocity_tensor = torch.load(time_vel_file) migrate_file_tensor = torch.load(migrate_file) well_log_tensor = torch.load(well_log_file) horizons_model_tensor = torch.load(horizens_file) return { 'migrate': migrate_file_tensor, # 叠后偏移成像 (batch,1, 1000, 70) 'time_vel': time_velocity_tensor, # 时间域速度模型 (batch, 1000, 70) 'model': model_file_tensor, # 深度域速度模型 (batch, 70, 70) 'well_log': well_log_tensor, # 测井 (batch, 70, 70) 'horizens': horizons_model_tensor # 层位 (batch, 70, 70) } @staticmethod def collate_fn(batch): """ 将大小为batch的字典列表转换为字典的大小为batch的列表 eg: {'a':}[batch] -> {'a':[batch]} 在dataloader中使用,指定collate_fn=dataset.collate_fn :param batch: :return: """ keys = batch[0].keys() result = {key: torch.stack([item[key] for item in batch]) for key in keys} return result @staticmethod def collate_fn_tolist(batch): """ 将大小为batch的字典列表转为大小为5的tensor[batch, ...] 在dataloader中使用,指定collate_fn=dataset.collate_fn_tolist :param batch: :return: [5, batch, ...] """ well_log_list = [] horizons_model_list = [] migrate_file_list = [] time_velocity_list = [] model_file_list = [] for sample in batch: well_log_list.append(sample['well_log']) horizons_model_list.append(sample['horizens']) migrate_file_list.append(sample['migrate']) time_velocity_list.append(sample['time_vel']) model_file_list.append(sample['model']) well_log_tensor = torch.stack(well_log_list) horizons_model_tensor = torch.stack(horizons_model_list) migrate_file_tensor = torch.stack(migrate_file_list) time_velocity_tensor = torch.stack(time_velocity_list) model_file_tensor = torch.stack(model_file_list) result = [well_log_tensor, # 测井 horizons_model_tensor, # 层位 migrate_file_tensor, # 叠后偏移成像 time_velocity_tensor, # 时间域速度模型 model_file_tensor] # 深度域速度模型 return result def test1(): # 创建数据集实例 dataset = OpenFWI('./CurveVelA', 'test') # 创建 DataLoader 实例 dataloader = DataLoader(dataset, batch_size=4, shuffle=False, collate_fn=dataset.collate_fn) # 获取第一个批次的数据 for batch in dataloader: print(f"叠后偏移成像: {batch['migrate'].shape}") # [4, 1, 1801, 291] print(f"时间域速度模型: {batch['time_vel'].shape}") # [4, 1, 1801, 301] print(f"深度域速度模型: {batch['model'].shape}") # [4, 1, 201, 301] print(f"测井:{batch['well_log'].shape}") # [4, 1, 201, 301] print(f"层位:{batch['horizens'].shape}") # [4, 1, 201, 301] print(f'数据集样本数:{dataset.__len__()}') break def test2(): dataset = OpenFWI('./CurveVelA', 'test') dataloader = DataLoader(dataset, batch_size=4, shuffle=False, collate_fn=dataset.collate_fn_tolist) # 返回值转为列表 collate_fn=dataset.collate_fn_tolist for b in dataloader: # [batch, 5, ...] print(f"测井:{b[0].shape}") # [1, 201, 301] print(f"层位:{b[1].shape}") # [1, 201, 301] print(f"叠后偏移成像: {b[2].shape}") # [1, 1801, 291] print(f"时间域速度模型: {b[3].shape}") # [1, 1801, 301] print(f"深度域速度模型: {b[4].shape}") # [1, 201, 301] break print(f'数据集样本数:{dataset.__len__()}') if __name__ == "__main__": test2()