Skip to content

Latest commit

Β 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GridNav-AI πŸ€–πŸ—ΊοΈ

Version Python PyTorch Streamlit

A robot navigation project exploring two fundamentally different approaches to the same problem β€” Supervised Learning (ResNet CNN imitating BFS) and Reinforcement Learning (DQN-LSTM learning through trial and error).


Demo

Supervised Learning (ResNet)

Robot Navigation

Reinforcement Learning (DQN-LSTM)

RL Navigation


Two Approaches, One Problem

Supervised (ResNet) Reinforcement (DQN-LSTM)
How it learns Imitates BFS optimal paths Trial and error with rewards
Needs labels? βœ… Yes β€” BFS solutions ❌ No
State input Full grid (3-channel tensor) 5Γ—5 vision window
Actions 8 directions 4 directions
Architecture ResNet CNN DQN-LSTM
Generalization Any grid same size βœ… Random grids, variable density
Training time ~10-15 min ~30-60 min

How Each Approach Works

Supervised Learning Pipeline

  1. Random Grid Generation β€” generates grids with obstacles, robot R, and target T
  2. BFS Optimal Pathfinding β€” finds shortest path (used as training labels)
  3. Dataset Creation β€” each BFS step becomes a (state, action) training pair
  4. ResNet Training β€” CNN learns to predict next optimal move from grid state
  5. Simulation β€” trained model navigates new unseen grids

Reinforcement Learning Pipeline

  1. Random Grid Generation β€” new random grid every episode, variable obstacle density
  2. DQN-LSTM Agent β€” 5Γ—5 vision window, hidden state carries memory between steps
  3. Reward Shaping β€” warm/cold signal toward target + revisit penalty
  4. Fixed Eval Set β€” 20 fixed grids measure true generalization (like validation set)
  5. Early Stopping β€” stops when 90% success rate achieved on eval set

Model Architectures

ResNet CNN (Supervised)

Input (3 channels):
β”œβ”€β”€ Channel 0: Obstacle map
β”œβ”€β”€ Channel 1: Robot position
└── Channel 2: Target position
        ↓
Initial Conv2D (32 filters) + BN + ReLU
        ↓
Residual Block 1 (32 β†’ 32)
        ↓
Residual Block 2 (32 β†’ 128, with 1Γ—1 downsample)
        ↓
Flatten β†’ Dropout(0.6) β†’ FC(128) β†’ Dropout(0.6) β†’ FC(8)
        ↓
Output: 8 actions (UP, DOWN, LEFT, RIGHT, diagonals)

DQN-LSTM (Reinforcement)

Input (30 values):
β”œβ”€β”€ 5Γ—5 vision window (25 values)
β”‚     0.0=free, 0.5=OOB, 1.0=obstacle, 3.0=target
β”œβ”€β”€ Normalized robot position (2 values)
β”œβ”€β”€ Normalized target position (2 values)
└── Exploration progress (1 value)
        ↓
Encoder: Linear(30β†’128) β†’ LayerNorm β†’ ReLU β†’ Linear(128β†’128) β†’ LayerNorm β†’ ReLU
        ↓
LSTM(128β†’128) β€” carries memory (h,c) between steps
        ↓
Decoder: Linear(128β†’64) β†’ ReLU β†’ Linear(64β†’4)
        ↓
Output: 4 Q-values (UP, DOWN, LEFT, RIGHT)

Training Results

Supervised Learning

Training Curves

Metric Value
Training Samples 5,000
Grid Size 15Γ—15
Obstacle Density 25%
Epochs 50 (early stopping)
Optimizer Adam (lr=5e-4, wd=5e-3)
Loss CrossEntropyLoss

Reinforcement Learning

RL Training Curves

Metric Value
Grid Size 15Γ—15
Obstacle Density 10%–35% (random each episode)
Episodes 5000–8000
Optimizer Adam (lr=1e-3, wd=1e-4)
Eval Set 20 fixed grids
Best Success Rate ~70% on unseen random grids

Project Structure

GridNav-AI/
β”œβ”€β”€ README.md
β”œβ”€β”€ requirements.txt
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ path_finder.py              # Supervised ResNet training + simulation
β”‚   β”œβ”€β”€ reinforcement_lesson_2.py   # Q-Table (fixed grid)
β”‚   β”œβ”€β”€ reinforcement_lesson_3.py   # DQN (position + target)
β”‚   β”œβ”€β”€ reinforcement_lesson_4.py   # DQN blind robot (reward shaping)
β”‚   β”œβ”€β”€ reinforcement_lesson_5.py   # DQN-LSTM (5Γ—5 vision, random grids) ← main RL
β”‚   └── reinforcement_lesson_6.py   # BPTT (sequence training, experimental)
β”‚
β”œβ”€β”€ demo/
β”‚   β”œβ”€β”€ app.py                      # Streamlit home page
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”œβ”€β”€ grid_utils.py           # Shared grid generation + rendering
β”‚   β”‚   β”œβ”€β”€ rl_model.py             # DQN-LSTM inference + training
β”‚   β”‚   └── supervised_model.py     # ResNet inference + training
β”‚   └── pages/
β”‚       β”œβ”€β”€ 1_Training.py           # Live training (RL + Supervised)
β”‚       β”œβ”€β”€ 2_Inference.py          # Side-by-side model comparison
β”‚       └── 3_Grid_Builder.py       # Draw custom grids, benchmark models
β”‚
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ stage3_best.pth             # Best RL model
β”‚   └── supervised_best.pth         # Best supervised model
β”‚
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ robot_animation.gif
β”‚   β”œβ”€β”€ stage3_animation.gif
β”‚   β”œβ”€β”€ training_history.png
β”‚   └── stage3_rewards.png
β”‚
└── .gitignore

Requirements

pip install -r requirements.txt
torch>=2.0.0
numpy>=1.24.0
matplotlib>=3.7.0
tqdm>=4.65.0
pillow>=9.0.0
streamlit>=1.28.0
plotly>=5.17.0

Setup & Run

Clone

git clone https://github.com/WeskerPRO/GridNav-AI.git
cd GridNav-AI

Train Supervised (ResNet)

cd src
python path_finder.py

Train Reinforcement (DQN-LSTM)

cd src
python reinforcement_lesson_5.py

Run Streamlit Demo

cd demo
streamlit run app.py

The demo has three pages:

  • Training β€” watch RL agent or ResNet train live with real-time curves
  • Inference β€” load trained models, compare RL vs Supervised side by side
  • Grid Builder β€” draw your own maze, benchmark all models + BFS

RL Training Notes

πŸ’‘ Per-episode reward oscillates on random grids β€” this is expected. Grid difficulty varies each episode. Use success rate on the fixed eval set as the true learning metric (equivalent to validation accuracy).

⚠️ Train on the same grid size you test on. RL model normalizes positions by grid dimensions β€” a model trained on 15Γ—15 expects 15Γ—15 inputs.

🧠 The model needs the target coordinates in its state to generalize across random grids. Without them, reward shaping sends contradictory signals on different grid layouts.


Roadmap

βœ… Completed

  • βœ… Random grid generator with guaranteed solvable paths
  • βœ… BFS optimal pathfinding for label generation
  • βœ… ResNet CNN with residual blocks + Dropout regularization
  • βœ… Training with early stopping + LR scheduler
  • βœ… Q-Table navigation (fixed grid)
  • βœ… DQN with position + target coordinates
  • βœ… DQN blind robot with reward shaping
  • βœ… DQN-LSTM with 5Γ—5 vision window
  • βœ… Random grid training for generalization
  • βœ… Fixed eval set (validation equivalent for RL)
  • βœ… Streamlit demo with live training + inference + grid builder
  • βœ… BPTT sequence training (experimental)
  • βœ… 3D grid pathfinding (experimental)

🚧 In Progress

  • πŸ”„ Model generalization improvement (target: 80%+ success rate)

πŸ“‹ Upcoming

  • Fog of war exploration (partial map reveal)
  • Larger grid support (35Γ—35+)

License

License: MIT

This project is licensed under the MIT License β€” free to use, modify, and distribute.


Made with ❀️ by WeskerPRO

Supervised Learning meets Reinforcement Learning β€” same robot, two minds.

About

Comparing ResNet imitation learning vs DQN-LSTM reinforcement learning for robot grid navigation. PyTorch + Streamlit demo.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages