一维卷积神经网络的特征可视化-建立模型并进行训练

时间:2024-04-14 13:21:15
# basic model
model = Sequential([
    Input(shape=[X_train.shape[1], 1]),
    Conv1D(filters=16, kernel_size=3, activation="relu"),
    BatchNormalization(),
    MaxPool1D(),
    Conv1D(filters=32, kernel_size=3, activation="relu"),
    BatchNormalization(),
    Conv1D(filters=64, kernel_size=3, activation="relu"),
    BatchNormalization(),
    Flatten(),
    Dense(20, activation="relu"),
    Dense(num_of_categories, activation="softmax")
]
)

# train process

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])

checkPoint = ModelCheckpoint(filepath="model.h5", save_weights_only=False, monitor='val_accuracy',
                            mode='max', save_best_only=True)

model.fit(x=np.expand_dims(X_train, axis=2), y=y_train,
          batch_size=128, epochs=10, validation_data=(np.expand_dims(X_test, axis=2), y_test),
          callbacks=[checkPoint])

model = tf.keras.models.load_model("model.h5")
loss, acc = model.evaluate(np.expand_dims(X_test, axis=2), y_test)

采用Occlusion Sensitivity方法进行可视化,相关的参考文献较多。

fig, axs = plt.subplots(nrows=5, ncols=1)
fig.suptitle("Occlusion sensitivity")
fig.tight_layout()
fig.set_size_inches(10, 10)
axs = axs.ravel()

for c, row, ax, title in zip(range(5), examples_to_visualise, axs, titles):
    sensitivity, _ = signal_screen.calculate_occlusion_sensitivity(model=model,
                                                         data=np.expand_dims(X_test[row, :], axis=(0, 2)),
                                                         c=c,
                                                         number_of_zeros=[15])

    # create gradient plot
    signal_screen_tools.plot_with_gradient(ax=ax, y=X_test[row, :].ravel(), gradient=sensitivity[0], title=title)
    ax.set_xlabel("Samples[-]")
    ax.set_ylabel("ECG [-]")

plt.show()

采用Saliency map方法进行可视化。

采用Grad-CAM方法进行可视化。