This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
/
files.ts
293 lines (269 loc) · 11 KB
/
files.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
/**
* @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.
* =============================================================================
*/
/**
* IOHandlers related to files.
*
* such as browser-triggered file downloads, user-selected files in browser and
* native files (node.js).
*/
// tslint:disable:max-line-length
import {stringByteLength} from './io_utils';
import {IOHandler, ModelArtifacts, SaveResult, WeightsManifestConfig} from './types';
// tslint:enable:max-line-length
const DEFAULT_FILE_NAME_PREFIX = 'model';
const DEFAULT_JSON_EXTENSION_NAME = '.json';
const DEFAULT_WEIHGT_DATA_EXTENSION_NAME = '.weights.bin';
export interface DownloadTriggerConfig {
/**
* The HTML anchor ('<a>') element to bind the downloading of the model
* architecture + weights manifest JSON file to.
*
* Optional. If not specified, a temporary, orphan anchor element will be
* create to bind the download to.
*/
jsonAnchor?: HTMLAnchorElement;
/**
* The HTML anchor ('<a>') element to bind the downloading of the binary
* weights file to.
*
* Optional. If not specified, a temporary, orphan anchor element will be
* create to bind the download to.
*/
weightDataAnchor?: HTMLAnchorElement;
/**
* Whether the download(s) should be triggered immediately (default: `true`).
*/
trigger?: boolean;
}
export class DownloadTrigger implements IOHandler {
private readonly modelTopologyFileName: string;
private readonly weightDataFileName: string;
private readonly jsonAnchor: HTMLAnchorElement;
private readonly weightDataAnchor: HTMLAnchorElement;
private readonly trigger: boolean;
constructor(fileNames?: string|string[], config?: DownloadTriggerConfig) {
if (!window) {
// TODO(cais): Provide info on what IOHandlers are available under the
// current environment.
throw new Error(
'triggerDownloads() cannot proceed because the current environment ' +
'is not a browser.');
}
if (fileNames == null) {
fileNames = [DEFAULT_FILE_NAME_PREFIX];
} else if (!Array.isArray(fileNames)) {
fileNames = [fileNames];
}
if (fileNames.length === 1) {
this.modelTopologyFileName = fileNames[0] + DEFAULT_JSON_EXTENSION_NAME;
this.weightDataFileName =
fileNames[0] + DEFAULT_WEIHGT_DATA_EXTENSION_NAME;
} else if (fileNames.length === 2) {
this.modelTopologyFileName = fileNames[0];
this.weightDataFileName = fileNames[1];
} else {
// TODO(cais): Support length === 3, for the case of GraphDef models, in
// which case there will be three files: GraphDef (binary), weight
// manifest (text), weight data (binary).
throw new Error(
`File names supplied to tf.io.triggerDownload() must be a single ` +
`string, an Array of a single string or an Array of two strings, ` +
`but received an Array of ${fileNames.length} strings.`);
}
if (config != null) {
this.jsonAnchor = config.jsonAnchor;
this.weightDataAnchor = config.weightDataAnchor;
this.trigger = config.trigger == null ? true : config.trigger;
} else {
this.trigger = true;
}
}
async save(modelArtifacts: ModelArtifacts): Promise<SaveResult> {
const weightsURL = window.URL.createObjectURL(new Blob(
[modelArtifacts.weightData], {type: 'application/octet-stream'}));
if (modelArtifacts.modelTopology instanceof ArrayBuffer) {
throw new Error(
'DownloadTrigger.save() does not support saving model topology ' +
'in binary formats yet.');
} else {
const weightsManifest: WeightsManifestConfig = [{
paths: ['./' + this.weightDataFileName],
weights: modelArtifacts.weightSpecs
}];
const modelTopologyAndWeightManifest = {
modelTopology: modelArtifacts.modelTopology,
weightsManifest
};
const modelTopologyAndWeightManifestURL =
window.URL.createObjectURL(new Blob(
[JSON.stringify(modelTopologyAndWeightManifest)],
{type: 'application/json'}));
// If anchor elements are not provided, create them without attaching them
// to parents, so that the downloaded file names can be controlled.
const jsonAnchor = this.jsonAnchor == null ?
document.createElement('a') as HTMLAnchorElement :
this.jsonAnchor;
jsonAnchor.download = this.modelTopologyFileName;
jsonAnchor.href = modelTopologyAndWeightManifestURL;
const weightDataAnchor = this.weightDataAnchor == null ?
document.createElement('a') as HTMLAnchorElement :
this.weightDataAnchor;
weightDataAnchor.download = this.weightDataFileName;
weightDataAnchor.href = weightsURL;
// Trigger downloads by calling the `click` methods on the download
// anchors.
if (this.trigger) {
jsonAnchor.click();
weightDataAnchor.click();
}
return {
modelArtifactsInfo: {
dateSaved: new Date(),
modelTopologyType: 'KerasJSON',
modelTopologyBytes:
stringByteLength(JSON.stringify(modelArtifacts.modelTopology)),
weightSpecsBytes:
stringByteLength(JSON.stringify(modelArtifacts.weightSpecs)),
weightDataBytes: modelArtifacts.weightData.byteLength,
}
};
}
}
}
export class Files implements IOHandler {
private readonly files: File[];
constructor(files?: File[]) {
this.files = files;
}
async load(): Promise<ModelArtifacts> {
if (this.files.length !== 2) {
throw new Error(
`Files.load() currently supports only loading from 2 files ` +
`(a JSON file and a binary weights file), but ` +
`received ${this.files.length} file(s).`);
}
return new Promise<ModelArtifacts>((resolve, reject) => {
const jsonReader = new FileReader();
// tslint:disable-next-line:no-any
jsonReader.onload = (event: any) => {
const modelJSON = JSON.parse(event.target.result);
const modelTopology = modelJSON.modelTopology as {};
if (modelTopology == null) {
reject(new Error(`modelTopology field is missing from file ${
this.files[0].name}`));
}
const weightsManifest =
modelJSON.weightsManifest as WeightsManifestConfig;
if (weightsManifest == null) {
reject(new Error(`weightManifest field is missing from file ${
this.files[0].name}`));
}
if (weightsManifest.length !== 1) {
reject(new Error(
`When uploading user-selected files, we current support only ` +
`a single weight group, but the weights manifest in ` +
`${this.files[0].name} indicates there are ` +
`${weightsManifest.length} weight groups.`));
}
const weightGroup = weightsManifest[0];
if (weightGroup.paths.length !== 1) {
reject(new Error(
`When uploading user-selected files, we current support only ` +
`a single weight file, but the weights manifest in ` +
`${this.files[0].name} indicates there are ` +
`${weightGroup.paths.length} weight files.`));
}
const weightSpecs = weightGroup.weights;
const weightsReader = new FileReader();
// tslint:disable-next-line:no-any
weightsReader.onload = (event: any) => {
const weightData = event.target.result;
resolve({modelTopology, weightSpecs, weightData});
};
weightsReader.readAsArrayBuffer(this.files[1]);
};
jsonReader.readAsText(this.files[0]);
});
}
}
/**
* Factory method for IOHandler that triggers file downloads.
*
* The returned `IOHandler` instance can be used as model exporting methods such
* as `tf.Model.save` and supports only saving.
*
* ```js
* const model = tf.sequential();
* model.add(tf.layers.dense(
* {units: 1, inputShape: [10], activation: 'sigmoid'}));
* const artifactsInfo = await model.save(tf.io.triggerDownloads(
* ['mymodel.json', 'mymodel.weights.bin'])):
* console.log(artifactsInfo);
* ```
*
* @param fileNames Name(s) of the files to be downloaded. For use with
* `tf.Model`, `fileNames` should follow one of the following three formats:
* 1. `fileNames` being `null` or `undefined`, in which case the default file
* names will be used:
* - 'model.json' for the JSON file containing the model topology and
* weights manifest.
* - 'model.weights.bin' for the binary file containing the binary weight
* values.
* 2. A single string or an Array of a single string, as the file name prefix.
* For example, if `'foo'` or `['foo']` is provided, the downloaded JSON
* file and binary weights file will be named 'foo.json' and
* 'foo.weights.bin', respectively.
* 3. An `Array` of two file names, as full names of the JSON and binary
* weight files, in that order.
* @param config Additional configuration for triggering downloads.
* @returns An instance of `DownloadTrigger` `IOHandler`.
*/
export function triggerDownloads(
fileNames?: string|string[],
config?: DownloadTriggerConfig): DownloadTrigger {
return new DownloadTrigger(fileNames, config);
}
/**
* Factory method for IOHandler that loads model artifacts from files.
*
* This method can be used for loading from files such as user-selected files
* in the browser.
* When used in conjunction with `tf.loadModel`, an instance of `tf.Model`
* (Keras-style) can be constructed from the loaded artifacts.
*
* ```js
* // Note: This code snippet won't run properly without the actual file input
* // elements in the HTML DOM.
*
* // Suppose there are two HTML file input (`<input type="file" ...>`)
* // elements.
* const uploadJSONInput = document.getElementById('upload-json');
* const uploadWeightsInput = document.getElementById('upload-weights');
* const model = await tfl.loadModel(
* tfc.io.files([uploadJSONInput.files[0], uploadWeightsInput.files[0]]));
* ```
*
* @param files `File`s to load from. Currently, this function supports only
* loading from files that contain Keras-style models (i.e., `tf.Model`s), for
* which an `Array` of two `File`s is expected (in that order):
* - A JSON file containing the model topology and weight manifest.
* - A binary file containing the binary weights.
* @returns An instance of `Files` `IOHandler`.
*/
export function files(files?: File[]): Files {
return new Files(files);
}