Remote Sensing Image Scene Classification Using PyTorch
Problem Overview
Remote sensing image scene classification represents a multi-class classification challenge. The availability of public datasets makes this task accessible, and PyTorch provides numerous pre-trained models suitable for image classification tasks, including ResNet, VGG, and Inception. While custom network architectures can be designed, this implementation utilizes ResNet50 as the backbone. The critical step involves adapting the model's output layer by modifying the num_classes parameter to match the specific number of categories in your target dataset.
Logging Configuration
A robust logging system serves as essential infrastructure throughout the entire machine learning pipeline, encompassing data preprocessing, model training, and validation phases. Thece logs capture operations, training metrics, validation performance, learning rate adjustments, and other vital indicators. Two primary appproaches exist: file-based logging and visualization tools like TensorBoard. This implementation employs both default file logging and Wandb for real-time monitoring through its web interface.
File-Based Logging
The following implementation writes log entries to both console and file, ensuring data persistence:
import logging
import os
import sys
__all__ = ['configure_logging']
_loggers = []
def configure_logging(name='trainer', output_path='training.log'):
"""Configure logging with both console and file handlers."""
log = logging.getLogger(name)
if log in _loggers:
return log
log.setLevel(logging.INFO)
log.propagate = False
log_format = logging.Formatter(
"[%(asctime)s] %(name)s %(levelname)s: %(message)s",
datefmt="%m/%d %H:%M:%S"
)
console_handler = logging.StreamHandler(stream=sys.stdout)
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(log_format)
log.addHandler(console_handler)
if output_path is not None:
if output_path.endswith('.txt') or output_path.endswith('.log'):
file_path = output_path
else:
file_path = os.path.join(output_path, 'log.txt')
log_dir = os.path.dirname(file_path)
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir)
file_handler = logging.FileHandler(file_path, mode='a')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter())
log.addHandler(file_handler)
_loggers.append(log)
return log
Wandb Integration
Wandb provides an intuitive interface for tracking experiments. After creating an account, the following configuration enables experiment tracking:
import wandb
# Authenticate with your Wandb API key
wandb_key = ''
wandb.login(key=wandb_key)
# Initialize experiment tracking
wandb.init(
project="aerial-scene-recognition",
config={
"batch_size": batch_size,
"epochs": epochs,
"learning_rate": lr,
"optimizer": "Adam",
"scheduler": "StepLR",
"scheduler_step": 10,
"scheduler_gamma": 0.1,
"weight_decay": 0.0,
"momentum": 0.9
}
)
The configuration dictionary captures hyperparameters that can be visualized and compared across different experiment runs.