Skip to content

Shu-Feather/Block-Tower-Stability-Predictor

Repository files navigation

Block Tower Stability Predictor

πŸ“‹ Project Overview

This project implements an InceptionV4-based deep learning model for predicting the stability of block towers. Using the ShapeStacks dataset, the model performs binary classification to determine whether a block tower will collapse (stable vs. unstable).

The project is herited from original codes, and is adapted for tensorflow 2+.

Background

Predicting structural stability is a critical challenge in architecture, robotics, and disaster prevention. Humans can quickly judge stability through visual intuition and experienceβ€”a skill rooted in implicit physical knowledge and causal reasoning. This project aims to replicate this capability using deep learning, bridging the gap between visual perception and physical reasoning.

Key Features

  • InceptionV4 Architecture: State-of-the-art CNN for image classification
  • Binary Classification: Predicts whether a tower is stable (0) or unstable (1)
  • ShapeStacks Dataset: Synthetic block tower images with stability annotations
  • Data Augmentation: Extensive augmentation pipeline for robust training
  • TensorBoard Integration: Real-time training monitoring and visualization

πŸ—‚οΈ Project Structure

Stability-Predictor/
β”œβ”€β”€ data_provider/
β”‚   └── shapestacks_provider.py      # Data loading and preprocessing
β”œβ”€β”€ tf_models/
β”‚   └── inception/
β”‚       β”œβ”€β”€ inception_v4.py          # InceptionV4 architecture
β”‚       └── inception_model.py       # Model definition and training logic
β”œβ”€β”€ intuitive_physics/
β”‚   └── stability_predictor/
β”‚       β”œβ”€β”€ train_inception_v4_shapestacks.py   # Training script
β”‚       └── test_inception_v4_shapestacks.py    # Testing script
β”œβ”€β”€ dataset/                         # This should be downloaded mannually
β”‚   └── shapestacks/                 # Dataset directory
β”‚       β”œβ”€β”€ recordings/              # Image data
β”‚       β”œβ”€β”€ splits/                  # Train/eval/test splits
β”‚       β”‚   └── ccs_all/
β”‚       β”‚       β”œβ”€β”€ train.txt
β”‚       β”‚       β”œβ”€β”€ eval.txt
β”‚       β”‚       β”œβ”€β”€ test.txt
β”‚       β”‚       └── *_bgr_mean.npy
β”‚       β”œβ”€β”€ mjcf/                    # MuJoCo simulation files
β”‚       └── meta/                    # Metadata and blacklists
β”œβ”€β”€ output_ccs_all/                  # Training output directory
β”‚   β”œβ”€β”€ snapshots/                   # Best model checkpoints
β”‚   └── events.out.tfevents.*        # TensorBoard logs
└── README.md

πŸ”§ Installation

Prerequisites

  • Python 3.8+
  • CUDA 11.0+ (for GPU support)
  • cuDNN 8.0+
  • tensorflow 2.0.0+

Environment Setup

Following is performed on Nvidia A100, 80G.

# Create conda environment compatible with A100
conda create -n stability_predictor python=3.8
conda activate stability_predictor

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1 -y

# Install TensorFlow (GPU version)
pip install tensorflow==2.8.0

# Install other dependencies
pip install numpy matplotlib Pillow

πŸ“¦ Dataset

Download

Download the ShapeStacks dataset from the provided link and extract it to lab3_dataset/shapestacks/.

Dataset Structure

shapestacks/
β”œβ”€β”€ recordings/          # RGB images of block towers
β”‚   └── env_*/
β”‚       β”œβ”€β”€ rgb-*.png
β”‚       β”œβ”€β”€ log.txt      # Contains "Stack collapse: True/False"
β”‚       └── depth_log.txt
β”œβ”€β”€ splits/
β”‚   └── ccs_all/         # Data split configuration
β”‚       β”œβ”€β”€ train.txt    # Training scenarios
β”‚       β”œβ”€β”€ eval.txt     # Validation scenarios
β”‚       β”œβ”€β”€ test.txt     # Test scenarios
β”‚       └── *_bgr_mean.npy  # Mean values for normalization
└── mjcf/                # MuJoCo configuration files

Label Convention

  • Label 0: Stable tower (Stack collapse: False)
  • Label 1: Unstable tower (Stack collapse: True)

πŸš€ Quick Start

Following is how to train and test this model quickly. You can view detailed command-line instructions in run.sh.

1. Set Environment Variable

export SHAPESTACKS_CODE_HOME=./

2. Training

# Basic training
export CUDA_VISIBLE_DEVICES=0  # Use single GPU, you can modify it

# use ccs_all split data
python intuitive_physics/stability_predictor/train_inception_v4_shapestacks.py \
  --data_dir /path/to/lab3_dataset/shapestacks \
  --model_dir ./output_ccs_all \
  --split_name ccs_all \
  --train_epochs 40 \
  --batch_size 32

# use blocks_all split data
python intuitive_physics/stability_predictor/train_inception_v4_shapestacks.py \
  --data_dir /path/to/lab3_dataset/shapestacks \
  --model_dir ./output_blocks_all \
  --split_name blocks_all \
  --train_epochs 40 \
  --batch_size 32

# or choose to run in background with logging
nohup python intuitive_physics/stability_predictor/train_inception_v4_shapestacks.py \
  --data_dir /path/to/lab3_dataset/shapestacks \
  --model_dir ./output_ccs_all \
  --split_name ccs_all \
  --train_epochs 40 \
  --batch_size 32 > train_ccs.log 2>&1 &

nohup python intuitive_physics/stability_predictor/train_inception_v4_shapestacks.py \
  --data_dir /data1/shuyang/lab3_dataset/shapestacks \
  --model_dir ./output_blocks_all \
  --split_name blocks_all \
  --train_epochs 40 \
  --batch_size 32 > train_blocks.log 2>&1 &

3. Monitor Training

# Monitor log file
tail -f train_ccs.log

tail -f train_blocks.log

# Monitor GPU usage
watch -n 1 nvidia-smi

# Start TensorBoard
tensorboard --logdir=./output_ccs_all --port=6006
# Open browser: http://localhost:6006

4. Testing

python intuitive_physics/stability_predictor/test_inception_v4_shapestacks.py \
  --data_dir /path/to/lab3_dataset/shapestacks \
  --model_dir ./output_ccs_all \
  --split_name ccs_all \
  --batch_size 32

python intuitive_physics/stability_predictor/test_inception_v4_shapestacks.py \
  --data_dir /data1/shuyang/lab3_dataset/shapestacks \
  --model_dir ./output_blocks_all \
  --split_name blocks_all \
  --batch_size 32

βš™οΈ Configuration

Training Parameters

Parameter Default Description
--data_dir Required Path to ShapeStacks dataset
--model_dir Required Directory for model outputs
--split_name ccs_all Dataset split to use
--train_epochs 40 Total training epochs
--epochs_per_eval 1 Evaluation frequency
--batch_size 32 Training batch size
--n_best_eval 5 Number of best checkpoints to keep
--memcap 0.8 GPU memory fraction to use
--n_prefetch 32 Number of batches to prefetch

Data Augmentation

Default augmentations applied during training:

  • Random cropping and resizing
  • Random horizontal flip
  • Random rotation (Β±2 degrees)
  • Color jittering (brightness, saturation, hue, contrast)
  • Gaussian noise
  • Value clipping
# Customize augmentation
python train_inception_v4_shapestacks.py \
  --augment crop flip recolour clip \
  ...

πŸ“Š Expected Results

Training Progress

Epoch 1/40
INFO:tensorflow:loss = 0.693147, step = 1
INFO:tensorflow:loss = 0.682340, step = 100
...

Evaluation results:
  accuracy: 0.652341
  global_step: 5000
  loss: 0.621234

Top-5 models so far:
  Accuracy: 0.652341 - eval=0.652341

πŸ“ˆ Monitoring and Visualization

TensorBoard Metrics

Available metrics:

  • Loss: Training and validation loss curves
  • Accuracy: Training and validation accuracy
  • Learning Rate: RMSProp learning rate schedule
  • Images: Sample input images (if --display_inputs > 0)

Output Files

output_ccs_all/
β”œβ”€β”€ model.ckpt-*              # Latest checkpoint
β”œβ”€β”€ checkpoint                # Checkpoint metadata
β”œβ”€β”€ events.out.tfevents.*     # TensorBoard logs
β”œβ”€β”€ graph.pbtxt               # Model graph
β”œβ”€β”€ snapshots/                # Best models
β”‚   β”œβ”€β”€ eval=0.850000/
β”‚   β”‚   β”œβ”€β”€ model.ckpt-*
β”‚   β”‚   └── checkpoint
β”‚   └── ...
└── info_*.txt                # Training configuration

πŸ”¬ Advanced Usage

Custom Learning Rate Schedule

Modify inception_model.py:

# Add learning rate decay
global_step = tf.train.get_global_step()
learning_rate = tf.train.exponential_decay(
    learning_rate=_START_LEARNING_RATE,
    global_step=global_step,
    decay_steps=1000,
    decay_rate=0.9,
    staircase=True
)

optimizer = tf.train.RMSPropOptimizer(
    learning_rate=learning_rate,
    decay=_DECAY,
    epsilon=_EPSILON
)

# Log learning rate
tf.summary.scalar('learning_rate', learning_rate)

Resume Training

Training automatically resumes from the latest checkpoint if model_dir contains previous checkpoints:

# Will resume from existing checkpoint
python train_inception_v4_shapestacks.py \
  --model_dir ./output_ccs_all \
  ...

πŸ“š References

  1. InceptionV4 Paper: Inception-v4, Inception-ResNet and the Impact of Residual Connections on Learning
  2. ShapeStacks Dataset: ShapeStacks: Learning Vision-Based Physical Intuition for Generalised Object Stacking
  3. TensorFlow Estimator API: TensorFlow Estimator Guide

πŸ€— Contact

If you find this project insightful and helpful, or if you find there exist bugs in the project, feel free to email: shuyoung@stanford.edu

About

The Lab Project for Cognition and Reasoning

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors