Libraryless. Click here for Pure Java version (8289L/48K).
1 | sclass OneColorTheoryChecker { |
2 | replace Cell with URecognizer.Cell. |
3 | |
4 | new ProbabilisticScheduler scheduler; |
5 | |
6 | sclass Theory { |
7 | // values are: withProbability(strengthOfEvidence, probabilityOfTheory) |
8 | new MultiMap<S, WithProbability<Double>> examples; |
9 | double strengthSum, probabilitySum; |
10 | |
11 | double currentProbabilityGuess() { |
12 | ret doubleRatio(probabilitySum, strengthSum); |
13 | } |
14 | |
15 | S renderProbabilityGuess() { |
16 | ret "Current probability guess (evidence count " + n2(l(examples)) + "): " + formatDouble3X(currentProbabilityGuess()) + " with strength " + formatDouble3(strengthSum); |
17 | } |
18 | |
19 | bool hasExampleNamed(S name) { ret examples.containsKey(name); } |
20 | |
21 | void addEvidence(S desc, double probabilityOfTheory, double strengthOfEvidence) { |
22 | examples.add(desc, withProbability(strengthOfEvidence, probabilityOfTheory)); |
23 | probabilitySum += probabilityOfTheory*strengthOfEvidence; |
24 | strengthSum += strengthOfEvidence; |
25 | } |
26 | |
27 | S toStringWithEvidence() { |
28 | ret toString() + "\n\n" |
29 | + pnlToString("EVIDENCE", multiMapToPairs(examples)); |
30 | } |
31 | } |
32 | |
33 | record OneColorTheory(S text, Cell cell, RGB color) > Theory { |
34 | toString { |
35 | ret renderProbabilityGuess() + ": " + dollarVarsMeanFields(text, this); |
36 | } |
37 | |
38 | selfType branch(O... _) { this; } |
39 | } |
40 | |
41 | // create the theory object and initiate the reasoning |
42 | // you have to step the scheduler afterwards to get results |
43 | OneColorTheory makeTheory(Cell cell) { |
44 | var color = cell.averageColor(); |
45 | |
46 | var theory = new OneColorTheory("Color of every pixel in $cell is $color.", cell, color); |
47 | |
48 | testOneColorTheory(cell, theory); |
49 | ret theory; |
50 | } |
51 | |
52 | void noteCellColor(Cell cell, OneColorTheory theory) { |
53 | var desc = "Pixel check at " + cell; |
54 | if (theory.hasExampleNamed(desc)) return; |
55 | var strength = doubleRatio(cell.area(), theory.cell.area()); // how much do we have to say about the cell in the theory? |
56 | var p = colorDistanceToProbability(cell.averageColor(), theory.color); |
57 | addExampleToTheory(theory, desc, p, strength); |
58 | } |
59 | |
60 | double colorDistanceToProbability(RGB col1, RGB col2) { |
61 | double sim = colorSimilarity(col1, col2), sqr = sqr(sim); |
62 | printFunctionCall colorDistanceToProbability(+col1, +col2, +sim, +sqr); |
63 | ret sqr; |
64 | } |
65 | |
66 | void testOneColorTheory(Cell cell, OneColorTheory theory) { |
67 | noteCellColor(cell, theory); |
68 | |
69 | scheduler.atRelative(dontZoomTooFar(cell), r { |
70 | for (var split : usefulSplits(cell)) |
71 | scheduler.atRelative(split.probability(), r { |
72 | for (var cell : split!) |
73 | testOneColorTheory(cell, theory.branch(description := "recursion to " + cell)); |
74 | }); |
75 | }); |
76 | } |
77 | |
78 | L<WithProbability<Cell[]>> usefulSplits(Cell cell) { |
79 | ret map withProbability1(llNonNulls(cell.split(0, 2), cell.split(1, 2))); |
80 | } |
81 | |
82 | // probability penalty .25 for zooming in 3 steps |
83 | double dontZoomTooFar(Cell cell) { |
84 | ret 0.75; |
85 | } |
86 | |
87 | void addExampleToTheory(Theory theory, S desc, double probabilityOfTheory, double strengthOfEvidence) { |
88 | theory.addEvidence(desc, probabilityOfTheory, /* scheduler.currentProbability()* */ strengthOfEvidence); |
89 | } |
90 | } |
Began life as a copy of #1031966
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #1032077 |
Snippet name: | OneColorTheoryChecker [backup] |
Eternal ID of this version: | #1032077/1 |
Text MD5: | 425bc9c8cfad2758684225e0b306c4fb |
Transpilation MD5: | f482bbaf7020180a045be784535971ba |
Author: | stefan |
Category: | javax / image recognition |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-08-11 12:11:46 |
Source code size: | 3272 bytes / 90 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 137 / 192 |
Referenced in: | [show references] |