Skip to content

Commit d3d5c32

Browse files
committed
Merge branch 'master' into plain-torch
2 parents c578083 + 06fb703 commit d3d5c32

17 files changed

+185
-85
lines changed

src/Http/Controllers/Api/AnnotationCandidateController.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function index($id)
4343
$job = MaiaJob::findOrFail($id);
4444
$this->authorize('access', $job);
4545

46-
return $job->annotationCandidates()
46+
$query = $job->annotationCandidates()
4747
->join('images', 'images.id', '=', 'maia_annotation_candidates.image_id')
4848
->select(
4949
'maia_annotation_candidates.id',
@@ -53,12 +53,18 @@ public function index($id)
5353
'images.uuid as uuid'
5454
)
5555
->orderBy('score', 'desc')
56-
->with('label')
57-
->get()
58-
->each(function ($candidate) {
59-
$candidate->makeHidden('label_id');
60-
})
61-
->toArray();
56+
->orderBy('id', 'desc')
57+
->with('label');
58+
59+
$yieldItems = function () use ($query): \Generator {
60+
foreach ($query->lazy() as $item) {
61+
$item->makeHidden('label_id');
62+
yield $item;
63+
}
64+
};
65+
66+
// Use a streamed response because there can be a lot of items.
67+
return response()->streamJson($yieldItems());
6268
}
6369

6470
/**

src/Http/Controllers/Api/TrainingProposalController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function index($id)
4343
$job = MaiaJob::findOrFail($id);
4444
$this->authorize('access', $job);
4545

46-
return $job->trainingProposals()
46+
$query = $job->trainingProposals()
4747
->join('images', 'images.id', '=', 'maia_training_proposals.image_id')
4848
->select(
4949
'maia_training_proposals.id',
@@ -52,8 +52,16 @@ public function index($id)
5252
'images.uuid as uuid'
5353
)
5454
->orderBy('score', 'desc')
55-
->get()
56-
->toArray();
55+
->orderBy('id', 'desc');
56+
57+
$yieldItems = function () use ($query): \Generator {
58+
foreach ($query->lazy() as $item) {
59+
yield $item;
60+
}
61+
};
62+
63+
// Use a streamed response because there can be a lot of items.
64+
return response()->streamJson($yieldItems());
5765
}
5866

5967
/**

src/Http/Controllers/Views/MaiaJobController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function show(Request $request, $id)
136136
->url(':prefix/:id.'.config('largo.patch_format'));
137137

138138
$tpLimit = config('maia.training_proposal_limit');
139+
$acLimit = config('maia.annotation_candidate_limit');
139140

140141
return view('maia::show', compact(
141142
'job',
@@ -144,7 +145,8 @@ public function show(Request $request, $id)
144145
'trees',
145146
'tpUrlTemplate',
146147
'acUrlTemplate',
147-
'tpLimit'
148+
'tpLimit',
149+
'acLimit'
148150
));
149151
}
150152
}

src/Jobs/DetectionJob.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,29 @@ protected function createMaiaAnnotation($annotation)
250250
];
251251
}
252252

253+
/**
254+
* Apply the limit for the maximum number of annotations.
255+
*
256+
* @param array $annotations
257+
* @param int $limit
258+
*
259+
* @return array
260+
*/
261+
protected function maybeLimitAnnotations($annotations, $limit)
262+
{
263+
if (count($annotations) <= $limit) {
264+
return $annotations;
265+
}
266+
267+
usort($annotations, function ($a, $b) {
268+
// The fourth array element is the score of the annotation. We want to sort
269+
// the annotations by descending scores.
270+
return round($b[4] - $a[4]);
271+
});
272+
273+
return array_slice($annotations, 0, $limit);
274+
}
275+
253276
/**
254277
* Insert one chunk of the MAIA annotations that should be created into the database.
255278
*

src/Jobs/NoveltyDetection.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,6 @@ protected function getTmpDirPath()
102102
return parent::getTmpDirPath()."-novelty-detection";
103103
}
104104

105-
/**
106-
* Apply the limit for the maximum number of annotations.
107-
*
108-
* @param array $annotations
109-
* @param int $limit
110-
*
111-
* @return array
112-
*/
113-
protected function maybeLimitAnnotations($annotations, $limit)
114-
{
115-
if (count($annotations) <= $limit) {
116-
return $annotations;
117-
}
118-
119-
usort($annotations, function ($a, $b) {
120-
// The fourth array element is the score of the annotation. We want to sort
121-
// the annotations by descending scores.
122-
return round($b[4] - $a[4]);
123-
});
124-
125-
return array_slice($annotations, 0, $limit);
126-
}
127-
128105
/**
129106
* {@inheritdoc}
130107
*/

src/Jobs/ObjectDetection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public function handle()
9292
$this->performInference($images, $output[0], $output[1]);
9393

9494
$annotations = $this->parseAnnotations($images);
95+
$limit = config('maia.annotation_candidate_limit');
96+
$annotations = $this->maybeLimitAnnotations($annotations, $limit);
9597

9698
// Make sure to roll back any DB modifications if an error occurs.
9799
DB::transaction(function () use ($annotations) {

src/config/maia.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
*/
1515
'training_proposal_limit' => 50000,
1616

17+
/*
18+
| Maximum number of automatically generated annottation candidates that are created
19+
| for a job. The limit applies to the list of annotation candidates sorted by
20+
| confidence score in descending order. Set to INF to allow any number.
21+
*/
22+
'annotation_candidate_limit' => 100000,
23+
1724
/*
1825
| Storage disk where the annotation candidate patch images will be stored
1926
*/
Lines changed: 29 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"src/resources/assets/js/main.js": {
3-
"file": "assets/main-CdD9oKBL.js",
3+
"file": "assets/main-BsJxabry.js",
44
"name": "main",
55
"src": "src/resources/assets/js/main.js",
66
"isEntry": true

src/resources/assets/js/components/selectCandidatesTab.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<template>
22
<div class="sidebar-tab__content sidebar-tab__content--maia">
33
<div class="maia-tab-content__top">
4+
<p class="lead">
5+
{{selectedCandidatesCount}} of {{candidatesCount}} selected
6+
</p>
7+
<div v-if="reachedLimit" class="panel panel-warning">
8+
<div class="panel-body text-warning">
9+
This job reached the allowed maximum of {{candidatesLimit}} annotation candidates!
10+
</div>
11+
</div>
412
<label-trees
513
:trees="labelTrees"
614
:show-favourites="true"
@@ -41,6 +49,23 @@ export default {
4149
type: Array,
4250
required: true,
4351
},
52+
candidatesCount: {
53+
type: Number,
54+
required: true,
55+
},
56+
selectedCandidatesCount: {
57+
type: Number,
58+
required: true,
59+
},
60+
candidatesLimit: {
61+
type: Number,
62+
default: Infinity,
63+
},
64+
},
65+
computed: {
66+
reachedLimit() {
67+
return this.candidatesCount >= this.candidatesLimit;
68+
},
4469
},
4570
methods: {
4671
handleSelectedLabel(label) {

0 commit comments

Comments
 (0)