@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.
Files changed (186) hide show
  1. package/.github/workflows/main.yml +8 -0
  2. package/LICENSE +7 -0
  3. package/README.md +290 -0
  4. package/components/dataset-input/dataset-input.styles.ts +20 -0
  5. package/components/dataset-input/dataset-input.ts +196 -0
  6. package/components/inference-fields/class-input.ts +83 -0
  7. package/components/inference-fields/class-output.ts +62 -0
  8. package/components/inference-fields/image-input.ts +182 -0
  9. package/components/inference-fields/image.ts +82 -0
  10. package/components/inference-fields/inference-interfaces.ts +15 -0
  11. package/components/inference-fields/number-input.ts +62 -0
  12. package/components/inference-fields/number-output.ts +56 -0
  13. package/components/model-editor.ts +273 -0
  14. package/components/model-tests/bad-samples.ts +152 -0
  15. package/components/model-tests/loss-distribution.ts +174 -0
  16. package/components/reference-element-select.ts +105 -0
  17. package/custom-elements.json +1446 -0
  18. package/custom.d.ts +180 -0
  19. package/dist/widgets/webwriter-feature-engineering.css +444 -0
  20. package/dist/widgets/webwriter-feature-engineering.js +112241 -0
  21. package/dist/widgets/webwriter-model-designing.css +444 -0
  22. package/dist/widgets/webwriter-model-designing.js +113967 -0
  23. package/dist/widgets/webwriter-model-evaluation.css +444 -0
  24. package/dist/widgets/webwriter-model-evaluation.js +119019 -0
  25. package/dist/widgets/webwriter-model-prediction.css +444 -0
  26. package/dist/widgets/webwriter-model-prediction.js +109260 -0
  27. package/dist/widgets/webwriter-model-training.css +444 -0
  28. package/dist/widgets/webwriter-model-training.js +121391 -0
  29. package/editing-config.json +73 -0
  30. package/icon.svg +1 -0
  31. package/lib/dataset/class.ts +155 -0
  32. package/lib/dataset/dataset.ts +293 -0
  33. package/lib/dataset/feature.ts +110 -0
  34. package/lib/dataset/image.ts +312 -0
  35. package/lib/dataset/linear.ts +120 -0
  36. package/lib/layers/activation.ts +48 -0
  37. package/lib/layers/add.ts +50 -0
  38. package/lib/layers/attention.ts +74 -0
  39. package/lib/layers/concat.ts +50 -0
  40. package/lib/layers/conv.ts +83 -0
  41. package/lib/layers/dropout.ts +44 -0
  42. package/lib/layers/fcl.ts +66 -0
  43. package/lib/layers/flatten.ts +48 -0
  44. package/lib/layers/index.ts +708 -0
  45. package/lib/layers/input.ts +55 -0
  46. package/lib/layers/noise.ts +43 -0
  47. package/lib/layers/output.ts +64 -0
  48. package/lib/layers/reshape.ts +76 -0
  49. package/lib/layers/transposeconv.ts +90 -0
  50. package/lib/tflib.ts +6 -0
  51. package/lit-localize.json +47 -0
  52. package/localization/ar.xlf +532 -0
  53. package/localization/bg.xlf +531 -0
  54. package/localization/cs.xlf +532 -0
  55. package/localization/da.xlf +531 -0
  56. package/localization/de.xlf +531 -0
  57. package/localization/el.xlf +533 -0
  58. package/localization/es.xlf +532 -0
  59. package/localization/et.xlf +532 -0
  60. package/localization/fi.xlf +531 -0
  61. package/localization/fr.xlf +532 -0
  62. package/localization/generated/ar.ts +158 -0
  63. package/localization/generated/bg.ts +157 -0
  64. package/localization/generated/cs.ts +158 -0
  65. package/localization/generated/da.ts +157 -0
  66. package/localization/generated/de.ts +157 -0
  67. package/localization/generated/el.ts +159 -0
  68. package/localization/generated/es.ts +158 -0
  69. package/localization/generated/et.ts +158 -0
  70. package/localization/generated/fi.ts +157 -0
  71. package/localization/generated/fr.ts +158 -0
  72. package/localization/generated/hu.ts +157 -0
  73. package/localization/generated/id.ts +158 -0
  74. package/localization/generated/index.js +3 -0
  75. package/localization/generated/it.ts +158 -0
  76. package/localization/generated/ja.ts +157 -0
  77. package/localization/generated/ko.ts +157 -0
  78. package/localization/generated/locale-codes.js +83 -0
  79. package/localization/generated/lt.ts +157 -0
  80. package/localization/generated/lv.ts +157 -0
  81. package/localization/generated/nb.ts +157 -0
  82. package/localization/generated/nl.ts +157 -0
  83. package/localization/generated/pl.ts +158 -0
  84. package/localization/generated/pt-BR.ts +158 -0
  85. package/localization/generated/pt-PT.ts +158 -0
  86. package/localization/generated/ro.ts +158 -0
  87. package/localization/generated/ru.ts +158 -0
  88. package/localization/generated/sk.ts +158 -0
  89. package/localization/generated/sl.ts +158 -0
  90. package/localization/generated/sv.ts +157 -0
  91. package/localization/generated/tr.ts +157 -0
  92. package/localization/generated/uk.ts +157 -0
  93. package/localization/generated/zh-hans.ts +140 -0
  94. package/localization/generated/zh-hant.ts +140 -0
  95. package/localization/hu.xlf +531 -0
  96. package/localization/id.xlf +532 -0
  97. package/localization/it.xlf +532 -0
  98. package/localization/ja.xlf +531 -0
  99. package/localization/ko.xlf +531 -0
  100. package/localization/lt.xlf +531 -0
  101. package/localization/lv.xlf +531 -0
  102. package/localization/nb.xlf +531 -0
  103. package/localization/nl.xlf +531 -0
  104. package/localization/pkg/ar.xliff +7 -0
  105. package/localization/pkg/bg.xliff +7 -0
  106. package/localization/pkg/cs.xliff +7 -0
  107. package/localization/pkg/da.xliff +7 -0
  108. package/localization/pkg/de.xliff +7 -0
  109. package/localization/pkg/el.xliff +7 -0
  110. package/localization/pkg/es.xliff +7 -0
  111. package/localization/pkg/et.xliff +7 -0
  112. package/localization/pkg/fi.xliff +7 -0
  113. package/localization/pkg/fr.xliff +7 -0
  114. package/localization/pkg/hu.xliff +7 -0
  115. package/localization/pkg/id.xliff +7 -0
  116. package/localization/pkg/it.xliff +7 -0
  117. package/localization/pkg/ja.xliff +7 -0
  118. package/localization/pkg/ko.xliff +7 -0
  119. package/localization/pkg/lt.xliff +7 -0
  120. package/localization/pkg/lv.xliff +7 -0
  121. package/localization/pkg/nb.xliff +7 -0
  122. package/localization/pkg/nl.xliff +7 -0
  123. package/localization/pkg/pl.xliff +7 -0
  124. package/localization/pkg/pt-BR.xliff +7 -0
  125. package/localization/pkg/pt-PT.xliff +7 -0
  126. package/localization/pkg/ro.xliff +7 -0
  127. package/localization/pkg/ru.xliff +7 -0
  128. package/localization/pkg/sk.xliff +7 -0
  129. package/localization/pkg/sl.xliff +7 -0
  130. package/localization/pkg/sv.xliff +7 -0
  131. package/localization/pkg/tr.xliff +7 -0
  132. package/localization/pkg/uk.xliff +7 -0
  133. package/localization/pkg/zh-hans.xliff +7 -0
  134. package/localization/pkg/zh-hant.xliff +7 -0
  135. package/localization/pl.xlf +532 -0
  136. package/localization/pt-BR.xlf +532 -0
  137. package/localization/pt-PT.xlf +532 -0
  138. package/localization/ro.xlf +532 -0
  139. package/localization/ru.xlf +532 -0
  140. package/localization/sk.xlf +532 -0
  141. package/localization/sl.xlf +532 -0
  142. package/localization/snippets/ar.xliff +16 -0
  143. package/localization/snippets/bg.xliff +16 -0
  144. package/localization/snippets/cs.xliff +16 -0
  145. package/localization/snippets/da.xliff +16 -0
  146. package/localization/snippets/de.xliff +16 -0
  147. package/localization/snippets/el.xliff +16 -0
  148. package/localization/snippets/es.xliff +16 -0
  149. package/localization/snippets/et.xliff +16 -0
  150. package/localization/snippets/fi.xliff +16 -0
  151. package/localization/snippets/fr.xliff +16 -0
  152. package/localization/snippets/hu.xliff +16 -0
  153. package/localization/snippets/id.xliff +16 -0
  154. package/localization/snippets/it.xliff +16 -0
  155. package/localization/snippets/ja.xliff +16 -0
  156. package/localization/snippets/ko.xliff +16 -0
  157. package/localization/snippets/lt.xliff +16 -0
  158. package/localization/snippets/lv.xliff +16 -0
  159. package/localization/snippets/nb.xliff +16 -0
  160. package/localization/snippets/nl.xliff +16 -0
  161. package/localization/snippets/pl.xliff +16 -0
  162. package/localization/snippets/pt-BR.xliff +16 -0
  163. package/localization/snippets/pt-PT.xliff +16 -0
  164. package/localization/snippets/ro.xliff +16 -0
  165. package/localization/snippets/ru.xliff +16 -0
  166. package/localization/snippets/sk.xliff +16 -0
  167. package/localization/snippets/sl.xliff +16 -0
  168. package/localization/snippets/sv.xliff +16 -0
  169. package/localization/snippets/tr.xliff +16 -0
  170. package/localization/snippets/uk.xliff +16 -0
  171. package/localization/snippets/zh-hans.xliff +16 -0
  172. package/localization/snippets/zh-hant.xliff +16 -0
  173. package/localization/sv.xlf +531 -0
  174. package/localization/tr.xlf +531 -0
  175. package/localization/uk.xlf +531 -0
  176. package/localization/zh-hans.xlf +531 -0
  177. package/localization/zh-hant.xlf +531 -0
  178. package/package.json +104 -3
  179. package/snippets/MNIST.html +21 -0
  180. package/test.json +63 -0
  181. package/tsconfig.json +8 -0
  182. package/widgets/webwriter-feature-engineering.ts +421 -0
  183. package/widgets/webwriter-model-designing.ts +300 -0
  184. package/widgets/webwriter-model-evaluation.ts +215 -0
  185. package/widgets/webwriter-model-prediction.ts +307 -0
  186. package/widgets/webwriter-model-training.ts +684 -0
@@ -0,0 +1,182 @@
1
+ import { SlButton } from "@shoelace-style/shoelace";
2
+ import { css, html } from 'lit';
3
+ import { property } from 'lit/decorators.js';
4
+ import { InputElement } from './inference-interfaces';
5
+
6
+ import type * as TF from "@tensorflow/tfjs";
7
+ import getTF from "../../lib/tflib";
8
+ import { msg } from "@lit/localize";
9
+ let tf: typeof TF;
10
+ (async () => {tf = await getTF()})()
11
+
12
+ export class DrawingCanvas extends InputElement {
13
+ static scopedElements = {
14
+ "sl-button": SlButton
15
+ }
16
+
17
+ @property({ type: Number }) width = 20;
18
+ @property({ type: Number }) height = 20;
19
+
20
+ private canvas?: HTMLCanvasElement;
21
+ private ctx?: CanvasRenderingContext2D;
22
+ private isDrawing = false;
23
+ private points: { x: number; y: number }[] = [];
24
+
25
+ static styles = css`
26
+ :host {
27
+ position: relative;
28
+ display: inline-block;
29
+ touch-action: none;
30
+ }
31
+ canvas {
32
+ background: white;
33
+ border: 1px solid #ccc;
34
+ touch-action: none;
35
+ image-rendering: pixelated; /* Makes low-res canvas look better */
36
+ }
37
+ sl-button {
38
+ position: absolute;
39
+ top: 0px;
40
+ left: 0px;
41
+ }
42
+ `;
43
+
44
+ updated() {
45
+ if (this.canvas) {
46
+ const w = this.width;
47
+ const h = this.height;
48
+
49
+ // Internal resolution
50
+ this.canvas.width = w;
51
+ this.canvas.height = h;
52
+
53
+ // Visual size (CSS) — scaled to at least 200px
54
+ const scale = Math.max(200 / w, 200 / h);
55
+ this.canvas.style.width = `${w * scale}px`;
56
+ this.canvas.style.height = `${h * scale}px`;
57
+
58
+ this.ctx = this.canvas.getContext('2d', { willReadFrequently: true })!;
59
+ this.ctx.lineCap = 'round';
60
+ this.ctx.lineJoin = 'round';
61
+ this.ctx.lineWidth = 2;
62
+ this.ctx.strokeStyle = 'black';
63
+ }
64
+ }
65
+
66
+ firstUpdated() {
67
+ this.canvas = this.shadowRoot!.querySelector('canvas')!;
68
+ this.addEventListeners();
69
+ this.updated(); // apply dimensions
70
+ }
71
+
72
+ private addEventListeners() {
73
+ const getCanvasCoords = (clientX: number, clientY: number) => {
74
+ const rect = this.canvas!.getBoundingClientRect();
75
+ const scaleX = this.canvas!.width / rect.width;
76
+ const scaleY = this.canvas!.height / rect.height;
77
+ return {
78
+ x: (clientX - rect.left) * scaleX,
79
+ y: (clientY - rect.top) * scaleY,
80
+ };
81
+ };
82
+
83
+ const start = (x: number, y: number) => {
84
+ this.isDrawing = true;
85
+ this.points = [{ x, y }];
86
+ };
87
+ const move = (x: number, y: number) => {
88
+ if (!this.isDrawing) return;
89
+ this.points.push({ x, y });
90
+ this.redraw();
91
+ };
92
+ const end = () => {
93
+ this.isDrawing = false;
94
+ this.points = [];
95
+ };
96
+
97
+ this.canvas!.addEventListener('mousedown', e => {
98
+ const { x, y } = getCanvasCoords(e.clientX, e.clientY);
99
+ start(x, y);
100
+ });
101
+ this.canvas!.addEventListener('mousemove', e => {
102
+ const { x, y } = getCanvasCoords(e.clientX, e.clientY);
103
+ move(x, y);
104
+ });
105
+ this.canvas!.addEventListener('mouseup', end);
106
+ this.canvas!.addEventListener('mouseleave', end);
107
+
108
+ this.canvas!.addEventListener('touchstart', e => {
109
+ e.preventDefault();
110
+ const touch = e.touches[0];
111
+ const { x, y } = getCanvasCoords(touch.clientX, touch.clientY);
112
+ start(x, y);
113
+ });
114
+ this.canvas!.addEventListener('touchmove', e => {
115
+ e.preventDefault();
116
+ const touch = e.touches[0];
117
+ const { x, y } = getCanvasCoords(touch.clientX, touch.clientY);
118
+ move(x, y);
119
+ });
120
+ this.canvas!.addEventListener('touchend', end);
121
+ }
122
+
123
+ private redraw() {
124
+ if (!this.ctx || this.points.length < 2) return;
125
+ const ctx = this.ctx;
126
+ const [p1, p2] = this.points.slice(-2);
127
+ ctx.beginPath();
128
+ ctx.moveTo(p1.x, p1.y);
129
+ ctx.lineTo(p2.x, p2.y);
130
+ ctx.stroke();
131
+ }
132
+
133
+ public clear() {
134
+ this.ctx!.fillStyle = "#ffffff";
135
+ this.ctx?.fillRect(0, 0, this.canvas!.width, this.canvas!.height);
136
+ }
137
+
138
+ public async getTensor(): Promise<TF.Tensor<TF.Rank>> {
139
+ const imgData = this.ctx!.getImageData(0, 0, this.canvas!.width, this.canvas!.height);
140
+ const grayData = new Float32Array(this.canvas!.width * this.canvas!.height);
141
+ for (let i = 0; i < grayData.length; i++) {
142
+ const r = imgData.data[i * 4];
143
+ const g = imgData.data[i * 4 + 1];
144
+ const b = imgData.data[i * 4 + 2];
145
+ const avg = (r + g + b) / 3;
146
+ grayData[i] = (255 - avg) / 255;
147
+ }
148
+ return tf.tensor(grayData, [this.canvas!.width, this.canvas!.height, 1]);
149
+ }
150
+
151
+ public async setTensor(tensor: TF.Tensor) {
152
+ const [h, w, c] = tensor.shape;
153
+
154
+ if (c !== 1) {
155
+ throw new Error('Tensor must have shape [height, width, 1]');
156
+ }
157
+
158
+ if (!this.canvas || !this.ctx) return;
159
+
160
+ // Update internal resolution and re-render canvas
161
+ this.width = w;
162
+ this.height = h;
163
+ await this.updateComplete; // Wait for Lit to apply changes
164
+
165
+ const imageData = this.ctx.createImageData(w, h);
166
+ const data = await tensor.data();
167
+
168
+ for (let i = 0; i < w * h; i++) {
169
+ const v = 255 - Math.floor(data[i] * 255); // invert normalization
170
+ imageData.data[i * 4 + 0] = v;
171
+ imageData.data[i * 4 + 1] = v;
172
+ imageData.data[i * 4 + 2] = v;
173
+ imageData.data[i * 4 + 3] = 255;
174
+ }
175
+
176
+ this.ctx.putImageData(imageData, 0, 0);
177
+ }
178
+
179
+ render() {
180
+ return html`<canvas></canvas><sl-button @click=${this.clear}>${msg("Clear")}</sl-button>`;
181
+ }
182
+ }
@@ -0,0 +1,82 @@
1
+ import { css, html } from 'lit';
2
+ import { query } from 'lit/decorators.js';
3
+ import ImageTransformation from '../../lib/dataset/image';
4
+ import { OutputElement } from './inference-interfaces';
5
+
6
+ import type * as TF from "@tensorflow/tfjs";
7
+ import getTF from "../../lib/tflib";
8
+ let tf: typeof TF;
9
+ (async () => {tf = await getTF()})()
10
+
11
+ export class TensorImage extends OutputElement {
12
+ static scopedElements = {}
13
+
14
+ static styles = css`
15
+ :host {
16
+ position: relative;
17
+ display: inline-block;
18
+ touch-action: none;
19
+ }
20
+ canvas {
21
+ background: white;
22
+ border: 1px solid #ccc;
23
+ touch-action: none;
24
+ image-rendering: pixelated; /* Makes low-res canvas look better */
25
+ min-width: 200px;
26
+ min-height: 200px;
27
+ }
28
+ sl-button {
29
+ position: absolute;
30
+ top: 0px;
31
+ left: 0px;
32
+ }
33
+ `;
34
+
35
+ @query("#img")
36
+ accessor image: HTMLCanvasElement;
37
+
38
+ public async setTensor(tensor: TF.Tensor3D, transform: ImageTransformation) {
39
+ tf.tidy(() => {
40
+ tensor = tensor.squeeze();
41
+ const [height, width, channels] = tensor.shape;
42
+
43
+ const data = tensor.dataSync();
44
+ const imageDataArray = new Uint8ClampedArray(width * height * 4);
45
+ for (let i = 0; i < width * height; i++) {
46
+ if (channels == 4) {
47
+ imageDataArray[i * 4 + 0] = (data[i * channels + 0] || 0) * 255; // R
48
+ imageDataArray[i * 4 + 1] = (data[i * channels + 1] || 0) * 255; // G
49
+ imageDataArray[i * 4 + 2] = (data[i * channels + 2] || 0) * 255 // B
50
+ imageDataArray[i * 4 + 3] = (data[i * channels + 3] || 0) * 255 // A
51
+ } else if (channels == 3) {
52
+ imageDataArray[i * 4 + 0] = (data[i * channels + 0] || 0) * 255; // R
53
+ imageDataArray[i * 4 + 1] = (data[i * channels + 1] || 0) * 255; // G
54
+ imageDataArray[i * 4 + 2] = (data[i * channels + 2] || 0) * 255 // B
55
+ imageDataArray[i * 4 + 3] = 255 // A
56
+ } else if (channels == 2) {
57
+ imageDataArray[i * 4 + 0] = (data[i * channels + 0] || 0) * 255; // R
58
+ imageDataArray[i * 4 + 1] = (data[i * channels + 1] || 0) * 255; // G
59
+ imageDataArray[i * 4 + 2] = 0 // B
60
+ imageDataArray[i * 4 + 3] = 255 // A
61
+ } else if (channels == 1 || channels == null) {
62
+ let gray = (data[i] || 0) * 255;
63
+ imageDataArray[i * 4 + 0] = gray; // R
64
+ imageDataArray[i * 4 + 1] = gray; // G
65
+ imageDataArray[i * 4 + 2] = gray; // B
66
+ imageDataArray[i * 4 + 3] = 255; // A
67
+ }
68
+ }
69
+
70
+ const canvas = this.image;
71
+ canvas.width = width;
72
+ canvas.height = height;
73
+ const ctx = canvas.getContext('2d');
74
+ const imageData = new ImageData(imageDataArray, width, height);
75
+ ctx.putImageData(imageData, 0, 0);
76
+ })
77
+ }
78
+
79
+ render() {
80
+ return html`<canvas id="img"></canvas>`;
81
+ }
82
+ }
@@ -0,0 +1,15 @@
1
+ import { LitElementWw } from '@webwriter/lit';
2
+ import { FeatureTransformation } from '../../lib/dataset/feature';
3
+
4
+ import type * as TF from "@tensorflow/tfjs";
5
+ import getTF from "../../lib/tflib";
6
+ let tf: typeof TF;
7
+ (async () => {tf = await getTF()})()
8
+
9
+ export abstract class OutputElement extends LitElementWw {
10
+ abstract setTensor(tensor:TF.Tensor, transform: FeatureTransformation): Promise<void>;
11
+ }
12
+
13
+ export abstract class InputElement extends OutputElement {
14
+ abstract getTensor(transform: FeatureTransformation) : Promise<TF.Tensor<TF.Rank>>;
15
+ }
@@ -0,0 +1,62 @@
1
+ import { SlRange } from "@shoelace-style/shoelace";
2
+ import { css, html, TemplateResult } from 'lit';
3
+ import LinearTransformation from '../../lib/dataset/linear';
4
+ import { OutputElement } from './inference-interfaces';
5
+
6
+ import type * as TF from "@tensorflow/tfjs";
7
+ import getTF from "../../lib/tflib";
8
+ let tf: typeof TF;
9
+ (async () => {tf = await getTF()})()
10
+
11
+ export class NumberInput extends OutputElement {
12
+ static scopedElements = {
13
+ "sl-range": SlRange
14
+ }
15
+
16
+ static styles = css`
17
+ :host {
18
+ position: relative;
19
+ display: inline-block;
20
+ touch-action: none;
21
+ width: 100%;
22
+ }
23
+ table {
24
+ width: 100%;
25
+ }
26
+ .class {
27
+ width: 30px;
28
+ }
29
+ canvas {
30
+ background: white;
31
+ border: 1px solid #ccc;
32
+ touch-action: none;
33
+ image-rendering: pixelated; /* Makes low-res canvas look better */
34
+ }
35
+ sl-range {
36
+ --track-color-active: var(--sl-color-primary-600);
37
+ --track-color-inactive: var(--sl-color-primary-100);
38
+ }
39
+ `;
40
+
41
+ private input: TemplateResult;
42
+
43
+ public async setTensor(tensor: TF.Tensor, transform: LinearTransformation) {
44
+ let that = this;
45
+ let probs = tf.tidy(() => {
46
+ return tensor.squeeze();
47
+ })
48
+ let value = parseFloat(await transform.inverse(probs as TF.Tensor1D));
49
+ let valueP = document.createElement("p");
50
+ valueP.innerText = value.toFixed(3);
51
+ that.input = html`<table><tr><td>${valueP}</td><td><sl-range step="0.01" id="input" min=${transform.aggrigatedInformation.min} max=${transform.aggrigatedInformation.max} value=${value} @sl-input=${(e: Event) => valueP.innerText = (e.target as SlRange).value.toFixed(3)}></sl-range></td></tr></table>`
52
+ this.requestUpdate();
53
+ }
54
+ public async getTensor(transform: LinearTransformation) {
55
+ let value = (this.shadowRoot.getElementById("input") as SlRange).value;
56
+ return transform.transform(String(value));
57
+ }
58
+
59
+ render() {
60
+ return html`${this.input}`;
61
+ }
62
+ }
@@ -0,0 +1,56 @@
1
+ import { SlRange } from "@shoelace-style/shoelace";
2
+ import { css, html, TemplateResult } from 'lit';
3
+ import LinearTransformation from '../../lib/dataset/linear';
4
+ import { OutputElement } from './inference-interfaces';
5
+
6
+ import type * as TF from "@tensorflow/tfjs";
7
+ import getTF from "../../lib/tflib";
8
+ let tf: typeof TF;
9
+ (async () => {tf = await getTF()})()
10
+
11
+ export class NumberOutput extends OutputElement {
12
+ static scopedElements = {
13
+ "sl-range": SlRange
14
+ }
15
+
16
+ static styles = css`
17
+ :host {
18
+ position: relative;
19
+ display: inline-block;
20
+ touch-action: none;
21
+ width: 100%;
22
+ }
23
+ table {
24
+ width: 100%;
25
+ }
26
+ .class {
27
+ width: 30px;
28
+ }
29
+ canvas {
30
+ background: white;
31
+ border: 1px solid #ccc;
32
+ touch-action: none;
33
+ image-rendering: pixelated; /* Makes low-res canvas look better */
34
+ }
35
+ sl-range {
36
+ --track-color-active: var(--sl-color-primary-600);
37
+ --track-color-inactive: var(--sl-color-primary-100);
38
+ }
39
+ `;
40
+
41
+ private input: TemplateResult;
42
+
43
+ public async setTensor(tensor: TF.Tensor, transform: LinearTransformation) {
44
+ let that = this;
45
+ let probs = tf.tidy(() => {
46
+ return tensor.squeeze();
47
+ })
48
+ let value = parseFloat(await transform.inverse(probs as TF.Tensor1D));
49
+ that.input = html`<table><tr><td>${value.toFixed(3)}</td><td><sl-range min=${transform.aggrigatedInformation.min} max=${transform.aggrigatedInformation.max} value=${value} disabled></sl-range></td></tr></table>`
50
+ this.requestUpdate();
51
+ }
52
+
53
+ render() {
54
+ return html`${this.input}`;
55
+ }
56
+ }
@@ -0,0 +1,273 @@
1
+ import { SlButton, SlButtonGroup, SlIcon, SlIconButton, SlOption, SlSelect } from "@shoelace-style/shoelace";
2
+ import { LitElementWw } from "@webwriter/lit";
3
+ import { css, html, TemplateResult } from "lit";
4
+ import { property } from "lit/decorators.js";
5
+ import { DatasetInput } from "../components/dataset-input/dataset-input";
6
+ import { Editor } from "../lib/layers";
7
+
8
+ export class ModelEditor extends LitElementWw {
9
+ static scopedElements = {
10
+ "sl-button": SlButton,
11
+ "sl-button-group": SlButtonGroup,
12
+ "dataset-input": DatasetInput,
13
+ "sl-option": SlOption,
14
+ "sl-select": SlSelect,
15
+ "sl-icon": SlIcon,
16
+ "sl-icon-button": SlIconButton
17
+ }
18
+
19
+ static styles = css`
20
+ :host {
21
+ min-height: 200px;
22
+ display:block;
23
+ font-size: 0.9rem;
24
+ }
25
+ #drawflow {
26
+ margin-top: 10px;
27
+ display:block;
28
+ height: 500px;
29
+ min-width: 300px;
30
+ border: 1px solid var(--sl-input-border-color);
31
+ }
32
+ #drawflow .drawflow::before {
33
+ content: "";
34
+ position: absolute;
35
+ top: -20000px;
36
+ left: -20000px;
37
+ right: -20000px;
38
+ bottom: -20000px;
39
+ background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV8/tCIVh1YQcchQneyiIrqVKhbBQmkrtOpgcukXNGlIUlwcBdeCgx+LVQcXZ10dXAVB8APEXXBSdJES/5cUWsR4cNyPd/ced+8Ab7PKFMMfAxTV1NOJuJDLrwqBV/RiCH7MISQyQ0tmFrNwHV/38PD1Lsqz3M/9OQbkgsEAj0AcY5puEm8Qz2yaGud94jArizLxOfGEThckfuS65PAb55LNXp4Z1rPpeeIwsVDqYqmLWVlXiKeJI7KiUr4357DMeYuzUq2z9j35C4MFdSXDdZqjSGAJSaQgQEIdFVRhIkqrSoqBNO3HXfwjtj9FLolcFTByLKAGBaLtB/+D390axalJJykYB3peLOtjDAjsAq2GZX0fW1brBPA9A1dqx19rArOfpDc6WuQIGNwGLq47mrQHXO4Aw0+aqIu25KPpLRaB9zP6pjwQugX615ze2vs4fQCy1NXyDXBwCIyXKHvd5d193b39e6bd3w/LsnLKuRkBrwAAAAZiS0dEAJQAlACU0L193gAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+kGFQQ7KvUYPNUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAHklEQVQ4y2NgGAWjYBQMbvAfiokCTKPhNQpGwXACAAPIAgEuqw0aAAAAAElFTkSuQmCC") repeat;
40
+ z-index: -1;
41
+ }
42
+
43
+ .drawflow,.drawflow .parent-node {
44
+ position:relative
45
+ }
46
+ .parent-drawflow {
47
+ display:flex;
48
+ overflow:hidden;
49
+ touch-action:none;
50
+ outline:0
51
+ }
52
+ .drawflow {
53
+ width:100%;
54
+ height:100%;
55
+ user-select:none;
56
+ perspective:0;
57
+ image-rendering: optimizeQuality;
58
+ }
59
+ .drawflow .drawflow-node {
60
+ display:flex;
61
+ align-items:flex-start;
62
+ position:absolute;
63
+ background:#fff;
64
+ min-width:100px;
65
+ max-width:200px;
66
+ min-height:20px;
67
+ border-radius:4px;
68
+ border:1px solid #000;
69
+ color:#000;
70
+ z-index:2;
71
+ padding:10px 15px;
72
+ }
73
+ .drawflow .drawflow-node.multi-input .output {
74
+ position:relative;
75
+ top:25px;
76
+ }
77
+ .drawflow .drawflow-node.multi-input p {
78
+ position:relative;
79
+ line-height: 80%;
80
+ padding:0px;
81
+ margin: 0px;
82
+ }
83
+ .drawflow .drawflow-node sl-select {
84
+ position: relative;
85
+ width: 150px;
86
+ z-index: 10;
87
+ }
88
+ .drawflow .drawflow-node sl-input {
89
+ position: relative;
90
+ width: 100px;
91
+ z-index: 10;
92
+ }
93
+ .drawflow .drawflow-node sl-select[open], .drawflow .drawflow-node sl-input[open] {
94
+ z-index: 100;
95
+ }
96
+ .drawflow .drawflow-node p {
97
+ position: relative;
98
+ margin: 0px;
99
+ }
100
+ .drawflow .drawflow-node.selected {
101
+ background:#ddd;
102
+ z-index: 1000;
103
+ }
104
+ .drawflow .drawflow-node:hover {
105
+ cursor:move
106
+ }
107
+ .drawflow .drawflow-node .inputs,.drawflow .drawflow-node .outputs {
108
+ width:0
109
+ }
110
+ .drawflow .drawflow-node .drawflow_content_node {
111
+ width:100%;
112
+ display: flex;
113
+ flex-direction: column;
114
+ flex-wrap: nowrap;
115
+ align-items: center;
116
+ justify-content: center;
117
+ }
118
+ .drawflow .drawflow-node .drawflow_content_node div {
119
+ width:100%;
120
+ display: flex;
121
+ flex-direction: column;
122
+ flex-wrap: nowrap;
123
+ align-items: center;
124
+ justify-content: center;
125
+ }
126
+ .drawflow .drawflow-node .input,.drawflow .drawflow-node .output {
127
+ position:relative;
128
+ width:20px;
129
+ height:fit-content;
130
+ background:#fff;
131
+ border-radius:0%;
132
+ border:1px solid #000;
133
+ cursor:crosshair;
134
+ z-index:1;
135
+ margin-bottom:5px;
136
+ display: flex;
137
+ flex-direction: row;
138
+ justify-content: center;
139
+ align-items: center;
140
+ writing-mode: sideways-rl;
141
+ text-orientation: mixed;
142
+ padding: 5px 2px;
143
+ }
144
+ .drawflow .drawflow-node .input {
145
+ left:-30px;
146
+ top:2px;
147
+ background:#999;
148
+ }
149
+ .drawflow .drawflow-node .output {
150
+ right:-2px;
151
+ top:2px;
152
+ background:#fff;
153
+ }
154
+ .drawflow .drawflow-node.invalid {
155
+ background:#ff7;
156
+ }
157
+ .drawflow .drawflow-node.invalid p span {
158
+ color:#f00;
159
+ }
160
+ .drawflow .drawflow-node .invalid {
161
+ background:#f77;
162
+ }
163
+ .drawflow .drawflow-node .valid {
164
+ background:#fff;
165
+ }
166
+ .drawflow svg {
167
+ z-index:0;
168
+ position:absolute;
169
+ overflow:visible!important
170
+ }
171
+ .drawflow .connection {
172
+ position:absolute;
173
+ pointer-events:none;
174
+ aspect-ratio:1/1
175
+ }
176
+ .drawflow .connection .main-path {
177
+ fill:none;
178
+ stroke-width:5px;
179
+ stroke:#4682b4;
180
+ pointer-events:all
181
+ }
182
+ .drawflow .connection .main-path:hover {
183
+ stroke:#1266ab;
184
+ cursor:pointer
185
+ }
186
+ .drawflow .connection .main-path.selected {
187
+ stroke:#43b993
188
+ }
189
+ .drawflow .connection .point {
190
+ cursor:move;
191
+ stroke:#000;
192
+ stroke-width:2;
193
+ fill:#fff;
194
+ pointer-events:all
195
+ }
196
+ .drawflow .connection .point.selected,.drawflow .connection .point:hover {
197
+ fill:#1266ab
198
+ }
199
+ .drawflow .main-path {
200
+ fill:none;
201
+ stroke-width:5px;
202
+ stroke:#4682b4
203
+ }
204
+ .drawflow-delete {
205
+ position:absolute;
206
+ display:block;
207
+ width:30px;
208
+ height:30px;
209
+ background:#000;
210
+ color:#fff;
211
+ z-index:4;
212
+ border:2px solid #fff;
213
+ line-height:30px;
214
+ font-weight:700;
215
+ text-align:center;
216
+ border-radius:50%;
217
+ font-family:monospace;
218
+ cursor:pointer
219
+ }
220
+ .drawflow>.drawflow-delete {
221
+ margin-left:-15px;
222
+ margin-top:15px
223
+ }
224
+ .parent-node .drawflow-delete {
225
+ right:-15px;
226
+ top:-15px
227
+ }`
228
+
229
+ public controler: Editor;
230
+
231
+ @property({ type: String, attribute:true }) accessor editmode = "edit";
232
+ @property({ type: Number, attribute:true }) accessor modelparams;
233
+ @property({ type: Number, attribute:true }) accessor layerparams;
234
+
235
+ private state;
236
+ public setState(state) {
237
+ this.state = state;
238
+ this.requestUpdate();
239
+ }
240
+
241
+ private fields;
242
+ public setFields(fields) {
243
+ this.fields = fields;
244
+ this.requestUpdate();
245
+ }
246
+
247
+ public createLayer(name) {
248
+ this.controler.createLayer(name, this.fields);
249
+ }
250
+
251
+ public render(): TemplateResult {
252
+ let canvas = document.createElement("div");
253
+ canvas.id = "drawflow";
254
+ this.controler = new Editor(canvas, this.editmode, this.modelparams, this.layerparams);
255
+ this.controler.import(this.state || [], this.fields);
256
+ let that = this;
257
+ canvas.addEventListener("mouseup", function () {
258
+ that.state = that.controler.export();
259
+ that.dispatchEvent(new CustomEvent('modelchange', {
260
+ detail: {
261
+ state:that.controler.export()
262
+ }
263
+ }));
264
+ });
265
+ canvas.addEventListener("keydown", function (e) {
266
+ if (e.key == "Delete") {
267
+ e.preventDefault();
268
+ return false;
269
+ }
270
+ })
271
+ return html`${canvas}`
272
+ }
273
+ }