Using Python to Classify Land Cover from Satellite Imagery with Convolutional Neural Networks (CNNs)

--

Introduction:

In this blog, we’ll walk through the process of using Python to classify land cover from satellite imagery using Convolutional Neural Networks (CNNs). We’ll use the freely available EuroSAT dataset, which contains Sentinel-2 satellite images covering 13 spectral bands and 10 different land cover classes.

A Convolutional Neural Network (CNN) is a specialized type of artificial neural network that is primarily used for the analysis and processing of visual data, such as images or videos. It plays a key role in various computer vision tasks, including image recognition, object detection, and image segmentation. One of the main strengths of CNNs lies in their ability to autonomously learn and extract relevant features from the input data by employing a series of convolutional layers. These layers consist of individual filters that traverse the input information. Carrying out convolutions to identify patterns and spatial relationships. The outcomes of these convolutions are subsequently subjected to activation functions and pooling operations to reduce dimensionality. If you’d like more detailed information on CNNs. I encourage you to consult my article on Convolutional Neural Networks.

Before we start, make sure you have installed the following Python libraries:

  • numpy
  • tensorflow
  • keras
  • matplotlib
  • scikit-learn

You can install them via pip using:

pip install numpy tensorflow keras matplotlib scikit-learn
  1. Import the necessary libraries:
import numpy as np
from tensorflow import keras
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split

2. Load and preprocess the EuroSAT dataset:

The EuroSAT dataset can be downloaded from the following link: EuroSAT dataset. It is a collection of Sentinel-2 satellite images representing different land cover classes like buildings, forests, sea, and more. Each image is 64x64 pixels and corresponds to one of 10 different classes.

# Load the data
data = np.load('EuroSAT.npz') # This is a hypothetical numpy array file
satellite_images, land_cover_labels = data['satellite_images'], data['land_cover_labels']

# Normalize pixel values to be between 0 and 1
satellite_images, land_cover_labels = satellite_images / 255.0, land_cover_labels

# Split the data into train and test datasets
train_images, test_images, train_labels, test_labels = train_test_split(satellite_images, land_cover_labels, test_size=0.2)

3. Build the CNN

model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 13)), # 13 spectral bands
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Dropout(0.2),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Dropout(0.2),
keras.layers.Flatten(),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(10) # 10 different classes in the EuroSAT dataset
])

4. Compile and train the model:

model.compile(optimizer='adam',
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])

# Train the model
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))

5. Evaluate the Model

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

6. Visualizing the Model’s Performance

plt.figure(figsize=(12, 4))

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.title('Training and Validation Loss')

plt.show()

This tutorial provides an introduction to how Python, along with the TensorFlow and Keras libraries, can be used to apply a Convolutional Neural Network (CNN) for the classification of land cover from satellite imagery. By using the freely available EuroSAT dataset, you can experiment and further explore the potential of deep learning for remote sensing applications.

--

--