-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gan_DataSet.py
116 lines (99 loc) · 5.31 KB
/
Gan_DataSet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
@File : Gan_DataSet.py
@Author : Pesion
@Date : 2023/11/1
@Desc :
"""
import json
import os
import numpy as np
import scipy.io as scio
import torch
from PIL import Image
from torch.utils.data.dataset import Dataset
import matplotlib.pyplot as plt
import yaml
class Marmousi2FromMat(Dataset):
def __init__(self, data_path, traces, train_traces: dict):
super(Marmousi2FromMat, self).__init__()
self.Train_Data = scio.loadmat(data_path)
self.traces = traces
self.start = train_traces['start'] if train_traces['start'] is not None else 0
self.stop = train_traces['stop'] if train_traces['stop'] is not None else self.traces
self.step = train_traces['step'] if train_traces['step'] is not None else 1
def __len__(self):
return int((self.stop - self.start) / self.step)
def __getitem__(self, index): # torch.tensor改为torch.from_numpy
# 这里的vp vs rho 大小为[layers, traces]
# 三参数标签
index = index*self.step + self.start
vp_label = torch.tensor(self.Train_Data['vp'][..., index], dtype=torch.float32)
vs_label = torch.tensor(self.Train_Data['vs'][..., index], dtype=torch.float32)
den_label = torch.tensor(self.Train_Data['rho'][..., index], dtype=torch.float32)
M_sample = torch.stack([vp_label, vs_label, den_label], dim=0)
# 地震数据 -> [Trace, 3, input_len]
Seis_low = torch.tensor(self.Train_Data['S_low'][index], dtype=torch.float32)
Seis_mid = torch.tensor(self.Train_Data['S_mid'][index], dtype=torch.float32)
Seis_large = torch.tensor(self.Train_Data['S_large'][index], dtype=torch.float32)
S_sample = torch.stack([Seis_low, Seis_mid, Seis_large], dim=0)
# 低频三参数数据 -> [Trace, 3, input_len]
vp_sample_initial = torch.tensor(self.Train_Data['vp_low'][..., index], dtype=torch.float32)
vs_sample_initial = torch.tensor(self.Train_Data['vs_low'][..., index], dtype=torch.float32)
den_sample_initial = torch.tensor(self.Train_Data['rho_low'][..., index], dtype=torch.float32)
M_sample_initial = torch.stack([vp_sample_initial, vs_sample_initial, den_sample_initial], dim=0)
return M_sample, S_sample, M_sample_initial
class WGAN_Data(Dataset):
def __init__(self, data_path, traces, train_traces: dict):
super(WGAN_Data, self).__init__()
self.raw_Data = scio.loadmat(data_path)
self.Train_Data = {}
self.traces = traces
self.start = train_traces['start'] if train_traces['start'] is not None else 0
self.stop = train_traces['stop'] if train_traces['stop'] is not None else self.traces
self.step = train_traces['step'] if train_traces['step'] is not None else 1
self.normalize()
def __len__(self):
return int((self.stop - self.start) / self.step)
def __getitem__(self, index): # torch.tensor改为torch.from_numpy
# 这里的vp vs rho 大小为[layers, traces]
# 三参数标签
index = index*self.step + self.start
vp_label = torch.tensor(self.Train_Data['vp'][..., index], dtype=torch.float32)
vs_label = torch.tensor(self.Train_Data['vs'][..., index], dtype=torch.float32)
den_label = torch.tensor(self.Train_Data['rho'][..., index], dtype=torch.float32)
M_sample = torch.stack([vp_label, vs_label, den_label], dim=0)
# 地震数据 -> [Trace, 3, input_len]
angle_gahter = torch.tensor(self.Train_Data['S'][index].swapaxes(-1,-2), dtype=torch.float32)
# 低频三参数数据 -> [Trace, 3, input_len]
vp_sample_initial = torch.tensor(self.Train_Data['vp_low'][..., index], dtype=torch.float32)
vs_sample_initial = torch.tensor(self.Train_Data['vs_low'][..., index], dtype=torch.float32)
den_sample_initial = torch.tensor(self.Train_Data['den_low'][..., index], dtype=torch.float32)
M_sample_initial = torch.stack([vp_sample_initial, vs_sample_initial, den_sample_initial], dim=0)
return M_sample, angle_gahter, M_sample_initial
def normalize(self):
param = ['vp', 'vs', 'rho', 'S', 'vp_low', 'vs_low', 'den_low']
if os.path.exists('normalize_dict.json'):
with open('normalize_dict.json','r') as json_file:
normalize_dict = json.load(json_file)
for i in param:
if i == 'xx':
imax = normalize_dict[i]
self.Train_Data[i] = self.raw_Data[i] / imax
else:
imin = normalize_dict[i]['min']
imax = normalize_dict[i]['max']
self.Train_Data[i] = (self.raw_Data[i] - imin) / (imax - imin)
else:
normalize_dict = {}
for i in param:
if i == 'xx':
imax = np.max(self.raw_Data[i])
self.Train_Data[i] = self.raw_Data[i] / imax
normalize_dict.update({f"{i}": imax})
else:
imin = np.min(self.raw_Data[i])
imax = np.max(self.raw_Data[i])
self.Train_Data[i] = (self.raw_Data[i] - imin) / (imax - imin)
normalize_dict.update({f"{i}": {"max": imax, "min": imin}})
with open('normalize_dict.json','w') as json_file:
json.dump(normalize_dict, json_file)