-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathYamlFileUploader.js
More file actions
487 lines (449 loc) · 13.9 KB
/
YamlFileUploader.js
File metadata and controls
487 lines (449 loc) · 13.9 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
// 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";
const YamlFileUploader = (props) => {
const context = useContext(AppContext);
const YAMLContext = useContext(YamlContext);
const { type } = props;
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 [
{
label: "Batch size",
min: 1,
max: 32,
marks: { 1: 1, 8: 8, 16: 16, 32: 32 },
value: YAMLContext.solverSamplesPerBatch,
location: "SOLVER",
property: "SAMPLES_PER_BATCH",
step: 1,
},
{
label: "GPUs",
min: 0,
max: 8,
marks: { 0: 0, 4: 4, 8: 8 },
value: YAMLContext.numGPUs,
location: "SYSTEM",
property: "NUM_GPUS",
step: 1,
},
{
label: "CPUs",
min: 1,
max: 16,
marks: { 1: 1, 8: 8, 16: 16 },
value: YAMLContext.numCPUs,
location: "SYSTEM",
property: "NUM_CPUS",
step: 1,
},
];
}
return [
{
label: "Batch size",
min: 1,
max: 32,
marks: { 1: 1, 8: 8, 16: 16, 32: 32 },
value: YAMLContext.inferenceSamplesPerBatch,
location: "INFERENCE",
property: "SAMPLES_PER_BATCH",
step: 1,
},
{
label: "Augmentations",
min: 1,
max: 16,
marks: { 1: 1, 8: 8, 16: 16 },
value: YAMLContext.augNum,
location: "INFERENCE",
property: "AUG_NUM",
step: 1,
},
];
}, [
type,
YAMLContext.numGPUs,
YAMLContext.numCPUs,
YAMLContext.solverSamplesPerBatch,
YAMLContext.augNum,
YAMLContext.inferenceSamplesPerBatch,
]);
const getCurrentConfig = () =>
type === "training" ? context.trainingConfig : context.inferenceConfig;
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(context.inputImage);
const inputLabelPath = getPathValue(context.inputLabel);
if (!inputImagePath || !inputLabelPath) {
return;
}
// INPUT_PATH is the shared parent directory for image/label so names can be relative.
const inputPath = findCommonPartOfString(inputImagePath, inputLabelPath);
yamlData.DATASET = yamlData.DATASET || {};
yamlData.DATASET.INPUT_PATH = inputPath || yamlData.DATASET.INPUT_PATH;
yamlData.DATASET.IMAGE_NAME = inputImagePath.replace(inputPath, "");
yamlData.DATASET.LABEL_NAME = inputLabelPath.replace(inputPath, "");
const outputPath = getPathValue(context.outputPath);
if (outputPath) {
yamlData.DATASET.OUTPUT_PATH = outputPath;
}
};
const syncYamlContext = (yamlData) => {
if (!yamlData) return;
if (yamlData.SYSTEM) {
if (typeof yamlData.SYSTEM.NUM_GPUS === "number") {
YAMLContext.setNumGPUs(yamlData.SYSTEM.NUM_GPUS);
}
if (typeof yamlData.SYSTEM.NUM_CPUS === "number") {
YAMLContext.setNumCPUs(yamlData.SYSTEM.NUM_CPUS);
}
}
if (yamlData.SOLVER) {
if (typeof yamlData.SOLVER.BASE_LR === "number") {
YAMLContext.setLearningRate(yamlData.SOLVER.BASE_LR);
}
if (typeof yamlData.SOLVER.SAMPLES_PER_BATCH === "number") {
YAMLContext.setSolverSamplesPerBatch(yamlData.SOLVER.SAMPLES_PER_BATCH);
}
}
if (yamlData.INFERENCE) {
if (typeof yamlData.INFERENCE.SAMPLES_PER_BATCH === "number") {
YAMLContext.setInferenceSamplesPerBatch(
yamlData.INFERENCE.SAMPLES_PER_BATCH,
);
}
if (typeof yamlData.INFERENCE.AUG_NUM === "number") {
YAMLContext.setAugNum(yamlData.INFERENCE.AUG_NUM);
}
}
};
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 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) => {
context.setUploadedYamlFile(file);
context.setSelectedYamlPreset(null);
setPresetYamlText(null);
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;
context.setSelectedYamlPreset(value);
context.setUploadedYamlFile("");
applyYamlData(yamlData, "Preset config");
} catch (error) {
message.error(error?.message || "Failed to load preset config.");
} finally {
setIsLoadingPresets(false);
}
};
const updateYamlValue = (location, property, newValue) => {
const currentConfig = getCurrentConfig();
if (!currentConfig) {
message.warning("Load a preset or upload a YAML file first.");
return null;
}
const yamlData = parseYaml(currentConfig) || {};
yamlData[location] = yamlData[location] || {};
yamlData[location][property] = newValue;
return yamlData;
};
const handleRevertPreset = () => {
if (!presetYamlText) return;
const yamlData = parseYaml(presetYamlText);
if (!yamlData) return;
applyYamlData(yamlData, "Preset restored");
};
const handleSliderChange = (location, property, newValue) => {
const yamlData = updateYamlValue(location, property, newValue);
if (!yamlData) return;
applyYamlData(yamlData);
};
const handleArchitectureChange = (value) => {
const yamlData = updateYamlValue("MODEL", "ARCHITECTURE", value);
if (!yamlData) 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]);
const currentArchitecture = useMemo(() => {
const currentConfig = getCurrentConfig();
if (!currentConfig) return undefined;
const yamlData = parseYaml(currentConfig, false);
return yamlData?.MODEL?.ARCHITECTURE;
}, [context.trainingConfig, context.inferenceConfig, type]);
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={{ width: 560 }}
loading={isLoadingPresets}
options={presetOptions}
onChange={handlePresetSelect}
value={context.selectedYamlPreset || undefined}
allowClear
onClear={() => context.setSelectedYamlPreset(null)}
/>
</Space>
{(context.uploadedYamlFile || context.selectedYamlPreset) && (
<div
style={{
marginBottom: 12,
display: "flex",
alignItems: "center",
gap: 8,
}}
>
<strong>Loaded:</strong>{" "}
{context.uploadedYamlFile?.name || context.selectedYamlPreset}
{context.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(context.inputImage) &&
getPathValue(context.inputLabel)
? findCommonPartOfString(
getPathValue(context.inputImage),
getPathValue(context.inputLabel),
)
: "—"}
</div>
<div>
Image name: {getFileName(getPathValue(context.inputImage)) || "—"}
</div>
<div>
Label name: {getFileName(getPathValue(context.inputLabel)) || "—"}
</div>
<div>Output path: {getPathValue(context.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}
/>
</Space>
</div>
</Col>
</Row>
<Divider style={{ margin: "12px 0" }} />
{yamlContent ? (
<Row>
{sliderData.map((param, index) => (
<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={param.value}
onChange={(newValue) =>
handleSliderChange(
param.location,
param.property,
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;