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