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

Add model category attributes to model template #2362

Merged
merged 9 commits into from
Jul 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ training_targets:
# Computational Complexity
gigaflops: 3.9
size: 168.4

# Model spec
model_category: SPEED
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ training_targets:
# Computational Complexity
gigaflops: 5.6
size: 21.1

# Model spec
model_category: ACCURACY
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ training_targets:
# Computational Complexity
gigaflops: 3.9
size: 168.4

# Model spec
model_category: SPEED
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ training_targets:
# Computational Complexity
gigaflops: 5.6
size: 21.1

# Model spec
model_category: ACCURACY
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ training_targets:
# Computational Complexity
gigaflops: 3.9
size: 168.4

# Model spec
model_category: SPEED
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ training_targets:
# Computational Complexity
gigaflops: 5.6
size: 21.1

# Model spec
model_category: ACCURACY
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ training_targets:
# Stats.
gigaflops: 0.81
size: 4.09

# Model spec
model_category: BALANCE
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ training_targets:
# Stats.
gigaflops: 5.76
size: 20.23

# Model spec
model_category: ACCURACY
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ training_targets:
# Stats.
gigaflops: 0.44
size: 4.29

# Model spec
model_category: SPEED
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ training_targets:
# Stats.
gigaflops: 6.5
size: 20.4
# # Inference options. Defined by OpenVINO capabilities, not Algo Backend or Platform.
# inference_targets:
# - CPU
# - GPU
# - VPU

# Model spec
model_category: SPEED
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ training_targets:
# Stats.
gigaflops: 20.6
size: 9.1
# # Inference options. Defined by OpenVINO capabilities, not Algo Backend or Platform.
# inference_targets:
# - CPU
# - GPU
# - VPU

# Model spec
model_category: ACCURACY
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ training_targets:
# Stats.
gigaflops: 9.4
size: 7.6
# # Inference options. Defined by OpenVINO capabilities, not Algo Backend or Platform.
# inference_targets:
# - CPU
# - GPU
# - VPU

# Model spec
model_category: BALANCE
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ training_targets:
# Stats.
gigaflops: 68.48
size: 13.27

# Model spec
model_category: SPEED
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ training_targets:
# Stats.
gigaflops: 533.8
size: 177.9

# Model spec
model_category: ACCURACY
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ training_targets:
# Stats.
gigaflops: 68.48
size: 13.27

# Model spec
model_category: SPEED
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ training_targets:
# Stats.
gigaflops: 533.8
size: 177.9

# Model spec
model_category: ACCURACY
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ training_targets:
# Stats.
gigaflops: 3.63
size: 4.8

# Model spec
model_category: BALANCE
is_default_for_task: true
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ training_targets:
# Stats.
gigaflops: 1.82
size: 3.5

# Model spec
model_category: SPEED
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ training_targets:
# Stats.
gigaflops: 13.97
size: 6.4

# Model spec
model_category: ACCURACY
32 changes: 31 additions & 1 deletion src/otx/api/entities/model_template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This file defines the ModelConfiguration, ModelEntity and Model classes."""

# Copyright (C) 2021-2022 Intel Corporation
# Copyright (C) 2021-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
import copy
Expand Down Expand Up @@ -459,6 +459,30 @@ class EntryPoints:
nncf: Optional[str] = None


class ModelCategory(Enum):
"""Represents model category regarding accuracy & speed trade-off."""

SPEED = auto()
BALANCE = auto()
ACCURACY = auto()
OTHER = auto()

def __str__(self) -> str:
"""Returns the name of the model category."""
return str(self.name)


class ModelStatus(Enum):
"""Represents model status regarding deprecation process."""

ACTIVE = auto()
DEPRECATED = auto()

def __str__(self) -> str:
"""Returns the name of the model status."""
return str(self.name)


# pylint: disable=too-many-instance-attributes
@dataclass
class ModelTemplate:
Expand Down Expand Up @@ -499,6 +523,9 @@ class ModelTemplate:
priority. mobilenet is less important, and has a higher value. Default is zero (the highest priority).
gigaflops (float): how many billions of operations are required to do inference on a single data item.
size (float): how much disk space the model will approximately take.
model_category (ModelCategory): Represents model category regarding accuracy & speed trade-off. Default to OTHER.
model_status (ModelStatus): Represents model status regarding deprecation process. Default to ACTIVE.
is_default_for_task (bool): Whether this model is a default recommendation for the task
"""

model_template_id: str
Expand Down Expand Up @@ -528,6 +555,9 @@ class ModelTemplate:
gigaflops: float = 0
size: float = 0
hpo: Optional[Dict] = None
model_category: ModelCategory = ModelCategory.OTHER
model_status: ModelStatus = ModelStatus.ACTIVE
is_default_for_task: bool = False

def __post_init__(self):
"""Do sanitation checks before loading the hyper-parameters."""
Expand Down
48 changes: 35 additions & 13 deletions tests/integration/cli/anomaly/test_anomaly_classification.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
"""Tests for anomaly classification with OTX CLI"""

# Copyright (C) 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# Copyright (C) 2021-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

import os

import pytest

from otx.api.entities.model_template import parse_model_template, ModelCategory, ModelStatus
from otx.cli.registry import Registry
from tests.test_suite.e2e_test_system import e2e_pytest_component
from tests.test_suite.run_test_command import (
Expand All @@ -44,6 +34,38 @@
templates_ids = [template.model_template_id for template in templates]


class TestAnomalyClassificationModelTemplates:
@e2e_pytest_component
def test_model_category(self):
stat = {
ModelCategory.SPEED: 0,
ModelCategory.BALANCE: 0,
ModelCategory.ACCURACY: 0,
ModelCategory.OTHER: 0,
}
for template in templates:
stat[template.model_category] += 1
goodsong81 marked this conversation as resolved.
Show resolved Hide resolved
assert stat[ModelCategory.SPEED] == 1
assert stat[ModelCategory.BALANCE] <= 1
assert stat[ModelCategory.ACCURACY] == 1

@e2e_pytest_component
def test_model_status(self):
goodsong81 marked this conversation as resolved.
Show resolved Hide resolved
for template in templates:
if template.model_status == ModelStatus.DEPRECATED:
assert template.model_category == ModelCategory.OTHER

@e2e_pytest_component
def test_default_for_task(self):
num_default_model = 0
for template in templates:
if template.is_default_for_task:
num_default_model += 1
assert template.model_category != ModelCategory.OTHER
assert template.model_status == ModelStatus.ACTIVE
assert num_default_model == 1


class TestToolsAnomalyClassification:
@e2e_pytest_component
@pytest.mark.parametrize("template", templates, ids=templates_ids)
Expand Down
48 changes: 35 additions & 13 deletions tests/integration/cli/anomaly/test_anomaly_detection.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
"""Tests for anomaly detection with OTX CLI."""

# Copyright (C) 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# Copyright (C) 2021-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions
# and limitations under the License.

import os

import pytest

from otx.api.entities.model_template import parse_model_template, ModelCategory, ModelStatus
from otx.cli.registry import Registry
from tests.test_suite.e2e_test_system import e2e_pytest_component
from tests.test_suite.run_test_command import (
Expand All @@ -44,6 +34,38 @@
templates_ids = [template.model_template_id for template in templates]


class TestAnomalyDetectionModelTemplates:
@e2e_pytest_component
def test_model_category(self):
stat = {
ModelCategory.SPEED: 0,
ModelCategory.BALANCE: 0,
ModelCategory.ACCURACY: 0,
ModelCategory.OTHER: 0,
}
for template in templates:
stat[template.model_category] += 1
assert stat[ModelCategory.SPEED] == 1
assert stat[ModelCategory.BALANCE] <= 1
assert stat[ModelCategory.ACCURACY] == 1

@e2e_pytest_component
def test_model_status(self):
for template in templates:
if template.model_status == ModelStatus.DEPRECATED:
assert template.model_category == ModelCategory.OTHER

@e2e_pytest_component
def test_default_for_task(self):
num_default_model = 0
for template in templates:
if template.is_default_for_task:
num_default_model += 1
assert template.model_category != ModelCategory.OTHER
assert template.model_status == ModelStatus.ACTIVE
assert num_default_model == 1


class TestToolsAnomalyDetection:
@e2e_pytest_component
@pytest.mark.parametrize("template", templates, ids=templates_ids)
Expand Down
Loading