The workflow to classify landscapes directly from raster data using neural networks (see vignette) requires keras3 and its correct configuration.
In our experience, it can be a bit tricky to set up correctly, therefore we provide this guide to help you with the installation.
The setup requires the installation of the keras3 package and the reticulate package, which provides an interface to Python from R.
Both packages are installed alongside the spatPatClassifyR package, so you only need to load them:
First, you can check if you already have the keras and tensorflow modules installed using:
reticulate::py_module_available("keras") &&
reticulate::py_module_available("tensorflow")If this returns TRUE, your installation is already working and you can skip the rest of this guide.
Specify Python Installation
Before installing Keras, you should specify which Python installation to use.
Step 1: Find your Python installations
Open a terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run:
where python # Windows
which python # macOS/LinuxThis will list all Python installations on your system.
Step 2: Tell reticulate which Python to use
Choose Python 3.10 or 3.11 (recommended for TensorFlow compatibility) and specify it:
# Replace this path with your actual Python 3.10 or 3.11 installation
reticulate::use_python(
"C:/Users/YourName/AppData/Local/Programs/Python/Python310/python.exe",
required = TRUE
)
# Verify reticulate found your Python
reticulate::py_discover_config()This should display your Python installation details, not NULL.
Warning
Avoid Python 3.13+ as TensorFlow does not fully support these newer versions yet. Use Python 3.10 or 3.11.
Install Keras and TensorFlow
Now install Keras using the install_keras() function from the keras3 package:
This will take a while (5-15 minutes) and will install all necessary components in a virtual environment.
Now, check again if the installation was successful:
reticulate::py_module_available("keras") &&
reticulate::py_module_available("tensorflow")Test Your Installation
To test if Keras is working correctly, you can run the following simple function:
Note
The first time you run Keras functions after installation, TensorFlow needs to initialize, which may show some informational messages. Subsequent runs will be fast.
keras3::to_categorical(0)If this prints a matrix without errors, your Keras installation is working correctly.