Skip to content

Commit

Permalink
Remove the quality parameter from lambda function call endpoints
Browse files Browse the repository at this point in the history
1. In practice, it is never used by the UI.

2. In theory, I don't think it should be provided either. I don't think it
   ever makes sense for a user to want to run a function on a
   reduced-quality image, unless it just doesn't work on the original one
   (e.g. if it is too big). And in this case, CVAT should just perform the
   recompression automatically instead of making the user do it.
  • Loading branch information
SpecLad committed Nov 11, 2024
1 parent 1e7ff33 commit cf9e35e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 69 deletions.
3 changes: 0 additions & 3 deletions cvat/apps/lambda_manager/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class FunctionCallRequestSerializer(serializers.Serializer):
function = serializers.CharField(help_text="The name of the function to execute")
task = serializers.IntegerField(help_text="The id of the task to be annotated")
job = serializers.IntegerField(required=False, help_text="The id of the job to be annotated")
quality = serializers.ChoiceField(choices=['compressed', 'original'], default="original",
help_text="The quality of the images to use in the model run"
)
max_distance = serializers.IntegerField(required=False)
threshold = serializers.FloatField(required=False)
cleanup = serializers.BooleanField(help_text="Whether existing annotations should be removed", default=False)
Expand Down
36 changes: 0 additions & 36 deletions cvat/apps/lambda_manager/tests/test_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ def test_api_v2_lambda_requests_read(self):
"task": self.main_task["id"],
"cleanup": True,
"threshold": 55,
"quality": "original",
"mapping": {
"car": { "name": "car" },
},
Expand Down Expand Up @@ -447,7 +446,6 @@ def test_api_v2_lambda_requests_create(self):
"task": self.main_task["id"],
"cleanup": True,
"threshold": 55,
"quality": "original",
"mapping": {
"car": { "name": "car" },
},
Expand All @@ -456,7 +454,6 @@ def test_api_v2_lambda_requests_create(self):
"function": id_func,
"task": self.assigneed_to_user_task["id"],
"cleanup": False,
"quality": "compressed",
"max_distance": 70,
"mapping": {
"car": { "name": "car" },
Expand Down Expand Up @@ -769,7 +766,6 @@ def test_api_v2_lambda_functions_create_reid(self):
OrderedDict([('attributes', []), ('frame', 1), ('group', None), ('id', 11260), ('label_id', 8), ('occluded', False), ('points', [1076.0, 199.0, 1218.0, 593.0]), ('source', 'auto'), ('type', 'rectangle'), ('z_order', 0)]),
OrderedDict([('attributes', []), ('frame', 1), ('group', None), ('id', 11261), ('label_id', 8), ('occluded', False), ('points', [924.0, 177.0, 1090.0, 615.0]), ('source', 'auto'), ('type', 'rectangle'), ('z_order', 0)]),
],
"quality": None,
"threshold": 0.5,
"max_distance": 55,
}
Expand All @@ -785,7 +781,6 @@ def test_api_v2_lambda_functions_create_reid(self):
OrderedDict([('attributes', []), ('frame', 1), ('group', None), ('id', 11260), ('label_id', 8), ('occluded', False), ('points', [1076.0, 199.0, 1218.0, 593.0]), ('source', 'auto'), ('type', 'rectangle'), ('z_order', 0)]),
OrderedDict([('attributes', []), ('frame', 1), ('group', 0), ('id', 11398), ('label_id', 8), ('occluded', False), ('points', [184.3935546875, 211.5048828125, 331.64968722073354, 97.27792672028772, 445.87667560321825, 126.17873100983161, 454.13404825737416, 691.8087578194827, 180.26452189455085]), ('source', 'manual'), ('type', 'polygon'), ('z_order', 0)]),
],
"quality": None,
}

response = self._post_request(f"{LAMBDA_FUNCTIONS_PATH}/{id_function_reid_with_response_data}", self.admin, data_main_task)
Expand Down Expand Up @@ -829,42 +824,11 @@ def test_api_v2_lambda_functions_create_negative(self):
self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)


def test_api_v2_lambda_functions_create_quality(self):
qualities = [None, "original", "compressed"]

for quality in qualities:
data = {
"task": self.main_task["id"],
"frame": 0,
"cleanup": True,
"quality": quality,
"mapping": {
"car": { "name": "car" },
},
}

response = self._post_request(f"{LAMBDA_FUNCTIONS_PATH}/{id_function_detector}", self.admin, data)
self.assertEqual(response.status_code, status.HTTP_200_OK)

data = {
"task": self.main_task["id"],
"frame": 0,
"cleanup": True,
"quality": "test-error-quality",
"mapping": {
"car": { "name": "car" },
},
}

response = self._post_request(f"{LAMBDA_FUNCTIONS_PATH}/{id_function_detector}", self.admin, data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_api_v2_lambda_functions_convert_mask_to_rle(self):
data_main_task = {
"function": id_function_detector,
"task": self.main_task["id"],
"cleanup": True,
"quality": "original",
"mapping": {
"car": { "name": "car" },
},
Expand Down
45 changes: 15 additions & 30 deletions cvat/apps/lambda_manager/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from rest_framework.request import Request

import cvat.apps.dataset_manager as dm
from cvat.apps.engine.frame_provider import FrameQuality, TaskFrameProvider
from cvat.apps.engine.frame_provider import TaskFrameProvider
from cvat.apps.engine.models import (
Job, ShapeType, SourceType, Task, Label, RequestAction, RequestTarget
)
Expand Down Expand Up @@ -257,7 +257,6 @@ def mandatory_arg(name: str) -> Any:
threshold = data.get("threshold")
if threshold:
payload.update({ "threshold": threshold })
quality = data.get("quality")
mapping = data.get("mapping", {})

model_labels = self.labels
Expand Down Expand Up @@ -387,19 +386,19 @@ def validate_attributes_mapping(attributes_mapping, model_attributes, db_attribu

if self.kind == FunctionKind.DETECTOR:
payload.update({
"image": self._get_image(db_task, mandatory_arg("frame"), quality)
"image": self._get_image(db_task, mandatory_arg("frame"))
})
elif self.kind == FunctionKind.INTERACTOR:
payload.update({
"image": self._get_image(db_task, mandatory_arg("frame"), quality),
"image": self._get_image(db_task, mandatory_arg("frame")),
"pos_points": mandatory_arg("pos_points"),
"neg_points": mandatory_arg("neg_points"),
"obj_bbox": data.get("obj_bbox", None)
})
elif self.kind == FunctionKind.REID:
payload.update({
"image0": self._get_image(db_task, mandatory_arg("frame0"), quality),
"image1": self._get_image(db_task, mandatory_arg("frame1"), quality),
"image0": self._get_image(db_task, mandatory_arg("frame0")),
"image1": self._get_image(db_task, mandatory_arg("frame1")),
"boxes0": mandatory_arg("boxes0"),
"boxes1": mandatory_arg("boxes1")
})
Expand All @@ -410,7 +409,7 @@ def validate_attributes_mapping(attributes_mapping, model_attributes, db_attribu
})
elif self.kind == FunctionKind.TRACKER:
payload.update({
"image": self._get_image(db_task, mandatory_arg("frame"), quality),
"image": self._get_image(db_task, mandatory_arg("frame")),
"shapes": data.get("shapes", []),
"states": data.get("states", [])
})
Expand Down Expand Up @@ -487,19 +486,9 @@ def transform_attributes(input_attributes, attr_mapping, db_attributes):

return response

def _get_image(self, db_task, frame, quality):
if quality is None or quality == "original":
quality = FrameQuality.ORIGINAL
elif quality == "compressed":
quality = FrameQuality.COMPRESSED
else:
raise ValidationError(
'`{}` lambda function was run '.format(self.id) +
'with wrong arguments (quality={})'.format(quality),
code=status.HTTP_400_BAD_REQUEST)

def _get_image(self, db_task, frame):
frame_provider = TaskFrameProvider(db_task)
image = frame_provider.get_frame(frame, quality=quality)
image = frame_provider.get_frame(frame)

return base64.b64encode(image.data.getvalue()).decode('utf-8')

Expand All @@ -523,7 +512,7 @@ def get_jobs(self):
return [LambdaJob(job) for job in jobs if job and job.meta.get("lambda")]

def enqueue(self,
lambda_func, threshold, task, quality, mapping, cleanup, conv_mask_to_poly, max_distance, request,
lambda_func, threshold, task, mapping, cleanup, conv_mask_to_poly, max_distance, request,
*,
job: Optional[int] = None
) -> LambdaJob:
Expand Down Expand Up @@ -576,7 +565,6 @@ def enqueue(self,
"threshold": threshold,
"task": task,
"job": job,
"quality": quality,
"cleanup": cleanup,
"conv_mask_to_poly": conv_mask_to_poly,
"mapping": mapping,
Expand Down Expand Up @@ -667,7 +655,6 @@ def _call_detector(
function: LambdaFunction,
db_task: Task,
labels: Dict[str, Dict[str, Any]],
quality: str,
threshold: float,
mapping: Optional[Dict[str, str]],
conv_mask_to_poly: bool,
Expand Down Expand Up @@ -799,7 +786,7 @@ def _map(sublabel_body):
continue

annotations = function.invoke(db_task, db_job=db_job, data={
"frame": frame, "quality": quality, "mapping": mapping,
"frame": frame, "mapping": mapping,
"threshold": threshold
})

Expand Down Expand Up @@ -854,7 +841,6 @@ def _call_reid(
cls,
function: LambdaFunction,
db_task: Task,
quality: str,
threshold: float,
max_distance: int,
*,
Expand Down Expand Up @@ -887,7 +873,7 @@ def _call_reid(
boxes1 = boxes_by_frame[frame1]
if boxes0 and boxes1:
matching = function.invoke(db_task, db_job=db_job, data={
"frame0": frame0, "frame1": frame1, "quality": quality,
"frame0": frame0, "frame1": frame1,
"boxes0": boxes0, "boxes1": boxes1, "threshold": threshold,
"max_distance": max_distance})

Expand Down Expand Up @@ -947,7 +933,7 @@ def _call_reid(
dm.task.put_task_data(db_task.id, serializer.data)

@classmethod
def __call__(cls, function, task: int, quality: str, cleanup: bool, **kwargs):
def __call__(cls, function, task: int, cleanup: bool, **kwargs):
# TODO: need logging
db_job = None
if job := kwargs.get('job'):
Expand Down Expand Up @@ -977,11 +963,11 @@ def convert_labels(db_labels):
labels = convert_labels(db_task.get_labels(prefetch=True))

if function.kind == FunctionKind.DETECTOR:
cls._call_detector(function, db_task, labels, quality,
cls._call_detector(function, db_task, labels,
kwargs.get("threshold"), kwargs.get("mapping"), kwargs.get("conv_mask_to_poly"),
db_job=db_job)
elif function.kind == FunctionKind.REID:
cls._call_reid(function, db_task, quality,
cls._call_reid(function, db_task,
kwargs.get("threshold"), kwargs.get("max_distance"), db_job=db_job)

def return_response(success_code=status.HTTP_200_OK):
Expand Down Expand Up @@ -1176,7 +1162,6 @@ def create(self, request):
threshold = request_data.get('threshold')
task = request_data['task']
job = request_data.get('job', None)
quality = request_data.get("quality")
cleanup = request_data.get('cleanup', False)
conv_mask_to_poly = request_data.get('convMaskToPoly', False)
mapping = request_data.get('mapping')
Expand All @@ -1190,7 +1175,7 @@ def create(self, request):
gateway = LambdaGateway()
queue = LambdaQueue()
lambda_func = gateway.get(function)
rq_job = queue.enqueue(lambda_func, threshold, task, quality,
rq_job = queue.enqueue(lambda_func, threshold, task,
mapping, cleanup, conv_mask_to_poly, max_distance, request, job=job)

handle_function_call(function, job or task, category="batch")
Expand Down

0 comments on commit cf9e35e

Please sign in to comment.