# Install necessary packages (if not already installed)
!pip install torch torchvision torchgeo arcgis gdal rasterio geopandas
Part 3: Hands-on Example - Land Cover Classification¶
In this section, we will perform land cover classification using satellite imagery. We will use torchgeo
and arcgis.learn
to build, train, and evaluate our models.
Dataset Preparation¶
We will use a sample dataset containing satellite images and their corresponding land cover labels.
# Import necessary libraries
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from torchgeo.datasets import EuroSAT
from torchgeo.transforms import indices
import matplotlib.pyplot as plt
import numpy as np
Load the Dataset¶
We will use the EuroSAT dataset, which contains Sentinel-2 satellite images covering 10 classes.
# Define transformations
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor()
])
# Load the dataset
dataset = EuroSAT(root="data/EuroSAT", split="train", transforms=transform, download=True)
# Split into training and validation sets
train_size = int(0.8 * len(dataset))
val_size = len(dataset) - train_size
train_dataset, val_dataset = torch.utils.data.random_split(dataset, [train_size, val_size])
# Create data loaders
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)
Define the Model¶
We will use a simple convolutional neural network for classification.
# Define the model
class SimpleCNN(nn.Module):
def __init__(self, num_classes):
super(SimpleCNN, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(13, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(64 * 16 * 16, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, num_classes)
)
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x
num_classes = 10
model = SimpleCNN(num_classes)
Training the Model¶
# Define loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
# Training loop
num_epochs = 5
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
for images, labels in train_loader:
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
# Validation
model.eval()
correct = 0
total = 0
with torch.no_grad():
for images, labels in val_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {running_loss/len(train_loader):.4f}, Validation Accuracy: {100 * correct / total:.2f}%")
Visualize Sample Predictions¶
# Get a batch of validation data
dataiter = iter(val_loader)
images, labels = dataiter.next()
# Get predictions
outputs = model(images)
_, predicted = torch.max(outputs, 1)
# Display images and predictions
fig = plt.figure(figsize=(12, 8))
for idx in range(8):
ax = fig.add_subplot(2, 4, idx+1)
img = images[idx].numpy().transpose((1, 2, 0))
img = np.clip(img, 0, 1)
ax.imshow(img)
ax.set_title(f"Predicted: {dataset.classes[predicted[idx]]}\nActual: {dataset.classes[labels[idx]]}")
ax.axis('off')
plt.show()
Part 4: Using arcgis.learn
for Object Detection¶
We will now use arcgis.learn
to perform object detection on geospatial data.
# Import necessary libraries
from arcgis.gis import GIS
from arcgis.learn import SingleShotDetector, prepare_data
# Connect to GIS (anonymous)
gis = GIS()
# Prepare the data
data_path = 'path_to_your_data'
data = prepare_data(data_path, batch_size=8, chip_size=224)
# Initialize the model
model = SingleShotDetector(data)
# Train the model
model.fit(epochs=5, lr=0.001)
Evaluate the Model¶
# Show results
model.show_results()
# Define augmentations
augmentation = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomRotation(30)
])
# Apply augmentations in data loader
train_dataset.dataset.transform = transforms.Compose([augmentation, transform])
Hyperparameter Tuning¶
- Adjust learning rates
- Modify network architectures
- Experiment with batch sizes
Follow-up Learning Path¶
- Explore advanced models like UNet, ResNet, and DenseNet
- Dive into semantic segmentation tasks
- Learn about deploying models in production environments
- Engage with the geospatial data science community
References¶
[1] Esri. (2023). Deep Learning Models in ArcGIS Learn
[2] Esri. (2023). GeoAI Overview
[3] PyTorch. (2023). PyTorch Documentation
[4] TorchGeo. (2023). TorchGeo Documentation
Thank You!¶
Feel free to reach out if you have any questions or need further assistance with your geospatial deep learning projects.