-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaframe.ts
305 lines (252 loc) · 7.79 KB
/
metaframe.ts
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
export type Base64String = string;
/**
* Prediction/training types
*/
/**
* Prediction/training types
*/
/* Generic map of keys to number arrays */
export interface SensorSeries {[key:string] : Float32Array};
export interface SensorSeriesBase64 {[key:string] : string };
/* Main persisted form of a single complete sensor example (e.g. a gesture) */
export interface PredictionInputEncoded {
series: SensorSeriesBase64;
requestId ?: string | number;
}
export interface PredictionInput {
series: SensorSeries;
requestId ?: string | number;
}
export const sensorSeriesDecode:(series :SensorSeriesBase64) => SensorSeries = series => {
const result :SensorSeries = {};
Object.keys(series).forEach(k => {
if (series[k]) {
result[k] = new Float32Array(base64decode(series[k]));
}
});
return result;
}
export const predictionDecode:(prediction :PredictionInputEncoded) => PredictionInput = prediction => {
console.log('predictionDecode', prediction);
const result :PredictionInput = {
requestId: prediction.requestId,
series: sensorSeriesDecode(prediction.series),
};
return result;
}
export const predictionEncode:(prediction :PredictionInput) => PredictionInputEncoded = prediction => {
const result :PredictionInputEncoded = {
requestId: prediction.requestId,
series: sensorSeriesEncode(prediction.series),
};
return result;
}
export const sensorSeriesEncode:(series :SensorSeries) => SensorSeriesBase64 = series => {
const result :SensorSeriesBase64 = {};
Object.keys(series).forEach(k => result[k] = base64encode(series[k].buffer));
return result;
}
// export const convertIMUSensorJsonToExample :(example:IMUSensorJson) => IMUSensorExample = example => {
// console.log('convertIMUSensorJsonToExample example', example);
// return {
// ax: new Float32Array(base64decode(example.ax)),
// ay: new Float32Array(base64decode(example.ay)),
// az: new Float32Array(base64decode(example.az)),
// at: new Int32Array(base64decode(example.at)),
// gx: new Float32Array(base64decode(example.gx)),
// gy: new Float32Array(base64decode(example.gy)),
// gz: new Float32Array(base64decode(example.gz)),
// gt: new Int32Array(base64decode(example.gt)),
// }
// }
/**
* This is the output from an input prection
* prediction: the class name of the prediction
* predictions: map of class names to score
*/
export interface PredictionResult {
prediction: string;
predictions: {
[className: string]: number
};
requestId ?: string | number;
modelHash: string;
modelId?: string;
note?:string;
}
export interface TrainingDataPoint {
version?: string; // TODO: solve versioning since this is a blob to the graphql API
name?: string;
label: string;
url?: string;
data: PredictionInputEncoded; // JSON transfer uses Base64String
}
export interface TrainingDataSet {
// stored with the model, for bookkeeping
modelId?: string;
examples: Array<TrainingDataPoint>;
/* examples for "no real gesture" */
controlLabels?:string[];
hash:string; // computed using object-hash
}
export interface PredictionMetadata {
classNames: string[];
imageHeight: number;
imageWidth: number;
// maxAbsoluteRawValue: number;
}
export interface TrainingMetadata {
date: Date;
hash: string;
// Derived from training data, use for normalizing predictions
// keys are series labels, e.g. ax, gy etc (**not** example labels)
ranges: Record<string, {min:number, max:number, absmax:number}>;
}
/**
* Low specific types
*/
/**
* Handle streaming IMU data and convert to more friendly formats.
* The internal data format is a dict of Float32Arrays representing
* 1D time series from sensors. This should be easy to extend to arbitrary
* sensor streams (or any set of 1D series).
*/
export interface IMUPoint {
x: number;
y: number;
z: number;
t: number;
}
// this could be any combination of points depending on when they're streaming in
export interface IMUPointCombined {
ax?: number;
ay?: number;
az?: number;
at?: number;
gx?: number;
gy?: number;
gz?: number;
gt?: number;
t?: number;
}
export interface IMUPointCombinedExample {
data :Array<IMUPointCombined>;
}
export interface IMUSensorGesture {
accelerometer :Array<IMUPoint>;
gyroscope :Array<IMUPoint>;
}
export interface IMUGestureChunk {
a ?: IMUPoint;
g ?: IMUPoint;
event? :string;
}
export interface IMUSensorJson {
ax :Base64String;
ay :Base64String;
az :Base64String;
at :Base64String;
gx :Base64String;
gy :Base64String;
gz :Base64String;
gt :Base64String;
}
export interface IMUSensorExample {
ax :Float32Array;
ay :Float32Array;
az :Float32Array;
at :Int32Array;
gx :Float32Array;
gy :Float32Array;
gz :Float32Array;
gt :Int32Array;
}
// the sensor gesture needs to be compacted
export const convertIMUSensorExampleToJson :(example:IMUSensorExample) => IMUSensorJson = example => {
return {
ax: base64encode(example.ax.buffer),
ay: base64encode(example.ay.buffer),
az: base64encode(example.az.buffer),
at: base64encode(example.at.buffer),
gx: base64encode(example.gx.buffer),
gy: base64encode(example.gy.buffer),
gz: base64encode(example.gz.buffer),
gt: base64encode(example.gt.buffer),
}
}
// SensorSeries
// the sensor gesture needs to be compacted
export const convertIMUSensorJsonToExample :(example:IMUSensorJson) => IMUSensorExample = example => {
return {
ax: new Float32Array(base64decode(example.ax)),
ay: new Float32Array(base64decode(example.ay)),
az: new Float32Array(base64decode(example.az)),
at: new Int32Array(base64decode(example.at)),
gx: new Float32Array(base64decode(example.gx)),
gy: new Float32Array(base64decode(example.gy)),
gz: new Float32Array(base64decode(example.gz)),
gt: new Int32Array(base64decode(example.gt)),
}
}
/*
* base64-arraybuffer
* https://github.com/niklasvh/base64-arraybuffer
*
* Copyright (c) 2012 Niklas von Hertzen
* Licensed under the MIT license.
*/
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Use a lookup table to find the index.
const lookup = new Uint8Array(256);
for (var i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
export function base64encode(arraybuffer : ArrayBuffer) :string {
let bytes = new Uint8Array(arraybuffer);
let i: number;
let len = bytes.length;
let base64 = "";
for (i = 0; i < len; i += 3) {
base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
base64 += chars[bytes[i + 2] & 63];
}
if (len % 3 === 2) {
base64 = base64.substring(0, base64.length - 1) + "=";
} else if (len % 3 === 1) {
base64 = base64.substring(0, base64.length - 2) + "==";
}
return base64;
}
export function base64decode(base64 : string) :ArrayBuffer {
if (!base64) {
throw new Error("base64decode string argument given");
}
let bufferLength = base64.length * 0.75,
len = base64.length,
i:number,
p = 0,
encoded1: number,
encoded2: number,
encoded3: number,
encoded4: number;
if (base64[base64.length - 1] === "=") {
bufferLength--;
if (base64[base64.length - 2] === "=") {
bufferLength--;
}
}
var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i += 4) {
encoded1 = lookup[base64.charCodeAt(i)];
encoded2 = lookup[base64.charCodeAt(i + 1)];
encoded3 = lookup[base64.charCodeAt(i + 2)];
encoded4 = lookup[base64.charCodeAt(i + 3)];
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
}