Skip to content

About

Python/TensorFlow toolkit for training neural networks to super-resolve gridded cosmological simulations

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cosmoSR

Neural-network super-resolution for gridded cosmological dark-matter simulations: learn the mapping from a low-resolution to a high-resolution density field, then apply it to a large, cheap low-resolution box.

This was a research project in my PhD (thesis chapter 6) at the Max Planck Institute for Astrophysics. The networks produce outputs that are visually close to the high-resolution ground truth. In 2D the power spectrum is also recovered to much smaller scales than the input. On full 3D boxes, the summary statistics in physical density units are not yet better than the low-resolution input; the analysis below points to how the large dynamic range is compressed before training.

Motivation

Running cosmological simulations at high resolution across a large volume is computationally prohibitive. The idea:

  1. Run the large low-resolution simulation box you need.
  2. Run two small boxes from matched initial conditions: one at low and one at high resolution.
  3. Cut the small boxes into training tiles.
  4. Train a network to predict the high-resolution structure from the low-resolution input.
  5. Apply the trained network to the full large box.
Low resolution (256³ particles) Super-resolved High resolution (512³ particles)
LR SR HR

Slice through an unseen test box, logarithmic colour scale.

Method

Data. Initial conditions from MUSIC for a (10 cMpc/h)³ box, run to redshift ≈ 2.3 at 256³ and 512³ particles with identical random seeds on shared scales. Both are gridded onto the same 512³ grid, so densely sampled regions keep their information in the low-resolution input. The training box is cut into 512 tiles of 64³ voxels, and rotations and flips expand this to 5,120 tiles. A second set of simulations with different seeds is held out for testing.

Scaling. Densities span many orders of magnitude, so voxels are compressed before training:

$$x_{\text{scaled}} = \frac{\ln(x + 10^{-9})}{25}$$

Architecture. Multi-pass approach with 2D kernels instead of 3D (far fewer parameters). Two residual convolutional generators, $G(x) = x + \varphi(x)$, so the network only learns the difference between low and high resolution. The first generator super-resolves slices along one axis; the second receives the result rotated by 90° and repairs discontinuities between slices along a second axis.

Losses.

  • Pixel losses: $\ell_1$, $\ell_2$, and $\ell_1 + \ell_2 + \ell_\infty$ (the $\ell_\infty$ term targets density peaks), with weights chosen so that each term contributes gradients of similar size.
  • Adversarial: Wasserstein critic with gradient penalty, 5 critic updates per generator update.

Training ran for about 24 hours (about 150 epochs) on a single NVIDIA V100.

Results

2D proof of concept

Single generator and critic (about 26k and 23k parameters), upscaling a held-out slice by a factor of 2 (the input here is the ground-truth slice degraded by a factor of 2, not an independent low-resolution simulation).

Power spectrum relative to ground truth Pixel-value histogram relative to ground truth
2D power spectrum 2D histogram

The super-resolved power spectrum stays at the ground-truth level up to roughly 10 h/Mpc, while the degraded input and bicubic interpolation fall off from roughly 3 to 4 h/Mpc. The super-resolved slice also keeps the high-density pixel counts closer to the truth.

3D boxes (thesis)

With the multi-pass generators on full 3D test boxes:

  • Slices look nearly indistinguishable from the ground truth for all pixel losses.
  • In physical density units, the statistics do not beat the low-resolution input. Only the median improves. The power spectrum has 20 to 35% too much power at k ≈ 1 to 5 h/Mpc and too little at the smallest scales, while the input is within a few percent (left; red: $\ell_1$, green: $\ell_2$, blue: $\ell_1+\ell_2+\ell_\infty$, cyan: input).
  • The adversarial version did not yet improve on the input.
Power spectrum relative to ground truth Replacing the densest voxels with input values
3D power spectrum Peak replacement

Diagnosis. The network is trained in log space, where errors on the rare, very dense voxels are tiny. After transforming back with the exponential, those errors dominate. Replacing only the voxels above 100 times the mean density (0.075% of all voxels) with their low-resolution values removes the small-scale suppression entirely (right; black: no replacement, blue: overdensity above 100, green: above 10, red: all overdense voxels, cyan: input). A roughly constant offset remains that is not yet explained.

Next steps

  • Apply the log scaling inside the loss instead of to the network input, or train without scaling (feasible because the residual design only learns small corrections).
  • Add the summary statistics that matter (power spectrum, density distribution) directly to the loss.
  • Replace the multi-pass approach with a single-pass 3D network, first with pixel losses, then adversarially.

Status of the code

The 2D training code used for the experiments is in deprecated/. The package in src/ is a refactoring towards the single-pass 3D network: it installs with pip install . and trains the residual 3D network with the pixel loss. The adversarial losses are not yet adapted to 3D.

import tensorflow as tf
from cosmoSR import NetworkTrainer
from cosmoSR.building_blocks import FullModel, NetworkModel, Loss
from cosmoSR.networks import cnn_3d
from cosmoSR.loss import l2_loss

# lr_tiles, hr_tiles: arrays of shape (n_tiles, 64, 64, 64, 1)
generator = NetworkModel(cnn_3d, tf.keras.optimizers.Adam(1e-3))
model = FullModel(generator, Loss(l2_loss))
history = NetworkTrainer(lr_tiles, model, hr_tiles, epochs=10, batch_size=4).train()

Repository layout

SuperResolution/
├── scripts/
│   └── config.py         # Run configuration
├── src/
│   ├── networks/         # Network architectures (cnn_3d.py: residual 3D network)
│   ├── loss/             # Pixel and Wasserstein critic losses
│   ├── building_blocks/  # Dataset generation, model assembly, run tracking
│   ├── network_trainer.py
│   └── utils.py
├── deprecated/           # 2D research code (training loop, GAN, scaling functions)
├── images/               # Figures used in this README
└── tests/

License

GPL-3.0

About

Python/TensorFlow toolkit for training neural networks to super-resolve gridded cosmological simulations

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages