Update unetlstm.py
Browse files- unetlstm.py +242 -242
unetlstm.py
CHANGED
@@ -1,243 +1,243 @@
|
|
1 |
-
import tensorflow as tf
|
2 |
-
from tensorflow.keras import layers, models # type: ignore
|
3 |
-
|
4 |
-
def encoder_block(inputs, filters):
|
5 |
-
x = layers.Conv3D(filters=filters, kernel_size=(3, 3, 4), padding="same", activation="relu")(inputs)
|
6 |
-
x = layers.BatchNormalization()(x)
|
7 |
-
return x
|
8 |
-
|
9 |
-
def convlstm_block(inputs, filters):
|
10 |
-
# Reshape to (timesteps, height, width, channels) for ConvLSTM
|
11 |
-
x = layers.Reshape((inputs.shape[1], inputs.shape[2], inputs.shape[3], inputs.shape[4]))(inputs)
|
12 |
-
x = layers.ConvLSTM2D(filters=filters, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
13 |
-
x = layers.BatchNormalization()(x)
|
14 |
-
# Reshape back to 3D conv format
|
15 |
-
x = layers.Reshape((inputs.shape[1], inputs.shape[2], inputs.shape[3], filters))(x)
|
16 |
-
return x
|
17 |
-
|
18 |
-
def decoder_block(inputs, skip_connection, filters):
|
19 |
-
x = layers.Conv3DTranspose(filters=filters, kernel_size=(3, 3, 4), padding="same", activation="relu")(inputs)
|
20 |
-
x = layers.BatchNormalization()(x)
|
21 |
-
skip_resized = layers.Conv3D(filters, (1, 1, 1), padding="same")(skip_connection)
|
22 |
-
x = layers.Concatenate()([x, skip_resized])
|
23 |
-
x = layers.ConvLSTM2D(filters=filters, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
24 |
-
return x
|
25 |
-
|
26 |
-
def build_unet_convlstm(input_shape=(8, 95, 95, 3)):
|
27 |
-
input_tensor = layers.Input(shape=input_shape)
|
28 |
-
|
29 |
-
# Encoder with ConvLSTM
|
30 |
-
skip1 = encoder_block(input_tensor, filters=8)
|
31 |
-
skip1 = convlstm_block(skip1, filters=8) # Added ConvLSTM
|
32 |
-
|
33 |
-
skip2 = encoder_block(skip1, filters=16)
|
34 |
-
skip2 = convlstm_block(skip2, filters=16) # Added ConvLSTM
|
35 |
-
|
36 |
-
# Bottleneck with ConvLSTM
|
37 |
-
x = layers.Conv3D(filters=32, kernel_size=(3, 3, 3), padding="same", activation="relu")(skip2)
|
38 |
-
x = layers.BatchNormalization()(x)
|
39 |
-
x = convlstm_block(x, filters=32) # Bottleneck ConvLSTM
|
40 |
-
|
41 |
-
# Decoder
|
42 |
-
x = decoder_block(x, skip2, filters=16)
|
43 |
-
x = decoder_block(x, skip1, filters=8)
|
44 |
-
|
45 |
-
# Final Output Layer
|
46 |
-
x = layers.Conv3D(filters=1, kernel_size=(1, 1, 1), activation="relu")(x)
|
47 |
-
x = layers.GlobalAveragePooling3D()(x)
|
48 |
-
|
49 |
-
model = models.Model(inputs=input_tensor, outputs=x)
|
50 |
-
return model
|
51 |
-
|
52 |
-
|
53 |
-
import tensorflow as tf
|
54 |
-
from tensorflow.keras import layers, models # type: ignore
|
55 |
-
|
56 |
-
def RSTNet(input_shape):
|
57 |
-
"""
|
58 |
-
Creates the subnet for extracting TC radial structure features using a five-branch CNN design with 2D convolutions.
|
59 |
-
|
60 |
-
Parameters:
|
61 |
-
- input_shape: tuple, shape of the input data (e.g., (95, 95, 3))
|
62 |
-
|
63 |
-
Returns:
|
64 |
-
- model: tf.keras.Model, the radial structure subnet model
|
65 |
-
"""
|
66 |
-
|
67 |
-
input_tensor = layers.Input(shape=input_shape)
|
68 |
-
|
69 |
-
# Divide input data into four quadrants (NW, NE, SW, SE)
|
70 |
-
# Assuming the input shape is (batch_size, height, width, channels)
|
71 |
-
|
72 |
-
# Quadrant extraction - using slicing to separate quadrants
|
73 |
-
nw_quadrant = input_tensor[:, :input_shape[0]//2, :input_shape[1]//2, :]
|
74 |
-
ne_quadrant = input_tensor[:, :input_shape[0]//2, input_shape[1]//2:, :]
|
75 |
-
sw_quadrant = input_tensor[:, input_shape[0]//2:, :input_shape[1]//2, :]
|
76 |
-
se_quadrant = input_tensor[:, input_shape[0]//2:, input_shape[1]//2:, :]
|
77 |
-
|
78 |
-
|
79 |
-
target_height = max(input_shape[0]//2, input_shape[0] - input_shape[0]//2) # 48
|
80 |
-
target_width = max(input_shape[1]//2, input_shape[1] - input_shape[1]//2) # 48
|
81 |
-
|
82 |
-
# Padding the quadrants to match the target size (48, 48)
|
83 |
-
nw_quadrant = layers.ZeroPadding2D(padding=((0, target_height - nw_quadrant.shape[1]),
|
84 |
-
(0, target_width - nw_quadrant.shape[2])))(nw_quadrant)
|
85 |
-
ne_quadrant = layers.ZeroPadding2D(padding=((0, target_height - ne_quadrant.shape[1]),
|
86 |
-
(0, target_width - ne_quadrant.shape[2])))(ne_quadrant)
|
87 |
-
sw_quadrant = layers.ZeroPadding2D(padding=((0, target_height - sw_quadrant.shape[1]),
|
88 |
-
(0, target_width - sw_quadrant.shape[2])))(sw_quadrant)
|
89 |
-
se_quadrant = layers.ZeroPadding2D(padding=((0, target_height - se_quadrant.shape[1]),
|
90 |
-
(0, target_width - se_quadrant.shape[2])))(se_quadrant)
|
91 |
-
|
92 |
-
print(nw_quadrant.shape)
|
93 |
-
print(ne_quadrant.shape)
|
94 |
-
print(sw_quadrant.shape)
|
95 |
-
print(se_quadrant.shape)
|
96 |
-
# Main branch (processing the entire structure)
|
97 |
-
main_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(input_tensor)
|
98 |
-
y=layers.MaxPool2D()(main_branch)
|
99 |
-
|
100 |
-
y = layers.ZeroPadding2D(padding=((0, target_height - y.shape[1]),
|
101 |
-
(0, target_width - y.shape[2])))(y)
|
102 |
-
# Side branches (processing the individual quadrants)
|
103 |
-
nw_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(nw_quadrant)
|
104 |
-
ne_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(ne_quadrant)
|
105 |
-
sw_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(sw_quadrant)
|
106 |
-
se_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(se_quadrant)
|
107 |
-
|
108 |
-
# Apply padding to the side branches to match the dimensions of the main branch
|
109 |
-
# nw_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(nw_branch)
|
110 |
-
# ne_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(ne_branch)
|
111 |
-
# sw_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(sw_branch)
|
112 |
-
# se_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(se_branch)
|
113 |
-
|
114 |
-
# Fusion operations (concatenate the outputs from the main branch and side branches)
|
115 |
-
fusion = layers.concatenate([y, nw_branch, ne_branch, sw_branch, se_branch], axis=-1)
|
116 |
-
|
117 |
-
# Additional convolution layer to combine the fused features
|
118 |
-
# x = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(fusion)
|
119 |
-
x=layers.Reshape((1, 48, 48, 40))(fusion)
|
120 |
-
x = layers.ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
121 |
-
x=layers.Reshape((48, 48, 16))(x)
|
122 |
-
x=layers.MaxPool2D(pool_size=(2, 2))(x)
|
123 |
-
# Final dense layer for further processing
|
124 |
-
nw_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(nw_branch)
|
125 |
-
|
126 |
-
ne_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(ne_branch)
|
127 |
-
sw_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(sw_branch)
|
128 |
-
se_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(se_branch)
|
129 |
-
nw_branch = layers.MaxPool2D(pool_size=(2, 2))(nw_branch)
|
130 |
-
ne_branch = layers.MaxPool2D(pool_size=(2, 2))(ne_branch)
|
131 |
-
sw_branch = layers.MaxPool2D(pool_size=(2, 2))(sw_branch)
|
132 |
-
se_branch = layers.MaxPool2D(pool_size=(2, 2))(se_branch)
|
133 |
-
|
134 |
-
fusion = layers.concatenate([x, nw_branch, ne_branch, sw_branch, se_branch], axis=-1)
|
135 |
-
# x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(fusion)
|
136 |
-
x=layers.Reshape((1, 24, 24, 80))(fusion)
|
137 |
-
x = layers.ConvLSTM2D(filters=32, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
138 |
-
x=layers.Reshape((24, 24, 32))(x)
|
139 |
-
x=layers.MaxPool2D(pool_size=(2, 2))(x)
|
140 |
-
|
141 |
-
nw_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(nw_branch)
|
142 |
-
|
143 |
-
ne_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(ne_branch)
|
144 |
-
sw_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(sw_branch)
|
145 |
-
se_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(se_branch)
|
146 |
-
nw_branch = layers.MaxPool2D(pool_size=(2, 2))(nw_branch)
|
147 |
-
ne_branch = layers.MaxPool2D(pool_size=(2, 2))(ne_branch)
|
148 |
-
sw_branch = layers.MaxPool2D(pool_size=(2, 2))(sw_branch)
|
149 |
-
se_branch = layers.MaxPool2D(pool_size=(2, 2))(se_branch)
|
150 |
-
|
151 |
-
fusion = layers.concatenate([x, nw_branch, ne_branch, sw_branch, se_branch], axis=-1)
|
152 |
-
# x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu')(fusion)
|
153 |
-
x=layers.Reshape((1,12, 12, 160))(fusion)
|
154 |
-
x = layers.ConvLSTM2D(filters=32, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
155 |
-
x=layers.Reshape((12, 12, 32))(x)
|
156 |
-
x=layers.Conv2D(filters=32, kernel_size=(3, 3), activation=None)(x)
|
157 |
-
# Create and return the model
|
158 |
-
x=layers.Flatten()(x)
|
159 |
-
model = models.Model(inputs=input_tensor, outputs=x)
|
160 |
-
return model
|
161 |
-
|
162 |
-
from tensorflow.keras import layers, models # type: ignore
|
163 |
-
|
164 |
-
def build_cnn_model(input_shape=(8, 8, 1)):
|
165 |
-
# Define the input layer
|
166 |
-
input_tensor = layers.Input(shape=input_shape)
|
167 |
-
|
168 |
-
# Convolutional layer
|
169 |
-
x = layers.Conv2D(64, (3, 3), padding='same')(input_tensor)
|
170 |
-
x = layers.BatchNormalization()(x)
|
171 |
-
x = layers.ReLU()(x)
|
172 |
-
|
173 |
-
# Flatten layer
|
174 |
-
x = layers.Flatten()(x)
|
175 |
-
|
176 |
-
# Create the model
|
177 |
-
model = models.Model(inputs=input_tensor, outputs=x)
|
178 |
-
|
179 |
-
return model
|
180 |
-
|
181 |
-
from tensorflow.keras import layers, models, Input # type: ignore
|
182 |
-
|
183 |
-
def build_combined_model():
|
184 |
-
# Define input shapes
|
185 |
-
input_shape_3d = (8, 95, 95, 2)
|
186 |
-
input_shape_radial = (95, 95, 8)
|
187 |
-
input_shape_cnn = (8, 8, 1)
|
188 |
-
|
189 |
-
input_shape_latitude = (8,)
|
190 |
-
input_shape_longitude = (8,)
|
191 |
-
input_shape_other = (9,)
|
192 |
-
|
193 |
-
# Build individual models
|
194 |
-
model_3d = build_unet_convlstm(input_shape=input_shape_3d)
|
195 |
-
model_radial = RSTNet(input_shape=input_shape_radial)
|
196 |
-
model_cnn = build_cnn_model(input_shape=input_shape_cnn)
|
197 |
-
|
198 |
-
# Define new inputs
|
199 |
-
input_latitude = Input(shape=input_shape_latitude ,name="latitude_input")
|
200 |
-
input_longitude = Input(shape=input_shape_longitude, name="longitude_input")
|
201 |
-
input_other = Input(shape=input_shape_other, name="other_input")
|
202 |
-
|
203 |
-
# Flatten the additional inputs
|
204 |
-
flat_latitude = layers.Dense(32,activation='relu')(input_latitude)
|
205 |
-
flat_longitude = layers.Dense(32,activation='relu')(input_longitude)
|
206 |
-
flat_other = layers.Dense(64,activation='relu')(input_other)
|
207 |
-
|
208 |
-
# Combine all outputs
|
209 |
-
combined = layers.concatenate([
|
210 |
-
model_3d.output,
|
211 |
-
model_radial.output,
|
212 |
-
model_cnn.output,
|
213 |
-
flat_latitude,
|
214 |
-
flat_longitude,
|
215 |
-
flat_other
|
216 |
-
])
|
217 |
-
|
218 |
-
# Add dense layers for final processing
|
219 |
-
x = layers.Dense(128, activation='relu')(combined)
|
220 |
-
x = layers.Dense(1, activation=None)(x)
|
221 |
-
|
222 |
-
# Create the final model
|
223 |
-
final_model = models.Model(
|
224 |
-
inputs=[model_3d.input, model_radial.input, model_cnn.input,
|
225 |
-
input_latitude, input_longitude, input_other ],
|
226 |
-
outputs=x
|
227 |
-
)
|
228 |
-
|
229 |
-
return final_model
|
230 |
-
|
231 |
-
import h5py
|
232 |
-
with h5py.File(r"
|
233 |
-
print(f.attrs.get('keras_version'))
|
234 |
-
print(f.attrs.get('backend'))
|
235 |
-
print("Model layers:", list(f['model_weights'].keys()))
|
236 |
-
|
237 |
-
model = build_combined_model() # Your original model building function
|
238 |
-
model.load_weights(r"
|
239 |
-
|
240 |
-
|
241 |
-
def predict_unetlstm(reduced_images_test,hov_m_test,test_vmax_3d,lat_test,lon_test,int_diff_test):
|
242 |
-
y=model.predict([reduced_images_test,hov_m_test,test_vmax_3d,lat_test,lon_test,int_diff_test ])
|
243 |
return y
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
from tensorflow.keras import layers, models # type: ignore
|
3 |
+
|
4 |
+
def encoder_block(inputs, filters):
|
5 |
+
x = layers.Conv3D(filters=filters, kernel_size=(3, 3, 4), padding="same", activation="relu")(inputs)
|
6 |
+
x = layers.BatchNormalization()(x)
|
7 |
+
return x
|
8 |
+
|
9 |
+
def convlstm_block(inputs, filters):
|
10 |
+
# Reshape to (timesteps, height, width, channels) for ConvLSTM
|
11 |
+
x = layers.Reshape((inputs.shape[1], inputs.shape[2], inputs.shape[3], inputs.shape[4]))(inputs)
|
12 |
+
x = layers.ConvLSTM2D(filters=filters, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
13 |
+
x = layers.BatchNormalization()(x)
|
14 |
+
# Reshape back to 3D conv format
|
15 |
+
x = layers.Reshape((inputs.shape[1], inputs.shape[2], inputs.shape[3], filters))(x)
|
16 |
+
return x
|
17 |
+
|
18 |
+
def decoder_block(inputs, skip_connection, filters):
|
19 |
+
x = layers.Conv3DTranspose(filters=filters, kernel_size=(3, 3, 4), padding="same", activation="relu")(inputs)
|
20 |
+
x = layers.BatchNormalization()(x)
|
21 |
+
skip_resized = layers.Conv3D(filters, (1, 1, 1), padding="same")(skip_connection)
|
22 |
+
x = layers.Concatenate()([x, skip_resized])
|
23 |
+
x = layers.ConvLSTM2D(filters=filters, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
24 |
+
return x
|
25 |
+
|
26 |
+
def build_unet_convlstm(input_shape=(8, 95, 95, 3)):
|
27 |
+
input_tensor = layers.Input(shape=input_shape)
|
28 |
+
|
29 |
+
# Encoder with ConvLSTM
|
30 |
+
skip1 = encoder_block(input_tensor, filters=8)
|
31 |
+
skip1 = convlstm_block(skip1, filters=8) # Added ConvLSTM
|
32 |
+
|
33 |
+
skip2 = encoder_block(skip1, filters=16)
|
34 |
+
skip2 = convlstm_block(skip2, filters=16) # Added ConvLSTM
|
35 |
+
|
36 |
+
# Bottleneck with ConvLSTM
|
37 |
+
x = layers.Conv3D(filters=32, kernel_size=(3, 3, 3), padding="same", activation="relu")(skip2)
|
38 |
+
x = layers.BatchNormalization()(x)
|
39 |
+
x = convlstm_block(x, filters=32) # Bottleneck ConvLSTM
|
40 |
+
|
41 |
+
# Decoder
|
42 |
+
x = decoder_block(x, skip2, filters=16)
|
43 |
+
x = decoder_block(x, skip1, filters=8)
|
44 |
+
|
45 |
+
# Final Output Layer
|
46 |
+
x = layers.Conv3D(filters=1, kernel_size=(1, 1, 1), activation="relu")(x)
|
47 |
+
x = layers.GlobalAveragePooling3D()(x)
|
48 |
+
|
49 |
+
model = models.Model(inputs=input_tensor, outputs=x)
|
50 |
+
return model
|
51 |
+
|
52 |
+
|
53 |
+
import tensorflow as tf
|
54 |
+
from tensorflow.keras import layers, models # type: ignore
|
55 |
+
|
56 |
+
def RSTNet(input_shape):
|
57 |
+
"""
|
58 |
+
Creates the subnet for extracting TC radial structure features using a five-branch CNN design with 2D convolutions.
|
59 |
+
|
60 |
+
Parameters:
|
61 |
+
- input_shape: tuple, shape of the input data (e.g., (95, 95, 3))
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
- model: tf.keras.Model, the radial structure subnet model
|
65 |
+
"""
|
66 |
+
|
67 |
+
input_tensor = layers.Input(shape=input_shape)
|
68 |
+
|
69 |
+
# Divide input data into four quadrants (NW, NE, SW, SE)
|
70 |
+
# Assuming the input shape is (batch_size, height, width, channels)
|
71 |
+
|
72 |
+
# Quadrant extraction - using slicing to separate quadrants
|
73 |
+
nw_quadrant = input_tensor[:, :input_shape[0]//2, :input_shape[1]//2, :]
|
74 |
+
ne_quadrant = input_tensor[:, :input_shape[0]//2, input_shape[1]//2:, :]
|
75 |
+
sw_quadrant = input_tensor[:, input_shape[0]//2:, :input_shape[1]//2, :]
|
76 |
+
se_quadrant = input_tensor[:, input_shape[0]//2:, input_shape[1]//2:, :]
|
77 |
+
|
78 |
+
|
79 |
+
target_height = max(input_shape[0]//2, input_shape[0] - input_shape[0]//2) # 48
|
80 |
+
target_width = max(input_shape[1]//2, input_shape[1] - input_shape[1]//2) # 48
|
81 |
+
|
82 |
+
# Padding the quadrants to match the target size (48, 48)
|
83 |
+
nw_quadrant = layers.ZeroPadding2D(padding=((0, target_height - nw_quadrant.shape[1]),
|
84 |
+
(0, target_width - nw_quadrant.shape[2])))(nw_quadrant)
|
85 |
+
ne_quadrant = layers.ZeroPadding2D(padding=((0, target_height - ne_quadrant.shape[1]),
|
86 |
+
(0, target_width - ne_quadrant.shape[2])))(ne_quadrant)
|
87 |
+
sw_quadrant = layers.ZeroPadding2D(padding=((0, target_height - sw_quadrant.shape[1]),
|
88 |
+
(0, target_width - sw_quadrant.shape[2])))(sw_quadrant)
|
89 |
+
se_quadrant = layers.ZeroPadding2D(padding=((0, target_height - se_quadrant.shape[1]),
|
90 |
+
(0, target_width - se_quadrant.shape[2])))(se_quadrant)
|
91 |
+
|
92 |
+
print(nw_quadrant.shape)
|
93 |
+
print(ne_quadrant.shape)
|
94 |
+
print(sw_quadrant.shape)
|
95 |
+
print(se_quadrant.shape)
|
96 |
+
# Main branch (processing the entire structure)
|
97 |
+
main_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(input_tensor)
|
98 |
+
y=layers.MaxPool2D()(main_branch)
|
99 |
+
|
100 |
+
y = layers.ZeroPadding2D(padding=((0, target_height - y.shape[1]),
|
101 |
+
(0, target_width - y.shape[2])))(y)
|
102 |
+
# Side branches (processing the individual quadrants)
|
103 |
+
nw_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(nw_quadrant)
|
104 |
+
ne_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(ne_quadrant)
|
105 |
+
sw_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(sw_quadrant)
|
106 |
+
se_branch = layers.Conv2D(filters=8, kernel_size=(3, 3), padding='same', activation='relu')(se_quadrant)
|
107 |
+
|
108 |
+
# Apply padding to the side branches to match the dimensions of the main branch
|
109 |
+
# nw_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(nw_branch)
|
110 |
+
# ne_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(ne_branch)
|
111 |
+
# sw_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(sw_branch)
|
112 |
+
# se_branch = layers.UpSampling2D(size=(2, 2), interpolation='nearest')(se_branch)
|
113 |
+
|
114 |
+
# Fusion operations (concatenate the outputs from the main branch and side branches)
|
115 |
+
fusion = layers.concatenate([y, nw_branch, ne_branch, sw_branch, se_branch], axis=-1)
|
116 |
+
|
117 |
+
# Additional convolution layer to combine the fused features
|
118 |
+
# x = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(fusion)
|
119 |
+
x=layers.Reshape((1, 48, 48, 40))(fusion)
|
120 |
+
x = layers.ConvLSTM2D(filters=16, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
121 |
+
x=layers.Reshape((48, 48, 16))(x)
|
122 |
+
x=layers.MaxPool2D(pool_size=(2, 2))(x)
|
123 |
+
# Final dense layer for further processing
|
124 |
+
nw_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(nw_branch)
|
125 |
+
|
126 |
+
ne_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(ne_branch)
|
127 |
+
sw_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(sw_branch)
|
128 |
+
se_branch = layers.Conv2D(filters=16, kernel_size=(3, 3), padding='same', activation='relu')(se_branch)
|
129 |
+
nw_branch = layers.MaxPool2D(pool_size=(2, 2))(nw_branch)
|
130 |
+
ne_branch = layers.MaxPool2D(pool_size=(2, 2))(ne_branch)
|
131 |
+
sw_branch = layers.MaxPool2D(pool_size=(2, 2))(sw_branch)
|
132 |
+
se_branch = layers.MaxPool2D(pool_size=(2, 2))(se_branch)
|
133 |
+
|
134 |
+
fusion = layers.concatenate([x, nw_branch, ne_branch, sw_branch, se_branch], axis=-1)
|
135 |
+
# x = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(fusion)
|
136 |
+
x=layers.Reshape((1, 24, 24, 80))(fusion)
|
137 |
+
x = layers.ConvLSTM2D(filters=32, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
138 |
+
x=layers.Reshape((24, 24, 32))(x)
|
139 |
+
x=layers.MaxPool2D(pool_size=(2, 2))(x)
|
140 |
+
|
141 |
+
nw_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(nw_branch)
|
142 |
+
|
143 |
+
ne_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(ne_branch)
|
144 |
+
sw_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(sw_branch)
|
145 |
+
se_branch = layers.Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(se_branch)
|
146 |
+
nw_branch = layers.MaxPool2D(pool_size=(2, 2))(nw_branch)
|
147 |
+
ne_branch = layers.MaxPool2D(pool_size=(2, 2))(ne_branch)
|
148 |
+
sw_branch = layers.MaxPool2D(pool_size=(2, 2))(sw_branch)
|
149 |
+
se_branch = layers.MaxPool2D(pool_size=(2, 2))(se_branch)
|
150 |
+
|
151 |
+
fusion = layers.concatenate([x, nw_branch, ne_branch, sw_branch, se_branch], axis=-1)
|
152 |
+
# x = layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu')(fusion)
|
153 |
+
x=layers.Reshape((1,12, 12, 160))(fusion)
|
154 |
+
x = layers.ConvLSTM2D(filters=32, kernel_size=(3, 3), padding="same", return_sequences=True)(x)
|
155 |
+
x=layers.Reshape((12, 12, 32))(x)
|
156 |
+
x=layers.Conv2D(filters=32, kernel_size=(3, 3), activation=None)(x)
|
157 |
+
# Create and return the model
|
158 |
+
x=layers.Flatten()(x)
|
159 |
+
model = models.Model(inputs=input_tensor, outputs=x)
|
160 |
+
return model
|
161 |
+
|
162 |
+
from tensorflow.keras import layers, models # type: ignore
|
163 |
+
|
164 |
+
def build_cnn_model(input_shape=(8, 8, 1)):
|
165 |
+
# Define the input layer
|
166 |
+
input_tensor = layers.Input(shape=input_shape)
|
167 |
+
|
168 |
+
# Convolutional layer
|
169 |
+
x = layers.Conv2D(64, (3, 3), padding='same')(input_tensor)
|
170 |
+
x = layers.BatchNormalization()(x)
|
171 |
+
x = layers.ReLU()(x)
|
172 |
+
|
173 |
+
# Flatten layer
|
174 |
+
x = layers.Flatten()(x)
|
175 |
+
|
176 |
+
# Create the model
|
177 |
+
model = models.Model(inputs=input_tensor, outputs=x)
|
178 |
+
|
179 |
+
return model
|
180 |
+
|
181 |
+
from tensorflow.keras import layers, models, Input # type: ignore
|
182 |
+
|
183 |
+
def build_combined_model():
|
184 |
+
# Define input shapes
|
185 |
+
input_shape_3d = (8, 95, 95, 2)
|
186 |
+
input_shape_radial = (95, 95, 8)
|
187 |
+
input_shape_cnn = (8, 8, 1)
|
188 |
+
|
189 |
+
input_shape_latitude = (8,)
|
190 |
+
input_shape_longitude = (8,)
|
191 |
+
input_shape_other = (9,)
|
192 |
+
|
193 |
+
# Build individual models
|
194 |
+
model_3d = build_unet_convlstm(input_shape=input_shape_3d)
|
195 |
+
model_radial = RSTNet(input_shape=input_shape_radial)
|
196 |
+
model_cnn = build_cnn_model(input_shape=input_shape_cnn)
|
197 |
+
|
198 |
+
# Define new inputs
|
199 |
+
input_latitude = Input(shape=input_shape_latitude ,name="latitude_input")
|
200 |
+
input_longitude = Input(shape=input_shape_longitude, name="longitude_input")
|
201 |
+
input_other = Input(shape=input_shape_other, name="other_input")
|
202 |
+
|
203 |
+
# Flatten the additional inputs
|
204 |
+
flat_latitude = layers.Dense(32,activation='relu')(input_latitude)
|
205 |
+
flat_longitude = layers.Dense(32,activation='relu')(input_longitude)
|
206 |
+
flat_other = layers.Dense(64,activation='relu')(input_other)
|
207 |
+
|
208 |
+
# Combine all outputs
|
209 |
+
combined = layers.concatenate([
|
210 |
+
model_3d.output,
|
211 |
+
model_radial.output,
|
212 |
+
model_cnn.output,
|
213 |
+
flat_latitude,
|
214 |
+
flat_longitude,
|
215 |
+
flat_other
|
216 |
+
])
|
217 |
+
|
218 |
+
# Add dense layers for final processing
|
219 |
+
x = layers.Dense(128, activation='relu')(combined)
|
220 |
+
x = layers.Dense(1, activation=None)(x)
|
221 |
+
|
222 |
+
# Create the final model
|
223 |
+
final_model = models.Model(
|
224 |
+
inputs=[model_3d.input, model_radial.input, model_cnn.input,
|
225 |
+
input_latitude, input_longitude, input_other ],
|
226 |
+
outputs=x
|
227 |
+
)
|
228 |
+
|
229 |
+
return final_model
|
230 |
+
|
231 |
+
import h5py
|
232 |
+
with h5py.File(r"final_model.h5", 'r') as f:
|
233 |
+
print(f.attrs.get('keras_version'))
|
234 |
+
print(f.attrs.get('backend'))
|
235 |
+
print("Model layers:", list(f['model_weights'].keys()))
|
236 |
+
|
237 |
+
model = build_combined_model() # Your original model building function
|
238 |
+
model.load_weights(r"final_model.h5")
|
239 |
+
|
240 |
+
|
241 |
+
def predict_unetlstm(reduced_images_test,hov_m_test,test_vmax_3d,lat_test,lon_test,int_diff_test):
|
242 |
+
y=model.predict([reduced_images_test,hov_m_test,test_vmax_3d,lat_test,lon_test,int_diff_test ])
|
243 |
return y
|