-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathYamlFileUploader.js
More file actions
541 lines (501 loc) · 15.3 KB
/
YamlFileUploader.js
File metadata and controls
541 lines (501 loc) · 15.3 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// global FileReader
import React, {
Fragment,
useContext,
useEffect,
useMemo,
useState,
} from "react";
import {
Button,
Col,
Divider,
message,
Row,
Select,
Slider,
Space,
Upload,
} from "antd";
import { UploadOutlined } from "@ant-design/icons";
import yaml from "js-yaml";
import { AppContext } from "../contexts/GlobalContext";
import { YamlContext } from "../contexts/YamlContext";
import {
getConfigPresets,
getConfigPresetContent,
getModelArchitectures,
} from "../api";
import { findCommonPartOfString } from "../utils";
import {
applyInputPaths,
getArchitectureValue,
getSliderValue,
isArchitectureSupported,
isSliderSupported,
setArchitectureValue,
setSliderValue,
} from "../configSchema";
const YamlFileUploader = (props) => {
const context = useContext(AppContext);
const YAMLContext = useContext(YamlContext);
const { type } = props;
const workflow =
type === "training" ? context.trainingState : context.inferenceState;
const [yamlContent, setYamlContent] = useState("");
const [presetOptions, setPresetOptions] = useState([]);
const [presetYamlText, setPresetYamlText] = useState(null);
const [architectureOptions, setArchitectureOptions] = useState([]);
const [isLoadingPresets, setIsLoadingPresets] = useState(false);
const [isLoadingArchitectures, setIsLoadingArchitectures] = useState(false);
const sliderData = useMemo(() => {
if (type === "training") {
return [
{
key: "batch_size",
label: "Batch size",
min: 1,
max: 32,
marks: { 1: 1, 8: 8, 16: 16, 32: 32 },
value: YAMLContext.solverSamplesPerBatch,
step: 1,
},
{
key: "gpus",
label: "GPUs",
min: 0,
max: 8,
marks: { 0: 0, 4: 4, 8: 8 },
value: YAMLContext.numGPUs,
step: 1,
},
{
key: "cpus",
label: "CPUs",
min: 1,
max: 16,
marks: { 1: 1, 8: 8, 16: 16 },
value: YAMLContext.numCPUs,
step: 1,
},
];
}
return [
{
key: "batch_size",
label: "Batch size",
min: 1,
max: 32,
marks: { 1: 1, 8: 8, 16: 16, 32: 32 },
value: YAMLContext.inferenceSamplesPerBatch,
step: 1,
},
{
key: "augmentations",
label: "Augmentations",
min: 1,
max: 16,
marks: { 1: 1, 8: 8, 16: 16 },
value: YAMLContext.augNum,
step: 1,
},
];
}, [
type,
YAMLContext.numGPUs,
YAMLContext.numCPUs,
YAMLContext.solverSamplesPerBatch,
YAMLContext.augNum,
YAMLContext.inferenceSamplesPerBatch,
]);
const getCurrentConfig = () =>
type === "training" ? context.trainingConfig : context.inferenceConfig;
const setCurrentOriginPath = (nextOriginPath) => {
workflow.setConfigOriginPath(nextOriginPath || "");
};
const setCurrentConfig = (nextContent) => {
if (type === "training") {
context.setTrainingConfig(nextContent);
} else {
context.setInferenceConfig(nextContent);
}
setYamlContent(nextContent);
};
const getPathValue = (val) => {
if (!val) return "";
if (typeof val === "string") return val;
return val.path || val.folderPath || "";
};
const getFileName = (path) => {
if (!path) return "";
const parts = path.split(/[/\\]/);
return parts[parts.length - 1];
};
const updateInputSelectorInformation = (yamlData) => {
const inputImagePath = getPathValue(workflow.inputImage);
const inputLabelPath = getPathValue(workflow.inputLabel);
const inputPath = findCommonPartOfString(inputImagePath, inputLabelPath);
const outputPath = getPathValue(workflow.outputPath);
applyInputPaths(yamlData, {
mode: type,
inputImagePath,
inputLabelPath,
inputPath,
outputPath,
});
};
const syncYamlContext = (yamlData) => {
if (!yamlData) return;
const gpus = getSliderValue(yamlData, "training", "gpus");
if (typeof gpus === "number") {
YAMLContext.setNumGPUs(gpus);
}
const cpus = getSliderValue(yamlData, "training", "cpus");
if (typeof cpus === "number") {
YAMLContext.setNumCPUs(cpus);
}
const trainBatch = getSliderValue(yamlData, "training", "batch_size");
if (typeof trainBatch === "number") {
YAMLContext.setSolverSamplesPerBatch(trainBatch);
}
const inferenceBatch = getSliderValue(yamlData, "inference", "batch_size");
if (typeof inferenceBatch === "number") {
YAMLContext.setInferenceSamplesPerBatch(inferenceBatch);
}
const augNum = getSliderValue(yamlData, "inference", "augmentations");
if (typeof augNum === "number") {
YAMLContext.setAugNum(augNum);
}
const learningRate =
yamlData.SOLVER?.BASE_LR ?? yamlData.optimization?.optimizer?.lr;
if (typeof learningRate === "number") {
YAMLContext.setLearningRate(learningRate);
}
};
const applyYamlData = (yamlData, sourceLabel) => {
if (!yamlData) {
message.error("Failed to load YAML configuration.");
return;
}
updateInputSelectorInformation(yamlData);
const serialized = yaml
.dump(yamlData, { indent: 2 })
.replace(/^\s*\n/gm, "");
setCurrentConfig(serialized);
syncYamlContext(yamlData);
if (sourceLabel) {
message.success(`${sourceLabel} loaded.`);
}
};
const serializeYaml = (yamlData) => {
return yaml.dump(yamlData, { indent: 2 }).replace(/^\s*\n/gm, "");
};
const normalizeYamlText = (text) => {
if (!text) return "";
try {
const parsed = yaml.load(text);
return yaml.dump(parsed, { indent: 2 }).replace(/^\s*\n/gm, "");
} catch (error) {
return text;
}
};
const parseYaml = (yamlText, showError = true) => {
if (!yamlText) return null;
try {
return yaml.load(yamlText);
} catch (error) {
if (showError) {
message.error("Error parsing YAML content.");
}
return null;
}
};
const handleFileUpload = (file) => {
workflow.setUploadedYamlFile(file);
workflow.setSelectedYamlPreset("");
setPresetYamlText(null);
setCurrentOriginPath(getPathValue(file));
const reader = new FileReader();
reader.onload = (e) => {
const contents = e.target.result;
const yamlData = parseYaml(contents);
if (!yamlData) return;
applyYamlData(yamlData, "YAML file");
};
reader.readAsText(file);
};
const handlePresetSelect = async (value) => {
setIsLoadingPresets(true);
try {
const res = await getConfigPresetContent(value);
setPresetYamlText(res.content || null);
const yamlData = parseYaml(res.content);
if (!yamlData) return;
workflow.setSelectedYamlPreset(value);
workflow.setUploadedYamlFile("");
setCurrentOriginPath(value);
applyYamlData(yamlData, "Preset config");
} catch (error) {
message.error(error?.message || "Failed to load preset config.");
} finally {
setIsLoadingPresets(false);
}
};
const handleRevertPreset = () => {
if (!presetYamlText) return;
const yamlData = parseYaml(presetYamlText);
if (!yamlData) return;
applyYamlData(yamlData, "Preset restored");
};
const handleSliderChange = (sliderKey, newValue) => {
const currentConfig = getCurrentConfig();
if (!currentConfig) {
message.warning("Load a preset or upload a YAML file first.");
return;
}
const yamlData = parseYaml(currentConfig) || {};
const updated = setSliderValue(yamlData, type, sliderKey, newValue);
if (!updated) {
message.info("This setting is not available for the loaded config.");
return;
}
applyYamlData(yamlData);
};
const handleArchitectureChange = (value) => {
const currentConfig = getCurrentConfig();
if (!currentConfig) {
message.warning("Load a preset or upload a YAML file first.");
return;
}
const yamlData = parseYaml(currentConfig) || {};
const updated = setArchitectureValue(yamlData, value);
if (!updated) {
message.info("Architecture field is not supported by this config.");
return;
}
applyYamlData(yamlData, "Model architecture updated");
};
useEffect(() => {
const loadPresets = async () => {
setIsLoadingPresets(true);
try {
const res = await getConfigPresets();
const options = (res.configs || []).map((configPath) => ({
value: configPath,
label: configPath,
}));
setPresetOptions(options);
} catch (error) {
setPresetOptions([]);
} finally {
setIsLoadingPresets(false);
}
};
const loadArchitectures = async () => {
setIsLoadingArchitectures(true);
try {
const res = await getModelArchitectures();
const options = (res.architectures || []).map((arch) => ({
value: arch,
label: arch,
}));
setArchitectureOptions(options);
} catch (error) {
setArchitectureOptions([]);
} finally {
setIsLoadingArchitectures(false);
}
};
loadPresets();
loadArchitectures();
}, []);
useEffect(() => {
const currentConfig = getCurrentConfig();
if (currentConfig) {
setYamlContent(currentConfig);
const yamlData = parseYaml(currentConfig);
if (yamlData) {
syncYamlContext(yamlData);
}
}
}, [context.trainingConfig, context.inferenceConfig, type]);
useEffect(() => {
const currentConfig = getCurrentConfig();
if (!currentConfig) return;
const yamlData = parseYaml(currentConfig, false);
if (!yamlData) return;
updateInputSelectorInformation(yamlData);
const nextSerialized = serializeYaml(yamlData);
if (nextSerialized !== currentConfig) {
setCurrentConfig(nextSerialized);
}
}, [
workflow.inputImage,
workflow.inputLabel,
workflow.outputPath,
context.trainingConfig,
context.inferenceConfig,
type,
]);
const currentYamlData = useMemo(() => {
const currentConfig = getCurrentConfig();
if (!currentConfig) return null;
return parseYaml(currentConfig, false);
}, [context.trainingConfig, context.inferenceConfig, type]);
const currentArchitecture = useMemo(() => {
return getArchitectureValue(currentYamlData);
}, [currentYamlData]);
const architectureSupported = useMemo(() => {
return isArchitectureSupported(currentYamlData);
}, [currentYamlData]);
return (
<div style={{ margin: "10px" }}>
<Space wrap size={12} style={{ marginBottom: 12 }}>
<Upload beforeUpload={handleFileUpload} showUploadList={false}>
<Button icon={<UploadOutlined />} size="small">
Upload YAML File
</Button>
</Upload>
<Select
placeholder="Choose a preset config"
style={{ minWidth: 280 }}
loading={isLoadingPresets}
options={presetOptions}
onChange={handlePresetSelect}
value={workflow.selectedYamlPreset || undefined}
allowClear
onClear={() => workflow.setSelectedYamlPreset("")}
/>
</Space>
{(workflow.uploadedYamlFile || workflow.selectedYamlPreset) && (
<div
style={{
marginBottom: 12,
display: "flex",
alignItems: "center",
gap: 8,
}}
>
<strong>Loaded:</strong>{" "}
{workflow.uploadedYamlFile?.name || workflow.selectedYamlPreset}
{workflow.selectedYamlPreset && presetYamlText && (
<>
<span style={{ color: "#fa8c16", fontSize: 12 }}>
{normalizeYamlText(getCurrentConfig()) !==
normalizeYamlText(presetYamlText)
? "Modified"
: "Preset"}
</span>
{normalizeYamlText(getCurrentConfig()) !==
normalizeYamlText(presetYamlText) && (
<Button size="small" type="link" onClick={handleRevertPreset}>
Revert to preset
</Button>
)}
</>
)}
</div>
)}
<Divider style={{ margin: "12px 0" }} />
<div
style={{
marginBottom: 12,
padding: "8px 12px",
background: "#fafafa",
border: "1px solid #f0f0f0",
borderRadius: 8,
fontSize: 12,
}}
>
<strong>Effective dataset paths</strong>
<div style={{ marginTop: 4 }}>
<div>
{/* Common folder mirrors DATASET.INPUT_PATH = shared parent dir */}
Common folder:{" "}
{getPathValue(workflow.inputImage) &&
getPathValue(workflow.inputLabel)
? findCommonPartOfString(
getPathValue(workflow.inputImage),
getPathValue(workflow.inputLabel),
)
: "—"}
</div>
<div>
Image name: {getFileName(getPathValue(workflow.inputImage)) || "—"}
</div>
<div>
Label name: {getFileName(getPathValue(workflow.inputLabel)) || "—"}
</div>
<div>Output path: {getPathValue(workflow.outputPath) || "—"}</div>
</div>
</div>
<Row gutter={[16, 16]}>
<Col span={12}>
<div>
<h4>Model architecture</h4>
<Space style={{ width: "100%" }} align="start">
<Select
placeholder="Select architecture"
loading={isLoadingArchitectures}
options={architectureOptions}
style={{ width: "100%" }}
value={currentArchitecture}
onChange={handleArchitectureChange}
disabled={!yamlContent || !architectureSupported}
/>
</Space>
</div>
</Col>
</Row>
<Divider style={{ margin: "12px 0" }} />
{yamlContent ? (
<Row>
{sliderData.map((param, index) => {
const sliderValue = getSliderValue(
currentYamlData,
type,
param.key,
);
const sliderSupported = isSliderSupported(
currentYamlData,
type,
param.key,
);
return (
<Fragment key={index}>
<Col span={8} offset={2}>
<div>
<Space align="center">
<h4 style={{ marginBottom: 0 }}>{param.label}</h4>
</Space>
<Slider
min={param.min}
max={param.max}
marks={param.marks}
value={
typeof sliderValue === "number"
? sliderValue
: param.value
}
disabled={!sliderSupported}
onChange={(newValue) =>
handleSliderChange(param.key, newValue)
}
step={param.step}
/>
</div>
</Col>
</Fragment>
);
})}
</Row>
) : (
<div style={{ color: "#8c8c8c" }}>
Load a preset or upload a YAML file to unlock the configuration
controls.
</div>
)}
</div>
);
};
export default YamlFileUploader;