Skip to content

Commit

Permalink
Fix handling of debug keep files flag again
Browse files Browse the repository at this point in the history
Previously if the flag was true, files of successful jobs were also kept.
  • Loading branch information
mzur committed Feb 1, 2023
1 parent 7104e08 commit 2fecd65
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 99 deletions.
31 changes: 13 additions & 18 deletions src/Jobs/NoveltyDetectionRequest.php
Expand Up @@ -27,24 +27,19 @@ public function handle()
{
$this->createTmpDir();

try {
$images = $this->getGenericImages();

FileCache::batch($images, function ($images, $paths) {
$script = config('maia.novelty_detection_script');
$path = $this->createInputJson($images, $paths);
$this->python("{$script} {$path}");
});

$annotations = $this->parseAnnotations($images);
$limit = config('maia.training_proposal_limit');
$annotations = $this->maybeLimitAnnotations($annotations, $limit);
$this->dispatchResponse($annotations);
} finally {
if (!config('maia.debug_keep_files')) {
$this->cleanup();
}
}
$images = $this->getGenericImages();

FileCache::batch($images, function ($images, $paths) {
$script = config('maia.novelty_detection_script');
$path = $this->createInputJson($images, $paths);
$this->python("{$script} {$path}");
});

$annotations = $this->parseAnnotations($images);
$limit = config('maia.training_proposal_limit');
$annotations = $this->maybeLimitAnnotations($annotations, $limit);
$this->dispatchResponse($annotations);
$this->cleanup();
}

/**
Expand Down
47 changes: 21 additions & 26 deletions src/Jobs/ObjectDetectionRequest.php
Expand Up @@ -61,38 +61,33 @@ public function handle()
{
$this->createTmpDir();

try {
$images = $this->getGenericImages();
$images = $this->getGenericImages();

if ($this->shouldUseKnowledgeTransfer()) {
$datasetImages = $this->getKnowledgeTransferImages();
} else {
$datasetImages = $images;
}
if ($this->shouldUseKnowledgeTransfer()) {
$datasetImages = $this->getKnowledgeTransferImages();
} else {
$datasetImages = $images;
}

// All images that contain selected training proposals.
$relevantImages = array_filter($datasetImages, function ($image) {
return array_key_exists($image->getId(), $this->trainingProposals);
});
// All images that contain selected training proposals.
$relevantImages = array_filter($datasetImages, function ($image) {
return array_key_exists($image->getId(), $this->trainingProposals);
});

$output = FileCache::batch($relevantImages, function ($images, $paths) {
$datasetOutputPath = $this->generateDataset($images, $paths);
// Training doesn't need the images and paths directly but expects the
// files to be there in the cache.
$trainingOutputPath = $this->performTraining($datasetOutputPath);
$output = FileCache::batch($relevantImages, function ($images, $paths) {
$datasetOutputPath = $this->generateDataset($images, $paths);
// Training doesn't need the images and paths directly but expects the
// files to be there in the cache.
$trainingOutputPath = $this->performTraining($datasetOutputPath);

return [$datasetOutputPath, $trainingOutputPath];
});
return [$datasetOutputPath, $trainingOutputPath];
});

$this->performInference($images, $output[0], $output[1]);
$this->performInference($images, $output[0], $output[1]);

$annotations = $this->parseAnnotations($images);
$this->dispatchResponse($annotations);
} finally {
if (!config('maia.debug_keep_files')) {
$this->cleanup();
}
}
$annotations = $this->parseAnnotations($images);
$this->dispatchResponse($annotations);
$this->cleanup();
}

/**
Expand Down
34 changes: 0 additions & 34 deletions tests/Jobs/NoveltyDetectionRequestTest.php
Expand Up @@ -21,7 +21,6 @@ public function testHandle()
FileCache::fake();
config([
'maia.max_workers' => 2,
'maia.debug_keep_files' => false,
]);

$params = [
Expand Down Expand Up @@ -72,7 +71,6 @@ public function testHandleLimit()
FileCache::fake();
config([
'maia.training_proposal_limit' => 2,
'maia.debug_keep_files' => false,
]);

$params = [
Expand Down Expand Up @@ -107,38 +105,6 @@ public function testHandleLimit()
}
}

public function testHandleDebug()
{
try {
$params = [
'nd_clusters' => 5,
'nd_patch_size' => 39,
'nd_threshold' => 99,
'nd_latent_size' => 0.1,
'nd_trainset_size' => 10000,
'nd_epochs' => 100,
'nd_stride' => 2,
'nd_ignore_radius' => 5,
'max_workers' => 2,
];
config([
'maia.tmp_dir' => '/tmp',
'maia.debug_keep_files' => true,
]);
$image = ImageTest::create();
$job = MaiaJobTest::create([
'volume_id' => $image->volume_id,
'params' => $params,
]);
$tmpDir = "/tmp/maia-{$job->id}-novelty-detection";
$request = new NdJobStub($job);
$request->handle();
$this->assertFalse($request->cleanup);
} finally {
File::deleteDirectory($tmpDir);
}
}

public function testFailed()
{
config(['maia.debug_keep_files' => false]);
Expand Down
21 changes: 0 additions & 21 deletions tests/Jobs/ObjectDetectionRequestTest.php
Expand Up @@ -24,7 +24,6 @@ public function testHandle()
config([
'maia.mmdet_train_batch_size' => 12,
'maia.max_workers' => 2,
'maia.debug_keep_files' => false,
]);

$params = [
Expand Down Expand Up @@ -124,7 +123,6 @@ public function testHandleKnowledgeTransfer()
config([
'maia.mmdet_train_batch_size' => 12,
'maia.max_workers' => 2,
'maia.debug_keep_files' => false,
]);

$otherImage = ImageTest::create();
Expand Down Expand Up @@ -236,7 +234,6 @@ public function testHandleAreaKnowledgeTransfer()
config([
'maia.mmdet_train_batch_size' => 12,
'maia.max_workers' => 2,
'maia.debug_keep_files' => false,
]);

$otherImage = ImageTest::create();
Expand Down Expand Up @@ -302,24 +299,6 @@ public function testHandleAreaKnowledgeTransfer()
}
}

public function testHandleDebug()
{
try {
config([
'maia.tmp_dir' => '/tmp',
'maia.debug_keep_files' => true,
]);
$image = ImageTest::create();
$job = MaiaJobTest::create(['volume_id' => $image->volume_id]);
$tmpDir = "/tmp/maia-{$job->id}-object-detection";
$request = new OdJobStub($job);
$request->handle();
$this->assertFalse($request->cleanup);
} finally {
File::deleteDirectory($tmpDir);
}
}

public function testFailed()
{
config(['maia.debug_keep_files' => false]);
Expand Down

0 comments on commit 2fecd65

Please sign in to comment.