Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Remote Sensing Image Scene Classification Using PyTorch

Tech 2

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.

Tags: pytorch

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.