from tensorflow.keras.models import Sequential, Model
epochs = 1 # Define the number of epochs
for epoch in range(epochs):
history = model.fit(x_train, np.array(y_train), batch_size=64, epochs=1, validation_split=0.2)
Model = Model(inputs=dilated_cnn_input, outputs=output)
# Choose an image from the validation set for visualization
img_array = x_val[0:1]
# Get the Grad-CAM for the first convolutional layer
cam = get_grad_cam(model, img_array, 'conv2d') # Replace 'conv2d' with the actual name of the convolutional layer
# Resize the CAM to the input image size
resized_cam = cv2.resize(cam, (img_array.shape[2], img_array.shape[1]))
# Superimpose the CAM on the original image
heatmap = cv2.applyColorMap(np.uint8(255 * resized_cam), cv2.COLORMAP_JET)
superimposed_img = cv2.addWeighted(img_array[0], 0.5, heatmap, 0.5, 0)
# Save or display the original image and the superimposed image
cv2.imwrite(f'grad_cam_epoch_{epoch}.jpg', np.hstack([img_array[0], superimposed_img]))