Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions crates/analysis/src/neural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ pub struct Trained {
/// The epochs run (fewer than asked when the work would pass [`MAX_WORK`] in a run that can't
/// be cancelled).
pub epochs: usize,
/// The epochs asked (within 1 to [`MAX_EPOCHS`]).
pub epochs_asked: usize,
/// The training samples fitted each epoch (fewer than [`Trained::training`] when they would
/// pass [`MAX_WORK`] in a run that can't be cancelled: a random subset).
pub fitted: usize,
/// The samples kept for training (the rest are held back to measure the accuracy).
pub training: usize,
}

impl Trained {
/// What a run that can't be cancelled left out to stay within its work limit, to be said: `None`
/// when it fitted every training sample for every epoch asked.
pub fn limited(&self) -> Option<String> {
if self.fitted >= self.training && self.epochs >= self.epochs_asked {
return None;
}
Some(format!(
"trained on {} of {} training samples for {} of {} epochs, the work limit of a run that can't be cancelled{}",
self.fitted,
self.training,
self.epochs,
self.epochs_asked,
crate::parallel::UNCAPPED_HINT
))
}
}

/// Trains a classifier. `samples` are (band values, class index); `hidden` the units per hidden
Expand Down Expand Up @@ -292,9 +317,14 @@ pub fn train(
let per_sample = 3.0 * layers.iter().map(|l| l.w.len() + l.b.len()).sum::<usize>() as f64;
let max_work = if crate::control::uncapped() { f64::INFINITY } else { MAX_WORK };
let mut fit = fit;
let training = fit.len();
fit.truncate(((max_work / per_sample).min(usize::MAX as f64) as usize).max(1));
let per_epoch = per_sample * fit.len().max(1) as f64;
let epochs = epochs.clamp(1, MAX_EPOCHS).min(((max_work / per_epoch).min(usize::MAX as f64) as usize).max(1));
if !(1..=MAX_EPOCHS).contains(&epochs) {
return Err(format!("epochs is 1 to {MAX_EPOCHS}, not {epochs}"));
}
let epochs_asked = epochs;
let epochs = epochs_asked.min(((max_work / per_epoch).min(usize::MAX as f64) as usize).max(1));
let (lr, momentum, batch) = (0.05f32, 0.9f32, 32usize);
let mut vel: Vec<(Vec<f32>, Vec<f32>)> = layers.iter().map(|l| (vec![0.0; l.w.len()], vec![0.0; l.b.len()])).collect();
crate::control::begin(epochs);
Expand Down Expand Up @@ -383,7 +413,7 @@ pub fn train(
let model = Mlp { bands: bands.to_vec(), mean, std, layers, classes };
let correct = test.iter().filter(|&&i| samples.get(i).is_some_and(|(x, c)| model.predict(x).0 == *c)).count();
let accuracy = if test.is_empty() { 1.0 } else { correct as f64 / test.len() as f64 };
Ok(Trained { model, accuracy, samples: samples.len(), epochs })
Ok(Trained { model, accuracy, samples: samples.len(), epochs, epochs_asked, fitted: fit.len(), training })
}

/// Classifies every pixel: class numbers (1-based, `nodata` where a band has no data) and each
Expand Down Expand Up @@ -534,6 +564,21 @@ mod tests {
// The largest network for every epoch would pass the work cap: fewer epochs run.
let big = train(&[0, 1], vec!["out".into(), "in".into()], &samples, &[MAX_UNITS; MAX_LAYERS], MAX_EPOCHS).unwrap();
assert!(big.epochs < MAX_EPOCHS, "{} epochs", big.epochs);
// ...and it says so, with what it ran of what was asked.
let said = big.limited().expect("a capped run says what it left out");
assert!(said.contains(&format!("for {} of {MAX_EPOCHS} epochs", big.epochs)), "{said}");
assert_eq!(t.limited(), None, "a run within the limit says nothing");
// Too many samples for even one epoch of the largest network: a random subset is fitted,
// and said with its count.
let many: Vec<(Vec<f32>, usize)> = (0..12_000).map(|i| (vec![(i % 97) as f32, (i % 89) as f32], i % 2)).collect();
let cut = train(&[0, 1], vec!["a".into(), "b".into()], &many, &[MAX_UNITS; MAX_LAYERS], 1).unwrap();
assert!(cut.fitted < cut.training && cut.epochs == 1, "{} of {}", cut.fitted, cut.training);
let said = cut.limited().expect("a cut sample set is said");
assert!(said.contains(&format!("on {} of {} training samples for 1 of 1 epochs", cut.fitted, cut.training)), "{said}");
// Run so it can be cancelled, it fits every sample for every epoch and says nothing.
let control = crate::control::Control::stoppable();
let whole = crate::run_under("Train", &control, || train(&[0, 1], vec!["out".into(), "in".into()], &samples, &[8], 3)).unwrap();
assert_eq!((whole.fitted, whole.epochs, whole.limited()), (whole.training, 3, None));
Comment thread
pick2510 marked this conversation as resolved.
Resolved
assert!(train(&[0, 1], vec!["a".into(), "b".into()], &samples[..2], &[4], 1).is_err(), "too few");
}
}
31 changes: 29 additions & 2 deletions crates/engine/src/cmd/deeplearning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ pub struct DlModel {
/// Epochs the last training ran (not saved).
#[serde(skip)]
pub epochs: usize,
/// What the last training left out to stay within the work limit of a run that can't be
/// cancelled ([`neural::Trained::limited`]), said when the model is kept (not saved).
#[serde(skip)]
pub limited: Option<String>,
}

impl DlModel {
/// Says what its training left out to stay within the work limit, if anything (AGENTS.md ›
/// Never silent), and puts it in the command's answer.
fn say_limited(&self, s: &mut Session, v: &mut Value) {
if let Some(what) = &self.limited {
s.warn(format!("{}: {what}", self.name));
Comment thread
pick2510 marked this conversation as resolved.
Resolved
v["limited"] = json!(what);
}
}
}

fn need_raster(s: &Session) -> Option<&'static str> {
Expand All @@ -51,6 +66,7 @@ pub fn commands() -> Vec<C> {
let (m, unread) = train(s, p)?;
unread.say(s);
let mut r = json!({"model": m.name, "accuracy": m.accuracy, "classes": m.model.classes, "epochs": m.epochs});
m.say_limited(s, &mut r);
r["replaced"] = json!(store(s, m));
Ok(r)
}).inputs(training_inputs).roles(&[]).effects(crate::policy::Effects::NONE)
Expand Down Expand Up @@ -209,7 +225,8 @@ impl Training {
return Err(format!("the class '{c}' has fewer than 5 pixels with data under its polygons"));
}
let t = neural::train(bands, classes, &samples, hidden, epochs)?;
Ok(DlModel { name, model: t.model, accuracy: t.accuracy, epochs: t.epochs })
let limited = t.limited();
Ok(DlModel { name, model: t.model, accuracy: t.accuracy, epochs: t.epochs, limited })
}
}

Expand Down Expand Up @@ -265,7 +282,16 @@ fn training(s: &Session, p: &Value) -> Result<Training, String> {
Some(a) => a.iter().filter_map(Value::as_u64).map(|u| u as usize).take(neural::MAX_LAYERS).collect(),
None => vec![32, 16],
};
let epochs = f(p, "epochs").unwrap_or(60.0).clamp(1.0, neural::MAX_EPOCHS as f64) as usize;
// A count given that isn't a whole number from 1 to 500 is refused, not cut to one or left
// for the default: the training would say nothing of the epochs left out.
let epochs = match p.get("epochs").filter(|v| !v.is_null()) {
None => 60.0,
// Read as other counts are (a numeric string too), then checked.
Some(v) => f(p, "epochs")
.filter(|e| (1.0..=neural::MAX_EPOCHS as f64).contains(e) && e.fract() == 0.0)
.ok_or_else(|| format!("epochs is 1 to {}, not {v}", neural::MAX_EPOCHS))?,
Comment thread
pick2510 marked this conversation as resolved.
Resolved
};
let epochs = epochs as usize;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// Without a name given, a new model never replaces one: it gets a name of its own.
let (name, auto) = match ps(p, "model") {
Some(_) => (name(&json!({"name": p.get("model")}), format!("{}_model", r.name)), None),
Expand Down Expand Up @@ -342,6 +368,7 @@ pub(crate) fn classify_work(s: &Session, p: &Value) -> Result<super::workflows::
m.name = free_name(s, &base);
v["model"] = json!(m.name);
}
m.say_limited(s, v);
v["replaced"] = json!(store(s, m));
}) as super::workflows::Then
});
Expand Down
53 changes: 53 additions & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38825,3 +38825,56 @@ fn a_topology_check_runs_ahead_and_is_taken() {
let mut s = Session::new(Project::default());
assert!(!read_ahead(&mut s, "topology.validate", &json!({})).unwrap().reads());
}

/// A model trained within the work limit of a run that can't be cancelled says what it left out,
/// in a notice naming it and as `"limited"` in the answer, from `dl.train` and from `dl.classify`
/// when it trains; one trained whole says nothing (AGENTS.md › Never silent).
#[test]
fn a_model_trained_within_the_work_limit_says_so() {
let mut s = world();
s.run("layer.addDataBytes", &json!({"fileName": "scene.tif", "data": two_class_b64()})).unwrap();
let training = json!({"type": "FeatureCollection", "features": [
{"type": "Feature", "properties": {"class": "water"}, "geometry": {"type": "Polygon", "coordinates": [[[10.002, 45.002], [10.015, 45.002], [10.015, 45.035], [10.002, 45.035], [10.002, 45.002]]]}},
{"type": "Feature", "properties": {"class": "field"}, "geometry": {"type": "Polygon", "coordinates": [[[10.025, 45.002], [10.038, 45.002], [10.038, 45.035], [10.025, 45.035], [10.025, 45.002]]]}}]});
s.run("layer.addDataText", &json!({"fileName": "training.geojson", "text": training.to_string()})).unwrap();
let limited = |s: &Session, mark: u64, name: &str| {
s.notices_since(mark)
.iter()
.filter(|n| n.text.starts_with(&format!("{name}: trained on ")) && n.severity == rustgis_data::diagnostics::Severity::Warning)
.count()
};
let small = json!({"layer": "scene", "training": "training", "classField": "class", "bands": [1, 2], "epochs": 5, "model": "small"});
let mark = s.notice_mark();
let r = s.run("dl.train", &small).unwrap();
assert!(r.get("limited").is_none(), "{r}");
assert_eq!(limited(&s, mark, "small"), 0);
// The largest network for every epoch passes the work limit: fewer epochs, said.
let big = json!({"layer": "scene", "training": "training", "classField": "class", "bands": [1, 2], "epochs": 500,
"hidden": [256, 256, 256, 256], "model": "big"});
let mark = s.notice_mark();
let r = s.run("dl.train", &big).unwrap();
let said = r["limited"].as_str().unwrap_or_else(|| panic!("{r}"));
assert!(said.contains("of 500 epochs"), "{said}");
assert_eq!(limited(&s, mark, "big"), 1);
// dl.classify that trains says it as the model is kept.
let mark = s.notice_mark();
let r = s
.run(
"dl.classify",
&json!({"layer": "scene", "training": "training", "classField": "class", "bands": [1, 2], "epochs": 500,
"hidden": [256, 256, 256, 256], "model": "big2"}),
)
.unwrap();
assert!(r["limited"].as_str().is_some(), "{r}");
assert_eq!(limited(&s, mark, "big2"), 1);
// More epochs than a training runs are refused, not cut without a word.
for epochs in [json!(0), json!(501), json!(5000), json!(2.5), json!("2.5"), json!("500 epochs")] {
let e = s.run("dl.train", &json!({"layer": "scene", "training": "training", "classField": "class", "epochs": epochs})).unwrap_err();
assert!(e.contains("epochs is 1 to 500"), "{epochs}: {e}");
}
// A count as a numeric string is read as other counts are.
let r = s
.run("dl.train", &json!({"layer": "scene", "training": "training", "classField": "class", "bands": [1, 2], "epochs": "3"}))
.unwrap();
assert_eq!(r["epochs"], 3, "{r}");
}
14 changes: 8 additions & 6 deletions crates/ui-egui/src/analysispane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,12 +902,14 @@ pub fn workflows(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
ui.horizontal(|ui| {
ui.add(egui::TextEdit::singleline(&mut app.workflow.number).hint_text(tl!("epochs")).desired_width(60.0));
if ui.button(tl!("Train And Classify")).clicked() {
let epochs = app.workflow.number.trim().parse::<f64>().unwrap_or(60.0);
run(
app,
"dl.classify",
json!({"layer": app.workflow.layer, "training": app.workflow.layer2, "classField": app.workflow.field, "epochs": epochs}),
);
// A blank box is the default; what is typed goes as it is, for the command to
// read or refuse (a fraction or text isn't cut or replaced without a word).
let mut p = json!({"layer": app.workflow.layer, "training": app.workflow.layer2, "classField": app.workflow.field});
let typed = app.workflow.number.trim();
if !typed.is_empty() {
p["epochs"] = json!(typed);
}
run(app, "dl.classify", p);
}
});
let models: Vec<(String, f64)> = app.session.project.dl_models.iter().map(|m| (m.name.clone(), m.accuracy)).collect();
Expand Down
4 changes: 2 additions & 2 deletions docs/user-guide/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ run` called from another program, such as an agent or a CI job, which may wait w
Viewshed, IDW Interpolation and Kriging refuse a run that would take more than a second or two of
work on one thread (a ray step, a cell-point pair), Kernel Density one whose points' search windows
(the square around each point's radius) cover more than 50 million cells, and Deep Learning a model
too large for its raster; its training fits a share of the sampled pixels, or fewer epochs, instead
(`dl.train` reports the epochs it ran). A task or model recorded from a pane run keeps these limits
too large for its raster; its training fits a share of the sampled pixels, or fewer epochs, instead,
and says how many of each it trained on (`"limited"` in `dl.train`'s and `dl.classify`'s answer). A task or model recorded from a pane run keeps these limits
when it runs again in the app, so a step the pane took on can be refused there, and a model trained
again can differ. On the desktop these limits grow with the threads the run takes (the cores, up to 16 unless
[`threads`](#analysis-workflows) says otherwise), as many
Expand Down
Loading