Python develops and builds a deep learning classification model, and explores the application of AI in earthquake event classification for interpretability analysis

I recently saw an interesting paper, as follows:

 It is very interesting to apply deep learning development to the field of earthquake event analysis and classification, so I want to experience it myself. The data set here is a seismic wave found on the Internet. It should be a data set for simulation experiments. Let’s first Looking at the overall situation of the dataset, the code implementation is as follows:

data=np.load(filename)
print("type_data: ", type(data))


for one_key in data:
    print("one_key: ", one_key)


X_waveform = data['X_waveform']
label=data['y']

print("X_waveform_shape: ", X_waveform.shape)
print("label_shape: ", label.shape)

The resulting output looks like this:

 You can see: This is a fairly small sample data set, containing 100 sample data.

Next, develop and build a simple basic CNN model to implement classification processing. The core code implementation is as follows:

data = np.load(filename)
X_waveform = data["X_waveform"]
label = data["y"]
x_train, y_train, x_test, y_test = dataSplit()
model = initModel()
history = model.fit(
    x_train,
    y_train,
    epochs=50,
    validation_split=0.2,
    batch_size=128,
    shuffle=True,
    callbacks=callbacks,
)
acc = history.history["acc"]
val_acc = history.history["val_acc"]
loss = history.history["loss"]
val_loss = history.history["val_loss"]
plt.subplot(1, 2, 1)
plt.plot(acc, label="Training Accuracy")
plt.plot(val_acc, label="Validation Accuracy")
plt.title("Training and Validation Accuracy")
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(loss, label="Training Loss")
plt.plot(val_loss, label="Validation Loss")
plt.title("Training and Validation Loss")
plt.legend()
plt.savefig("train1.png")

Among them, the model construction implementation is as follows:

def initModel(
    input_shape,
    kernel_size,
    pooling_size,
    root_filters,
    clip_filters,
    dense_dropout,
    cnn_dropout,
    n_layers,
    activation,
    output_class,
    output_activation,
):
    inputs = Input(shape=input_shape)
    y = layers.Conv2D(
        filters=root_filters,
        kernel_size=kernel_size,
        activation=activation,
        padding="same",
    )(inputs)
    y = layers.MaxPooling2D(pooling_size)(y)
    n_kernels = root_filters
    for i in range(n_layers):
        if clip_filters:
            if n_kernels > clip_filters:
                n_kernels = clip_filters
            else:
                n_kernels *= 2
        else:
            n_kernels *= 2
        y = layers.Conv2D(
            filters=n_kernels,
            kernel_size=kernel_size,
            padding="same",
            activation=activation,
        )(y)
        y = layers.Dropout(cnn_dropout)(y)
        y = layers.MaxPooling2D(pooling_size)(y)
    y = layers.Flatten()(y)
    y = layers.Dropout(dense_dropout)(y)
    y = layers.Dense(100, activation=activation)(y)
    outputs = layers.Dense(output_class, activation=output_activation)(y)
    baseline_model = models.Model(inputs=inputs, outputs=outputs)
    baseline_model.summary()
    baseline_model.compile(
        optimizer=keras.optimizers.Adam(learning_rate=1e-3),
        loss=tf.keras.losses.SparseCategoricalCrossentropy(),
        metrics=["acc"],
    )
    return baseline_model

The training visualization looks like this:

 Only 30 epochs are trained here, and then we train 100 epochs to see the effect:

 Because the data is limited, continuing to train later will not improve the effect.

GradCAM (Gradient-weighted Class Activation Mapping) is a technology for heat map visualization that can help understand the decision-making basis of convolutional neural networks (CNN) in classification tasks. The following is a detailed introduction to the principle of GradCAM technology:

  1. Convolutional Neural Network (CNN): GradCAM technology is applied to CNN, a deep learning model commonly used in image classification tasks. It consists of multiple convolutional and pooling layers, and finally a fully connected layer is connected for classification.

  2. Feature Maps and Class Activation Maps: In CNNs, the output of a convolutional layer is called a feature map. Each feature map contains a specific set of features, corresponding to feature representations of the input image at different locations and scales.

    The class activation map is the heat map to be generated by GradCAM. It represents the importance distribution of CNN for a specific category, that is, which regions play a key role in the classification of this category.

  3. Backpropagation and Gradient: Gradient-weighted Class Activation Mapping uses the gradient information in the backpropagation process. In CNN, the error backpropagation algorithm updates the parameter values ​​by calculating the partial derivative of the error function with respect to each parameter of the network. These partial derivatives can also be used to compute the gradient of the input image.

  4. Steps of GradCAM:

    • Forward propagation: The input image is forward-propagated through CNN to generate feature maps and final classification results.
    • Backpropagation: Calculate the gradient of the classification result for the feature map. This gradient represents the importance of the feature map for the final classification.
    • Spatial weights of feature maps: The gradients on the channels of each feature map are spatially weighted and summed to obtain the importance score of each feature map.
    • Heat map generation: For each feature map, its importance score is multiplied by the corresponding feature map to obtain a weighted feature map. Then all the weighted feature maps are superimposed to form the final heat map.
  5. Visualization: The generated GradCAM heatmap can be visualized by superimposing it on the original input image. In the heatmap, brighter regions indicate greater significant contributions to the classification of a particular class. By observing the heat map, you can understand the areas that the CNN model focuses on when making classification decisions.

In summary, GradCAM technology uses the gradient information in the backpropagation process in CNN to calculate the spatial weight of the feature map, and then generates a heat map to show the area of ​​concern of the convolutional neural network in the classification task. This visualization technique helps explain and understand the decision-making process of CNN models, as well as identify important regions and features in image classification.

Next, based on GradCAM, I want to realize the visual analysis of the data heat map. The core code implementation is as follows:

model = keras.models.load_model("best.h5")
y_pred = model.predict(X)
ix = np.array([i])
sampling_rate = 100
times = np.arange(2000) / sampling_rate
cam = GradCAM(model, 0)
heatmap_eq = cam.compute_heatmap(X[ix])
cam = GradCAM(model, 1)
heatmap_exp = cam.compute_heatmap(X[ix])
cam = GradCAM(model, 2)
heatmap_noise = cam.compute_heatmap(X[ix])
heatmap_exp /= np.max(heatmap_exp)
heatmap_eq /= np.max(heatmap_eq)
heatmap_noise /= np.max(heatmap_noise)
tr_data = X[ix][0, :, 0, 0]
y_true = y[ix][0]
fig, axes = plt.subplots(nrows=3, figsize=(12, 8))
axes[0].plot(times, tr_data, "grey")
ymin, ymax = axes[0].get_ylim()
sc = axes[0].scatter(
    times, tr_data, c=heatmap_exp, cmap=plt.cm.jet, zorder=10, s=5, alpha=0.8
)
axes[0].set_title(
    f"True:  {mapping[y_true]}, Estimate: EQ, with probability {y_pred[ix][0][0]:.2f}"
)
axes[0].set_xlim(0, 20)
axes[0].set_xticks([])
axes[1].plot(times, tr_data, "grey")
sc = axes[1].scatter(
    times, tr_data, c=heatmap_eq, cmap=plt.cm.jet, zorder=10, s=5, alpha=0.8
)
axes[1].set_title(
    f"True:  {mapping[y_true]}, Estimate: noise, with probability {y_pred[ix][0][1]:.2f}"
)
axes[1].set_xlim(0, 20)
axes[1].set_xticks([])
axes[2].plot(times, tr_data, "grey")
sc = axes[2].scatter(
    times, tr_data, c=heatmap_noise, cmap=plt.cm.jet, zorder=10, s=5, alpha=0.8
)
axes[2].set_title(
    f"True:  {mapping[y_true]}, Estimate: EXP, with probability {y_pred[ix][0][2]:.2f}"
)
axes[2].set_xlabel("Time (sec)")
axes[2].set_xlim(0, 20)
fig.colorbar(sc, ax=[axes[0], axes[1], axes[2]], label="Normalized Grad-CAM weights")
path = "GradCAM/" + b[i] + ".jpg"
plt.savefig(path)

Next, look at the corresponding renderings:

 On the whole, it is still good: it is quite similar to the overall presentation results in the paper.

 Next, we further generated a large batch of data sets through simulation experiments, as follows:

 It contains the characteristic data of the main four dimensions.

Here we have designed and developed various convolutional neural network models for the model development level: lenet, alexnet, vgg, resnet, mobilenet and the original paper model.

The time relationship will not be introduced here, let's directly look at the results:

 The training process is visualized as follows:

Here, the visual display of the heat map is also realized by means of the GradCAM algorithm, as follows:

 

Guess you like

Origin blog.csdn.net/Together_CZ/article/details/131249212