Project
Introduction
This project implements a domain-adversarial neural network (DANN) that addresses the critical challenge of model robustness in computer vision by enabling ResNet50 to maintain high accuracy on distorted images without requiring labeled data from the target domain. The system leverages adversarial training with a gradient reversal layer to learn domain-invariant features that generalize across both clean and distorted image distributions. Unlike traditional approaches requiring extensive labeled data for each distortion type, this unsupervised adaptation method achieves 67% accuracy on distorted images compared to only 25% with vanilla ResNet50, representing a 42% improvement. The architecture consists of three key components: a shared feature extractor based on ResNet50, a label predictor for 1000 ImageNet classes, and a domain classifier that discriminates between source and target domains. Through minimax optimization, the feature extractor learns to fool the domain discriminator while maintaining classification performance, effectively aligning feature distributions across domains. The implementation handles 10 different distortion types including grayscale conversion, false coloring, high/low-pass filtering, noise addition, rotations, and contrast adjustments, making it applicable to real-world scenarios where image quality varies significantly.
Objectives
-
To develop unsupervised domain adaptation eliminating the need for labeled distorted images during training
-
To implement gradient reversal layer for adversarial feature learning and domain confusion
-
To achieve robust classification across 10+ image distortion types simultaneously
-
To maintain high accuracy on clean images while improving distorted image performance by 40%+
-
To create domain-invariant feature representations through minimax optimization
-
To demonstrate scalability to ImageNet-scale datasets with 1000 classes
Tools and Technologies
-
Framework: PyTorch 1.4+ with CUDA support
-
Base Model: ResNet50 (pretrained on ImageNet)
-
Optimizer: Adam (lr=0.001) and SGD (momentum=0.9)
-
Loss Functions: CrossEntropy (classification), BCE (domain discrimination)
-
Dataset: ImageNet-mini (1000 classes)
-
Distortion Library: SciPy, Scikit-image for image manipulation
-
Gradient Reversal: Custom autograd Function implementation
-
Augmentation: torchvision.transforms
-
Visualization: Matplotlib for training curves
-
Hardware: NVIDIA GPU with CUDA 11.8
-
Batch Size: 48 (limited by GPU memory)
-
Version Control: Git
Source Code
-
GitHub Repository: Domain-Adversarial Learning
-
Documentation: Implementation details and results
Live Presentation
Architecture Diagram:
showing dual classifiers with gradient reversal

-
Distortion Examples:
applied to test images

-
Training Curves:
.jpeg)
on source and target domains
Performance Metrics: 67% accuracy on distorted images vs 25% baseline
Process and Development
The project is structured into six critical components: baseline ResNet50 training, distortion generation pipeline, domain discriminator design, gradient reversal implementation, adversarial training loop, and evaluation framework.
Task 1: Baseline ResNet50 Training
Model Architecture Setup: Loaded pretrained ResNet50 with ImageNet weights, replaced final FC layer with 1000-class classifier matching ImageNet-mini categories, achieving 72% validation accuracy after 10 epochs.
Training Configuration: Implemented SGD optimizer with lr=0.001 and momentum=0.9, used CrossEntropyLoss for classification, applied standard ImageNet normalization (mean=[0.5,0.5,0.5], std=[0.5,0.5,0.5]).
Checkpoint Management: Saved model state_dict after each epoch, tracked training/validation loss and accuracy curves, established baseline performance metrics for comparison.
Task 2: Image Distortion Pipeline
Distortion Implementation: Created 10 distortion functions using SciPy/Scikit-image: grayscale conversion via rgb2gray(), false color through opponent color transformation, contrast reduction to 10% using linear scaling.
Noise Addition: Implemented uniform noise with width=0.1 additive range, salt-and-pepper noise with p=0.1 corruption probability, maintaining pixel values within [0,1] range through clipping.
Filtering Operations: Applied Gaussian high-pass filter with σ=3 for edge enhancement, low-pass filter with σ=10 for blur effects, phase scrambling in Fourier domain for texture disruption.
Geometric Transformations: Implemented 90°/180°/270° rotations using numpy array operations, preserved aspect ratios during transformation, converted to grayscale to maintain consistency.
Task 3: Domain Discriminator Architecture
Network Design: Created 2-layer MLP discriminator with 2048→256→1 architecture, used ReLU activation for hidden layer, sigmoid output for binary domain classification.
Feature Extraction: Extracted 2048-dim features from ResNet50's penultimate layer, flattened spatial dimensions for fully-connected processing, shared features between label predictor and discriminator.
Loss Configuration: Implemented BCEWithLogitsLoss for stable gradient flow, assigned domain labels: 0 for source (clean), 1 for target (distorted), balanced mini-batches with equal domain representation.
Task 4: Gradient Reversal Layer Implementation
Custom Autograd Function: Created GradientReversalLayer extending torch.autograd.Function, implemented identity forward pass preserving tensor values, reversed gradients in backward pass with configurable α scaling.
Dynamic Alpha Scheduling: Started with α=0.0 for initial feature learning, gradually increased to α=0.2 over training epochs, balanced classification and domain confusion objectives.
Integration Points: Inserted gradient reversal between feature extractor and domain classifier, maintained computational graph for proper backpropagation, enabled gradient flow control through context managers.
Task 5: Adversarial Training Loop
Two-Stage Updates: First trained discriminator to distinguish domains with frozen feature extractor, then trained feature extractor to fool discriminator while maintaining classification accuracy.
Loss Formulation: Combined classification loss on source domain with domain confusion loss, total loss = L_classification - α * L_domain, optimized using separate Adam optimizers for each component.
Batch Coordination: Synchronized source and target data loaders with iterator cycling, maintained 48 samples per domain per batch, handled dataset size mismatches through modulo indexing.
Task 6: Evaluation Framework
Test Set Preparation: Applied all 10 distortions to test_undistorted folder, generated test_distorted with preserved class structure, maintained consistent preprocessing pipeline.
Performance Metrics: Evaluated on both clean and distorted test sets separately, computed per-class and overall accuracy, compared vanilla ResNet50 vs domain-adapted model.
Result Analysis: Tracked domain discriminator loss convergence to 0.5 (maximum confusion), visualized feature distributions using t-SNE, analyzed per-distortion type performance breakdown.
Results
The domain-adversarial training successfully improves model robustness to image distortions with minimal impact on clean image performance. Vanilla ResNet50 achieves 72% accuracy on undistorted images but drops to 25% on distorted images. The domain-adapted model maintains 70% accuracy on undistorted images while achieving 67% on distorted images, a 42% absolute improvement. Domain discriminator loss converges to approximately 0.5, indicating successful domain confusion and feature alignment. The model shows consistent improvement across all 10 distortion types, with best performance on geometric transformations (75% accuracy) and moderate performance on noise-based distortions (62% accuracy). Training converges within 15 epochs with stable loss curves and no signs of mode collapse. Feature visualization reveals overlapping distributions between source and target domains after adaptation. The 35% performance gap between clean and distorted images is reduced to just 3% after domain adaptation.
Key Insights
-
Gradient Reversal Efficacy: The gradient reversal layer successfully creates domain-invariant features by forcing the feature extractor to produce indistinguishable representations across domains.
-
Unsupervised Adaptation Power: Achieving 67% accuracy without any labeled distorted images demonstrates the effectiveness of adversarial domain adaptation.
-
Alpha Parameter Criticality: The domain confusion weight α=0.2 provides optimal balance between maintaining source accuracy and improving target performance.
-
Distortion Type Variance: Geometric transformations are easier to adapt to than pixel-level corruptions, suggesting different feature dependencies.
-
Feature Alignment Success: Domain discriminator's inability to distinguish domains (loss→0.5) confirms successful feature distribution alignment.
Future Work
-
Multi-Source Domain Adaptation: Extend to handle multiple source domains simultaneously for improved generalization
-
Attention Mechanisms: Incorporate domain-specific attention modules for selective feature adaptation
-
Cycle-Consistent Adaptation: Add reconstruction loss to preserve semantic content during adaptation
-
Online Adaptation: Develop continuous adaptation capabilities for streaming data scenarios
-
Semantic Segmentation: Extend framework to dense prediction tasks beyond classification
-
Adversarial Robustness: Combine with adversarial training for robustness to both natural and adversarial perturbations

