ocr 0.1.1

A pure Rust CLI OCR tool for printed text extraction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Loss functions for OCR training

use anyhow::Result;
use ndarray::{s, Array1, Array2};

/// Trait for loss functions
pub trait LossFunction {
    fn compute(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<f32>;
    fn gradient(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<Array2<f32>>;
    fn name(&self) -> &'static str;
}

/// Connectionist Temporal Classification (CTC) Loss
///
/// CTC is commonly used for sequence-to-sequence tasks like OCR where
/// the alignment between input and output sequences is unknown.
pub struct CTCLoss {
    blank_token: usize,
    reduction: ReductionType,
}

#[derive(Debug, Clone)]
pub enum ReductionType {
    Mean,
    Sum,
    None,
}

impl CTCLoss {
    pub fn new(blank_token: usize) -> Self {
        Self {
            blank_token,
            reduction: ReductionType::Mean,
        }
    }

    pub fn with_reduction(mut self, reduction: ReductionType) -> Self {
        self.reduction = reduction;
        self
    }

    /// Compute CTC loss for a batch of sequences
    fn compute_ctc_loss(
        &self,
        log_probs: &Array2<f32>,
        targets: &Array1<usize>,
        input_lengths: &Array1<usize>,
        target_lengths: &Array1<usize>,
    ) -> Result<f32> {
        let batch_size = log_probs.shape()[0];
        let mut total_loss = 0.0;

        for i in 0..batch_size {
            let input_len = input_lengths[i];
            let target_len = target_lengths[i];

            let sequence_log_probs = log_probs
                .slice_axis(ndarray::Axis(0), ndarray::Slice::from(i..i + 1))
                .slice_axis(ndarray::Axis(1), ndarray::Slice::from(0..input_len))
                .slice_axis(ndarray::Axis(2), ndarray::Slice::from(..))
                .to_owned();
            let target_sequence = targets
                .slice_axis(ndarray::Axis(0), ndarray::Slice::from(i..i + 1))
                .slice_axis(ndarray::Axis(1), ndarray::Slice::from(0..target_len))
                .to_owned();

            let loss = self.forward_algorithm(&sequence_log_probs, &target_sequence)?;
            total_loss += loss;
        }

        match self.reduction {
            ReductionType::Mean => Ok(total_loss / batch_size as f32),
            ReductionType::Sum => Ok(total_loss),
            ReductionType::None => Ok(total_loss),
        }
    }

    /// Forward algorithm for CTC
    fn forward_algorithm(&self, log_probs: &Array2<f32>, target: &Array1<usize>) -> Result<f32> {
        let t = log_probs.shape()[0];
        let s = target.len() * 2 + 1; // Extended target with blanks

        // Create extended target with blanks
        let mut extended_target = vec![self.blank_token; s];
        for (i, &token) in target.iter().enumerate() {
            extended_target[i * 2 + 1] = token;
        }

        // Initialize alpha matrix
        let mut alpha = Array2::<f32>::zeros((t, s));

        // Initialize first column
        alpha[[0, 0]] = log_probs[[0, self.blank_token]];
        if s > 1 {
            alpha[[0, 1]] = log_probs[[0, extended_target[1]]];
        }

        // Forward pass
        for t_idx in 1..t {
            for s_idx in 0..s {
                let current_token = extended_target[s_idx];
                let mut sum = alpha[[t_idx - 1, s_idx]] + log_probs[[t_idx, current_token]];

                if s_idx > 0 {
                    sum =
                        sum.max(alpha[[t_idx - 1, s_idx - 1]] + log_probs[[t_idx, current_token]]);
                }

                if s_idx > 1
                    && extended_target[s_idx] != self.blank_token
                    && extended_target[s_idx] != extended_target[s_idx - 2]
                {
                    sum =
                        sum.max(alpha[[t_idx - 1, s_idx - 2]] + log_probs[[t_idx, current_token]]);
                }

                alpha[[t_idx, s_idx]] = sum;
            }
        }

        // Compute final loss
        let final_loss = if s > 0 {
            alpha[[t - 1, s - 1]].max(alpha[[t - 1, s - 2]])
        } else {
            alpha[[t - 1, 0]]
        };

        Ok(-final_loss)
    }
}

impl LossFunction for CTCLoss {
    fn compute(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<f32> {
        // Convert targets to indices and lengths
        let batch_size = predictions.shape()[0];
        let mut target_indices = Vec::new();
        let mut target_lengths = Vec::new();
        let mut input_lengths = Vec::new();

        for i in 0..batch_size {
            // Find non-zero elements in target
            let target_row = targets.slice(s![i, ..]);
            let non_zero_indices: Vec<usize> = target_row
                .iter()
                .enumerate()
                .filter(|&(_, &val)| val != 0.0)
                .map(|(idx, _)| idx)
                .collect();

            target_indices.extend(non_zero_indices.clone());
            target_lengths.push(non_zero_indices.len());
            input_lengths.push(predictions.shape()[1]);
        }

        let target_array = Array1::from(target_indices);
        let target_lengths_array = Array1::from(target_lengths);
        let input_lengths_array = Array1::from(input_lengths);

        self.compute_ctc_loss(
            predictions,
            &target_array,
            &input_lengths_array,
            &target_lengths_array,
        )
    }

    fn gradient(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<Array2<f32>> {
        // Simplified gradient computation
        // In practice, this would use the backward algorithm
        let mut gradients = predictions.clone();
        gradients -= targets;
        Ok(gradients)
    }

    fn name(&self) -> &'static str {
        "CTC Loss"
    }
}

/// Cross-Entropy Loss for classification tasks
pub struct CrossEntropyLoss {
    reduction: ReductionType,
    label_smoothing: f32,
}

impl CrossEntropyLoss {
    pub fn new() -> Self {
        Self {
            reduction: ReductionType::Mean,
            label_smoothing: 0.0,
        }
    }

    pub fn with_label_smoothing(mut self, smoothing: f32) -> Self {
        self.label_smoothing = smoothing;
        self
    }

    pub fn with_reduction(mut self, reduction: ReductionType) -> Self {
        self.reduction = reduction;
        self
    }
}

impl LossFunction for CrossEntropyLoss {
    fn compute(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<f32> {
        let batch_size = predictions.shape()[0];
        let num_classes = predictions.shape()[1];

        let mut total_loss = 0.0;

        for i in 0..batch_size {
            let pred_row = predictions.slice(s![i, ..]);
            let target_row = targets.slice(s![i, ..]);

            // Apply softmax to predictions
            let max_val = pred_row.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
            let exp_pred: Array1<f32> = pred_row.mapv(|x| (x - max_val).exp());
            let sum_exp: f32 = exp_pred.sum();
            let softmax_pred = exp_pred.mapv(|x| x / sum_exp);

            // Compute cross-entropy loss
            let mut loss = 0.0;
            for j in 0..num_classes {
                if target_row[j] > 0.0 {
                    loss += -target_row[j] * softmax_pred[j].ln();
                }
            }

            total_loss += loss;
        }

        match self.reduction {
            ReductionType::Mean => Ok(total_loss / batch_size as f32),
            ReductionType::Sum => Ok(total_loss),
            ReductionType::None => Ok(total_loss),
        }
    }

    fn gradient(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<Array2<f32>> {
        let batch_size = predictions.shape()[0];
        let num_classes = predictions.shape()[1];
        let mut gradients = Array2::<f32>::zeros((batch_size, num_classes));

        for i in 0..batch_size {
            let pred_row = predictions.slice(s![i, ..]);
            let target_row = targets.slice(s![i, ..]);

            // Apply softmax to predictions
            let max_val = pred_row.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
            let exp_pred: Array1<f32> = pred_row.mapv(|x| (x - max_val).exp());
            let sum_exp: f32 = exp_pred.sum();
            let softmax_pred = exp_pred.mapv(|x| x / sum_exp);

            // Compute gradients
            for j in 0..num_classes {
                gradients[[i, j]] = softmax_pred[j] - target_row[j];
            }
        }

        Ok(gradients)
    }

    fn name(&self) -> &'static str {
        "Cross-Entropy Loss"
    }
}

/// Focal Loss for handling class imbalance
pub struct FocalLoss {
    alpha: f32,
    gamma: f32,
    reduction: ReductionType,
}

impl FocalLoss {
    pub fn new(alpha: f32, gamma: f32) -> Self {
        Self {
            alpha,
            gamma,
            reduction: ReductionType::Mean,
        }
    }

    pub fn with_reduction(mut self, reduction: ReductionType) -> Self {
        self.reduction = reduction;
        self
    }
}

impl LossFunction for FocalLoss {
    fn compute(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<f32> {
        let batch_size = predictions.shape()[0];
        let num_classes = predictions.shape()[1];

        let mut total_loss = 0.0;

        for i in 0..batch_size {
            let pred_row = predictions.slice(s![i, ..]);
            let target_row = targets.slice(s![i, ..]);

            // Apply softmax to predictions
            let max_val = pred_row.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
            let exp_pred: Array1<f32> = pred_row.mapv(|x| (x - max_val).exp());
            let sum_exp: f32 = exp_pred.sum();
            let softmax_pred = exp_pred.mapv(|x| x / sum_exp);

            // Compute focal loss
            let mut loss = 0.0;
            for j in 0..num_classes {
                if target_row[j] > 0.0 {
                    let pt = softmax_pred[j];
                    let focal_weight = self.alpha * (1.0 - pt).powf(self.gamma);
                    loss += -target_row[j] * focal_weight * pt.ln();
                }
            }

            total_loss += loss;
        }

        match self.reduction {
            ReductionType::Mean => Ok(total_loss / batch_size as f32),
            ReductionType::Sum => Ok(total_loss),
            ReductionType::None => Ok(total_loss),
        }
    }

    fn gradient(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<Array2<f32>> {
        // Simplified gradient computation for focal loss
        let mut gradients = predictions.clone();
        gradients -= targets;
        Ok(gradients)
    }

    fn name(&self) -> &'static str {
        "Focal Loss"
    }
}

/// Attention-based loss for sequence-to-sequence models
pub struct AttentionLoss {
    reduction: ReductionType,
}

impl AttentionLoss {
    pub fn new() -> Self {
        Self {
            reduction: ReductionType::Mean,
        }
    }

    pub fn with_reduction(mut self, reduction: ReductionType) -> Self {
        self.reduction = reduction;
        self
    }
}

impl LossFunction for AttentionLoss {
    fn compute(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<f32> {
        // Attention loss combines cross-entropy with attention alignment
        let ce_loss = CrossEntropyLoss::new().with_reduction(ReductionType::None);
        let ce_value = ce_loss.compute(predictions, targets)?;

        // Add attention alignment penalty (simplified)
        let attention_penalty = 0.1 * predictions.sum();

        let total_loss = ce_value + attention_penalty;

        match self.reduction {
            ReductionType::Mean => Ok(total_loss / predictions.shape()[0] as f32),
            ReductionType::Sum => Ok(total_loss),
            ReductionType::None => Ok(total_loss),
        }
    }

    fn gradient(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<Array2<f32>> {
        let ce_loss = CrossEntropyLoss::new();
        let mut gradients = ce_loss.gradient(predictions, targets)?;

        // Add attention gradient
        gradients += 0.1;

        Ok(gradients)
    }

    fn name(&self) -> &'static str {
        "Attention Loss"
    }
}

/// Combined loss function that can use multiple loss functions
pub struct CombinedLoss {
    losses: Vec<Box<dyn LossFunction + Send + Sync>>,
    weights: Vec<f32>,
}

impl CombinedLoss {
    pub fn new() -> Self {
        Self {
            losses: Vec::new(),
            weights: Vec::new(),
        }
    }

    pub fn add_loss(mut self, loss: Box<dyn LossFunction + Send + Sync>, weight: f32) -> Self {
        self.losses.push(loss);
        self.weights.push(weight);
        self
    }
}

impl LossFunction for CombinedLoss {
    fn compute(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<f32> {
        let mut total_loss = 0.0;

        for (loss, &weight) in self.losses.iter().zip(self.weights.iter()) {
            let loss_value = loss.compute(predictions, targets)?;
            total_loss += weight * loss_value;
        }

        Ok(total_loss)
    }

    fn gradient(&self, predictions: &Array2<f32>, targets: &Array2<f32>) -> Result<Array2<f32>> {
        let mut total_gradient = Array2::<f32>::zeros(predictions.raw_dim());

        for (loss, &weight) in self.losses.iter().zip(self.weights.iter()) {
            let gradient = loss.gradient(predictions, targets)?;
            total_gradient = &total_gradient + &(weight * &gradient);
        }

        Ok(total_gradient)
    }

    fn name(&self) -> &'static str {
        "Combined Loss"
    }
}