-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
readers.ts
282 lines (276 loc) · 9.15 KB
/
readers.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
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
import {TensorContainer} from '@tensorflow/tfjs-core';
import {Dataset, datasetFromIteratorFn} from './dataset';
import {CSVDataset} from './datasets/csv_dataset';
import {iteratorFromFunction} from './iterators/lazy_iterator';
import {MicrophoneIterator} from './iterators/microphone_iterator';
import {WebcamIterator} from './iterators/webcam_iterator';
import {URLDataSource} from './sources/url_data_source';
import {CSVConfig, MicrophoneConfig, WebcamConfig} from './types';
/**
* Create a `CSVDataset` by reading and decoding CSV file(s) from provided URL
* or local path if it's in Node environment.
*
* Note: If isLabel in columnConfigs is `true` for at least one column, the
* element in returned `CSVDataset` will be an object of
* `{xs:features, ys:labels}`: xs is a dict of features key/value pairs, ys
* is a dict of labels key/value pairs. If no column is marked as label,
* returns a dict of features only.
*
* ```js
* const csvUrl =
* 'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv';
*
* async function run() {
* // We want to predict the column "medv", which represents a median value of
* // a home (in $1000s), so we mark it as a label.
* const csvDataset = tf.data.csv(
* csvUrl, {
* columnConfigs: {
* medv: {
* isLabel: true
* }
* }
* });
*
* // Number of features is the number of column names minus one for the label
* // column.
* const numOfFeatures = (await csvDataset.columnNames()).length - 1;
*
* // Prepare the Dataset for training.
* const flattenedDataset =
* csvDataset
* .map(({xs, ys}) =>
* {
* // Convert xs(features) and ys(labels) from object form (keyed by
* // column name) to array form.
* return {xs:Object.values(xs), ys:Object.values(ys)};
* })
* .batch(10);
*
* // Define the model.
* const model = tf.sequential();
* model.add(tf.layers.dense({
* inputShape: [numOfFeatures],
* units: 1
* }));
* model.compile({
* optimizer: tf.train.sgd(0.000001),
* loss: 'meanSquaredError'
* });
*
* // Fit the model using the prepared Dataset
* return model.fitDataset(flattenedDataset, {
* epochs: 10,
* callbacks: {
* onEpochEnd: async (epoch, logs) => {
* console.log(epoch + ':' + logs.loss);
* }
* }
* });
* }
*
* await run();
* ```
*
* @param source URL or local path to get CSV file. If it's a local path, it
* must have prefix `file://` and it only works in node environment.
* @param csvConfig (Optional) A CSVConfig object that contains configurations
* of reading and decoding from CSV file(s).
*
* @doc {
* heading: 'Data',
* subheading: 'Creation',
* namespace: 'data',
* configParamIndices: [1]
* }
*/
export function csv(
source: RequestInfo, csvConfig: CSVConfig = {}): CSVDataset {
return new CSVDataset(new URLDataSource(source), csvConfig);
}
/**
* Create a `Dataset` that produces each element by calling a provided function.
*
* Note that repeated iterations over this `Dataset` may produce different
* results, because the function will be called anew for each element of each
* iteration.
*
* Also, beware that the sequence of calls to this function may be out of order
* in time with respect to the logical order of the Dataset. This is due to the
* asynchronous lazy nature of stream processing, and depends on downstream
* transformations (e.g. .shuffle()). If the provided function is pure, this is
* no problem, but if it is a closure over a mutable state (e.g., a traversal
* pointer), then the order of the produced elements may be scrambled.
*
* ```js
* let i = -1;
* const func = () =>
* ++i < 5 ? {value: i, done: false} : {value: null, done: true};
* const ds = tf.data.func(func);
* await ds.forEachAsync(e => console.log(e));
* ```
*
* @param f A function that produces one data element on each call.
*/
export function func<T extends TensorContainer>(
f: () => IteratorResult<T>| Promise<IteratorResult<T>>): Dataset<T> {
const iter = iteratorFromFunction(f);
return datasetFromIteratorFn(async () => iter);
}
/**
* Create a `Dataset` that produces each element from provided JavaScript
* generator, which is a function*
* (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generator_functions),
* or a function that returns an
* iterator
* (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Generator_functions).
*
* The returned iterator should have `.next()` function that returns element in
* format of `{value: TensorContainer, done:boolean}`.
*
* Example of creating a dataset from an iterator factory:
* ```js
* function makeIterator() {
* const numElements = 10;
* let index = 0;
*
* const iterator = {
* next: () => {
* let result;
* if (index < numElements) {
* result = {value: index, done: false};
* index++;
* return result;
* }
* return {value: index, done: true};
* }
* };
* return iterator;
* }
* const ds = tf.data.generator(makeIterator);
* await ds.forEachAsync(e => console.log(e));
* ```
*
* Example of creating a dataset from a generator:
* ```js
* function* dataGenerator() {
* const numElements = 10;
* let index = 0;
* while (index < numElements) {
* const x = index;
* index++;
* yield x;
* }
* }
*
* const ds = tf.data.generator(dataGenerator);
* await ds.forEachAsync(e => console.log(e));
* ```
*
* @param generator A JavaScript generator function that returns a JavaScript
* iterator.
*
* @doc {
* heading: 'Data',
* subheading: 'Creation',
* namespace: 'data',
* configParamIndices: [1]
* }
*/
export function generator<T extends TensorContainer>(
generator: () => Iterator<T>| Promise<Iterator<T>>): Dataset<T> {
return datasetFromIteratorFn(async () => {
const gen = await generator();
return iteratorFromFunction(() => gen.next());
});
}
/**
* Create an iterator that generates `Tensor`s from webcam video stream. This
* API only works in Browser environment when the device has webcam.
*
* Note: this code snippet only works when the device has a webcam. It will
* request permission to open the webcam when running.
* ```js
* const videoElement = document.createElement('video');
* videoElement.width = 100;
* videoElement.height = 100;
* const cam = await tf.data.webcam(videoElement);
* const img = await cam.capture();
* img.print();
* cam.stop();
* ```
*
* @param webcamVideoElement A `HTMLVideoElement` used to play video from
* webcam. If this element is not provided, a hidden `HTMLVideoElement` will
* be created. In that case, `resizeWidth` and `resizeHeight` must be
* provided to set the generated tensor shape.
* @param webcamConfig A `WebcamConfig` object that contains configurations of
* reading and manipulating data from webcam video stream.
*
* @doc {
* heading: 'Data',
* subheading: 'Creation',
* namespace: 'data',
* ignoreCI: true
* }
*/
export async function webcam(
webcamVideoElement?: HTMLVideoElement,
webcamConfig?: WebcamConfig): Promise<WebcamIterator> {
return WebcamIterator.create(webcamVideoElement, webcamConfig);
}
/**
* Create an iterator that generates frequency-domain spectrogram `Tensor`s from
* microphone audio stream with browser's native FFT. This API only works in
* browser environment when the device has microphone.
*
* Note: this code snippet only works when the device has a microphone. It will
* request permission to open the microphone when running.
* ```js
* const mic = await tf.data.microphone({
* fftSize: 1024,
* columnTruncateLength: 232,
* numFramesPerSpectrogram: 43,
* sampleRateHz:44100,
* includeSpectrogram: true,
* includeWaveform: true
* });
* const audioData = await mic.capture();
* const spectrogramTensor = audioData.spectrogram;
* spectrogramTensor.print();
* const waveformTensor = audioData.waveform;
* waveformTensor.print();
* mic.stop();
* ```
*
* @param microphoneConfig A `MicrophoneConfig` object that contains
* configurations of reading audio data from microphone.
*
* @doc {
* heading: 'Data',
* subheading: 'Creation',
* namespace: 'data',
* ignoreCI: true
* }
*/
export async function microphone(microphoneConfig?: MicrophoneConfig):
Promise<MicrophoneIterator> {
return MicrophoneIterator.create(microphoneConfig);
}