-
Notifications
You must be signed in to change notification settings - Fork 1
/
FFCC.mm
299 lines (262 loc) · 9.67 KB
/
FFCC.mm
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
#import "FFCC.h"
#import "Lib/Toastbox/Mac/Color.h"
#import "Lib/Toastbox/Mac/MetalUtil.h"
#import "Lib/Toastbox/Mac/Renderer.h"
#import "Lib/Toastbox/Mac/Mod.h"
using namespace Toastbox;
using Mat64 = FFCC::Mat64;
using Mat64c = FFCC::Mat64c;
using Vec2 = FFCC::Vec2;
using Vec3 = FFCC::Vec3;
#define _ShaderNamespace "FFCC::"
static Renderer::Txt _createMaskedImage(Renderer& renderer, id<MTLTexture> img, id<MTLTexture> mask) {
Renderer::Txt maskedImg = renderer.textureCreate(img);
renderer.render(maskedImg,
renderer.FragmentShader(_ShaderNamespace "ApplyMask",
// Texture args
img,
mask
)
);
return maskedImg;
}
static Renderer::Txt _createAbsDevImage(Renderer& renderer, id<MTLTexture> img, id<MTLTexture> mask) {
Renderer::Txt coeff = renderer.textureCreate(MTLPixelFormatR32Float, [img width], [img height]);
renderer.render(coeff,
renderer.FragmentShader(_ShaderNamespace "LocalAbsoluteDeviationCoeff",
mask
)
);
Renderer::Txt absDevImage = renderer.textureCreate(img);
renderer.render(absDevImage,
renderer.FragmentShader(_ShaderNamespace "LocalAbsoluteDeviation",
img,
mask,
coeff
)
);
return absDevImage;
}
static Mat64 _calcXFromImage(const FFCC::Model& model, Renderer& renderer, id<MTLTexture> img, id<MTLTexture> mask) {
const uint32_t w = (uint32_t)[img width];
const uint32_t h = (uint32_t)[img height];
Renderer::Txt u = renderer.textureCreate(MTLPixelFormatR32Float, w, h);
renderer.render(u,
renderer.FragmentShader(_ShaderNamespace "CalcU",
// Texture args
img
)
);
Renderer::Txt v = renderer.textureCreate(MTLPixelFormatR32Float, w, h);
renderer.render(v,
renderer.FragmentShader(_ShaderNamespace "CalcV",
// Texture args
img
)
);
using ValidPixelCount = uint32_t;
Renderer::Buf validPixelCountBuf = renderer.bufferCreate(sizeof(ValidPixelCount), MTLStorageModeManaged);
renderer.bufferClear(validPixelCountBuf);
Renderer::Txt maskUV = renderer.textureCreate(MTLPixelFormatR8Unorm, w, h);
{
const float thresh = model.params.histogram.minIntensity;
renderer.render(maskUV,
renderer.FragmentShader(_ShaderNamespace "CalcMaskUV",
// Buffer args
thresh,
validPixelCountBuf,
// Texture args
img,
mask
)
);
}
const uint32_t binCount = (uint32_t)model.params.histogram.binCount;
const float binSize = model.params.histogram.binSize;
const float binMin = model.params.histogram.startingUV;
renderer.render(u,
renderer.FragmentShader(_ShaderNamespace "CalcBinUV",
// Buffer args
binCount,
binSize,
binMin,
// Texture args
u
)
);
renderer.render(v,
renderer.FragmentShader(_ShaderNamespace "CalcBinUV",
// Buffer args
binCount,
binSize,
binMin,
// Texture args
v
)
);
const size_t binsBufCount = binCount*binCount;
const size_t binsBufLen = sizeof(std::atomic_uint)*binsBufCount;
Renderer::Buf binsBuf = renderer.bufferCreate(binsBufLen, MTLStorageModeManaged);
renderer.bufferClear(binsBuf);
renderer.compute(w, h,
renderer.ComputeKernel(_ShaderNamespace "CalcHistogram",
// Buffer args
binCount,
binsBuf,
// Texture args
u,
v,
maskUV
)
);
Renderer::Txt Xc = renderer.textureCreate(MTLPixelFormatR32Float, binCount, binCount);
renderer.render(Xc,
renderer.FragmentShader(_ShaderNamespace "LoadHistogram",
// Buffer args
binCount,
binsBuf
)
);
renderer.render(Xc,
renderer.FragmentShader(_ShaderNamespace "NormalizeHistogram",
// Buffer args
validPixelCountBuf,
// Texture args
Xc
)
);
Renderer::Txt XcTransposed = renderer.textureCreate(MTLPixelFormatR32Float, binCount, binCount);
renderer.render(XcTransposed,
renderer.FragmentShader(_ShaderNamespace "Transpose",
// Texture args
Xc
)
);
renderer.sync(XcTransposed);
renderer.commitAndWait();
// Copy the histogram from a texture -> Mat64
std::vector<float> histFloats = renderer.textureRead<float>(XcTransposed);
Mat64 hist;
// Copy the floats into the matrix
// The source matrix (XcTransposed) is transposed, so the data is already
// in column-major order. (If we didn't transpose it, it would be in row-major
// order, since that's how textures are normally laid out in memory...)
std::copy(histFloats.begin(), histFloats.end(), hist.begin());
return hist;
}
static Mat64 _softmaxForward(const Mat64& H) {
Mat64 r = H;
// Find the max value in `r`
double maxVal = -INFINITY;
for (double x : r) maxVal = std::max(maxVal, x);
// Subtract `maxVal` from every element
r -= maxVal;
// Raise e to each element in `r`
for (double& x : r) x = std::exp(x);
// Normalize `r` using its sum
r /= r.sum();
return r;
}
static Vec2 _fitBivariateVonMises(const Mat64& P) {
constexpr size_t H = Mat64::Height;
// Given a 2D PDF histogram (PMF), approximately fits a bivariate Von Mises
// distribution to that PDF by computing the local moments. This produces a
// center of mass of the PDF, where the PDF is assumed to lie on a torus rather
// than a cartesian space.
//
// Outputs:
// mu - the 2D location of the center of the bivariate Von Mises distribution,
// which is in 1-indexed coordinates of the input PDF P.
// Sigma - The covariance matrix which was used to compute "confidence".
const size_t n = H;
const double angleStep = (2*M_PI) / n;
Mat<double,H,1> angles;
double angle = 0;
for (double& x : angles) {
x = angle;
angle += angleStep;
}
// Fit the mean of the distribution by finding the first moments of of the
// histogram on both axes, which can be done by finding the first moment of the
// sin and cosine of both axes and computing the arctan. Taken from Section 6.2
// of "Bayesian Methods in Structural Bioinformatics", but adapted to a
// histogram. This process can be optimized by computing the rowwise and
// columnwise sums of P and finding the moments on each axis independently.
Mat<double,H,1> P1 = P.sumRows();
Mat<double,H,1> P2 = P.sumCols().trans();
Mat<double,H,1> sinAngles;
Mat<double,H,1> cosAngles;
for (size_t i=0; i<angles.Count; i++) {
sinAngles[i] = sin(angles[i]);
cosAngles[i] = cos(angles[i]);
}
const double y1 = P1.elmMul(sinAngles).sumCols()[0];
const double x1 = P1.elmMul(cosAngles).sumCols()[0];
const double y2 = P2.elmMul(sinAngles).sumCols()[0];
const double x2 = P2.elmMul(cosAngles).sumCols()[0];
const double mu1 = Mod(std::atan2(y1,x1), 2*M_PI) / angleStep;
const double mu2 = Mod(std::atan2(y2,x2), 2*M_PI) / angleStep;
return {mu1, mu2}; // 0-indexed; note that the MATLAB version is 1-indexed!
}
static Vec2 _uvForIdx(const FFCC::Model& model, const Vec2& idx) {
return (idx*model.params.histogram.binSize) + model.params.histogram.startingUV;
}
static Vec3 _rgbForUV(const Vec2& uv) {
Vec3 rgb(std::exp(-uv[0]), 1., std::exp(-uv[1]));
rgb /= sqrt(rgb.elmMul(rgb).sum());
return rgb;
}
FFCC::Vec3 FFCC::Run(
const FFCC::Model& model,
Renderer& renderer,
const CFADesc& cfaDesc,
id<MTLTexture> raw
) {
const uint32_t w = 64;
const uint32_t h = (uint32_t)((w*[raw height])/[raw width]);
Renderer::Txt img = renderer.textureCreate(MTLPixelFormatRGBA32Float, w, h);
// De-bayer and scale
{
Renderer::Txt rgb = renderer.textureCreate(MTLPixelFormatRGBA32Float, [raw width], [raw height]);
renderer.render(rgb,
renderer.FragmentShader(_ShaderNamespace "Debayer",
// Buffer args
cfaDesc,
// Texture args
raw
)
);
// Scale the image to w * h
renderer.render(img,
renderer.FragmentShader(_ShaderNamespace "Scale",
// Texture args
rgb
)
);
}
Renderer::Txt mask = renderer.textureCreate(MTLPixelFormatR8Unorm, w, h);
renderer.render(mask,
renderer.FragmentShader(_ShaderNamespace "CreateMask",
// Texture args
img
)
);
const Renderer::Txt maskedImg = _createMaskedImage(renderer, img, mask);
const Renderer::Txt absDevImg = _createAbsDevImage(renderer, img, mask);
const Mat64 X1 = _calcXFromImage(model, renderer, maskedImg, mask);
const Mat64 X2 = _calcXFromImage(model, renderer, absDevImg, mask);
const Mat64c X1_fft = X1.fft();
const Mat64c X2_fft = X2.fft();
const Mat64c X_fft_Times_F_fft[2] = { X1_fft.elmMul(model.F_fft[0]), X2_fft.elmMul(model.F_fft[1]) };
const Mat64c FX_fft = X_fft_Times_F_fft[0] + X_fft_Times_F_fft[1];
const Mat64c FXc = FX_fft.ifft();
Mat64 FX;
for (size_t i=0; i<FXc.Count; i++) {
FX[i] = FXc[i].real();
}
const Mat64 H = FX+model.B;
const Mat64 P = _softmaxForward(H);
const Vec2 fit = _fitBivariateVonMises(P);
const Vec2 uv = _uvForIdx(model, fit);
return _rgbForUV(uv);
}