Skip to content

Commit

Permalink
Merge pull request #149 from biigle/largo-sort
Browse files Browse the repository at this point in the history
Largo sort
  • Loading branch information
mzur committed Jan 26, 2024
2 parents 5a573e7 + d2e7bc5 commit e924e25
Show file tree
Hide file tree
Showing 21 changed files with 475 additions and 406 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
composer config repositories.module --json '{"type": "path", "url": "'${GITHUB_WORKSPACE}'", "options": {"symlink": false}}'
composer require --no-ansi --no-interaction --no-scripts --ignore-platform-reqs ${GITHUB_REPOSITORY}:@dev
sed -i "/Insert Biigle module service providers/i Biigle\\\\Modules\\\\${MODULE_NAME}\\\\${MODULE_NAME}ServiceProvider::class," config/app.php
sed -i "/Insert Biigle module service providers/i Biigle\\\\Modules\\\\Largo\\\\LargoServiceProvider::class," config/app.php
mkdir -p tests/php/Modules
ln -sf ../../../vendor/${GITHUB_REPOSITORY}/tests tests/php/Modules/${MODULE_NAME}
working-directory: ../core
Expand Down
6 changes: 2 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
torch==2.0.*
torchvision==0.15.*
torch==2.1.*
torchvision==0.16.*
mmcv==2.0.*
# Update CUDA Version if necessary.
# See: https://mmcv.readthedocs.io/en/latest/get_started/installation.html#install-with-pip
Expand All @@ -8,5 +8,3 @@ mmdet==3.1.*
albumentations
scikit-learn
scikit-image
Pillow==10.2.0
xformers==0.0.18
16 changes: 10 additions & 6 deletions src/Http/Controllers/Api/AnnotationCandidateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Biigle\Modules\Maia\Http\Controllers\Api;

use Biigle\Http\Controllers\Api\Controller;
use Biigle\Modules\Largo\Jobs\GenerateImageAnnotationPatch;
use Biigle\Modules\Maia\AnnotationCandidateFeatureVector;
use Biigle\Modules\Maia\Http\Requests\SubmitAnnotationCandidates;
use Biigle\Modules\Maia\Http\Requests\UpdateAnnotationCandidate;
use Biigle\Modules\Maia\Jobs\ConvertAnnotationCandidates;
use Biigle\Modules\Maia\Jobs\ProcessObjectDetectedImage;
use Biigle\Modules\Maia\MaiaJob;
use Illuminate\Http\Response;
use Pgvector\Laravel\Distance;
Expand Down Expand Up @@ -150,17 +150,21 @@ public function submit(SubmitAnnotationCandidates $request)
*/
public function update(UpdateAnnotationCandidate $request)
{
$candidate = $request->candidate;
if ($request->filled('points')) {
$request->candidate->points = $request->input('points');
$disk = config('maia.annotation_candidate_storage_disk');
GenerateImageAnnotationPatch::dispatch($request->candidate, $disk)
$candidate->points = $request->input('points');
ProcessObjectDetectedImage::dispatch($candidate->image,
only: [$candidate->id],
maiaJob: $candidate->job,
targetDisk: config('maia.annotation_candidate_storage_disk')
)
->onQueue(config('largo.generate_annotation_patch_queue'));
}

if ($request->has('label_id')) {
$request->candidate->label_id = $request->input('label_id');
$candidate->label_id = $request->input('label_id');
}

$request->candidate->save();
$candidate->save();
}
}
17 changes: 11 additions & 6 deletions src/Http/Controllers/Api/TrainingProposalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Biigle\Modules\Maia\Http\Controllers\Api;

use Biigle\Http\Controllers\Api\Controller;
use Biigle\Modules\Largo\Jobs\GenerateImageAnnotationPatch;
use Biigle\Modules\Maia\Events\MaiaJobContinued;
use Biigle\Modules\Maia\Http\Requests\ContinueMaiaJob;
use Biigle\Modules\Maia\Http\Requests\UpdateTrainingProposal;
use Biigle\Modules\Maia\Jobs\ProcessNoveltyDetectedImage;
use Biigle\Modules\Maia\MaiaJob;
use Biigle\Modules\Maia\MaiaJobState as State;
use Biigle\Modules\Maia\TrainingProposalFeatureVector;
Expand Down Expand Up @@ -149,14 +149,19 @@ public function submit(ContinueMaiaJob $request)
*/
public function update(UpdateTrainingProposal $request)
{
$proposal = $request->proposal;
if ($request->filled('points')) {
$request->proposal->points = $request->input('points');
$disk = config('maia.training_proposal_storage_disk');
GenerateImageAnnotationPatch::dispatch($request->proposal, $disk)
$proposal->points = $request->input('points');
ProcessNoveltyDetectedImage::dispatch($proposal->image,
only: [$proposal->id],
maiaJob: $proposal->job,
targetDisk: config('maia.training_proposal_storage_disk')
)
->onQueue(config('largo.generate_annotation_patch_queue'));
}

$request->proposal->selected = $request->input('selected', $request->proposal->selected);
$request->proposal->save();
$proposal->selected = $request->input('selected', $proposal->selected);

$proposal->save();
}
}
21 changes: 14 additions & 7 deletions src/Jobs/ConvertAnnotationCandidates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
use Biigle\ImageAnnotation;
use Biigle\ImageAnnotationLabel;
use Biigle\Jobs\Job;
use Biigle\Modules\Largo\Jobs\GenerateImageAnnotationPatch;
use Biigle\Modules\Largo\Jobs\ProcessAnnotatedImage;
use Biigle\Modules\Maia\MaiaJob;
use Biigle\User;
use Carbon\Carbon;
use DB;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

class ConvertAnnotationCandidates extends Job
{
Expand Down Expand Up @@ -47,9 +48,9 @@ class ConvertAnnotationCandidates extends Job
/**
* Newly created annotations.
*
* @var array
* @var Collection
*/
protected $newAnnotations = [];
protected $newAnnotations;

/**
* Create a new isntance.
Expand All @@ -62,6 +63,7 @@ public function __construct(MaiaJob $job, User $user)
$this->queue = config('maia.convert_annotations_queue');
$this->job = $job;
$this->user = $user;
$this->newAnnotations = collect([]);
}

/**
Expand All @@ -79,10 +81,15 @@ public function handle()
->chunkById(10000, [$this, 'processChunk']);
});

foreach ($this->newAnnotations as $annotation) {
GenerateImageAnnotationPatch::dispatch($annotation)
->onQueue(config('largo.generate_annotation_patch_queue'));
}
$this->newAnnotations
->groupBy('image_id')
->each(function ($group) {
$image = $group[0]->image;
$ids = $group->pluck('id')->all();
ProcessAnnotatedImage::dispatch($image, only: $ids)
->onQueue(config('largo.generate_annotation_patch_queue'));
});

} finally {
$this->job->convertingCandidates = false;
$this->job->save();
Expand Down
43 changes: 33 additions & 10 deletions src/Jobs/GenerateAnnotationCandidatePatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,46 @@

namespace Biigle\Modules\Maia\Jobs;

class GenerateAnnotationCandidatePatches extends GenerateAnnotationPatches
use Biigle\Jobs\Job;
use Biigle\Modules\Maia\MaiaJob;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;

class GenerateAnnotationCandidatePatches extends Job implements ShouldQueue
{
use SerializesModels;

/**
* Get a query for the annotations that have been created by this job.
* Ignore this job if the MAIA job does not exist any more.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @var bool
*/
protected function getCreatedAnnotations()
{
return $this->job->annotationCandidates();
}
protected $deleteWhenMissingModels = true;

/**
* Get the storage disk to store the annotation patches to.
* Create a new isntance.
*/
protected function getPatchStorageDisk()
public function __construct(public MaiaJob $maiaJob)
{
//
}

public function handle(): void
{
return config('maia.annotation_candidate_storage_disk');
$this->maiaJob->volume->images()
->whereExists(fn ($q) =>
$q->select(\DB::raw(1))
->from('maia_annotation_candidates')
->where('maia_annotation_candidates.job_id', $this->maiaJob->id)
->whereColumn('maia_annotation_candidates.image_id', 'images.id')
)
->eachById(fn ($image) =>
ProcessObjectDetectedImage::dispatch($image, $this->maiaJob,
// Feature vectors are generated in a separate job on the GPU.
skipFeatureVectors: true,
targetDisk: config('maia.annotation_candidate_storage_disk')
)
->onQueue(config('largo.generate_annotation_patch_queue'))
);
}
}

0 comments on commit e924e25

Please sign in to comment.