Skip to content

Commit 341433a

Browse files
committed
Update glyphnet.
1 parent 9396163 commit 341433a

File tree

10 files changed

+616
-127
lines changed

10 files changed

+616
-127
lines changed

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/GlyphNet.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,20 @@ public void ApplyGradients()
147147
/// Make a forward pass through the computation graph.
148148
/// </summary>
149149
/// <returns>The gradient of the loss wrt the output.</returns>
150-
public (Matrix, Matrix, Matrix, Matrix, Matrix, Matrix, Matrix, Matrix, Matrix, Matrix) Forward(Matrix input, Matrix rotationTargets, double targetAngle)
150+
public (Matrix, (Matrix Loss, Matrix Gradient)[]) Forward(Matrix input, Matrix rotationTargets)
151151
{
152152

153153
var gatNet = this.GlyphNetwork;
154-
gatNet.TargetAngle = targetAngle;
155154
gatNet.InitializeState();
156155
gatNet.RotationTargets.Replace(rotationTargets.ToArray());
157156
gatNet.AutomaticForwardPropagate(input);
158-
var output = gatNet.Output;
159157
var glyph = gatNet.Glyph;
160-
var targetedSum0 = gatNet.TargetedSum0;
161-
var targetedSum1 = gatNet.TargetedSum1;
162158

163159
int maxMag0 = 0;
164160
int maxMag1 = 0;
165-
for (int i = 0; i < 15; i++)
161+
for (int i = 0; i < 8; i++)
166162
{
167-
for (int j = 0; j < 15; j++)
163+
for (int j = 0; j < 8; j++)
168164
{
169165
if (rotationTargets[i, j] == 1)
170166
{
@@ -179,29 +175,30 @@ public void ApplyGradients()
179175

180176
Console.WriteLine($"Max Mag 0: {maxMag0}, Max Mag 1: {maxMag1}");
181177

182-
SquaredArclengthEuclideanLossOperation arclengthLoss0 = SquaredArclengthEuclideanLossOperation.Instantiate(gatNet);
183-
var loss0 = arclengthLoss0.Forward(targetedSum0, (3 * Math.PI) / 4d, 0);
184-
var gradient0 = arclengthLoss0.Backward();
185-
186-
SquaredArclengthEuclideanLossOperation arclengthLoss1 = SquaredArclengthEuclideanLossOperation.Instantiate(gatNet);
187-
var loss1 = arclengthLoss1.Forward(targetedSum1, (1 * Math.PI) / 4d, 1);
188-
var gradient1 = arclengthLoss1.Backward();
189-
190-
SquaredArclengthEuclideanMagnitudeLossOperation arclengthLoss = SquaredArclengthEuclideanMagnitudeLossOperation.Instantiate(gatNet);
191-
var loss = arclengthLoss.Forward(output, targetAngle, 225);
192-
var gradient = arclengthLoss.Backward();
178+
(Matrix Loss, Matrix Gradient)[] values = new (Matrix Loss, Matrix Gradient)[64];
179+
for (int i = 0; i < 64; ++i)
180+
{
181+
Matrix m = new Matrix(1, 2);
182+
m[0, 0] = glyph[i, 0];
183+
m[0, 1] = glyph[i, 1];
184+
SquaredArclengthEuclideanLossOperation arclengthLoss = SquaredArclengthEuclideanLossOperation.Instantiate(gatNet);
185+
var loss = arclengthLoss.Forward(m, rotationTargets[i / 8, i % 8] == 1 ? (3 * Math.PI) / 4d : Math.PI / 4d);
186+
var gradient = arclengthLoss.Backward();
187+
values[i].Loss = loss;
188+
values[i].Gradient = gradient;
189+
}
193190

194-
return (gradient, gradient0, gradient1, output, targetedSum0, targetedSum1, glyph, loss, loss0, loss1);
191+
return (glyph, values);
195192
}
196193

197194
/// <summary>
198195
/// The backward pass through the computation graph.
199196
/// </summary>
200-
/// <param name="gradientOfLossWrtOutput">The gradient of the loss wrt the output.</param>
197+
/// <param name="gradients">The gradient of the loss wrt the output.</param>
201198
/// <returns>A task.</returns>
202-
public async Task<Matrix> Backward(Matrix gradientOfLossWrtOutput, Matrix gradient0, Matrix gradient1)
199+
public async Task<Matrix> Backward((Matrix Loss, Matrix Gradient)[] gradients)
203200
{
204-
return await this.GlyphNetwork.AutomaticBackwardPropagate(gradientOfLossWrtOutput, gradient0, gradient1);
201+
return await this.GlyphNetwork.AutomaticBackwardPropagate(gradients);
205202
}
206203
}
207204
}

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/GlyphNetTrainer.cs

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ await files.WithRepeatAsync(async (pngFile, token) =>
4747
int uIndex = file.IndexOf("_");
4848
var prefix = file.Substring(0, uIndex);
4949

50-
var glyphFile = pngFile.Replace("\\" + prefix, "\\" + prefix + "_glyph").Replace("svg\\", "svg-glyph\\");
51-
Node[,] glyphNodes = ImageSerializer.DeserializeImageWithoutAntiAlias(glyphFile);
52-
Matrix rotationTargets = new Matrix(15, 15);
53-
Vector3[] glyphs = new Vector3[225];
50+
//var glyphFile = pngFile.Replace("\\" + prefix, "\\" + prefix + "_glyph").Replace("svg\\", "svg-glyph\\");
51+
//Node[,] glyphNodes = ImageSerializer.DeserializeImageWithoutAntiAlias(glyphFile);
52+
53+
var glyphNodes = AnalyzeImageSections(interpolated);
54+
55+
Matrix rotationTargets = new Matrix(8, 8);
56+
Vector3[] glyphs = new Vector3[64];
5457
int m = 0;
55-
for (int k = 0; k < 15; ++k)
58+
for (int k = 0; k < 8; ++k)
5659
{
57-
for (int l = 0; l < 15; ++l)
60+
for (int l = 0; l < 8; ++l)
5861
{
59-
rotationTargets[k, l] = glyphNodes[k, l].IsForeground ? 1d : 0d;
62+
rotationTargets[k, l] = glyphNodes[k, l];
6063
glyphs[m] = new Vector3(0f, 0f, (float)rotationTargets[k, l]);
6164
m++;
6265
}
@@ -73,30 +76,31 @@ await files.WithRepeatAsync(async (pngFile, token) =>
7376

7477
i++;
7578

76-
var res = net.Forward(matrix, rotationTargets, Math.PI / 4d);
77-
var gradient = res.Item1;
78-
var gradient0 = res.Item2;
79-
var gradient1 = res.Item3;
80-
var output = res.Item4;
81-
var o0 = res.Item5;
82-
var o1 = res.Item6;
83-
var glyph = res.Item7;
84-
var loss = res.Item8;
85-
var loss0 = res.Item9;
86-
var loss1 = res.Item10;
87-
88-
for (int j = 0; j < 225; ++j)
79+
var res = net.Forward(matrix, rotationTargets);
80+
var glyph = res.Item1;
81+
var gradients = res.Item2;
82+
//var gradient1 = res.Item3;
83+
//var output = res.Item4;
84+
//var o0 = res.Item5;
85+
//var o1 = res.Item6;
86+
//var glyph = res.Item7;
87+
//var loss = res.Item8;
88+
//var loss0 = res.Item9;
89+
//var loss1 = res.Item10;
90+
91+
for (int j = 0; j < 64; ++j)
8992
{
9093
glyphs[j].X = (float)glyph[j, 0];
9194
glyphs[j].Y = (float)glyph[j, 1];
9295
}
9396

9497

95-
Console.WriteLine($"Iteration {i} Output X: {output[0, 0]}, Output Y: {output[0, 1]}, Grad: {gradient[0, 0]}, {gradient[0, 1]}");
96-
Console.WriteLine($"Loss: {loss[0, 0]}");
97-
Console.WriteLine($"O0 X: {o0[0, 0]}, O0 Y: {o0[0, 1]}, Loss 0: {loss0[0, 0]}");
98-
Console.WriteLine($"O1 X: {o1[0, 0]}, O1 Y: {o1[0, 1]}, Loss 1: {loss1[0, 0]}");
99-
await net.Backward(gradient, gradient0, gradient1);
98+
//Console.WriteLine($"Iteration {i} Output X: {output[0, 0]}, Output Y: {output[0, 1]}, Grad: {gradient[0, 0]}, {gradient[0, 1]}");
99+
Console.WriteLine($"Iteration {i} Glyph: {glyphs[0].X}, {glyphs[0].Y}");
100+
Console.WriteLine($"Loss: {gradients.Sum(x => x.Loss[0, 0])}");
101+
//Console.WriteLine($"O0 X: {o0[0, 0]}, O0 Y: {o0[0, 1]}, Loss 0: {loss0[0, 0]}");
102+
//Console.WriteLine($"O1 X: {o1[0, 0]}, O1 Y: {o1[0, 1]}, Loss 1: {loss1[0, 0]}");
103+
await net.Backward(gradients);
100104
net.ApplyGradients();
101105
//}
102106

@@ -121,5 +125,41 @@ await files.WithRepeatAsync(async (pngFile, token) =>
121125
CudaBlas.Instance.Dispose();
122126
}
123127
}
128+
129+
private int[,] AnalyzeImageSections(Node[,] interpolated)
130+
{
131+
// Assuming Node is a custom type with a GrayValue property.
132+
// Size of the output matrix.
133+
const int outputSize = 8;
134+
// Calculate section size based on the input image dimensions and desired output matrix size.
135+
int sectionWidth = interpolated.GetLength(0) / outputSize;
136+
int sectionHeight = interpolated.GetLength(1) / outputSize;
137+
138+
// Initialize the output matrix.
139+
int[,] sectionAnalysis = new int[outputSize, outputSize];
140+
141+
// Process each section.
142+
for (int sectionX = 0; sectionX < outputSize; sectionX++)
143+
{
144+
for (int sectionY = 0; sectionY < outputSize; sectionY++)
145+
{
146+
// Calculate the start and end indices for the section.
147+
int startX = sectionX * sectionWidth;
148+
int startY = sectionY * sectionHeight;
149+
int endX = startX + sectionWidth;
150+
int endY = startY + sectionHeight;
151+
152+
// Flatten the section into a single collection for easier analysis.
153+
var sectionPixels = Enumerable.Range(startX, sectionWidth).SelectMany(
154+
x => Enumerable.Range(startY, sectionHeight),
155+
(x, y) => (256d - interpolated[x, y].GrayValue) / (255d));
156+
157+
// Determine if 40% or more of the pixels in the section are greater than 0.5.
158+
sectionAnalysis[sectionX, sectionY] = sectionPixels.Count(val => val > 0.5) >= sectionPixels.Count() * 0.4 ? 1 : 0;
159+
}
160+
}
161+
162+
return sectionAnalysis;
163+
}
124164
}
125165
}

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/GlyphNetwork/Architecture/glyphnet.json

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,6 @@
129129
"type": "ElementwiseVectorCartesianGlyphOperation",
130130
"inputs": [ "vector_attention", "vector_queries_attention", "summation_weights_square" ],
131131
"setResultTo": "Glyph"
132-
},
133-
{
134-
"id": "targeted_sum_0",
135-
"type": "ElementwiseVectorCartesianTargetedSumOperation",
136-
"inputs": [ "glyph", "RotationTargets", "Target0" ],
137-
"setResultTo": "TargetedSum0"
138-
},
139-
{
140-
"id": "targeted_sum_1",
141-
"type": "ElementwiseVectorCartesianTargetedSumOperation",
142-
"inputs": [ "glyph", "RotationTargets", "Target1" ],
143-
"setResultTo": "TargetedSum1"
144-
},
145-
{
146-
"id": "output",
147-
"type": "ElementwiseVectorCartesianRotationAndSumOperation",
148-
"inputs": [ "glyph", "RotationTargets" ],
149-
"setResultTo": "Output"
150132
}
151133
]
152134
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
{
2+
"timeSteps": [
3+
{
4+
"startOperations": [
5+
{
6+
"id": "angles_square",
7+
"type": "ElementwiseSquareOperation",
8+
"inputs": [ "Angles" ],
9+
"gradientResultTo": [ "DAngles" ]
10+
},
11+
{
12+
"id": "vectorize_input",
13+
"type": "VectorizeOperation",
14+
"inputs": [ "Input", "angles_square" ]
15+
},
16+
{
17+
"id": "projection_vectors_square",
18+
"type": "ElementwiseSquareOperation",
19+
"inputs": [ "ProjectionVectors" ],
20+
"gradientResultTo": [ "DProjectionVectors" ]
21+
},
22+
{
23+
"id": "vector_decomposition",
24+
"type": "ElementwiseVectorMiniDecompositionOperation",
25+
"inputs": [ "vectorize_input", "projection_vectors_square", "ProjectionWeights" ],
26+
"gradientResultTo": [ null, null, "DProjectionWeights" ]
27+
},
28+
{
29+
"id": "weight_vectors_square",
30+
"type": "ElementwiseSquareOperation",
31+
"inputs": [ "WeightVectors" ],
32+
"gradientResultTo": [ "DWeightVectors" ]
33+
},
34+
{
35+
"id": "weight_vectors_square2",
36+
"type": "ElementwiseSquareOperation",
37+
"inputs": [ "WeightVectors2" ],
38+
"gradientResultTo": [ "DWeightVectors2" ]
39+
},
40+
{
41+
"id": "weight_square",
42+
"type": "ElementwiseSquareOperation",
43+
"inputs": [ "Weights" ],
44+
"gradientResultTo": [ "DWeights" ]
45+
},
46+
{
47+
"id": "weight_square2",
48+
"type": "ElementwiseSquareOperation",
49+
"inputs": [ "Weights2" ],
50+
"gradientResultTo": [ "DWeights2" ]
51+
},
52+
{
53+
"id": "start_solar_system",
54+
"type": "ElementwiseVectorConstituentMonoMultiplyOperation",
55+
"inputs": [ "vector_decomposition", "weight_vectors_square", "weight_square" ]
56+
},
57+
{
58+
"id": "start_solar_system_2",
59+
"type": "ElementwiseVectorConstituentMonoMultiplyOperation",
60+
"inputs": [ "vector_decomposition", "weight_vectors_square2", "weight_square2" ]
61+
},
62+
{
63+
"id": "vector_add",
64+
"type": "ElementwiseVectorAddOperation",
65+
"inputs": [ "start_solar_system", "start_solar_system_2" ]
66+
},
67+
{
68+
"id": "vector_keys",
69+
"type": "NewGpuMatrixMultiplyOperation",
70+
"inputs": [ "vector_add", "Keys" ],
71+
"gradientResultTo": [ null, "DKeys" ]
72+
},
73+
{
74+
"id": "vector_add_broadcasting",
75+
"type": "MatrixAddBroadcastingOperation",
76+
"inputs": [ "vector_keys", "KB" ],
77+
"gradientResultTo": [ null, "DKB" ]
78+
},
79+
{
80+
"id": "vector_act",
81+
"type": "LeakyReLUOperation",
82+
"inputs": [ "vector_add_broadcasting" ]
83+
},
84+
{
85+
"id": "vector_queries",
86+
"type": "NewGpuMatrixMultiplyOperation",
87+
"inputs": [ "vector_add", "Queries" ],
88+
"gradientResultTo": [ null, "DQueries" ]
89+
},
90+
{
91+
"id": "vector_queries_add_broadcasting",
92+
"type": "MatrixAddBroadcastingOperation",
93+
"inputs": [ "vector_queries", "QB" ],
94+
"gradientResultTo": [ null, "DQB" ]
95+
},
96+
{
97+
"id": "vector_queries_act",
98+
"type": "LeakyReLUOperation",
99+
"inputs": [ "vector_queries_add_broadcasting" ]
100+
},
101+
{
102+
"id": "vector_softmax",
103+
"type": "PairwiseSineSoftmaxOperation",
104+
"inputs": [ "vector_act" ]
105+
},
106+
{
107+
"id": "vector_attention",
108+
"type": "VectorAttentionOperation",
109+
"inputs": [ "vector_add", "vector_softmax" ]
110+
},
111+
{
112+
"id": "vector_queries_softmax",
113+
"type": "PairwiseSineSoftmaxOperation",
114+
"inputs": [ "vector_queries_act" ]
115+
},
116+
{
117+
"id": "vector_queries_attention",
118+
"type": "VectorAttentionOperation",
119+
"inputs": [ "vector_add", "vector_queries_softmax" ]
120+
},
121+
{
122+
"id": "summation_weights_square",
123+
"type": "ElementwiseSquareOperation",
124+
"inputs": [ "SummationWeights" ],
125+
"gradientResultTo": [ "DSummationWeights" ]
126+
},
127+
{
128+
"id": "glyph",
129+
"type": "ElementwiseVectorCartesianGlyphOperation",
130+
"inputs": [ "vector_attention", "vector_queries_attention", "summation_weights_square" ],
131+
"setResultTo": "Glyph"
132+
},
133+
{
134+
"id": "targeted_sum_0",
135+
"type": "ElementwiseVectorCartesianTargetedSumOperation",
136+
"inputs": [ "glyph", "RotationTargets", "Target0" ],
137+
"setResultTo": "TargetedSum0"
138+
},
139+
{
140+
"id": "targeted_sum_1",
141+
"type": "ElementwiseVectorCartesianTargetedSumOperation",
142+
"inputs": [ "glyph", "RotationTargets", "Target1" ],
143+
"setResultTo": "TargetedSum1"
144+
},
145+
{
146+
"id": "output",
147+
"type": "ElementwiseVectorCartesianRotationAndSumOperation",
148+
"inputs": [ "glyph", "RotationTargets" ],
149+
"setResultTo": "Output"
150+
}
151+
]
152+
}
153+
]
154+
}

0 commit comments

Comments
 (0)