It seems you are directly loading all images to your RAM in this cell:
print("[INFO] loading images...")
imagePaths = list(paths.list_images(args["dataset_1"]))
data = []
labels = []
for imagePath in imagePaths:
    label = imagePath.split(os.path.sep)[-2]
    image = cv2.imread(imagePath)
    image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    image = cv2.resize(image, (224, 224))
    data.append(image)
    labels.append(label)
data = np.array(data) / 255.0
labels = np.array(labels)

Depending on the number of images and their size, this might take a lot of memory.
If you are dealing with a large dataset, it’s recommended to lazily load the batches using a Dataset and DataLoader as described in this tutorial 7.
How many images does your dataset have and how large is each image after preprocessing?