library(petrographer)
validate_dataset("data/processed/my_dataset")This vignette explains the current petrographer training workflow at a high level. It covers:
For a runnable end-to-end template, see inst/notebooks/templates/train_model.qmd.
petrographer supports two RF-DETR training paths:
Use detection when you mainly need counts, locations, and class labels. Use segmentation when you need mask-derived measurements such as area, eccentricity, circularity, or orientation.
Training expects a COCO-style dataset with train/ and valid/ splits.
library(petrographer)
validate_dataset("data/processed/my_dataset")Expected structure:
my_dataset/
├── train/
│ ├── _annotations.coco.json
│ └── *.jpg / *.png
└── valid/
├── _annotations.coco.json
└── *.jpg / *.png
If the source images are very large or contain many small objects, slicing can make training more stable and improve downstream detection performance.
slice_dataset(
input_dir = "data/raw/large_images",
output_dir = "data/processed/my_dataset_sliced",
slice_size = 1024,
overlap = 0.2
)This is usually most helpful for detection workflows. Segmentation currently trains on the prepared dataset the same way, but prediction does not yet use SAHI slicing for RF-DETR segmentation models.
Pinning makes the training data reproducible and lets the resulting model record the exact dataset id/version used during training.
pin_dataset(
data_dir = "data/processed/my_dataset",
dataset_id = "my_dataset_v1"
)
list_datasets()The main entry point is train_model(). You can supply either a raw dataset directory or a previously pinned dataset_id.
detector_id <- train_model(
dataset_id = "my_dataset_v1",
model_id = "my_detector_v1",
model_variant = "small",
epochs = 50,
batch_size = 2,
device = "cuda"
)segmenter_id <- train_model(
dataset_id = "my_dataset_v1",
model_id = "my_segmenter_v1",
model_variant = "seg_small",
epochs = 50,
batch_size = 4,
device = "cuda"
)Training mode is auto-detected:
hipergator::hpg_configure() has been set up, train_model() uses the HPC pathThat means the user-facing training call stays the same in both environments.
Local training is the simplest option. It is appropriate when:
HPC training is useful when:
Example configuration:
library(hipergator)
hpg_configure(
host = "hpg",
base_dir = "/blue/mygroup/myusername/petrographer"
)Once configured, the same train_model() call will submit through the HPC path instead of running locally.
Detection variants:
nanosmallmediumlargeSegmentation variants:
seg_nanoseg_smallseg_mediumseg_largeseg_xlargeseg_2xlargeseg_previewIn general:
nano / small are good starting points for detection. seg_nano / seg_small are good starting points for segmentation.
Successful training pins a model to the local model board and writes:
checkpoint_best_total.pthmanifest.jsontraining_summary.jsonmetrics.csv, hparams.yaml, log.txt, or results.json when availableThe stable package contract is:
manifest.json for model/task/category/artifact metadatatraining_summary.json for normalized training history and final metricsLoad the trained model:
model <- from_pretrained("my_detector_v1", board = "local", device = "cpu")
modelInspect normalized training outputs:
eval_result <- evaluate_training(model_id = "my_detector_v1")
eval_result$summaryDetection models can be evaluated with SAHI + COCO metrics:
sahi_eval <- evaluate_model_sahi(
model = model,
annotation_json = "data/processed/my_dataset/valid/_annotations.coco.json",
image_dir = "data/processed/my_dataset/valid",
use_slicing = TRUE,
slice_size = 640,
overlap = 0.2,
max_dets = 300
)
sahi_eval$summarySegmentation currently does not use the SAHI evaluation path. Instead, the main downstream value is prediction plus morphology analysis.
seg_model <- from_pretrained("my_segmenter_v1", board = "local", device = "cpu")
seg_batch <- analyze_segmentation_dir(
input_dir = "data/processed/my_dataset/valid",
model = seg_model,
output_dir = "results/segmentation_batch"
)
seg_batch$summary
seg_batch$population_statsEvery successful training run is pinned locally. Maintainers can copy a completed local pin to a shared board with pin_model() and then refresh the destination board manifest with pins::write_board_manifest().
See inst/notebooks/templates/model_from_pretrained.qmd for the maintainer publishing workflow.
CUDA out of memory
batch_sizeTraining is slow
Predictions are unlabeled or mislabeled
categories are correct before trainingmodel$manifest$categories after loadingNeed a runnable example
inst/notebooks/templates/train_model.qmd as the executable templateops/ notebooks only for maintainer or library-building workflows