Show HN: TerrainSR – fast, realistic heightmap upscaling model

原始链接: https://huggingface.co/joe-gibbs/terrainsr

相关文章

原文

TerrainSR is a model that allows you to generate realistic higher-detail terrain height data from lower-detail terrain height data. It is trained on 100m -> 10m pairs, and will give good results for higher-resolution than 100m data (even extrapolating down past 10m detail) but will break down with lower-resolution data than that.

TerrainSR was deliberately trained without built-up areas or human-modified land such as open-cut mines or cities, so that the generated outputs will be free of such artifacts.

Uses

The model was originally produced (by me) to allow a lower-resolution 100m map of Europe to be used for a real-scale historical strategy game - a 10m dataset will both be hundreds of gigabytes and therefore unfeasible, and will also require a very, very large amount of manual labour to remove the obvious artifacts of current-day Europe (for instance roads, docks, potentially buildings), while a 100m-resolution dataset will be compact and also smooth these details out, so that the model can add plausible terrain detail on to this data. In some cases this can do odd things like turn steep data for large buildings in the 100m data into hills.

TerrainSR is faster and more varied than using a traditional erosion simulator for large terrains.

Speed

On my RTX 4070, TerrainSR generates a 50x50km patch from 100m to 10m in 0.74s after loading the model. This makes it applicable for realtime applications such as gaming.

Comparisons

London - Hampstead and Highgate

Alps - Saint-Gervais, France

French hills - Ardeche

Morocco - Tafraoute, Anti-Atlas

Rocky Mountains - Estes Park, Colorado

Gentle hills in Japan - Kaji Hills

Flat land - Cambridgeshire Fens, England

How to use

Download the repository, including the Python files and the weights folder.

Use Python 3.10 or newer and install PyTorch 2.6 or newer using the PyTorch installation instructions. For an NVIDIA GPU, choose a CUDA build that works with your system. Open a terminal in the model folder and run:

python -m pip install -r requirements.txt
python terrainsr.py --input examples/synthetic_input.npz --output terrain_10m.npy --crop 32

This runs the included example and saves a 160x160 array of terrain heights. You can add --device cpu to run it on the CPU instead, although that will be slower.

For your own terrain, prepare a 2D array of heights on a 100m grid (in metres) and a water mask on a 10m grid. The mask should be 1 for water and 0 for land, with ten times as many rows and columns as the height array.

Include 3.2km of surrounding terrain on every side of the area you want. crop=32 removes those 32 cells of context from each edge of the result. For example, a 104x104 input at 100m, with a 1040x1040 water mask, gives a 400x400 output at 10m after cropping - a 4x4km patch.

Run this from the model folder, using your .npy array:

import numpy as np
from terrainsr import TerrainSR

coarse = np.load("coarse_100m.npy")
water = np.load("water_10m.npy")

model = TerrainSR(device="auto")
heights = model.predict(coarse, water, seed=0, crop=32)
np.save("terrain_10m.npy", heights)

The input dimensions must be even and no larger than 564 cells on either side, the output is a float32 array of heights in metres. You can also run your own arrays through the command line. Save them with:

np.savez_compressed("input.npz", coarse=coarse, water=water)

Then run:

python terrainsr.py --input input.npz --output terrain_10m.npy --crop 32 --seed 0

Licence

The model weights and inference code are released under the Apache 2.0 licence. You can use, modify and redistribute them, including in commercial projects, as long as you follow the licence terms and keep the required licence and attribution notices with redistributed copies.

The elevation and land-cover datasets have their own terms. Training data comes from swisstopo, Kartverket, IGN and USGS, with Copernicus DEM, ESA WorldCover and OpenStreetMap used for inputs and masks.

联系我们 contact @ memedata.com