Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check images before creating maia annotations #171

Merged
merged 7 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Jobs/DetectionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use File;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Queue;

abstract class DetectionJob implements ShouldQueue
{
Expand Down Expand Up @@ -217,6 +216,9 @@ protected function createMaiaAnnotations(array $annotations)
return $this->createMaiaAnnotation($annotation);
}, $annotations);

$existingIds = $this->job->volume->images()->pluck('id', 'id')->toArray();
$maiaAnnotations = array_filter($maiaAnnotations, fn ($a) => array_key_exists($a['image_id'], $existingIds));

// Chunk the insert because PDO's maximum number of query parameters is
// 65535. Each annotation has 7 parameters so we can store roughly 9000
// annotations in one call.
Expand Down
36 changes: 36 additions & 0 deletions tests/Jobs/ObjectDetectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Support\Facades\Queue;
use Str;
use TestCase;
use Biigle\Image;

class ObjectDetectionTest extends TestCase
{
Expand Down Expand Up @@ -128,6 +129,31 @@ public function testHandle()
}
}

public function testDeletedImage()
{
FileCache::fake();

$job = MaiaJobTest::create();
$image = ImageTest::create(['volume_id' => $job->volume_id]);
$image2 = ImageTest::create(['volume_id' => $job->volume_id, 'filename' => 'a']);

config(['maia.tmp_dir' => '/tmp']);
$tmpDir = "/tmp/maia-{$job->id}-object-detection";

try {
$request = new OdJobStub($job);
$request->deleteImage = true;
$request->handle();

$annotations = $job->annotationCandidates()->get();
// One image was deleted, only one image and annotation remains.
$this->assertEquals(1, $annotations->count());
$this->assertEquals($image2->id, $annotations[0]->image_id);
} finally {
File::deleteDirectory($tmpDir);
}
}

public function testHandleKnowledgeTransfer()
{
FileCache::fake();
Expand Down Expand Up @@ -383,6 +409,7 @@ class OdJobStub extends ObjectDetection
public $commands = [];
public $cleanup = false;
public $crash = false;
public $deleteImage = false;

protected function maybeDownloadWeights($from, $to)
{
Expand Down Expand Up @@ -418,4 +445,13 @@ protected function updateJobState()

return parent::updateJobState();
}

public function createMaiaAnnotations($annotations)
{
// Simulate image removal after saving its annotations
if ($this->deleteImage && !empty($annotations)) {
Image::where('id', '=', $annotations[0][0])->delete();
}
parent::createMaiaAnnotations($annotations);
}
}