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
35 changes: 34 additions & 1 deletion crates/analysis/src/neural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,39 @@ fn softmax(a: &mut [f32]) {
}
}

/// The samples of a model's classes under its training polygons, each polygon's outline given
/// with its class and its share of the class's samples, and each class taking up to
/// `max_per_class`: the samples, and how many each class got. The polygons are counted for the
/// run's progress, and a cancelled run stops between rows of a polygon (see [`crate::control`]).
#[allow(clippy::type_complexity)]
pub fn training_samples(
r: &Raster,
bands: &[usize],
polygons: &[(Vec<Vec<Coord>>, usize, usize)],
classes: usize,
max_per_class: usize,
) -> Result<(Vec<(Vec<f32>, usize)>, Vec<usize>), String> {
crate::control::begin(polygons.len());
let mut samples: Vec<(Vec<f32>, usize)> = Vec::new();
let mut per_class = vec![0usize; classes];
for (rings, ci, share) in polygons {
let room = max_per_class.saturating_sub(per_class.get(*ci).copied().unwrap_or(0)).min(*share);
let got = polygon_samples(r, bands, rings, room);
// Stopped part of the way through the polygon: what it got isn't its share.
crate::control::check()?;
if let Some(n) = per_class.get_mut(*ci) {
*n += got.len();
}
samples.extend(got.into_iter().map(|x| (x, *ci)));
crate::control::advance(1);
}
Ok((samples, per_class))
}

/// Band values of the pixels inside a polygon (`rings`, longitude/latitude), up to `max`. Only
/// the pixels under the polygon's outline are visited, every `step`-th one in each direction when
/// there are many more than `max`, so the samples spread across the polygon.
/// there are many more than `max`, so the samples spread across the polygon. A cancelled run
/// stops between rows, with the pixels found so far.
pub fn polygon_samples(r: &Raster, bands: &[usize], rings: &[Vec<Coord>], max: usize) -> Vec<Vec<f32>> {
let mut out = Vec::new();
let tr = r.transformer();
Expand Down Expand Up @@ -155,6 +185,9 @@ pub fn polygon_samples(r: &Raster, bands: &[usize], rings: &[Vec<Coord>], max: u
out.clear();
let mut row = r0;
'scan: while row < r1 {
if crate::control::cancelled() {
return out;
}
let mut col = c0;
while col < c1 {
if out.len() >= max {
Expand Down
152 changes: 105 additions & 47 deletions crates/engine/src/cmd/deeplearning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustgis_geom::Coord;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use super::imagery::{add, bands_param, name, raster};
use super::imagery::{bands_param, class_layer, name, raster};
use super::{f, ps};
use crate::{CmdResult, CommandSpec as C, Session};

Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn commands() -> Vec<C> {
.when(need_raster)
.params(r#"{"layer": raster, "training": polygon layer, "classField": field naming each polygon's class, "bands"?: [1-based], "hidden"?: [units per hidden layer] (default [32, 16]), "epochs"?: 1..500 (default 60), "model"?: name}"#),
C::new("dl.classify", "Deep Learning Tools", "Analyze › Image Classification", classify).when(need_raster).params(
r#"{"layer": raster, "model"?: a trained model's name, "training"?: polygon layer, "classField"?: field, "bands"?: [1-based], "hidden"?: [units], "epochs"?: 1..500 (without a model: train one first, as dl.train), "name"?: output}"#,
r#"{"layer": raster, "model"?: a trained model's name, "training"?: polygon layer, "classField"?: field, "bands"?: [1-based], "hidden"?: [units], "epochs"?: 1..500 (without a model: train one first, as dl.train), "name"?: output, "ran"?: (internal: classes the app has worked out)}"#,
),
C::new("dl.package", "Deep Learning Package", "Publish › Package", |s, p| {
let path = ps(p, "path").ok_or("path is required")?;
Expand Down Expand Up @@ -149,6 +149,42 @@ fn model_index(s: &Session, p: &Value) -> Result<usize, String> {

/// Trains a model from the pixels under training polygons.
fn train(s: &Session, p: &Value) -> Result<DlModel, String> {
training(s, p)?.fit()
}

/// A model to train: the raster, the training polygons and their classes, read of the session, and
/// how to train it ([`Training::fit`], which reads nothing more and runs on any thread: the pixels
/// under the polygons are sampled there, as the network learns).
struct Training {
raster: std::sync::Arc<rustgis_data::raster::Raster>,
bands: Vec<usize>,
classes: Vec<String>,
/// Each polygon's outline, its class and its share of the class's samples.
polygons: Vec<(Vec<Vec<Coord>>, usize, usize)>,
hidden: Vec<usize>,
epochs: usize,
name: String,
/// No name was given: the name's base, for a name of its own when the model is kept (a model
/// kept meanwhile may have taken this one).
auto: Option<String>,
}

impl Training {
fn fit(self) -> Result<DlModel, String> {
let Training { raster, bands, classes, polygons, hidden, epochs, name, .. } = self;
let (samples, per_class) = neural::training_samples(&raster, &bands, &polygons, classes.len(), MAX_SAMPLES_PER_CLASS)?;
// Every class needs pixels of its own (its polygons may miss the raster or cover no data).
if let Some((c, _)) = classes.iter().zip(&per_class).find(|(_, n)| **n < 5) {
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 })
}
}

/// What training reads of the session: the training polygons and their classes, and the model's
/// name.
fn training(s: &Session, p: &Value) -> Result<Training, String> {
let r = raster(s, p)?;
r.require_coordinates()?;
let bands = bands_param(p, &r)?;
Expand All @@ -160,8 +196,7 @@ fn train(s: &Session, p: &Value) -> Result<DlModel, String> {
.ok_or_else(|| format!("no polygon layer '{tl}'"))?;
let fi = l.dataset.field_index(ps(p, "classField").ok_or("classField is required")?).ok_or("no such class field")?;
let mut classes: Vec<String> = Vec::new();
let mut samples: Vec<(Vec<f32>, usize)> = Vec::new();
let mut per_class: Vec<usize> = Vec::new();
let mut parts: Vec<(Vec<Vec<Coord>>, usize, usize)> = Vec::new();
// Polygons per class, so each polygon of a class gets a fair share of its samples.
let mut polygons: HashMap<String, usize> = HashMap::new();
for (row, ft) in l.dataset.features.iter().enumerate() {
Expand All @@ -182,73 +217,96 @@ fn train(s: &Session, p: &Value) -> Result<DlModel, String> {
Some(i) => i,
None if classes.len() < neural::MAX_CLASSES => {
classes.push(key);
per_class.push(0);
classes.len() - 1
}
None => return Err(format!("training holds at most {} classes", neural::MAX_CLASSES)),
};
for rings in polys {
let room = MAX_SAMPLES_PER_CLASS.saturating_sub(per_class.get(ci).copied().unwrap_or(0)).min(share);
let got = neural::polygon_samples(&r, &bands, rings, room);
if let Some(n) = per_class.get_mut(ci) {
*n += got.len();
}
samples.extend(got.into_iter().map(|x| (x, ci)));
}
}
// Every class needs pixels of its own (its polygons may miss the raster or cover no data).
if let Some((c, _)) = classes.iter().zip(&per_class).find(|(_, n)| **n < 5) {
return Err(format!("the class '{c}' has fewer than 5 pixels with data under its polygons"));
parts.extend(polys.iter().map(|rings| (rings.clone(), ci, share)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance (low confidence): training runs on the UI thread before the run starts, and this line copies every training polygon's rings (rings.clone()), so the copy's cost and a second in-memory copy of the training geometry scale with the layer's total vertex count. The sampling that was the real freeze has moved to the run's thread, so this is minor. It's worth a look only for training layers with very many or very large polygons, or if the PR text says the UI thread does only the reading. If it matters, the layer's geometry could be shared (for example an Arc) instead of copied.

}
let hidden: Vec<usize> = match p.get("hidden").and_then(Value::as_array) {
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;
let t = neural::train(&bands, classes, &samples, &hidden, epochs)?;
// Without a name given, a new model never replaces one: it gets a name of its own.
let name = match ps(p, "model") {
Some(_) => name(&json!({"name": p.get("model")}), format!("{}_model", r.name)),
let (name, auto) = match ps(p, "model") {
Some(_) => (name(&json!({"name": p.get("model")}), format!("{}_model", r.name)), None),
None => {
let base = format!("{}_model", r.name);
let taken = |n: &str| s.project.dl_models.iter().any(|x| x.name.eq_ignore_ascii_case(n));
let mut n = base.clone();
let mut k = 1;
while taken(&n) {
k += 1;
n = format!("{base} {k}");
}
n
(free_name(s, &base), Some(base))
}
};
Ok(DlModel { name, model: t.model, accuracy: t.accuracy, epochs: t.epochs })
Ok(Training { raster: r, bands, classes, polygons: parts, hidden, epochs, name, auto })
}

/// `base`, or `base 2`, `base 3`… : the first no model of the project has (whatever its case).
fn free_name(s: &Session, base: &str) -> String {
let taken = |n: &str| s.project.dl_models.iter().any(|x| x.name.eq_ignore_ascii_case(n));
let mut n = base.to_string();
let mut k = 1;
while taken(&n) {
k += 1;
n = format!("{base} {k}");
}
n
}

fn classify(s: &mut Session, p: &Value) -> CmdResult {
super::workflows::made_or_ran(s, p, super::workflows::DEEP_LEARNING, classify_work)
}

/// The model a classification uses: one of the project's, or one it trains first.
enum Model {
Saved(DlModel),
Train(Training),
}

/// What a deep learning classification reads of the session (the raster, and the model or what
/// trains one), and the work that trains the model if need be and classifies the pixels. A model
/// trained is kept in the project once the classes are added.
pub(crate) fn classify_work(s: &Session, p: &Value) -> Result<super::workflows::Work, String> {
let r = raster(s, p)?;
r.require_coordinates()?;
let (m, trained) = if p.get("training").is_some() {
(train(s, p)?, true)
let model = if p.get("training").is_some() {
Model::Train(training(s, p)?)
} else {
let i = model_index(s, p)?;
(s.project.dl_models.get(i).cloned().ok_or("no model")?, false)
Model::Saved(s.project.dl_models.get(i).cloned().ok_or("no model")?)
};
let (class, prob) = neural::classify(&r, &m.model, rasterops::NODATA)?;
let mut counts = vec![0usize; m.model.classes.len()];
for v in class.iter().filter(|v| **v >= 1.0) {
if let Some(c) = counts.get_mut(*v as usize - 1) {
*c += 1;
let out = name(p, format!("{}_dl", r.name));
Ok(Box::new(move || {
let (m, trained) = match model {
Model::Saved(m) => (m, None),
Model::Train(t) => {
let auto = t.auto.clone();
(t.fit()?, Some(auto))
}
};
let (class, prob) = neural::classify(&r, &m.model, rasterops::NODATA)?;
let mut counts = vec![0usize; m.model.classes.len()];
for v in class.iter().filter(|v| **v >= 1.0) {
if let Some(c) = counts.get_mut(*v as usize - 1) {
*c += 1;
}
}
}
let out = rasterops::derive(&r, &name(p, format!("{}_dl", r.name)), vec![class, prob])?;
let v = add(s, out, true)?;
let legend: Vec<Value> =
m.model.classes.iter().zip(&counts).enumerate().map(|(i, (c, n))| json!({"value": i + 1, "class": c, "pixels": n})).collect();
let mut r = json!({"layer": v["layer"], "model": m.name, "accuracy": m.accuracy, "classes": legend});
if trained {
r["replaced"] = json!(store(s, m));
}
Ok(r)
let layer = class_layer(rasterops::derive(&r, &out, vec![class, prob])?)?;
let legend: Vec<Value> =
m.model.classes.iter().zip(&counts).enumerate().map(|(i, (c, n))| json!({"value": i + 1, "class": c, "pixels": n})).collect();
let extra = json!({"model": m.name, "accuracy": m.accuracy, "classes": legend});
// Kept once its classes are added. One that was given no name is named then, so it never
// replaces a model kept while it trained.
let then: Option<super::workflows::Then> = trained.map(|auto| {
Box::new(move |s: &mut Session, v: &mut Value| {
let mut m = m;
if let Some(base) = auto {
m.name = free_name(s, &base);
v["model"] = json!(m.name);
}
v["replaced"] = json!(store(s, m));
}) as super::workflows::Then
});
Ok(super::workflows::made_layer(out, layer, Some(extra), then))
}))
}

/// A deep learning package: the model and a description of its inputs and classes.
Expand Down
44 changes: 30 additions & 14 deletions crates/engine/src/cmd/imagery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,9 @@ pub fn commands() -> Vec<C> {
.view()
.params(r#"{"layer": raster, "lon"?, "lat"?, "zone"?: polygon layer (the selected or first polygon)}"#),
C::new("raster.classify", "Classification Wizard", "Analyze › Image Classification", |s, p| {
let r = raster(s, p)?;
let bands = bands_param(p, &r)?;
if bands.is_empty() {
return Err("give at least one band".into());
}
let k = match p.get("classes") {
None => 5,
Some(_) => uint(p, "classes").filter(|k| (2..=50).contains(k)).ok_or("classes is 2 to 50")? as usize,
};
let classes = rasterops::kmeans(&r, &bands, k, 20)?;
add(s, rasterops::derive(&r, &name(p, format!("{}_classes", r.name)), vec![classes])?, true)
super::workflows::made_or_ran(s, p, super::workflows::CLASSIFY, classify_work)
})
.params(r#"{"layer": raster, "classes"?: 2..50, "bands"?: [1-based], "name"?: output}"#),
.params(r#"{"layer": raster, "classes"?: 2..50, "bands"?: [1-based], "name"?: output, "ran"?: (internal: classes the app has worked out)}"#),
C::new("raster.classifySupervised", "Classification Tools", "Analyze › Image Classification", supervised)
.params(r#"{"layer": raster, "training": polygon layer, "classField": field, "bands"?: [1-based], "name"?: output}"#),
C::new("raster.index", "Indices", "Analyze › Imagery Tools", index).params(
Expand Down Expand Up @@ -151,11 +141,37 @@ fn layer_of(r: Raster) -> Result<rustgis_map::RasterLayer, String> {
/// Adds a raster layer; classes get unique-value colours.
pub(crate) fn add(s: &mut Session, r: Raster, classes: bool) -> CmdResult {
let n = r.name.clone();
let layer = if classes { class_layer(r)? } else { layer_of(r)? };
super::raster::add_raster(s, &n, layer)
}

/// A raster of classes as a layer, each class a colour of its own.
pub(crate) fn class_layer(r: Raster) -> Result<rustgis_map::RasterLayer, String> {
let mut layer = layer_of(r)?;
if classes && let Some(d) = layer.data.clone() {
if let Some(d) = layer.data.clone() {
layer.style.renderer = rustgis_map::raster::unique_values(&d, 0, rustgis_map::Ramp::Harbour);
}
super::raster::add_raster(s, &n, layer)
Ok(layer)
}

/// What the Classification Wizard (k-means) reads of the session, and the work that makes its
/// classes.
pub(crate) fn classify_work(s: &Session, p: &Value) -> Result<super::workflows::Work, String> {
let r = raster(s, p)?;
let bands = bands_param(p, &r)?;
if bands.is_empty() {
return Err("give at least one band".into());
}
let k = match p.get("classes") {
None => 5,
Some(_) => uint(p, "classes").filter(|k| (2..=50).contains(k)).ok_or("classes is 2 to 50")? as usize,
};
let out = name(p, format!("{}_classes", r.name));
Ok(Box::new(move || {
let classes = rasterops::kmeans(&r, &bands, k, 20)?;
let layer = class_layer(rasterops::derive(&r, &out, vec![classes])?)?;
Ok(super::workflows::made_layer(out, layer, None, None))
}))
}

/// Replaces a raster layer's pixels, keeping its look.
Expand Down
Loading
Loading