Minecraft Ai for the lonely souls:
This readme is written by AI because I am very lazy This project currently only collects Minecraft environment data and trains a segmentation model on it. Further addition will be added if I'm in the mood
The mod jar is provided in this repository.
The recorder captures 3 files per frame:
- RGB image (
frame_xxxxx.png) - Masked semantic image (
frame_xxxxx_masked.png) - Metadata JSON (
frame_xxxxx.json)
It writes data in this structure:
And a shared label map file at root:
Default output location:
- Singleplayer:
.minecraft/saves/<current_world>/ai_data - Multiplayer fallback:
.minecraft/ai_data
-
/recordplayer <username>: choose which player to track for raycast labeling/metadata context. -
/recordframes <1-120>: set target capture rate in frames per second (triplets per second). -
/startrecord: start recording. -
/stoprecord: stop recording. -
/setoutputfolder <path>: override output directory.
- Install Fabric Loader for Minecraft
1.21.3. - Install Fabric API.
- Put the mod jar and Fabric API jar in
.minecraft/mods. - Launch Minecraft using the Fabric profile.
This repository expects:
Each split should contain:
PNG/Masked/Metadata/block_labels.json
Use this to verify paths if imports or file loading fail. If your folder tree is different, then you might have to rewrite some part of the code in the training python file
Ai Project/
best_minecraft_model.pth
Environment trainer.py
Environment tester.py
README.md
Data/
train_data/
block_labels.json
PNG/
Masked/
Metadata/
val_data/
block_labels.json
PNG/
Masked/
Metadata/
test_data/
block_labels.json
PNG/
Masked/
Metadata/
If you get path errors, check these first:
- Run scripts from project root (
Ai Project/). - Make sure split names are exactly
train_data,val_data,test_data. - Make sure subfolder names are exactly
PNG,Masked,Metadata. - Make sure
block_labels.jsonexists in each split folder. - Make sure frame triplets are matched (same frame id across PNG/mask/json).
Main trainer script:
EnvironmentDataset loads RGB images, masked PNGs, and metadata JSON files.
Masked PNGs are RGB-encoded class IDs, decoded as:
So mask tensors become H x W integer class maps.
The trainer reads semantic labels from block_labels.json (id_to_semantic if present, else id_to_block).
It builds:
- global
semantic_to_train - global
train_to_semantic - a LUT for fast remapping
This guarantees stable class meaning across all images.
Unknown/out-of-range labels are mapped to IGNORE_INDEX = -1 and ignored by losses.
The current baseline is simpleUNet (lightweight encoder/decoder segmentation model).
Training uses a combined loss:
- Cross-Entropy
- Focal Loss
Combined as:
This helps reduce collapse to dominant classes (like predicting mostly air).
Per epoch:
- Train on Data/train_data
- Validate on Data/val_data
- Save best checkpoint by validation loss
After training ends:
- Evaluate once on Data/test_data
Checkpoint file:
If it exists, training resumes from saved:
- model weights
- optimizer state
- last epoch
- best validation loss
This prevents losing progress during long CPU training runs.
Evaluation helper script:
Use it to test model behavior and inspect outputs without retraining.