@webwriter/deep-learning-model 0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/main.yml +8 -0
- package/LICENSE +7 -0
- package/README.md +290 -0
- package/components/dataset-input/dataset-input.styles.ts +20 -0
- package/components/dataset-input/dataset-input.ts +196 -0
- package/components/inference-fields/class-input.ts +83 -0
- package/components/inference-fields/class-output.ts +62 -0
- package/components/inference-fields/image-input.ts +182 -0
- package/components/inference-fields/image.ts +82 -0
- package/components/inference-fields/inference-interfaces.ts +15 -0
- package/components/inference-fields/number-input.ts +62 -0
- package/components/inference-fields/number-output.ts +56 -0
- package/components/model-editor.ts +273 -0
- package/components/model-tests/bad-samples.ts +152 -0
- package/components/model-tests/loss-distribution.ts +174 -0
- package/components/reference-element-select.ts +105 -0
- package/custom-elements.json +1446 -0
- package/custom.d.ts +180 -0
- package/dist/widgets/webwriter-feature-engineering.css +444 -0
- package/dist/widgets/webwriter-feature-engineering.js +112241 -0
- package/dist/widgets/webwriter-model-designing.css +444 -0
- package/dist/widgets/webwriter-model-designing.js +113967 -0
- package/dist/widgets/webwriter-model-evaluation.css +444 -0
- package/dist/widgets/webwriter-model-evaluation.js +119019 -0
- package/dist/widgets/webwriter-model-prediction.css +444 -0
- package/dist/widgets/webwriter-model-prediction.js +109260 -0
- package/dist/widgets/webwriter-model-training.css +444 -0
- package/dist/widgets/webwriter-model-training.js +121391 -0
- package/editing-config.json +73 -0
- package/icon.svg +1 -0
- package/lib/dataset/class.ts +155 -0
- package/lib/dataset/dataset.ts +293 -0
- package/lib/dataset/feature.ts +110 -0
- package/lib/dataset/image.ts +312 -0
- package/lib/dataset/linear.ts +120 -0
- package/lib/layers/activation.ts +48 -0
- package/lib/layers/add.ts +50 -0
- package/lib/layers/attention.ts +74 -0
- package/lib/layers/concat.ts +50 -0
- package/lib/layers/conv.ts +83 -0
- package/lib/layers/dropout.ts +44 -0
- package/lib/layers/fcl.ts +66 -0
- package/lib/layers/flatten.ts +48 -0
- package/lib/layers/index.ts +708 -0
- package/lib/layers/input.ts +55 -0
- package/lib/layers/noise.ts +43 -0
- package/lib/layers/output.ts +64 -0
- package/lib/layers/reshape.ts +76 -0
- package/lib/layers/transposeconv.ts +90 -0
- package/lib/tflib.ts +6 -0
- package/lit-localize.json +47 -0
- package/localization/ar.xlf +532 -0
- package/localization/bg.xlf +531 -0
- package/localization/cs.xlf +532 -0
- package/localization/da.xlf +531 -0
- package/localization/de.xlf +531 -0
- package/localization/el.xlf +533 -0
- package/localization/es.xlf +532 -0
- package/localization/et.xlf +532 -0
- package/localization/fi.xlf +531 -0
- package/localization/fr.xlf +532 -0
- package/localization/generated/ar.ts +158 -0
- package/localization/generated/bg.ts +157 -0
- package/localization/generated/cs.ts +158 -0
- package/localization/generated/da.ts +157 -0
- package/localization/generated/de.ts +157 -0
- package/localization/generated/el.ts +159 -0
- package/localization/generated/es.ts +158 -0
- package/localization/generated/et.ts +158 -0
- package/localization/generated/fi.ts +157 -0
- package/localization/generated/fr.ts +158 -0
- package/localization/generated/hu.ts +157 -0
- package/localization/generated/id.ts +158 -0
- package/localization/generated/index.js +3 -0
- package/localization/generated/it.ts +158 -0
- package/localization/generated/ja.ts +157 -0
- package/localization/generated/ko.ts +157 -0
- package/localization/generated/locale-codes.js +83 -0
- package/localization/generated/lt.ts +157 -0
- package/localization/generated/lv.ts +157 -0
- package/localization/generated/nb.ts +157 -0
- package/localization/generated/nl.ts +157 -0
- package/localization/generated/pl.ts +158 -0
- package/localization/generated/pt-BR.ts +158 -0
- package/localization/generated/pt-PT.ts +158 -0
- package/localization/generated/ro.ts +158 -0
- package/localization/generated/ru.ts +158 -0
- package/localization/generated/sk.ts +158 -0
- package/localization/generated/sl.ts +158 -0
- package/localization/generated/sv.ts +157 -0
- package/localization/generated/tr.ts +157 -0
- package/localization/generated/uk.ts +157 -0
- package/localization/generated/zh-hans.ts +140 -0
- package/localization/generated/zh-hant.ts +140 -0
- package/localization/hu.xlf +531 -0
- package/localization/id.xlf +532 -0
- package/localization/it.xlf +532 -0
- package/localization/ja.xlf +531 -0
- package/localization/ko.xlf +531 -0
- package/localization/lt.xlf +531 -0
- package/localization/lv.xlf +531 -0
- package/localization/nb.xlf +531 -0
- package/localization/nl.xlf +531 -0
- package/localization/pkg/ar.xliff +7 -0
- package/localization/pkg/bg.xliff +7 -0
- package/localization/pkg/cs.xliff +7 -0
- package/localization/pkg/da.xliff +7 -0
- package/localization/pkg/de.xliff +7 -0
- package/localization/pkg/el.xliff +7 -0
- package/localization/pkg/es.xliff +7 -0
- package/localization/pkg/et.xliff +7 -0
- package/localization/pkg/fi.xliff +7 -0
- package/localization/pkg/fr.xliff +7 -0
- package/localization/pkg/hu.xliff +7 -0
- package/localization/pkg/id.xliff +7 -0
- package/localization/pkg/it.xliff +7 -0
- package/localization/pkg/ja.xliff +7 -0
- package/localization/pkg/ko.xliff +7 -0
- package/localization/pkg/lt.xliff +7 -0
- package/localization/pkg/lv.xliff +7 -0
- package/localization/pkg/nb.xliff +7 -0
- package/localization/pkg/nl.xliff +7 -0
- package/localization/pkg/pl.xliff +7 -0
- package/localization/pkg/pt-BR.xliff +7 -0
- package/localization/pkg/pt-PT.xliff +7 -0
- package/localization/pkg/ro.xliff +7 -0
- package/localization/pkg/ru.xliff +7 -0
- package/localization/pkg/sk.xliff +7 -0
- package/localization/pkg/sl.xliff +7 -0
- package/localization/pkg/sv.xliff +7 -0
- package/localization/pkg/tr.xliff +7 -0
- package/localization/pkg/uk.xliff +7 -0
- package/localization/pkg/zh-hans.xliff +7 -0
- package/localization/pkg/zh-hant.xliff +7 -0
- package/localization/pl.xlf +532 -0
- package/localization/pt-BR.xlf +532 -0
- package/localization/pt-PT.xlf +532 -0
- package/localization/ro.xlf +532 -0
- package/localization/ru.xlf +532 -0
- package/localization/sk.xlf +532 -0
- package/localization/sl.xlf +532 -0
- package/localization/snippets/ar.xliff +16 -0
- package/localization/snippets/bg.xliff +16 -0
- package/localization/snippets/cs.xliff +16 -0
- package/localization/snippets/da.xliff +16 -0
- package/localization/snippets/de.xliff +16 -0
- package/localization/snippets/el.xliff +16 -0
- package/localization/snippets/es.xliff +16 -0
- package/localization/snippets/et.xliff +16 -0
- package/localization/snippets/fi.xliff +16 -0
- package/localization/snippets/fr.xliff +16 -0
- package/localization/snippets/hu.xliff +16 -0
- package/localization/snippets/id.xliff +16 -0
- package/localization/snippets/it.xliff +16 -0
- package/localization/snippets/ja.xliff +16 -0
- package/localization/snippets/ko.xliff +16 -0
- package/localization/snippets/lt.xliff +16 -0
- package/localization/snippets/lv.xliff +16 -0
- package/localization/snippets/nb.xliff +16 -0
- package/localization/snippets/nl.xliff +16 -0
- package/localization/snippets/pl.xliff +16 -0
- package/localization/snippets/pt-BR.xliff +16 -0
- package/localization/snippets/pt-PT.xliff +16 -0
- package/localization/snippets/ro.xliff +16 -0
- package/localization/snippets/ru.xliff +16 -0
- package/localization/snippets/sk.xliff +16 -0
- package/localization/snippets/sl.xliff +16 -0
- package/localization/snippets/sv.xliff +16 -0
- package/localization/snippets/tr.xliff +16 -0
- package/localization/snippets/uk.xliff +16 -0
- package/localization/snippets/zh-hans.xliff +16 -0
- package/localization/snippets/zh-hant.xliff +16 -0
- package/localization/sv.xlf +531 -0
- package/localization/tr.xlf +531 -0
- package/localization/uk.xlf +531 -0
- package/localization/zh-hans.xlf +531 -0
- package/localization/zh-hant.xlf +531 -0
- package/package.json +104 -3
- package/snippets/MNIST.html +21 -0
- package/test.json +63 -0
- package/tsconfig.json +8 -0
- package/widgets/webwriter-feature-engineering.ts +421 -0
- package/widgets/webwriter-model-designing.ts +300 -0
- package/widgets/webwriter-model-evaluation.ts +215 -0
- package/widgets/webwriter-model-prediction.ts +307 -0
- package/widgets/webwriter-model-training.ts +684 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { SlButton, SlButtonGroup, SlIcon, SlIconButton, SlInput, SlOption, SlSelect } from "@shoelace-style/shoelace";
|
|
2
|
+
import { LitElementWw } from "@webwriter/lit";
|
|
3
|
+
import { css, html, TemplateResult } from "lit";
|
|
4
|
+
import { msg } from "@lit/localize";
|
|
5
|
+
import { customElement, property, query } from "lit/decorators.js";
|
|
6
|
+
import { AiWidgetsReferenceSelect } from "../components/reference-element-select";
|
|
7
|
+
import { AiWidgetsFeatures } from "./webwriter-feature-engineering";
|
|
8
|
+
//import {setWasmPaths, setThreadsCount} from '@tensorflow/tfjs-backend-wasm';
|
|
9
|
+
import { until } from 'lit-html/directives/until.js';
|
|
10
|
+
import { ClassInput } from '../components/inference-fields/class-input';
|
|
11
|
+
import { ClassTable } from '../components/inference-fields/class-output';
|
|
12
|
+
import { TensorImage } from '../components/inference-fields/image';
|
|
13
|
+
import { DrawingCanvas } from '../components/inference-fields/image-input';
|
|
14
|
+
import { InputElement, OutputElement } from '../components/inference-fields/inference-interfaces';
|
|
15
|
+
import { NumberInput } from '../components/inference-fields/number-input';
|
|
16
|
+
import { NumberOutput } from '../components/inference-fields/number-output';
|
|
17
|
+
import { AiWidgetsTraining } from './webwriter-model-training';
|
|
18
|
+
import LOCALIZE from "../localization/generated";
|
|
19
|
+
//setWasmPaths('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@4.22.0/dist/');
|
|
20
|
+
//setThreadsCount(4);
|
|
21
|
+
|
|
22
|
+
import type * as TF from "@tensorflow/tfjs";
|
|
23
|
+
import getTF from "../lib/tflib";
|
|
24
|
+
let tf: typeof TF;
|
|
25
|
+
(async () => {tf = await getTF()})()
|
|
26
|
+
|
|
27
|
+
async function waitFor(condition: Function, interval = 10) {
|
|
28
|
+
if (condition())
|
|
29
|
+
return;
|
|
30
|
+
return new Promise(async function(res, rej) {
|
|
31
|
+
let inter = setInterval(function() {
|
|
32
|
+
if (condition()) {
|
|
33
|
+
clearInterval(inter)
|
|
34
|
+
res(null);
|
|
35
|
+
}
|
|
36
|
+
}, interval)
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A widget to run inference using a trained model and features created in the feature engineering widget.
|
|
42
|
+
*/
|
|
43
|
+
@customElement("webwriter-model-prediction")
|
|
44
|
+
export class AiWidgetsInference extends LitElementWw {
|
|
45
|
+
/** @internal */
|
|
46
|
+
static scopedElements = {
|
|
47
|
+
"sl-button": SlButton,
|
|
48
|
+
"sl-button-group": SlButtonGroup,
|
|
49
|
+
"sl-option": SlOption,
|
|
50
|
+
"sl-select": SlSelect,
|
|
51
|
+
"sl-input":SlInput,
|
|
52
|
+
"sl-icon": SlIcon,
|
|
53
|
+
"sl-icon-button": SlIconButton,
|
|
54
|
+
"ref-select": AiWidgetsReferenceSelect,
|
|
55
|
+
"drawing-canvas": DrawingCanvas,
|
|
56
|
+
"class-table": ClassTable,
|
|
57
|
+
"tensor-image": TensorImage,
|
|
58
|
+
"class-input": ClassInput,
|
|
59
|
+
"number-output": NumberOutput,
|
|
60
|
+
"number-input": NumberInput
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected localize = LOCALIZE;
|
|
64
|
+
|
|
65
|
+
static styles = css`
|
|
66
|
+
:host div[part='options'] {
|
|
67
|
+
display:none;
|
|
68
|
+
}
|
|
69
|
+
:host([contenteditable]) div[part='options'] {
|
|
70
|
+
display:block;
|
|
71
|
+
}
|
|
72
|
+
#container {
|
|
73
|
+
display: flex;
|
|
74
|
+
flex-direction: row;
|
|
75
|
+
flex-wrap: wrap;
|
|
76
|
+
align-content: flex-start;
|
|
77
|
+
justify-content: center;
|
|
78
|
+
align-items: flex-start;
|
|
79
|
+
gap: 5px;
|
|
80
|
+
}
|
|
81
|
+
#container * {
|
|
82
|
+
flex-grow: 1;
|
|
83
|
+
}
|
|
84
|
+
#top {
|
|
85
|
+
width: 100%;
|
|
86
|
+
display: flex;
|
|
87
|
+
align-items: flex-end;
|
|
88
|
+
}
|
|
89
|
+
.input, .output {
|
|
90
|
+
padding: 5px 10px;
|
|
91
|
+
position: relative;
|
|
92
|
+
border: 1px solid lightgrey;
|
|
93
|
+
border-radius: 5px;
|
|
94
|
+
}
|
|
95
|
+
.input > *, .output > * {
|
|
96
|
+
width: 100%;
|
|
97
|
+
}
|
|
98
|
+
.input > p, .output > p {
|
|
99
|
+
margin: 0px;
|
|
100
|
+
font-size: 16px;
|
|
101
|
+
}
|
|
102
|
+
#inputs, #outputs {
|
|
103
|
+
max-width: 50%;
|
|
104
|
+
}
|
|
105
|
+
p {
|
|
106
|
+
margin: 5px 0px;
|
|
107
|
+
}
|
|
108
|
+
`
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The id of the feature engineering widget to use as input.
|
|
112
|
+
*/
|
|
113
|
+
@property({ type: String, attribute: true, reflect: true })
|
|
114
|
+
accessor featureWidgetId: string;
|
|
115
|
+
|
|
116
|
+
@query("#feature-select")
|
|
117
|
+
private accessor featureSelect: AiWidgetsReferenceSelect;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The id of the model training widget to use as model source.
|
|
121
|
+
*/
|
|
122
|
+
@property({ type: String, attribute: true, reflect: true })
|
|
123
|
+
accessor modelWidgetId: string;
|
|
124
|
+
|
|
125
|
+
@query("#model-select")
|
|
126
|
+
private accessor modelSelect: AiWidgetsReferenceSelect;
|
|
127
|
+
|
|
128
|
+
@query("#epoch-select")
|
|
129
|
+
private accessor modelName: SlSelect;
|
|
130
|
+
|
|
131
|
+
@property({ type: String, attribute: true, reflect: true })
|
|
132
|
+
private accessor selectedModelName: string;
|
|
133
|
+
|
|
134
|
+
private model:TF.LayersModel;
|
|
135
|
+
private predictionInterval: number;
|
|
136
|
+
|
|
137
|
+
constructor() {
|
|
138
|
+
super();
|
|
139
|
+
let that = this;
|
|
140
|
+
this.predictionInterval = setInterval(function() {
|
|
141
|
+
that.predict();
|
|
142
|
+
}, 200) as unknown as number;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
disconnectedCallback() {
|
|
146
|
+
super.disconnectedCallback();
|
|
147
|
+
if (this.predictionInterval) {
|
|
148
|
+
clearInterval(this.predictionInterval);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Renders the model select field allowing the user to change the model checkpoint that is used for inference.
|
|
154
|
+
*/
|
|
155
|
+
private async getModelSelect() {
|
|
156
|
+
let that = this;
|
|
157
|
+
await waitFor(function() {return that.modelSelect != null && that.modelSelect.getRefElement != null && (that.modelSelect.getRefElement() as AiWidgetsTraining).getAvailableModels != null});
|
|
158
|
+
let models = (this.modelSelect.getRefElement() as AiWidgetsTraining).getAvailableModels();
|
|
159
|
+
if (this.model == null)
|
|
160
|
+
setTimeout(function() {
|
|
161
|
+
that.selectedModelChange();
|
|
162
|
+
})
|
|
163
|
+
return html`<sl-select label=${msg("Model version")} id="epoch-select" value=${this.selectedModelName} @sl-change=${this.selectedModelChange}>${models.map(model => html`<sl-option value=${model}>${model}</sl-option>`)}</sl-select>`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Renders the left input fields which the user can edit and which values are fed into the model periodically. */
|
|
167
|
+
private getFeatureInputs() {
|
|
168
|
+
if (this.model == null)
|
|
169
|
+
return html`<p>${msg("No Model avaiable.")}</p>`;
|
|
170
|
+
let that = this;
|
|
171
|
+
let {rawDataset, tensorDataset} = (this.featureSelect.getRefElement() as AiWidgetsFeatures).getDataset();
|
|
172
|
+
let elements = [html`<div class="input"><p>🡺 ${msg("Inputs")}</p></div>`];
|
|
173
|
+
for (let input of this.model.inputNames) {
|
|
174
|
+
let featureName = input.replace("in_","");
|
|
175
|
+
let transform = tensorDataset.featureMaps[featureName];
|
|
176
|
+
elements.push(html`<div class="input"><p>${featureName}</p>${transform.getInputElement(input)}</div>`);
|
|
177
|
+
}
|
|
178
|
+
return html`<div id="inputs">${elements}</div>`
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Renders the right output fields which are generated by the model output periodically. */
|
|
182
|
+
private getFeatureOutputs() {
|
|
183
|
+
if (this.model == null)
|
|
184
|
+
return html``;
|
|
185
|
+
let that = this;
|
|
186
|
+
let {rawDataset, tensorDataset} = (this.featureSelect.getRefElement() as AiWidgetsFeatures).getDataset();
|
|
187
|
+
let elements = [html`<div class="output"><p>${msg("Outputs")} 🡺</p></div>`];
|
|
188
|
+
for (let input of this.model.outputNames) {
|
|
189
|
+
let featureName = input.replace("out_","");
|
|
190
|
+
let transform = tensorDataset.featureMaps[featureName];
|
|
191
|
+
elements.push(html`<div class="output"><p>${featureName}</p>${transform.getOutputElement(input)}</div>`);
|
|
192
|
+
}
|
|
193
|
+
return html`<div id="outputs">${elements}</div>`
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public render(): TemplateResult {
|
|
197
|
+
if (this.selectedModelName == null || this.selectedModelName == "")
|
|
198
|
+
this.selectedModelName = "best";
|
|
199
|
+
|
|
200
|
+
let that = this;
|
|
201
|
+
setTimeout(function() {
|
|
202
|
+
that.fillWithRandomSample()
|
|
203
|
+
}, 1000);
|
|
204
|
+
return html`<div id="container">
|
|
205
|
+
<div id="top">
|
|
206
|
+
${until(this.getModelSelect(), html`${msg("Loading models...")}`)}
|
|
207
|
+
<sl-button @click=${this.fillWithRandomSample}>${msg("Show Random Test Sample")}</sl-button>
|
|
208
|
+
</div>
|
|
209
|
+
${this.getFeatureInputs()}
|
|
210
|
+
${this.getFeatureOutputs()}
|
|
211
|
+
</div>
|
|
212
|
+
<div part="options">
|
|
213
|
+
<p>${msg("Feature Source")}</p>
|
|
214
|
+
<ref-select id="feature-select" typeName="Features" elemId=${this.featureWidgetId} elementType="webwriter-feature-engineering" @refchange=${this.featureRefChange} ownerId=${this.id}></ref-select>
|
|
215
|
+
<p>${msg("Model Source")}</p>
|
|
216
|
+
<ref-select id="model-select" typeName="Model" elemId=${this.modelWidgetId} elementType="webwriter-model-training" @refchange=${this.modelRefChange} ownerId=${this.id}></ref-select>
|
|
217
|
+
</div>`
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private modelRefChange(e:CustomEvent) {
|
|
221
|
+
this.modelWidgetId = e.detail.id;
|
|
222
|
+
this.model = null;
|
|
223
|
+
|
|
224
|
+
let that = this;
|
|
225
|
+
function modelUpdate() {
|
|
226
|
+
that.model = null;
|
|
227
|
+
that.requestUpdate();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (e.detail.lastRef != null)
|
|
231
|
+
e.detail.lastRef.removeEventListener("finished", modelUpdate);
|
|
232
|
+
this.modelSelect.getRefElement().addEventListener("finished", modelUpdate);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private featureRefChange(e:CustomEvent) {
|
|
236
|
+
this.featureWidgetId = e.detail.id;
|
|
237
|
+
|
|
238
|
+
let that = this;
|
|
239
|
+
function featuresUpdate() {
|
|
240
|
+
that.requestUpdate();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (e.detail.lastRef != null)
|
|
244
|
+
e.detail.lastRef.removeEventListener("change", featuresUpdate);
|
|
245
|
+
this.modelSelect.getRefElement().addEventListener("change", featuresUpdate);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private async selectedModelChange() {
|
|
249
|
+
let name = this.modelName.value as string;
|
|
250
|
+
this.selectedModelName = name;
|
|
251
|
+
try {
|
|
252
|
+
this.model.dispose();
|
|
253
|
+
} catch(e) {}
|
|
254
|
+
this.model = await (this.modelSelect.getRefElement() as AiWidgetsTraining).getModel(name);
|
|
255
|
+
this.requestUpdate()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
private fillWithRandomSample() {
|
|
260
|
+
if (!this.featureSelect || !this.featureSelect.getRefElement())
|
|
261
|
+
return;
|
|
262
|
+
let {rawDataset, tensorDataset} = (this.featureSelect.getRefElement() as AiWidgetsFeatures).getDataset()
|
|
263
|
+
this.fillWithSample(Math.floor(Math.random() * (rawDataset.validationEnd - rawDataset.length()) + rawDataset.validationEnd))
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Sets the state of the input fields to the value of a sample.
|
|
268
|
+
* @param id The id of the sample in the dataset.
|
|
269
|
+
*/
|
|
270
|
+
private async fillWithSample(id:number) {
|
|
271
|
+
if (this.model == null || !this.featureSelect || !this.featureSelect.getRefElement())
|
|
272
|
+
return;
|
|
273
|
+
let {rawDataset, tensorDataset} = (this.featureSelect.getRefElement() as AiWidgetsFeatures).getDataset()
|
|
274
|
+
let sample = await tensorDataset.getSample(id);
|
|
275
|
+
for (let input of this.model.inputNames) {
|
|
276
|
+
let inElem = this.shadowRoot.getElementById(input) as InputElement;
|
|
277
|
+
let featureName = input.replace("in_","");
|
|
278
|
+
inElem.setTensor(sample[featureName], tensorDataset.featureMaps[featureName]);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** Is periodically called and will invoke the input fields values on the model to update the output fields. */
|
|
283
|
+
private async predict() {
|
|
284
|
+
if (this.model == null || !this.featureSelect || !this.featureSelect.getRefElement())
|
|
285
|
+
return;
|
|
286
|
+
|
|
287
|
+
let that = this;
|
|
288
|
+
let {rawDataset, tensorDataset} = (this.featureSelect.getRefElement() as AiWidgetsFeatures).getDataset()
|
|
289
|
+
let inputs = [];
|
|
290
|
+
for (let input of this.model.inputNames) {
|
|
291
|
+
let inElem = this.shadowRoot.getElementById(input) as InputElement;
|
|
292
|
+
let tensor = await inElem.getTensor(tensorDataset.featureMaps[input.replace("in_","")]);
|
|
293
|
+
inputs.push(tensor.reshape([1, ...tensor.shape]))
|
|
294
|
+
tensor.dispose();
|
|
295
|
+
}
|
|
296
|
+
let out = that.model.predict(inputs, {batchSize:1}) as TF.Tensor;
|
|
297
|
+
for (let i = 0; i < this.model.outputNames.length; i++) {
|
|
298
|
+
let output = this.model.outputNames[i];
|
|
299
|
+
let outElem = this.shadowRoot.getElementById(output) as OutputElement;
|
|
300
|
+
await outElem.setTensor(out, tensorDataset.featureMaps[output.replace("out_","")]);
|
|
301
|
+
}
|
|
302
|
+
for (let input of inputs)
|
|
303
|
+
input.dispose();
|
|
304
|
+
out.dispose();
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|