-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegressionMultiInput.py
More file actions
193 lines (146 loc) · 5.87 KB
/
RegressionMultiInput.py
File metadata and controls
193 lines (146 loc) · 5.87 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization, Conv2D, MaxPooling2D, Conv2DTranspose, Reshape
from tensorflow.keras.layers import Activation, Dropout, Dense, Flatten, Input, UpSampling2D, Concatenate
from tensorflow.keras.models import Model
from tensorflow.keras.models import load_model
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import tensorflow as tf
import pickle5 as pickle
from tensorflow.keras import backend as K
import locale
def load_dataset(path="/content/drive/MyDrive/DeepLearning/DataEmoda.npy"):
with open(path, "rb") as fh:
df = pickle.load(fh)
return df
def create_input_output(df, input_labels, output_labels):
inputImage = []
outputImage = []
for i in df.index:
inputImage.append(df[input_labels][i].reshape(256,256,1))
outputImage.append(df[output_labels][i].reshape(256,256,1))
return np.array(inputImage), np.array(outputImage)
def create_input(df, input_labels):
return df[input_labels].values
def create_mlp(dim):
model = Sequential()
model.add(Dense(64, input_dim=dim, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(256*256, activation='relu'))
model.build((None, 256*256))
return model
def create_cnn(width, height, depth):
inputShape = (height, width, depth)
#Main Branch
inputs = Input(shape=inputShape)
x = Conv2D(64, (3,3), padding="same")(inputs)
x = Activation("relu")(x)
x = BatchNormalization()(x)
ax = MaxPooling2D(pool_size=(2,2))(x)
#Branch 1
x = Conv2D(32, (3,3), padding="same")(ax)
x = Activation("relu")(x)
x = BatchNormalization()(x)
bx = MaxPooling2D(pool_size=(2,2))(x)
#Branch 2
x = Conv2D(32, (3,3), padding="same")(bx)
x = Activation("relu")(x)
x = BatchNormalization()(x)
cx = MaxPooling2D(pool_size=(2,2))(x)
#Branch 3
x = Conv2D(32, (3,3), padding="same")(cx)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = MaxPooling2D(pool_size=(2,2))(x)
x = Conv2D(32, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = UpSampling2D(size=(2,2))(x)
x = Conv2DTranspose(32, (3,3), padding="same")(x)
#Branch 2
x = Concatenate()([x, cx])
x = Conv2D(32, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = UpSampling2D(size=(2,2))(x)
x = Conv2DTranspose(32, (3,3), padding="same")(x)
#Branch 1
x = Concatenate()([x, bx])
x = Conv2D(32, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = UpSampling2D(size=(2,2))(x)
x = Conv2DTranspose(32, (3,3), padding="same")(x)
#Main Branch
x = Concatenate()([x,ax])
x = Conv2D(16, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
model = Model(inputs, x)
return model
def full_model(cnn_model, mlp_model):
x = cnn_model.output
cx = mlp_model.output
conv_shape = K.int_shape(x)
cx = Reshape((conv_shape[1],conv_shape[2],int(conv_shape[3]/4)))(cx)
x = Concatenate()([x,cx])
x = Conv2D(16, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = Conv2D(16, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = Conv2D(16, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = Conv2D(16, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = Conv2D(16, (3,3), padding="same")(x)
x = Activation("relu")(x)
x = BatchNormalization()(x)
x = UpSampling2D(size=(2,2))(x)
x = Conv2DTranspose(1, (3,3), padding="same", activation="linear")(x)
model = Model(inputs=[cnn_model.input, mlp_model.input], outputs = x)
return model
df = load_dataset()
(inputImages,outputImages) = create_input_output(df, 'bathy', 'hs')
(inputImages, outputImages) = ((inputImages - np.nanmean(inputImages))/np.nanstd(inputImages), (outputImages - np.nanmean(outputImages)/np.nanstd(outputImages)))
inputAttr = create_input(df, ['$\eta$', '$\zeta$','$\theta_{wave}$'])
inputAttr[:,0] = (inputAttr[:,0] - np.mean(inputAttr[:,0])) / np.std(inputAttr[:,0])
inputAttr[:,1] = (inputAttr[:,1] - np.mean(inputAttr[:,1])) / np.std(inputAttr[:,1])
inputAttr[:,2] = inputAttr[:,2] / (2*np.pi)
(inputImages, outputImages) = (np.nan_to_num(inputImages,nan=-10.), np.nan_to_num(outputImages, nan=-10.))
split = train_test_split(inputImages, inputAttr, outputImages, test_size=0.25, random_state=42)
(trainImgX, testImgX, trainAttrX, testAttrX, trainY, testY) = split
cnn_model = create_cnn(256, 256, 1, regress=True)
mlp_model = create_mlp(trainAttrX.shape[1])
model = full_model(cnn_model, mlp_model)
opt = Adam(learning_rate=1e-4, decay=1e-4/200)
model.compile(loss="mean_squared_error", optimizer=opt)
print("[INFO] training model...")
model.fit(x=[trainImgX, trainAttrX], y=trainY,
validation_data=([testImgX, testAttrX], testY),
epochs=200, batch_size=8)
model.save('/content/drive/MyDrive/DeepLearning/ModelV6')
#%% Plotting
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
model = load_model('/content/drive/MyDrive/DeepLearning/ModelV6', compile = True)
Prediction = model.predict([testImgX[0:7], testAttrX[0:7]])[6][:,:,0]
Truehs = testY[6][:,:,0]
fig = plt.figure(figsize=(6,3))
ax = fig.add_subplot(1,2,1)
ax.set_title('colorMap')
plt.imshow(Prediction)
qx = fig.add_subplot(1,2,2)
plt.imshow(Truehs)
cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()