Skip to content

Commit 3578637

Browse files
Fix: Job working directory and logging for onnx export
1 parent d592272 commit 3578637

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

client/platform/desktop/backend/native/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ async function createWorkingDirectory(settings: Settings, jsonMetaList: JsonMeta
101101
return runFolderPath;
102102
}
103103

104+
async function createCustomWorkingDirectory(settings: Settings, prefix: string, pipeline: string) {
105+
const jobFolderPath = path.join(settings.dataPath, JobsFolderName);
106+
// Formating prefix if for any reason the prefix is input by the user in the futur
107+
// eslint-disable-next-line no-useless-escape
108+
const safePrefix = prefix.replace(/[\.\s/]+/g, '_');
109+
const runFolderName = moment().format(`[${safePrefix}_${pipeline}]_MM-DD-yy_hh-mm-ss.SSS`);
110+
const runFolderPath = path.join(jobFolderPath, runFolderName);
111+
if (!fs.existsSync(jobFolderPath)) {
112+
await fs.mkdir(jobFolderPath);
113+
}
114+
await fs.mkdir(runFolderPath);
115+
return runFolderPath;
116+
}
117+
104118
/* same as os.path.splitext */
105119
function splitExt(input: string): [string, string] {
106120
const ext = path.extname(input);
@@ -112,6 +126,7 @@ export {
112126
getBinaryPath,
113127
jobFileEchoMiddleware,
114128
createWorkingDirectory,
129+
createCustomWorkingDirectory,
115130
spawnResult,
116131
splitExt,
117132
};

client/platform/desktop/backend/native/viame.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { observeChild } from 'platform/desktop/backend/native/processManager';
1313

1414
import { MultiType, stereoPipelineMarker, multiCamPipelineMarkers } from 'dive-common/constants';
1515
import * as common from './common';
16-
import { jobFileEchoMiddleware, createWorkingDirectory } from './utils';
16+
import { jobFileEchoMiddleware, createWorkingDirectory, createCustomWorkingDirectory } from './utils';
1717
import {
1818
getMultiCamImageFiles, getMultiCamVideoPath,
1919
writeMultiCamStereoPipelineArgs,
@@ -242,39 +242,55 @@ async function exportTrainedPipeline(settings: Settings,
242242
throw new Error("Your pipeline has no trained weights (yolo.weights is missing)");
243243
}
244244

245+
const jobWorkDir = await createCustomWorkingDirectory(settings, 'OnnxExport', pipeline.name);
246+
247+
const converterOutput = npath.join(jobWorkDir, 'model.onnx');
248+
const joblog = npath.join(jobWorkDir, 'runlog.txt');
249+
245250
const command = [
246251
`${viameConstants.setupScriptAbs} &&`,
247252
`"${viameConstants.kwiverExe}" runner ${exportPipelinePath}`,
248253
`-s "onnx_convert:model_path=${weightsPath}"`,
249-
`-s "onnx_convert:onnx_model_prefix=${path}"`,
254+
`-s "onnx_convert:onnx_model_prefix=${converterOutput}"`,
250255
];
251256

252257
const job = observeChild(spawn(command.join(' '), {
253258
shell: viameConstants.shell,
254-
cwd: undefined,
259+
cwd: jobWorkDir,
255260
}));
256261

257262
const jobBase: DesktopJob = {
258-
key: `pipeline_${job.pid}`,
263+
key: `pipeline_${job.pid}_${jobWorkDir}`,
259264
command: command.join(' '),
260265
jobType: 'export',
261266
pid: job.pid,
262267
args: exportTrainedPipelineArgs,
263268
title: `${exportTrainedPipelineArgs.pipeline.name} to ONNX`,
269+
workingDir: jobWorkDir,
264270
datasetIds: [],
265271
exitCode: job.exitCode,
266272
startTime: new Date(),
267273
};
268274

275+
fs.writeFile(npath.join(jobWorkDir, DiveJobManifestName), JSON.stringify(jobBase, null, 2));
276+
269277
updater({
270278
...jobBase,
271279
body: [''],
272280
});
273281

274-
job.stdout.on('data', jobFileEchoMiddleware(jobBase, updater));
275-
job.stderr.on('data', jobFileEchoMiddleware(jobBase, updater));
282+
job.stdout.on('data', jobFileEchoMiddleware(jobBase, updater, joblog));
283+
job.stderr.on('data', jobFileEchoMiddleware(jobBase, updater, joblog));
276284

277285
job.on('exit', async (code) => {
286+
if (code === 0) {
287+
if (fs.existsSync(converterOutput)) {
288+
// We move instead of copying because .onnx files can be huge
289+
fs.moveSync(converterOutput, path);
290+
} else {
291+
console.error("An error occured while creating the ONNX file.");
292+
}
293+
}
278294
updater({
279295
...jobBase,
280296
body: [''],

client/platform/desktop/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export interface DesktopJob {
191191
// pid of the process spawned
192192
pid: number;
193193
// workingDir of the job's output
194-
workingDir?: string;
194+
workingDir: string;
195195
// exitCode if set, the job exited already
196196
exitCode: number | null;
197197
// startTime time of process initialization

0 commit comments

Comments
 (0)