Browse Source

Add segmentation model

pull/1899/head
Hironsan 2 years ago
parent
commit
fe795dd0c4
5 changed files with 87 additions and 2 deletions
  1. 8
      backend/labels/admin.py
  2. 5
      backend/labels/managers.py
  3. 46
      backend/labels/migrations/0016_segmentation.py
  4. 8
      backend/labels/models.py
  5. 22
      backend/labels/serializers.py

8
backend/labels/admin.py

@ -1,6 +1,6 @@
from django.contrib import admin
from .models import BoundingBox, Category, Span, TextLabel
from .models import BoundingBox, Category, Segmentation, Span, TextLabel
class SpanAdmin(admin.ModelAdmin):
@ -23,7 +23,13 @@ class BoundingBoxAdmin(admin.ModelAdmin):
ordering = ("example",)
class SegmentationAdmin(admin.ModelAdmin):
list_display = ("example", "label", "user", "points")
ordering = ("example",)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Span, SpanAdmin)
admin.site.register(TextLabel, TextLabelAdmin)
admin.site.register(BoundingBox, BoundingBoxAdmin)
admin.site.register(Segmentation, SegmentationAdmin)

5
backend/labels/managers.py

@ -86,3 +86,8 @@ class RelationManager(LabelManager):
class BoundingBoxManager(LabelManager):
def can_annotate(self, label, project) -> bool:
return True
class SegmentationManager(LabelManager):
def can_annotate(self, label, project) -> bool:
return True

46
backend/labels/migrations/0016_segmentation.py

@ -0,0 +1,46 @@
# Generated by Django 4.0.4 on 2022-06-30 05:24
import uuid
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("examples", "0006_alter_example_upload_name"),
("label_types", "0007_delete_relationtypeold"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("labels", "0015_create_boundingbox_table"),
]
operations = [
migrations.CreateModel(
name="Segmentation",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("uuid", models.UUIDField(default=uuid.uuid4, unique=True)),
("prob", models.FloatField(default=0.0)),
("manual", models.BooleanField(default=False)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("points", models.JSONField(default=list)),
(
"example",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="segmentations", to="examples.example"
),
),
(
"label",
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="label_types.categorytype"),
),
("user", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
"abstract": False,
},
),
]

8
backend/labels/models.py

@ -9,6 +9,7 @@ from .managers import (
CategoryManager,
LabelManager,
RelationManager,
SegmentationManager,
SpanManager,
TextLabelManager,
)
@ -145,3 +146,10 @@ class BoundingBox(Label):
models.CheckConstraint(check=models.Q(width__gte=0), name="width >= 0"),
models.CheckConstraint(check=models.Q(height__gte=0), name="height >= 0"),
]
class Segmentation(Label):
objects = SegmentationManager()
points = models.JSONField(default=list)
label = models.ForeignKey(to=CategoryType, on_delete=models.CASCADE)
example = models.ForeignKey(to=Example, on_delete=models.CASCADE, related_name="segmentations")

22
backend/labels/serializers.py

@ -1,6 +1,6 @@
from rest_framework import serializers
from .models import BoundingBox, Category, Relation, Span, TextLabel
from .models import BoundingBox, Category, Relation, Segmentation, Span, TextLabel
from examples.models import Example
from label_types.models import CategoryType, RelationType, SpanType
@ -91,3 +91,23 @@ class BoundingBoxSerializer(serializers.ModelSerializer):
"height",
)
read_only_fields = ("user",)
class SegmentationSerializer(serializers.ModelSerializer):
example = serializers.PrimaryKeyRelatedField(queryset=Example.objects.all())
label = serializers.PrimaryKeyRelatedField(queryset=CategoryType.objects.all())
class Meta:
model = Segmentation
fields = (
"id",
"uuid",
"prob",
"user",
"example",
"created_at",
"updated_at",
"label",
"points",
)
read_only_fields = ("user",)
Loading…
Cancel
Save