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

Tried to optimize annotation fetching #5974

Merged
merged 21 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
7 changes: 4 additions & 3 deletions cvat/apps/dataset_manager/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ def _extend_attributes(attributeval_set, default_attribute_values):
attributeval_set.append(dotdict([
('spec_id', db_attr.spec_id),
('value', db_attr.value),
('id', None),
bsekachev marked this conversation as resolved.
Show resolved Hide resolved
]))

def _init_tags_from_db(self):
Expand Down Expand Up @@ -399,7 +400,7 @@ def _init_tags_from_db(self):
self._extend_attributes(db_tag.labeledimageattributeval_set,
self.db_attributes[db_tag.label_id]["all"].values())

serializer = serializers.LabeledImageSerializer(db_tags, many=True)
serializer = serializers.LabeledImageSerializerFromDB(db_tags, many=True)
self.ir_data.tags = serializer.data

def _init_shapes_from_db(self):
Expand Down Expand Up @@ -453,7 +454,7 @@ def _init_shapes_from_db(self):
for shape_id, shape_elements in elements.items():
shapes[shape_id].elements = shape_elements

serializer = serializers.LabeledShapeSerializer(list(shapes.values()), many=True)
serializer = serializers.LabeledShapeSerializerFromDB(list(shapes.values()), many=True)
self.ir_data.shapes = serializer.data

def _init_tracks_from_db(self):
Expand Down Expand Up @@ -546,7 +547,7 @@ def _init_tracks_from_db(self):
for track_id, track_elements in elements.items():
tracks[track_id].elements = track_elements

serializer = serializers.LabeledTrackSerializer(list(tracks.values()), many=True)
serializer = serializers.LabeledTrackSerializerFromDB(list(tracks.values()), many=True)
self.ir_data.tracks = serializer.data

def _init_version_from_db(self):
Expand Down
3 changes: 1 addition & 2 deletions cvat/apps/engine/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,7 @@ def serialize_annotations():
for db_job_id in db_job_ids:
annotations = dm.task.get_job_data(db_job_id)
annotations_serializer = LabeledDataSerializer(data=annotations)
annotations_serializer.is_valid(raise_exception=True)
job_annotations.append(self._prepare_annotations(annotations_serializer.data, self._label_mapping))
job_annotations.append(self._prepare_annotations(annotations_serializer.initial_data, self._label_mapping))
bsekachev marked this conversation as resolved.
Show resolved Hide resolved

return job_annotations

Expand Down
3 changes: 1 addition & 2 deletions cvat/apps/engine/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ def export_annotations(self, request, db_obj, export_func, callback, get_data=No

data = get_data(self._object.pk)
serializer = LabeledDataSerializer(data=data)
if serializer.is_valid(raise_exception=True):
return Response(serializer.data)
return Response(serializer.initial_data)
bsekachev marked this conversation as resolved.
Show resolved Hide resolved

def import_annotations(self, request, db_obj, import_func, rq_func, rq_id):
is_tus_request = request.headers.get('Upload-Length', None) is not None or \
Expand Down
60 changes: 59 additions & 1 deletion cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,6 @@ def run_child_validation(self, data):

raise exceptions.ValidationError(errors)


class ShapeSerializer(serializers.Serializer):
type = serializers.ChoiceField(choices=models.ShapeType.choices())
occluded = serializers.BooleanField(default=False)
Expand All @@ -1207,6 +1206,65 @@ class SubLabeledShapeSerializer(ShapeSerializer, AnnotationSerializer):
class LabeledShapeSerializer(SubLabeledShapeSerializer):
elements = SubLabeledShapeSerializer(many=True, required=False)

bsekachev marked this conversation as resolved.
Show resolved Hide resolved
class LabeledImageSerializerFromDB(serializers.BaseSerializer):
SpecLad marked this conversation as resolved.
Show resolved Hide resolved
def to_representation(self, instance):
def convert_tag(tag):
tag['attributes'] = [OrderedDict(attr) for attr in tag['labeledimageattributeval_set']]
SpecLad marked this conversation as resolved.
Show resolved Hide resolved
del tag['labeledimageattributeval_set']
if 'parent' in tag:
del tag['parent']
for attr in tag['attributes']:
del attr['id']

return OrderedDict(tag)
bsekachev marked this conversation as resolved.
Show resolved Hide resolved

return convert_tag(instance)

class LabeledShapeSerializerFromDB(serializers.BaseSerializer):
def to_representation(self, instance):
def convert_shape(shape):
shape['attributes'] = [OrderedDict(attr) for attr in shape['labeledshapeattributeval_set']]
del shape['labeledshapeattributeval_set']

if 'parent' in shape:
if 'elements' in shape:
del shape['elements']
del shape['parent']
for attr in shape['attributes']:
del attr['id']
if 'elements' in shape:
for element in shape['elements']:
convert_shape(element)
return OrderedDict(shape)

return convert_shape(instance)

class LabeledTrackSerializerFromDB(serializers.BaseSerializer):
def to_representation(self, instance):
def convert_track(track):
track['shapes'] = [OrderedDict(attr) for attr in track['trackedshape_set']]
del track['trackedshape_set']
track['attributes'] = [OrderedDict(attr) for attr in track['labeledtrackattributeval_set']]
del track['labeledtrackattributeval_set']
if 'parent' in track:
del track['parent']
for attr in track['attributes']:
del attr['id']

for shape in track['shapes']:
shape['attributes'] = [OrderedDict(attr) for attr in shape['trackedshapeattributeval_set']]
del shape['trackedshapeattributeval_set']
for attr in shape['attributes']:
del attr['id']

if 'elements' in track:
for element in track['elements']:
convert_track(element)

return OrderedDict(track)

return convert_track(instance)

class TrackedShapeSerializer(ShapeSerializer):
id = serializers.IntegerField(default=None, allow_null=True)
frame = serializers.IntegerField(min_value=0)
Expand Down